Sha256: 770f67c8e076e04f1cc0031bf0d92e7cdadbf6f6976c93fe8d3901576e18e994

Contents?: true

Size: 1.33 KB

Versions: 3

Compression:

Stored size: 1.33 KB

Contents

module Patron::HeaderParser
  HTTP_STATUS_LINE_START_RE = /^HTTP\/\d(\.\d)? \d+/
  HEADER_LINE_START_RE = /^[^:]+\:/

  # Returned for each response parsed out
  class SingleResponseHeaders < Struct.new(:status_line, :headers)
  end

  # 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.
  #
  # @param [String] the string of headers, with responses delimited by empty lines. All lines must end with CRLF
  # @return Array<SingleResponseHeaders>
  def self.parse(string_of_headers_from_multiple_responses_in_sequence)
    responses = []
    string_of_headers_from_multiple_responses_in_sequence.each_line do |matched_line|
      if matched_line =~ HTTP_STATUS_LINE_START_RE
        responses << SingleResponseHeaders.new(matched_line.strip, [])
      elsif matched_line =~ HEADER_LINE_START_RE
        raise "Header should follow an HTTP status line" unless responses.any?
        responses[-1].headers << matched_line.strip
      end # else it is the end of the headers for the request
    end
    responses
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
patron-0.13.3 lib/patron/header_parser.rb
patron-0.13.1 lib/patron/header_parser.rb
patron-0.12.1 lib/patron/header_parser.rb