Skip to content

Fix #1972: bypass body-reading for HEAD requests in io_client - #1974

Closed
amanmaurya92 wants to merge 1 commit into
dart-lang:masterfrom
amanmaurya92:fix-1972
Closed

amanmaurya92 wants to merge 1 commit into
dart-lang:masterfrom
amanmaurya92:fix-1972

Conversation

@amanmaurya92

Copy link
Copy Markdown

Fixes #1972

This PR resolves an issue where HEAD requests unexpectedly throw a ClientException due to a mismatch between the Content-Length header and the actual response body length (which is naturally empty for HEAD requests).

Because dart:io's HttpClient will sometimes fail when verifying the Content-Length of an empty HEAD response body, we now intercept HEAD requests in io_client.dart. Instead of attempting to read the body stream, we asynchronously drain the underlying connection (safely swallowing any spurious dart:io length-mismatch exceptions) and immediately return the headers with a safe, empty stream.


  • I’ve reviewed the contributor guide and applied the relevant portions to this PR.
Contribution guidelines:

Many Dart repos have a weekly cadence for reviewing PRs - please allow for some latency before initial review feedback.

Note: The Dart team is trialing Gemini Code Assist. Don't take its comments as final Dart team feedback. Use the suggestions if they're helpful; otherwise, wait for a human reviewer.

@amanmaurya92

Copy link
Copy Markdown
Author

While making this change, I also noticed a TODO in io_client.dart regarding dart-lang/sdk#53005 (removing the .trimRight() call when parsing headers).

Since that SDK issue appears to be resolved, we could probably clean this up now. Should I make a separate PR to refactor and remove the .trimRight() workaround, or is someone already planning to tackle that alongside a minimum SDK version bump?

@Keetz

Keetz commented Aug 12, 2026

Copy link
Copy Markdown

I have no experience with the Dart Lang codebase, but out of curiosity I checked your code (I created the issue).
Why not find the place where the content length and response length check is made, and omit it from HEAD calls, instead of this approach of "swallowing" the exception. What if it is another exception? That doesn't seem to be handled correctly, it would just ignore any exceptions here right?

@brianquinlan

Copy link
Copy Markdown
Collaborator

I added a comment to #1972 and #1979 attempts to reproduce the problem without success.

Do you have a better reproduction?

@brianquinlan brianquinlan added the needs-info Additional information needed from the issue author label Sep 2, 2026
@amanmaurya92

Copy link
Copy Markdown
Author

Hi @brianquinlan,

Thanks for looking into this and creating #1979.

I do not have a reproduction either. I tested this against master (both with your conformance test from #1979 and with a local test server returning Content-Length: 100 with an empty body on a HEAD request), and IOClient handles it without throwing any ClientException (since dart:io's HTTP parser already sets isHead = true and _noMessageBody = true).

This PR was opened under the assumption from #1972 that IOClient was failing on HEAD responses with a Content-Length header, but it appears IOClient is not the culprit here. The issue reporter (@Keetz) might be using a different client (e.g. browser/Fetch, a proxy, or a third-party wrapper), or experiencing an edge case with a specific server behavior.

Given that, I'm happy to close this PR for now until @Keetz provides a minimal reproduction and specifies their client and environment on #1972. If it turns out to affect a different client in this repo, we can address it there.

@brianquinlan

Copy link
Copy Markdown
Collaborator

Thanks @amanmaurya92 !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-info Additional information needed from the issue author package:http

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HEAD call results in ClientException: Content-Length is larger than actual response length.

3 participants