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

Ignore Content-Length from env.request_headers #52

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

pilaf
Copy link

@pilaf pilaf commented Feb 24, 2025

Closes #51

This makes the adapter ignore Content-Length if given in Faraday's env.request_headers, but only if capitalized, since Async::HTTP will set content-length anyway.

I'd like to add tests too, but I wasn't sure how to test what headers end up in the final request without setting up a dummy server to receive them. Any suggestions?

@@ -181,6 +181,12 @@ def perform_request(env)
end

if headers = env.request_headers
# Ignore Content-Length if given, it will be set for us later anyway (lowercased)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking good.

I'd suggest extracting this into length and then assigning to BodyReadWrapper, which should have this:

class BodyReadWrapper
  def initialize(body, length = nil, block_size: 4096)
    @body = body
    @length = length
    @block_size = block_size
  end

  # ...

  def length
    @length # if known
  end

You'll need to move the if headers = env.request_headers first to extract a local variable if possible.

Is headers.key?(...) case sensitive?

Testing is a bit tricky, but maybe we can mock the client. If you can get the code updated, I can help write the tests.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ioquatix

Is headers.key?(...) case sensitive?

Yes. I'd change this to check for both Content-Length and content-length if the intent is to use that value for BodyReadWrapper#length, if you agree.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be easier, for a first pass, to construct headers = ::Protocol::HTTP::Headers[headers] first, and then length = headers.delete("content-length").

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

Successfully merging this pull request may close these issues.

POST requests with no body include content-length header twice
2 participants