parse_request

in gems/aws-sigv4/spec/spec_helper.rb [16:64]


    def parse_request(request, normalize=false)
      lines = request.lines.to_a

      req_def = lines.shift
      http_method, request_uri = req_def.split(' ', 2)
      request_uri = request_uri.gsub(" HTTP/1.1\n", '')

      
      uri_path, querystring = request_uri.split('?', 2)
      if querystring
        querystring = querystring.split('&').map do |key_value|
          key, value = key_value.split('=')
          key = Aws::Sigv4::Signer.uri_escape(key) unless key.include? '%E1'
          value = Aws::Sigv4::Signer.uri_escape(value.to_s)
          "#{key}=#{value}"
        end.join('&')
      end

      request_uri = Aws::Sigv4::Signer.uri_escape_path(uri_path)
      request_uri += '?' + querystring if querystring

      
      headers = Hash.new { |h,k| h[k] = [] }
      prev_key = nil
      until lines.empty?
        line = lines.shift
        if line.strip == ''
          break
        elsif line =~ /^\s+/ 
          headers[prev_key][0] = "#{headers[prev_key][0]} #{line.strip}"
        else
          key, value = line.strip.split(':')
          headers[key] << value
          prev_key = key
        end
      end
      headers = headers.inject({}) do |h,(k,v)|
        h[k] = headers[k].join(',')
        h
      end

      {
        http_method: http_method,
        url: "https://#{headers['Host']}#{request_uri}",
        headers: headers,
        body: lines.join,
      }
    end