Skip to content

Conversation

@djelinski
Copy link
Member

@djelinski djelinski commented Oct 9, 2025

Do not send the Content-Length header on HTTP/1.1 requests when the content length is known to be zero and the method does not expect content or is unknown.

This brings the HTTP/1.1 implementation in line with the recommendations from RFC 9110.

The existing ContentLengthHeaderTest was extended to cover the modified scenarios.

Tier1-3 tests continue to pass.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Change requires CSR request JDK-8369533 to be approved
  • Commit message must refer to an issue

Issues

  • JDK-8358942: HttpClient adds Content-Length: 0 for a GET request with a BodyPublishers.noBody() (Bug - P4)
  • JDK-8369533: HttpClient adds Content-Length: 0 for a GET request with a BodyPublishers.noBody() (CSR)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/27727/head:pull/27727
$ git checkout pull/27727

Update a local copy of the PR:
$ git checkout pull/27727
$ git pull https://git.openjdk.org/jdk.git pull/27727/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 27727

View PR using the GUI difftool:
$ git pr show -t 27727

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/27727.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Oct 9, 2025

👋 Welcome back djelinski! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Oct 9, 2025

@djelinski This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8358942: HttpClient adds Content-Length: 0 for a GET request with a BodyPublishers.noBody()

Reviewed-by: dfuchs, vyazici

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 118 new commits pushed to the master branch:

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot changed the title 8358942 8358942: HttpClient adds Content-Length: 0 for a GET request with a BodyPublishers.noBody() Oct 9, 2025
@openjdk
Copy link

openjdk bot commented Oct 9, 2025

@djelinski The following label will be automatically applied to this pull request:

  • net

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the csr Pull request needs approved CSR before integration label Oct 10, 2025
@djelinski djelinski marked this pull request as ready for review October 10, 2025 10:18
@openjdk openjdk bot added the rfr Pull request is ready for review label Oct 10, 2025
@mlbridge
Copy link

mlbridge bot commented Oct 10, 2025

Webrevs

HttpResponse<String> resp = hc.send(req, HttpResponse.BodyHandlers.ofString(UTF_8));
assertEquals(resp.statusCode(), 200, resp.body());
assertEquals(resp.version(), version);
assertEquals(resp.body(), "Request completed");
Copy link
Contributor

Choose a reason for hiding this comment

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

If possible, you may consider verifying the response body in other test methods too, since, in particular, OptionalContentLengthHandler responds with 200 in two different scenarios.

Copy link
Member Author

Choose a reason for hiding this comment

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

That's not possible in most cases: in HTTP/2 and HTTP/3 we don't send the content-length:0 header, we only send content-length when it's known up front and non-zero.
In cases where it is possible it's usually not necessary - most of the time we don't really care if the content length was sent or not, as long as the server is able to figure it out.

I'll add a check for the HTTP/1 + PUT/POST case, since that's special-cased by this PR.

Copy link
Member

Choose a reason for hiding this comment

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

If I'm not mistaken in case of H2/H3 you could verify that:

assertTrue(List.of("Request completed, no content length", "Request completed").contains(resp.body()), "Unexpected response: " + resp.body());

Copy link
Member Author

Choose a reason for hiding this comment

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

I ended up removing the OptionalContentLengthHandler. It didn't feel right anyway.

Copy link
Contributor

Choose a reason for hiding this comment

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

OptionalContentLengthHandler gone feels better. This also implies that the check is stricter: we either expect the Content-Length, or not.

@vy
Copy link
Contributor

vy commented Oct 13, 2025

Why is this an HTTP/1.1-only problem?

HTTP/2 and HTTP/3 implementations of HttpClient already add the Content-Length header iff it is greater than zero:

if (contentLength > 0) {
h.setHeader("content-length", Long.toString(contentLength));
}

if (requestContentLen > 0) {
h.setHeader("content-length", Long.toString(requestContentLen));
}

They do this, unlike the POST and PUT exemption for HTTP/1.1 in the proposed PR, for all methods.
This behavior difference between HTTP/1 and HTTP/{2,3} is not a problem because

  1. HTTP/2 and HTTP/3 are implicitly chunked when Content-Length is missing.
  2. Content-Length is always optional for HTTP/2 and HTTP/3, for all methods. A user agent may add it, in which case it must match the total number of bytes sent in the body request.

Credits: Thanks to @dfuch and @djelinski for patiently answering my questions.

HttpResponse<String> resp = hc.send(req, HttpResponse.BodyHandlers.ofString(UTF_8));
assertEquals(resp.statusCode(), 200, resp.body());
assertEquals(resp.version(), version);
assertEquals(resp.body(), "Request completed");
Copy link
Contributor

Choose a reason for hiding this comment

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

OptionalContentLengthHandler gone feels better. This also implies that the check is stricter: we either expect the Content-Length, or not.

@dfuch
Copy link
Member

dfuch commented Oct 15, 2025

@djelinski you should probably Finalize the CSR JDK-8369533 now.

@openjdk openjdk bot added ready Pull request is ready to be integrated and removed csr Pull request needs approved CSR before integration labels Oct 15, 2025
@djelinski
Copy link
Member Author

Thanks for the reviews!

/integrate

@openjdk
Copy link

openjdk bot commented Oct 16, 2025

Going to push as commit ead35a7.
Since your change was applied there have been 129 commits pushed to the master branch:

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Oct 16, 2025
@openjdk openjdk bot closed this Oct 16, 2025
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Oct 16, 2025
@openjdk
Copy link

openjdk bot commented Oct 16, 2025

@djelinski Pushed as commit ead35a7.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

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

Labels

integrated Pull request has been integrated net [email protected]

Development

Successfully merging this pull request may close these issues.

3 participants