Module: Patron::HeaderParser

Defined in:
lib/patron/header_parser.rb

Defined Under Namespace

Classes: SingleResponseHeaders

Constant Summary collapse

CRLF =
/#{Regexp.escape("\r\n")}/

Class Method Summary collapse

Class Method Details

.parse(headers_from_multiple_responses_in_sequence) ⇒ Object

Parses a string with lines delimited with CRLF into an Array of SingleResponseHeaders objects. libCURL supplies us multiple responses in sequence, so if we encounter multiple redirect or operate through a proxy - that adds ConnectionEstablished status at the beginning of the response - we need to account for parsing multiple response headres and potentially preserving them.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/patron/header_parser.rb', line 19

def self.parse(headers_from_multiple_responses_in_sequence)
  s = StringScanner.new(headers_from_multiple_responses_in_sequence)
  responses = []
  until s.eos?
    return unless scanned = s.scan_until(CRLF)
    matched_line = scanned[0..-3]
    if matched_line =~ /^HTTP\/\d\.\d \d+/
      responses << SingleResponseHeaders.new(matched_line.strip, [])
    elsif matched_line =~ /^[^:]+\:/
      raise "Header should follow an HTTP status line" unless responses.any?
      responses[-1].headers << matched_line
    end # else it is the end of the headers for the request
  end
  responses
end