-
Notifications
You must be signed in to change notification settings - Fork 6.1k
8292044: HttpClient doesn't handle 102 or 103 properly #10169
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
Conversation
👋 Welcome back jpai! A progress list of the required criteria for merging this PR into |
Webrevs
|
if (debug.on()) { | ||
debug.log("Ignoring (1xx informational) response code " | ||
+ rsp.statusCode()); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably log that using one of the Log.xxx methods too ?
Have the response headers in Response already been logged (using Log) by the time we reach here? If not we need to log them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello Daniel,
We should probably log that using one of the Log.xxx methods too ?
I've now updated the PR to add the Log.logTrace
call too.
Have the response headers in Response already been logged (using Log) by the time we reach here?
Yes, both for HTTP1 and HTTP2 the response headers get logged as soon as a Response
instance is created here https://github.com/openjdk/jdk/blob/master/src/java.net.http/share/classes/jdk/internal/net/http/Http1Response.java#L209 and here https://github.com/openjdk/jdk/blob/master/src/java.net.http/share/classes/jdk/internal/net/http/Stream.java#L516. So by the time, the control reaches here those response headers will already be logged. Do note that the spec doesn't mandate any response headers for these informational responses. So there may not be any response headers.
The PR has been updated to include a new test to verify that the request timeout isn't impacted when informational responses are involved and the application receives the timeout exception if the server doesn't respond with a final response, within the timeout duration. tier1, tier2 and tier3 testing passed with the changes in this PR. |
We probably should consider how a future API change would work, which allows an application to receive the intermediate responses and make sure this change doesn't cause any problems for that. Though, I don't see an issue currently. Regarding a new API, maybe a method could be added to HttpResponse.BodyHandler to supply intermediate responses? |
A new JBS issue (https://bugs.openjdk.org/browse/JDK-8293574) got reported for this same bug. The reporter there provided test cases to reproduce the issue. Those tests helped identify an additional fix that was needed in this PR. Specifically, the RFC states that if the request is configured for "Expect-Continue" and if the server still sends a |
That should read "the RFC states that if the request is not configured for "Expect-Continue" ..." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left a few comments regarding which RFCs are relevant.
final int statusCode = rsp.statusCode(); | ||
// we ignore any response code which is 1xx. | ||
// For 100 (with the request configured to expect-continue) and 101, we handle it | ||
// specifically as defined in the RFC-2616 (HTTP 1.1 spec), outside of this method. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RFC 2616 is irrelevant. You need to look at RFC 9110 and 9112.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you Julian for those references. I've cleaned up this comment in the updated version of this PR.
I see that RFC 9110 was published as recently as June 2022 and obsoletes RFC 7231. However, in context of what we are doing in this PR, the 1xx informational handling continues to be the same, so referring to this spec in the comment, I think is fine.
// we ignore any response code which is 1xx. | ||
// For 100 (with the request configured to expect-continue) and 101, we handle it | ||
// specifically as defined in the RFC-2616 (HTTP 1.1 spec), outside of this method. | ||
// As noted in RFC-7231, section 6.2.1, if response code is 100 and if the request wasn't |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So is RFC 7231.
// configured with expectContinue, then we ignore the 100 response and wait for the final | ||
// response (just like any other 1xx response) | ||
// Any other response code between 102 and 199 (both inclusive) aren't specified in the | ||
// HTTP 1.1 spec. The spec states that these 1xx response codes are informational and |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not specific for version 1.1 of HTTP. See RFC 9110.
Hello Michael,
I had a look at the HttpClient API and as you note, we could add a new method on
I plan to experiment with this in a separate JBS issue. |
// response (just like any other 1xx response) | ||
// Any other response code between 102 and 199 (both inclusive) aren't specified in the | ||
// "HTTP semantics" RFC-9110. The spec states that these 1xx response codes are informational | ||
// and interim and the client will ignore them and will continue to wait to receive the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rephrase that as "clients *can' ignore them if they choose to"
http1RequestURIBase = "http://" + serverSocket.getInetAddress().getHostAddress() | ||
+ ":" + serverSocket.getLocalPort(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will not work if the address is the IPv6 loopback. I suggest using the test URIBuilder to build the URI.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right - I hadn't taken that aspect into account. I have now updated the PR to use the URIBuilder
test library. Manual testing by passing -Djava.net.preferIPv6Addresses=true
to this test, with and without this URIBuilder reproduced the URI related failures and verified that this change works.
FWIW, it would be awesome if an equivalent fix could be applied to HTTPURLConnection. |
A brief look at the code in that area suggests that it might be relatively straightforward to fix that too. I'll check it out in more detail. |
@jaikiran 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:
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 10 new commits pushed to the
Please see this link for an up-to-date comparison between the source branch of this pull request and the ➡️ To integrate this PR with the above commit message to the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good.
In #10229 Daniel raised a valid question about how we deal with a 101 response when the request didn't ask for an Upgrade. I've updated this PR to ignore such a response too (as allowed by the spec) and updated the test to include this scenario. Test fails without the change and passes with the change. |
A 101 response implies that the wire protocol is switching to the protocol specified in the "Upgrade" header field. I don't think it would be wise to ignore that, unless the protocol actually stays the same. Note that this is indeed an edge case because it implies that the server is misbehaving. Thus, I'd rather raise an exception instead of making assumptions that will likely be incorrect. |
I agree with Julian - if we didn't ask for an upgrade we should probably consider that as a protocol violation and close the connection. |
Thank you Julian and Daniel for that input. I've now updated this PR to take into account that receiving an unexpected 101 should be considered a protocol error and subsequently necessary action be taken in the client. New tests have been added to test this scenario. |
try { | ||
exchImpl.onProtocolError(errMsg); | ||
} catch (Throwable ignore){ | ||
// ignored |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I decided to intentionally ignore any exceptions here because I couldn't think of anything different that we could do here, would there?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. But I believe you should create the exception outside of the try and pass it both to the exchangeImpl and the returned failed future. The concrete implementation of onProtocolError
should end up calling cancelImpl
on the concrete subclass in both cases. Calling cancelImpl
should take care of proper exception recording and take care of operation ref counting too.
@@ -1238,6 +1238,16 @@ void cancel(IOException cause) { | |||
cancelImpl(cause); | |||
} | |||
|
|||
@Override | |||
void onProtocolError(final String errorMessage) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this should take an IOException
or ProtocolException
as parameter and call cancelImpl(IOException)
.
It should take care of resetting the stream if needed, and properly account for the ref counting of inflight operation.
@@ -502,6 +502,16 @@ void cancel(IOException cause) { | |||
cancelImpl(cause); | |||
} | |||
|
|||
@Override | |||
void onProtocolError(final String errorMessage) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should take the IOException/ProtocolException as parameter and call cancelImpl(IOException). cancelImpl should do the right thing.
try { | ||
exchImpl.onProtocolError(errMsg); | ||
} catch (Throwable ignore){ | ||
// ignored |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. But I believe you should create the exception outside of the try and pass it both to the exchangeImpl and the returned failed future. The concrete implementation of onProtocolError
should end up calling cancelImpl
on the concrete subclass in both cases. Calling cancelImpl
should take care of proper exception recording and take care of operation ref counting too.
@@ -64,9 +68,14 @@ public class Response1xxTest { | |||
private String http1RequestURIBase; | |||
|
|||
|
|||
private HttpServerAdapters.HttpTestServer http2Server; | |||
private HttpServerAdapters.HttpTestServer http2Server; // h2c |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we remove the outer class from the declaration?
private String http2RequestURIBase; | ||
|
||
|
||
private SSLContext sslContext; | ||
private HttpServerAdapters.HttpTestServer https2Server; // h2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here?
if (https2Server != null) { | ||
https2Server.stop(); | ||
System.out.println("Stopped (https) HTTP2 server"); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test should probably use a final ReferenceTracker TRACKER = ReferenceTracker.INSTANCE
now, to double check that all clients have properly terminated without any dangling operations at the end of the test.
You can grep TRACKER
in existing HttpClient tests to see how this is used.
The TRACKER can be used to track created clients, and invoked at the end of the test to verify that all resources have been properly reclaimed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Thank you everyone for the inputs and the reviews. |
/integrate |
Going to push as commit 800e68d.
Your commit was automatically rebased without conflicts. |
Tested in https://github.com/greenbytes/java-http-1xx-tests and found to be working as advertised. Thanks! |
It would be great if a new ticket would be opened for tracking this enhancement request. |
Thank you Julian for running those tests.
That is being tracked in https://bugs.openjdk.org/browse/JDK-8294196 |
Can I please get a review of this change which proposes to fix https://bugs.openjdk.org/browse/JDK-8292044?
The linked JBS issue notes two parts to fixing this. Part one is to (internally) ignore the intermediate 1xx informational responses, in the client and wait for subsequent final response from the server. Part two is to introduce newer APIs to let applications using HttpClient, to have access to these intermediate response (codes). This commit (only) addresses part one. Part two is out of scope of this change and a separate issue will be opened to address it (at a later time).
The commit in this PR introduces a check to see if the returned response is an informational response (as defined by RFC-2616 https://www.rfc-editor.org/rfc/rfc2616#page-58). If the response code is between 102 and 199 (inclusive), then this change ignores that response and keeps waiting for a subsequent final response from the server.
The request timeout (if set) will not be reset when a intermediate informational response is received (and we ignore it). The request timeout handling continues to be the same as what it is currently and will span from the request start till the final response is received. If no final response is received within the duration of request timeout (if set) then the application will continue to receive a request timeout exception.
A new test class has been introduced to reproduce the issue and test the fix. The test tests both HTTP/1.1 and HTTP2.
tier1, tier2 and tier3 testing is in progress.
Progress
Issue
Reviewers
Reviewing
Using
git
Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk pull/10169/head:pull/10169
$ git checkout pull/10169
Update a local copy of the PR:
$ git checkout pull/10169
$ git pull https://git.openjdk.org/jdk pull/10169/head
Using Skara CLI tools
Checkout this PR locally:
$ git pr checkout 10169
View PR using the GUI difftool:
$ git pr show -t 10169
Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/10169.diff