Skip to content

Keep the Content-Length of HEAD responses in the async-http adapter - #1143

Merged
bblimke merged 1 commit into
bblimke:masterfrom
andrewfader:async-http-head-content-length
Nov 20, 2026
Merged

bblimke merged 1 commit into
bblimke:masterfrom
andrewfader:async-http-head-content-length

Conversation

@andrewfader

Copy link
Copy Markdown
Contributor

With the async-http adapter, the response to a HEAD request loses its size, both for stubs and for real requests made while a callback is registered (VCR recording).

async-http moves Content-Length off a HEAD response's headers and onto the body, as Protocol::HTTP::Body::Head.new(length). So response.body.length is the only place a client can read the size, and S3-style clients (aws-sdk's head_object.content_length, for example) do exactly that.

  • Stubbed: stub_request(:head, url).to_return(headers: { 'Content-Length' => '42' }) serves the response with the framing headers dropped and an empty body, so response.body is nil.
  • Real, with a callback: build_webmock_response reads the Head body (which returns nil) and replaces it with Buffered.wrap(nil), so the caller gets nil where it had a length. The recorded response has no Content-Length either, so a cassette recorded from it replays without one.

Repro against async-http 0.105.0, protocol-http 0.72.1:

stub_request(:head, "http://example.com/obj").to_return(headers: { "Content-Length" => "42" })
Sync { Async::HTTP::Client.new(Async::HTTP::Endpoint.parse("http://example.com")).head("/obj").body&.length }
# => nil (42 without WebMock)

Fix

  • For a HEAD request, WebMockApplication serves a Body::Head carrying the stub's Content-Length. Stubs without one behave as before.
  • build_webmock_response leaves a Body::Head in place and records its length as Content-Length, which is what the server sent.

Both are guarded on Protocol::HTTP::Body::Head being defined, for older protocol-http.

Tests

Two examples in async_http_client_spec.rb, one stubbed and one against WebMockServer. Both fail on master (expected: 42, got: nil and expected: 11, got: nil) and pass with the change. The rest of the file gives the same result before and after: here, 10 examples that make real requests to external hosts fail with EOFError both with and without this change.

@andrewfader
andrewfader force-pushed the async-http-head-content-length branch from d8b52bd to f38833d Compare November 2, 2026 09:40
@andrewfader
andrewfader force-pushed the async-http-head-content-length branch 2 times, most recently from a124743 to 92644b5 Compare November 17, 2026 04:25
@bblimke
bblimke merged commit e06de88 into bblimke:master Nov 20, 2026
5 of 8 checks passed
bblimke added a commit that referenced this pull request Nov 20, 2026
Keep any Protocol::HTTP::Body::Head a real response has, also one without
a length, and record its length as the Content-Length. Pick the body of a
HEAD stub in its own method, let the spec's make_request yield the raw
response, add specs and a changelog entry.
@bblimke

bblimke commented Nov 20, 2026

Copy link
Copy Markdown
Owner

Thank you @andrewfader! This is released in 3.36.0. I made a small change on top: over HTTP/2, a real response from a server that sends no Content-Length now keeps the body async-http gives it, instead of losing it.

@andrewfader
andrewfader deleted the async-http-head-content-length branch November 20, 2026 18:01
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.

2 participants