With the expectation that we'll want to expose this in 2.1, implement Expect100ContinueTimeout and ConnectTimeout. The members are currently internal but can be flipped public easily once the APIs are approved. This also fixes an issue with cancellation around the connect phase, where a cancellation request that came in during the SSL auth phase would not be respected.
Commit migrated from https://github.com/dotnet/corefx/commit/01fa16ffd618846a913cad719269c8bb441ceb28
* Improve test coverage of SocketsHttpHandler
Used code coverage information to write targeted tests to cover various uncovered paths in SocketsHttpHandler. Along the way fixed a variety of issues relating to throwing the wrong exception type, a stack overflow due to an unexpected recursive call, etc.
* Address PR feedback
* Re-disable a test
Commit migrated from https://github.com/dotnet/corefx/commit/39677219aaeab8c6591b625ab2874acb17eaf4ef
* Enable SocketsHttpHandler cancellation support
This change significantly improves the cancellation support in SocketsHttpHandler. Previously we were passing the CancellationToken around to every method, eventually bottoming out in calls to the underlying Stream which then ends up passing them down to the underlying Socket. But today Socket's support for cancellation is minimal, only doing up-front checks; if cancellation is requested during the socket operation rather than before, the request will be ignored. Since HttpClient implements features like timeouts on top of cancellation support, it's important to do better than this.
The change implements cancellation by registering with the CancellationToken to dispose of the connection. This will cause any reads/writes to wake up. We then translate resulting exceptions into cancellation exceptions. When in the main SendAsync method, we register once for the whole body of the operation until the point that we're returning the response message. For individual operations on the response content stream, we register per operation; however, when feasible we try to avoid the registration costs by only registering if operations don't complete synchronously. We also account for the case that on Unix, closing the connection may result in read operations waking up not with an exception but rather with EOF, which we also need to translate into cancellation when appropriate.
Along the way I cleaned up a few minor issues as well.
I also added a bunch of cancellation-related tests:
- Test cancellation occurring while sending request content
- Test cancellation occurring while receiving response headers
- Test cancellation occurring while receiving response body and using a buffered operation
- Test that all of the above are triggerable with CancellationTokenSource.Cancel, HttpClient.CancelPendingRequests, and HttpClient.Dispose
- Test cancellation occurring while receiving response body and using an unbuffered operation, either a ReadAsync or CopyToAsync on the response stream
- Test that a CancelPendingRequests doesn't affect unbuffered operations on the response stream
There are deficiencies here in the existing handlers, and tests have been selectively disabled accordingly (I also fixed a couple cases that naturally fell out of the changes I was making for SocketsHttpHandler). SocketsHttpHandler passes now for all of them.
* Add test that Dispose doesn't cancel response stream
Commit migrated from https://github.com/dotnet/corefx/commit/53be85c2fe473fdea8c001e3d9fd81dd478b858e
When MaxConnectionsPerServer is set to anything other than int.MaxValue, the SocketsHttpHandler pool keeps track of the number of connections handed out, and this count is updated when a connection is disposed. But if a response stream isn't disposed of, resulting in the connection never being disposed of, the count may never be updated. This fix adds an HttpConnection derived type that simply adds a finalizer, making it pay-for-play when MaxConnectionsPerServer is set to something other than the default.
Commit migrated from https://github.com/dotnet/corefx/commit/ffa5db357252913c52e0cdad63c1573020185f6e
* Clear initlocals in a bunch of assemblies
Sets
```XML
<ILLinkClearInitLocals>true</ILLinkClearInitLocals>
```
for the assemblies:
- System.IO.FileSystem
- System.IO.Pipes
- System.Memory
- System.Net.Http
- System.Net.Http.WinHttpHandler
- System.Net.NameResolution
- System.Net.NetworkInformation
- System.Net.Primitives
- System.Net.Requests
- System.Net.Security
- System.Net.Sockets
- System.Net.WebSockets
- System.Net.WebSockets.Client
- System.Net.WebSockets.WebSocketsProtocol
- System.Private.Uri
- System.Runtime
- System.Runtime.Extensions
Eventually we'd ideally set the flag for the whole repo; these are just the ones I audited thus far. In a few cases I had to make code changes to adapt, where the code was expecting stackallocs to be cleared. I also looked at a few other projects (System.Collections, System.Runtime.Numerics) where there were more significant dependencies on clearing and I decided to leave those for later.
* Disable ILLinkClearInitLocals running on Unix until it works there
Commit migrated from https://github.com/dotnet/corefx/commit/f0a032e4a1c8a848574a5db3e69e8a6fa0aae91e
When a user has added an Authorization header to a request via the HttpClient DefaultRequestHeaders Authorization field, ensure that header is stripped on redirect.
This change adds a method to strip Authorization headers that were set via the HttpClient DefaultRequestHeaders Authorization field, and calls that method on redirect. In addition, it adds a test to ensure that this behavior occurs as expected.
The new method ResetAuthRequestHeaders is based on the method ResetCookieRequestHeaders, which is used to reset cookie headers on redirect.
The issue also occurs in CurlHandler, but the fix is slightly different. Rather than remove the headers that have already been set, we can instead just skip them when copying over the new headers to the redirected request.
Fixesdotnet/corefx#26426
Commit migrated from https://github.com/dotnet/corefx/commit/7e6396b2bcb4d372b199cfb7b3b13e290c441124
For the common case, we can just use a singleton cached instance with settings computed when the pool is created. Only if it's accessed via HttpClientHandler with a server certificate callback do we need to still create an instance per request, in order to wrap the delegate to provide the expected semantics.
Commit migrated from https://github.com/dotnet/corefx/commit/911d4aff095f1c8648334d4a221c217cd09e466b
- Renames ManagedHandler to SocketsHttpHandler
- Moves a bunch of files around accordingly
- Updates System.Net.WebSockets.Client to use SocketsHttpHandler directly
- Updates System.Net.Http's tests with a reflection-based rather than TLS-based method for instantiating an HttpClientHandler backed by a SocketsHttpHandler.
Commit migrated from https://github.com/dotnet/corefx/commit/852d40e723c849e8d0c486676d40332343e12ca5
Simplifies the public ManagedHandler API by using the now existing SslClientAuthenticationOptions bag as a way to pass in all of the SSL-related options. This also means we start enabling anything already on SslClientAuthenticationOptions as well as anything new added in the future, without requiring new API on ManagedHandler.
In the process of doing that, also added an implemention of HttpClientHandler.ClientCertificateOption for ManagedHandler; previously Automatic was just ignored.
I also removed UseDefaultCredentials from ManagedHandler, as it's ignored and not relevant until additional auth mechanisms are supported.
Commit migrated from https://github.com/dotnet/corefx/commit/ee7d4599ed8ac2bca796cc7627ebf33b75f8b7cb
* Add Connection{Idle}Timeouts to ManagedHandler
We can agree on the final name(s) when going through API reviews. For now, I chose what I think are easy to understand names:
- ConnectionTimeout: how long a connection is allowed to be reused before it's no longer usable, defaults to infinite
- ConnectionIdleTimeout: how long a connection can sit idle in the pool before it's no longer usable, defaults to 2 minutes (that value was hardcoded before this change)
* Address PR feedback
Commit migrated from https://github.com/dotnet/corefx/commit/1a5cdbe1af7a5befe16bb4581f29f0af64f4a101