On Unix when an HttpClientHandler is disposed, we cancel all outstanding operations associated with that handler. It turns out this isn't the desired behavior (aka the behavior of WinHttpHandler), which is that we should cancel all outstanding operations that haven't yet published its HttpResponseMessage, but for requests where the HttpResponseMessage has been published, it should remain valid.
This commit tweaks the behavior accordingly and adds a test for it.
Commit migrated from https://github.com/dotnet/corefx/commit/f949d92b3ee26168277f56fbd2c6686505ea0b4a
One of the HttpClient client tests hangs on a machine with a single core.
xunit by default uses a SynchronizationContext that has N threads to do
work, where N defaults to Environment.ProcessorCount. Thus on a single
core machine, there's only one thread available to run tests, and if that
thread blocks waiting for an async operation that'll schedule work back to
that context, it'll deadlock.
Commit migrated from https://github.com/dotnet/corefx/commit/916798757c122b81e61666c4f0af0c446d378824
* Improve WinHttpHandler logic regarding default credentials
In some cases, WinHttpHandler was not sending default credentials that were specified inside a custom ICredentials object. In other cases, default credentials would be sent even though CredentialCache.DefaultCredentials was not specified. The code was incorrectly comparing the proxy and server credential (ICredential interface) values against the DefaultCredentials object. Instead, the ICredentials.GetCredential() method should be called to compute the final NetworkCredential object. This takes into account the given uri and auth scheme.
This PR fixes the logic by delay the setting in WinHTTP for releasing default credentials. This ensures that default crdentials are released when requested.
Fixesdotnet/corefx#8176.
* Address PR feedback
Reword comments for clarity.
Add TODO issue.
Sending default creds to proxy only needs MEDIUM security policy.
Commit migrated from https://github.com/dotnet/corefx/commit/90b8ff36c8c9f9cfb1a2cf1302ff7a98ac7801ed
We currently make a lot of calls off-box in our unit tests. By adding SSL support to the loopback server, we should be able to convert many more tests to avoid needing to actually hit the network. This adds such basic support, and converts a few tests to use it.
Also, since I was moving the certificate configuration code to a central location, I fixed an issue related to cleanup.
Commit migrated from https://github.com/dotnet/corefx/commit/b90795e1d5d7e8d8c68b9ca23d53b1ba0e392f46
These tests require the client and server to be on a Windows Active Directory domain. These tests are Windows specific. Currently this cannot be done in the CI system. So, these tests are only enabled when an environment variable is set which points to the test server.
There is a separate todo item dotnet/corefx#4989 to provide more automated scripts for setting up the test server. For now, there is a readme file with some details.
These tests will help support fixes for dotnet/corefx#8176 and related bugs.
Commit migrated from https://github.com/dotnet/corefx/commit/4739da8c8d4965fdaf2fc45ae915297208672d15
HttpContentAsyncStream is used by CurlHandler as the request content stream, with writes to it done via HttpContent.CopyToAsync. We expect the writes to all be asynchronous via WriteAsync. However, it's possible for an override of CopyToAsync to use Write, and if it does, and if we just delegated from Write(...) to WriteAsync(...).GetAwaiter().GetResult(), we would likely deadlock, as the CopyToAsync call is invoked from the single thread responsible for processing libcurl interactions, including supplying the reader that would unblock the writer. To handle this, we have two options: schedule the CopyToAsync to be invoked asynchronously just in case it does a synchronous write, or implement the write to buffer the data.
The former is what was done in https://github.com/dotnet/corefx/pull/4095. It adds some cost to the expected / common case, but more importantly and worse it can also lead to concurrency problems and spurious exceptions, as for example if the request is faulted/canceled between the time that a task to invoke CopyToAsync is scheduled and the time that it actually executes, we could be invoking CopyToAsync on a disposed HttpContent; worse, we could
end up invoking CopyToAsync while it's being disposed, which could lead to arbitrary state corruption, and which means we can't guard against this with checks to see if the task has already completed. The only way to fully guard against that is via a lock, but such a lock would itself lead to deadlocks, as the reader that would unblock the writer would have to take the same lock.
In comparison, buffering has the downside of taking up more and arbitrary amounts of memory to store all of the input. But, we expect synchronous Writes to be rare, and code shouldn't be doing it anyway; if it does, the fix should really be to switch to asynchronous writes. This commit changes from the first approach to the second, using buffering in synchronous Write calls in order to ensure correctness and to optimize for the expected case.
Commit migrated from https://github.com/dotnet/corefx/commit/1ac3f5e62a0d094e7e4277afc43cb1e0c6260b83
The current tests validating HTTP request message version were depending on a server behavior that is not guaranteed. In particular, HTTP RFC 7230 states that servers are NOT required to respond back with the same minor version if they support a higher minor version. In fact, the RFC states that servers SHOULD send back the highest minor version they support. So, testing the response version to see if the client sent a particular request version only works for some servers. In particular the 'Http2Servers' used in these tests seemed to echo the minor version of the request.
However, depending on network configuration, intermediate proxies can change the HTTP version that ends up getting sent to the destination server. And as a result, we can have unpredictable behavior in these tests.
To fix this, the tests were switched to use a loopback server that has deterministic behavior.
Commit migrated from https://github.com/dotnet/corefx/commit/d1ffcbcc120d3797dadce6196f499a0631e47caf
- The GetAsync_ResponseWithSetCookieHeaders_AllCookiesRead test has been failing with some regularity in CI, with strange GatewayTimeout errors. I don't know why this particular test is so much more susceptible to that, but I've changed the test to use a local loopback server rather than an external site.
- The SendAsync_ReadFromSlowStreamingServer_PartialDataReturned test was relying on timings from the server that might not always be correct, and due to internal differences between curl and WinHttp were very frequently wrong on Unix such that the test was disabled there. I reimplemented it with a local loopback server, too.
- I've added another mini-stress test, with posts instead of gets.
Commit migrated from https://github.com/dotnet/corefx/commit/9b6a0224c3e404afe3f2c58dfb59e2068391c879
We've standardized on using Unix in most cases where we only cross-
compile once for Unix. This change cleans up mentions of Linux/OSX when
we aren't actually cross-compiling for those OSes.
The remaining projects that still cross-compile for Linux/OSX are:
System.Diagnostics.Process
System.IO.FileSystem.Watcher
System.Net.NetworkInformation
The System.Net.Security functional tests seem to still have a distinct
difference in functional behavior on OSX so I have also left these
configured to cross-compile for Linux/OSX.
Commit migrated from https://github.com/dotnet/corefx/commit/16ee317fa4e194ff7cf61e923caf103909e391f1
Several "mini-stress" tests that verify HttpClient doesn't completely fall over under some load:
- Use one HttpClient instance to make many requests serially and in parallel
- Use multiple HttpClient instances to make many requests serially and in parallel
- Ensure that HttpResponseMessages can be GCd even if the associated response hasn't been fully drained. This one fails right now on unix due to an open issue.
Commit migrated from https://github.com/dotnet/corefx/commit/4358712d3db870725a394478702e643018af52bf
* Force CurlHandler active operations to shutdown when handler disposed
If the CurlHandler is disposed of while active operations are in progress, fail them. This was generally already happening, but not reliably, and not with the right exception type (a cancellation exception).
Commit migrated from https://github.com/dotnet/corefx/commit/77cb6a2cddd973aeea08e0ceccb578603c1c1364
* Size CurlHandler's SendTransferState based on content length
libcurl will never request more than 16K at a time from a request content stream, so our transfer state object allocates a 16K buffer to use the first time data needs to be transferred. This size can be efficient in limiting the number of operations required in passing data around, but for small posts, it's an unnecessarily large size. This commit changes the code to inspect the request content length, and if it's known to be less than 16K, that size is used instead.
Commit migrated from https://github.com/dotnet/corefx/commit/3c362496e358a8097538d81cf8aa6f69bb5493cc