Class: HttpMimic::ResponseParser

Inherits:
Object
  • Object
show all
Defined in:
lib/http_mimic/response_parser.rb

Constant Summary collapse

HTTP_STATUS_LINE_REGEX =
/\AHTTP\/(?<version>[\d\.]+)\s+(?<code>\d{3})(?:\s+(?<message>.*))?/i
HTTP_STATUS_LINE_B =
/\AHTTP\/(?:[\d\.]+)\s+\d{3}/i
BINARY_MIME_KEYWORDS =
%w[image/ audio/ video/ pdf octet-stream zip gzip tar compressed stream font wasm].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_output, exit_status: nil, stderr: nil, command: nil, request_url: nil) ⇒ ResponseParser

Returns a new instance of ResponseParser.



13
14
15
16
17
18
19
# File 'lib/http_mimic/response_parser.rb', line 13

def initialize(raw_output, exit_status: nil, stderr: nil, command: nil, request_url: nil)
  @raw_output   = raw_output.to_s
  @exit_status  = exit_status
  @stderr       = stderr.to_s
  @command      = command
  @request_url  = request_url
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



11
12
13
# File 'lib/http_mimic/response_parser.rb', line 11

def command
  @command
end

#exit_statusObject (readonly)

Returns the value of attribute exit_status.



11
12
13
# File 'lib/http_mimic/response_parser.rb', line 11

def exit_status
  @exit_status
end

#raw_outputObject (readonly)

Returns the value of attribute raw_output.



11
12
13
# File 'lib/http_mimic/response_parser.rb', line 11

def raw_output
  @raw_output
end

#request_urlObject (readonly)

Returns the value of attribute request_url.



11
12
13
# File 'lib/http_mimic/response_parser.rb', line 11

def request_url
  @request_url
end

#stderrObject (readonly)

Returns the value of attribute stderr.



11
12
13
# File 'lib/http_mimic/response_parser.rb', line 11

def stderr
  @stderr
end

Instance Method Details

#parseObject



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
# File 'lib/http_mimic/response_parser.rb', line 21

def parse
  # Split header blocks and body in binary-safe manner
  header_blocks, raw_body = split_headers_and_body(@raw_output)

  final_header_block = header_blocks.last || ''
  history_header_blocks = header_blocks.size > 1 ? header_blocks[0...-1] : []

  # Parse final status line and headers
  code, http_version, status_message, headers, cookies = parse_header_block(final_header_block)

  # Parse redirect history
  history = history_header_blocks.map do |block|
    h_code, h_version, h_msg, h_headers, h_cookies = parse_header_block(block)
    {
      code: h_code,
      http_version: h_version,
      status_message: h_msg,
      headers: h_headers,
      cookies: h_cookies,
      raw_headers: block
    }
  end

  # Process body: retain raw bytes for binary, or UTF-8 encode for text
  content_type = headers['content-type'].to_s.downcase
  body = if binary_content?(content_type, raw_body)
           raw_body
         else
           raw_body.dup.force_encoding('UTF-8').scrub
         end

  # Parse body (auto-detect JSON)
  parsed_body = parse_body(body, headers)

  Response.new(
    code: code,
    http_version: http_version,
    status_message: status_message,
    headers: headers,
    cookies: cookies,
    body: body,
    parsed_response: parsed_body,
    raw_headers: final_header_block,
    history: history,
    request_url: request_url,
    stderr: stderr,
    exit_code: exit_status ? exit_status.exitstatus : 0,
    command: command
  )
end