module Patron::HeaderParser

def self.parse(headers_from_multiple_responses_in_sequence)

Parameters:
  • the (String) -- string of headers, with responses delimited by empty lines. All lines must end with CRLF
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