RFR: 8314978: Multiple server call from connection failing with expect100 in getOutputStream [v2]
Daniel Fuchs
dfuchs at openjdk.org
Tue Sep 12 12:08:43 UTC 2023
On Sun, 3 Sep 2023 09:16:24 GMT, Vyom Tewari <vtewari at openjdk.org> wrote:
>> With the current implementation of HttpURLConnection if server rejects the “Expect 100-continue” then there will be ‘java.net.ProtocolException’ will be thrown from 'expect100Continue()' method.
>>
>> After the exception thrown, If we call any other method on the same instance (ex getHeaderField(), or getHeaderFields()). They will internally call getOuputStream() which invokes writeRequests(), which make the actual server call.
>>
>> The code change will sets the existing variable ‘rememberedException’ when there is exception and getOutputStream0() will re-throw ‘rememberedException’ if the ‘rememberedException’ is not null.
>>
>> Note: getOutputStream0() also call’s ‘expect100Continue()’ if ‘expectContinue’ is true.
>
> Vyom Tewari has updated the pull request incrementally with one additional commit since the last revision:
>
> modified the junit tests names
Hi Vyom, the HttpClient is a different API. We're talking about the HttpURLConnection here. You're calling `HttpURLConnection::getOutputStream()` ; it blocks for a while waiting for 100 from the server. If the server returns 100, or takes too long to return 100, the method call returns an OutputStream that you can use to write the request body. If the server returns anything but 100, the method call throws a `ProtocolException`, as there is no point sending a body at this point. Therefore no OutputStream will be returned. You should catch the exception and look at the status code: then you can decide how to proceed depending on whether the status code returned is 200, 417, or something else. This may not be ideal but this is how it is supposed to work in HttpURLConnection. If we had a different API we could have returned an `Optional<OutputStream>` but that's not how the API was designed. `Optional` didn't exists at the time.
-------------
PR Comment: https://git.openjdk.org/jdk/pull/15483#issuecomment-1715598026
More information about the net-dev
mailing list