HttpRequestMessages are meant to be used for a single request. However, that checking is done by HttpClient rather than by HttpClientHandler. As a result, if code bypasses HttpClient, e.g. by using HttpMessageInvoker or by using a custom HttpMessageHandler sitting between the HttpClient and the HttpClientHandler, the same HttpRequestMessage can be used multiple times without an exception being thrown warning of the invalid usage. And on Windows, things happen to work, but with our Unix implementation was never expecting this scenario and doesn't work well with it:
- The handler is explicitly disposing of the request content stream rather than leaving that up to the request content's disposal to handle.
- The handler is removing the Content-Length header from the request message content, as a simple way to avoid overriding the Content-Length header that libcurl itself may send based on previous configuration.
- The handler is using ReadAsStreamAsync to get the request stream to send, but ReadAsStreamAsync caches the returned stream, so if the same request message is reused with the same Content, the stream will have already advanced to the end of the stream, and when the message gets reused, we won't have content to send.
This commit fixes that:
- We now don't dispose the request content stream. That'll be left up to the request content's disposal.
- We now don't remove the ContentLength header from the request message. We instead just check as we're writing out the headers whether the header is Content-Length, skipping it if it is.
- We now store the original position of a seekable stream that's provided to us, and at the end of the request, rather than disposing of the stream, we rewind to the original position if it exists. (If the stream isn't seekable, reusing the request message wouldn't be possible anyway.)
This last point also fixes another theoretical issue. When libcurl wants to go back to the beginning of the stream (e.g. in a redirect scenario where it needs to rewind), it asks us to seek back to the beginning. We're currently seeking back to the exact position asked for. But the stream may have come in to us already advanced beyond 0, in which case we need to seek back to the requested offset plus that original position rather than just the requested offset.
And in doing this, I noticed that we were inadvertently allowing a set of closure/delegate allocations due to inadvertently using some captured state in a place where we were trying to avoid that. I fixed that as well.
We were also marking everything complete before we'd cleaned up all resources, which meant that any failures from such cleanup would go unnoticed. I've fixed that to ensure we clean up all state before we officially declare the async operation done.
Commit migrated from https://github.com/dotnet/corefx/commit/650cb2698f02635ea69da3ec9637f6ad9e7229c5
The only change to the logic is that the tests now use RemoteInvoke to run each test in its own process, so as to avoid EventSource and DiagnosticSource interference.
Also removed the xunit attributes from the assembly that prevented parallelization. It's no longer needed as of this change.
Commit migrated from https://github.com/dotnet/corefx/commit/542d85e15bfe5fdeb59d7e6f7fb3b15a08640ebd
The TLS context is set up to make this chain:
CN=http2.akamai.com, O=Akamai Technologies Inc., L=Santa Clara, S=CA, C=US
CN=Verizon Akamai SureServer CA G14-SHA2, OU=Cybertrust, O=Verizon Enterprise Solutions, L=Amsterdam, C=NL
CN=Baltimore CyberTrust Root, OU=CyberTrust, O=Baltimore, C=IE
CN=GTE CyberTrust Global Root, OU="GTE CyberTrust Solutions, Inc.", O=GTE Corporation, C=US
making use of the cross-signed Baltimore CyberTrust Root certificate.
Unfortunately, that chain is not trusted on Debian 8.4 (GTE CyberTrust Global Root was removed), and a combination of data and installed software results in the valid chain using the self-signed Baltimore root is never built.
To prevent this situation from making the tests show as a failure, selectively allow Debian 8 to succeed on an incomplete chain.
Commit migrated from https://github.com/dotnet/corefx/commit/316370ac1d58a90f6788ecb924b41cc93aec043e
CoreFX tests will now compile against netstandard1.3 as references and will now deploy for the platforms listed in their respective supports clause, provided the appropriate TestTFM is passed in. The supports are fully listed inside runtime.json in the new TestSuite metapackage in buildtools. The tests will default to netcoreapp1.0 as their TestTFM.
CoreFx test projects now utilise a test csproj to pkgproj reference which determine the appropriate compile time and runtime assets needed.
Dependent on https://github.com/dotnet/buildtools/pull/770
Commit migrated from https://github.com/dotnet/corefx/commit/b4841454de7ddcd75e92e2bb24ebb238a82df43f
MultipartContent doesn't override CreateContentReadStreamAsync. This means that a ReadAsStreamAsync operation will end up serializing all of the content to a MemoryStream, which can be problematic for very large content, preventing such content from being uploaded with some HttpClientHandlers. This change overrides CreateContentReadStreamAsync to use a custom stream that delegates to all of the inner streams from each content.
Commit migrated from https://github.com/dotnet/corefx/commit/b1d20b427fb68f468740b5ad55c04d41064e1396
We've been plagued for the last few weeks by CI jobs on Windows that hang in System.Net.Http.Functional.Tests, causing the whole job to fail after it eventually times out after two hours. I managed to find a CI server that had a process still alive after having experienced the hang and was able to debug it. The Dispose_DisposingHandlerCancelsActiveOperationsWithoutResponses test is creating three loopback servers and using HttpClient.GetAsync to access each of them. It's then Disposing of the HttpClient to verify that requests which should have been canceled are canceled. The problem is that there's a race condition. We may end up canceling one of the requests before it even gets a chance to connect to the server. As a result, the server remains stuck in an asynchronous accept on it socket, and at the end of the test, we're waiting for all of the servers to shutdown... deadlock. The fix is simple: don't wait for the servers... we don't care about their results, and they'll all be torn down when we exit the inner delegate.
Commit migrated from https://github.com/dotnet/corefx/commit/99b7b5a9b9f23e8b94bd8c75ecf86bb71d24b6c4
libcurl wants to be able to call back to CurlHandler at arbitrary times to ask it to seek in the request content stream. This can happen for redirects, for auth challenges, etc. Handling this has been a challenge due to the way HttpContent is designed.
HttpContent provides two modes for accessing the data via Streams:
- Push: You call CopyToAsync(Stream, TransportContext) to have the data from the HttpContent copied into the target stream.
- Pull: You call ReadAsStreamAsync to get a stream that represents the data in the HttpContent, and you can read from that.
The pull model is ideal for libcurl. It allows us to get a stream which we can jump around in if it's seekable, and we can request a new stream if it's not seekable. The push model is challenging, as we're forced to put in plcae an intermediary, a push-pull adapter that let's us effectively read from a type that's trying to write to us.
There is however an issue with the pull model. With the push model, StreamContent's CopyToAsync tracks whether the stream has already been accessed, and if it has and if it's non-seekable, trying to call CopyToAsync again throws to alert you that rewinding simply isn't possible. The pull StreamContent's ReadAsStreamAsync, however, does not. This means that if you get a non-seekable stream and you call ReadAsStreamAsync again, you get back the same stream (or, rather, a read-only wrapper around the same stream) that is at the existing position rather than the original position. Then resending that content will end up sending less data than it should have, resulting in failure, just not abruptly.
That issue caused us to go down the path of the more challenging push model and trying to use CopyToAsync. However, recently that's been shown to have even worse effects. We don't have the ability to cancel CopyToAsync, which means that if the first copy is still in-flight when we try to seek, we have no choice but to fail, even if the stream is actually seekable, as most are (e.g. memory streams, file streams, etc.) This can happen deterministically in cases where libcurl decides to redirect before it's fully sent the request content, but it's also prone to race conditions even for cases where the request content is fully sent but the copy hasn't been officially marked as complete by the time libcurl tries to redirect. ASP.NET recently began hitting this frequently on some machines in their lab runs.
While there's no perfect solution, the best solution is to go with the one that's the simplest code, the fastest code, and the deterministic outcome: the pull model. This commit deletes a whole bunch of code that was there in support of the push model, adds just a bit of code to enable the pull model, and fixes up a couple of tests that were written to expect the push model.
Commit migrated from https://github.com/dotnet/corefx/commit/74cd3d619b3d3b1e963ca8bd33749af3aec99c05