Class: HttpMimic::Request
- Inherits:
-
Object
- Object
- HttpMimic::Request
- Defined in:
- lib/http_mimic/request.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#initialize(method, url, options = {}, config = nil) ⇒ Request
constructor
A new instance of Request.
- #perform ⇒ Object
Constructor Details
#initialize(method, url, options = {}, config = nil) ⇒ Request
Returns a new instance of Request.
9 10 11 12 13 14 |
# File 'lib/http_mimic/request.rb', line 9 def initialize(method, url, = {}, config = nil) @method = method @url = url @options = .dup @config = config || HttpMimic.configuration end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
7 8 9 |
# File 'lib/http_mimic/request.rb', line 7 def config @config end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
7 8 9 |
# File 'lib/http_mimic/request.rb', line 7 def method @method end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/http_mimic/request.rb', line 7 def @options end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
7 8 9 |
# File 'lib/http_mimic/request.rb', line 7 def url @url end |
Instance Method Details
#perform ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/http_mimic/request.rb', line 16 def perform mode = ([:mode] || config.mode || :auto).to_sym auto_fallback = .fetch(:auto_fallback, config.auto_fallback) retry_statuses = [:retry_statuses] || config.retry_statuses || [403, 429, 503] # Persistent CookieStore integration = .fetch(:persist_cookies, config.) host = CookieStore.extract_host(url) if && host = CookieStore.load(host) if && !.empty? log_debug("[HttpMimic::CookieStore] Loaded #{.size} persistent cookies for #{host}") = [:cookies] ? ([:cookies].is_a?(Hash) ? [:cookies] : [:cookies].to_h) : {} [:cookies] = .merge() end end attempts = [] profiles_to_try = determine_profiles(mode, auto_fallback) waf_solve_attempted = false response = nil final_status = nil final_stderr = nil final_command = nil profiles_to_try.each_with_index do |profile, index| current_opts = .merge(profile: profile) builder = CommandBuilder.new(method, url, current_opts, config) binary, args, stdin_data, final_url = builder.build full_command = [binary] + args log_debug("Executing HttpMimic command (attempt #{index + 1}, profile: #{profile}): #{full_command.join(' ')}") log_debug("Stdin data: #{stdin_data}") if stdin_data stdout, stderr, status = execute_open3(full_command, stdin_data) log_debug("Curl exit status: #{status.exitstatus}") log_debug("Curl stderr: #{stderr}") unless stderr.empty? response = ResponseParser.new( stdout, exit_status: status, stderr: stderr, command: full_command, request_url: final_url ).parse response.mode_used = profile response.fallback_triggered = (index > 0) attempts << { attempt: index + 1, profile: profile, command: full_command, code: response.code, exit_code: status.exitstatus, success: response.success? } response.attempts = attempts final_status = status final_stderr = stderr final_command = full_command # Forward any received cookies to subsequent attempts if response. && !response..empty? = [:cookies] ? ([:cookies].is_a?(Hash) ? [:cookies] : [:cookies].to_h) : {} [:cookies] = response..to_h.merge() end auto_solve_waf = .fetch(:solve_waf, config.auto_solve_waf) is_blocked = (status.exitstatus != 0) || retry_statuses.include?(response.code) || Waf::Detector.challenge_page?(response) # Only attempt WAF resolution once per request to avoid unnecessary latency on subsequent fallbacks if is_blocked && auto_solve_waf && !waf_solve_attempted && method.to_s.upcase == 'GET' waf_type = Waf::Detector.detect(response) if waf_type waf_solve_attempted = true log_debug("[HttpMimic] Detected #{waf_type.to_s.capitalize} WAF challenge. Attempting to solve with QuickJS...") solved_resp = Waf.solve(url, response, current_opts) if solved_resp response = solved_resp is_blocked = (response.code != 0 && retry_statuses.include?(response.code)) if response. && !response..empty? [:cookies] = ([:cookies] || {}).merge(response..to_h) end end end end if !is_blocked || (index == profiles_to_try.size - 1) break end log_debug("[HttpMimic] Attempt #{index + 1} with #{profile} resulted in status #{response.code}. Triggering smart fallback to next profile...") end # Persist cookies back to store if enabled if && host && response && response. && !response..empty? CookieStore.save(host, response.) log_debug("[HttpMimic::CookieStore] Saved #{response..size} cookies for #{host}") end handle_errors(final_status, final_stderr, final_command, response) response end |