Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Repeat Content-Length header when making requests with EM-HTTP-Request #981

Open
nbrookie opened this issue Jul 10, 2022 · 0 comments
Open

Comments

@nbrookie
Copy link

nbrookie commented Jul 10, 2022

Hi there,

I am seeing an issue where the Content-Length header is being repeated when making real network requests with the EM-HTTP-Request library. Here is the scenario:

WebMock.allow_net_connect!

EM::HttpRequest.new("http://httpbin.org/post").post(body: "foo", head: {"Content-Length" => "3"})

# sends this request string --> "POST /post HTTP/1.1\r\nContent-Length: 3\r\nConnection: close\r\nHost: httpbin.org\r\nUser-Agent: EventMachine HttpClient\r\nAccept-Encoding: gzip, compressed\r\nContent-Length: 3\r\n\r\n"

# Notice the Content-Length header is repeated

I wrote a test that replicates the issue here if helpful

I think this boils down to the WebMock::RequestSignature#headers= where we call WebMock::Util::Headers.normalize_headers(headers) and that changes the case of the headers.

However in https://github.com/igrigorik/em-http-request/blob/master/lib/em-http/client.rb#L185, EventMachine assumes the request headers are downcased when attempting to assign a Content-Length, so we end up with both an capitalized and downcased version in the request headers.

Filing this because this presents an issue with Puma > 5.6.2 since they are now checking for invalid headers (see this line) and the Content-Length header that results here looks like Content-Length: 123, 123 which Puma rejects

This monkey patch appears to fix the issue:

module EventMachine
  class WebMockHttpClient
    def connection_completed
      @state = :response_header
      headers = request_signature.headers.inject({}) { |h, (k, v)| h[k.to_s.downcase] = v; h }
      send_request(headers, request_signature.body)
    end   
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant