CancellationToken.Register captures the current ExecutionContext and uses it to invoke the callback if/when it's invoked. That's generally desirable and is the right default, but in cases where we know for certain the callback doesn't care about EC (e.g. we're not invoking any 3rd-party code), we can use UnsafeRegister instead (newly added in 3.0), which skips capturing the ExecutionContext, as if Capture returned null. This helps few a couple of small costs:
- Avoids thread local lookups to capture the current EC.
- Avoids additional delegate invocations and thread local gets/sets to invoke the callback with the captured EC.
- Avoids holding on to the EC in case it's needed, which can potentially keep alive an unbounded amount of state due to AsyncLocals.
Commit migrated from https://github.com/dotnet/corefx/commit/b1a1bfa0997ce35e8540fbbe74276858e315807e
- string.IndexOf(string) and string.IndexOf(string, int) are culture-aware. For most situations in System.Net, that's undesirable and Ordinal is both more correct and faster.
- string.IndexOf("c", StringComparison.Ordinal) is better off as string.IndexOf('c') for perf and simplicity.
- string.IndexOf('c') >= 0 is better off as string.Contains('c') for perf and simplicity.
Commit migrated from https://github.com/dotnet/corefx/commit/6acc044c36df2138d86620e2c27d3234c4fa0d3d
* Log exceptions from asynchronously failing Expect: 100-continue sends
When Expect: 100-continue is used, the sending of the request body content is allowed to run concurrently with the receipt of the response headers. If the processing of the response headers encounters an exception, we currently fail to ever join with that send task, which results in a TaskScheduler.UnobservedTaskException event. This PR changes to observe the exception in such cases and log it.
(In doing so, I also came across an async void method, and changed all of the remaining ones in the assembly to be async Task instead. The current implementation of async void isn't any cheaper than async Task, and is actually more expensive in certain ways, plus it unnecessarily interacts with any SynchronizationContext that may exist.)
* Address PR feedback
Commit migrated from https://github.com/dotnet/corefx/commit/0bc3ee4a4f1debaa0dd3c957d5201ae2a47757a1
* improve error handling on failed Http/2 handshake
* remove extra space
* add test for HTTP2 client talking to HTTP1 server.
* fix broken tests
* improve exception handling
* update exception handling
* ws update
* fix bad merge
* update test and changes from dotnet/corefx#37223
* use _abortException only if the stream is not aborted already
* fix IsAborted check
* updates to syncup with recent changes
Commit migrated from https://github.com/dotnet/corefx/commit/c412fa7dc60559dc38050c63b2e34b5156a2499e
The server in the test was sending down the entire response and then trying to read the data from the client. If the client sent its whole payload and read the response before the server go around to doing the read, the client might close the connection, in which case the read might fail. The fix just changes the server code to only send down the headers, then read the data from the client, and only then finish the response, at which point it's fine if the client closes the connection.
Commit migrated from https://github.com/dotnet/corefx/commit/772aa2d1029caa8be3b360168b63dadd42ae788b
We now respect the version: if it's 1.1, we use 1.1, and if it's 2.0, we try to use HTTP/2. At the same time, the default for HttpRequestMessage.Version goes back to 1.1, so to use HTTP/2, you opt-in by setting the version explicitly to 2.0.
Commit migrated from https://github.com/dotnet/corefx/commit/d69d441dfb0710c2a34155c7c4745db357b14c96
This PR implements the new static HttpClient.DefaultProxy property which was approved
during API review.
Modify the SystemProxyInfo.ConstructSystemProxy method to a Singleton.
Modify SocketsHttpHandler to use the HttpClient.DefaultProxy property.
Rename the HttpSystemProxy class to HttpWindowsProxy.
Add some HttpClient tests for the new property.
Closesdotnet/corefx#36553
Commit migrated from https://github.com/dotnet/corefx/commit/c661db6ce2da91033671c0000afc185de1ad3fe9
In order to provide a consistent experience with the HttpClient IWebProxy objects,
the returned internal proxy object which represent the system/platform proxy settings
should never be null. If the platform's settings indicate that no proxy is being used,
then return an instance of the internal HttpNoProxy object.
Note that even if the platform settings indicate that a proxy could be used, any
particular Http request might still not go thru a proxy. The final determination of
what proxy is being used for a request is still governed by the return of the
IWebProxy.IsBypassed and IWebProxy.GetProxy methods.
Contributes to dotnet/corefx#36553
Commit migrated from https://github.com/dotnet/corefx/commit/830bf7fc0346d9453d85ad4ec726c92ac3e72998
A few tweaks:
- We hand out the ChunkedEncodingWriteStream and ContentLengthWriteStream to an HttpContent.SerializeToStreamAsync. If that user code disposes of the Stream, then we end up null ref'ing when we try to finish the processing. We should instead throw a more descriptive error about the misuse.
- Make the non-pooled response streams slightly more robust against concurrent disposal (which is not a supported use, but it happens).
Commit migrated from https://github.com/dotnet/corefx/commit/422565673deb849b42f656f4a68bec791ee4bed9
* Clean up HTTP/2 protocol exception handling
- Propagate the GOAWAY and RST_STREAM error codes to the calling code via an exception.
- Propagate to the Http2Stream exceptions incurred in the Http2Connection and that result in an Abort.
- Don't Dispose of the Http2Stream before we try to Cancel it.
- Make Http2ProtocolException serializable, as we generally do that for all of our exception types.
- Add tests that verify sufficient HTTP/2 protocol details are in the exception messages.
- Clean up the error message for the exception.
- Separate the error code and exception into their own files.
- Leave Http2ProtocolException internal for now; we can expose it publicly when we design the all-up public error model for HttpClient.
* Address PR feedback
Commit migrated from https://github.com/dotnet/corefx/commit/7237da089c09531c36a2110a3bdd9ccaf7c05d77
Changed the Windows version of SocketsHttpHandler so that it will honor the same environment
variables similar to the Linux and OSX versions. If the environment variables are not set
then it reverts back to the Windows WinInet/IE settings behavior.
I added several kinds of unit and end-to-end tests to verify the SystemProxyInfo class is
making the correct choice for all the different platforms regarding whether the
HttpEnvironmentProxy or platform proxy (HttpSystemProxy for Windows and MacProxy for OSX)
is used.
Fixesdotnet/corefx#37187
Commit migrated from https://github.com/dotnet/corefx/commit/4f27d0f84c17850ef02ea7b330908fddcdf73c86
This is a redo of PR dotnet/corefx#35887.
Added support for uppercase HTTP_PROXY and NO_PROXY environment variables.
The uppercase versions are being used by Docker and other tools. This now
completes the set of environment variables that are already handling both
lower and upper case variants.
Note: This HttpEnvironmentProxy class is currently only used on Linux and OSX.
It is not used on Windows yet. I do plan to add support for this to Windows
at a later time, see dotnet/corefx#37187.
Added detection of CGI environments in order to suppress using the uppercase
HTTP_PROXY environment variable. See https://httpoxy.org/
Fixed some typos in the CSPROJ of the Http Unit Tests.
Commit migrated from https://github.com/dotnet/corefx/commit/dfe7593397bf5c598548486c6cb774ebf60e9d69
SocketsHttpHandler was not using the proxy bypass list specified in IE settings in cases where
there is a combination of both 'AutoDetect' and 'Manual' settings in the IE settings dialog.
SocketsHttpHandler uses WinHttpHandler's WinInetProxyHelper class as part of the HttpSystemProxy
class. But it was consuming it in an incorrect way. WinInetProxyHelper was originally written to
be used only with the rest of the WinHTTP stack for WinHttpHandler. When WinHTTP GetProxyForUrl() was returning a ProxyBypass string, HttpSystemProxy was ignoring it. It was
assuming that the string for Proxy was correct. But in cases where ProxyBypass is returned, the
Proxy string is only used if the destination uri doesn't match any of the strings in the ProxyBypass
list. That logic would normally be handled automatically by WinHttpHandler. But HttpSystemProxy
was simply discarding the ProxyBypass string returned by WinHTTP GetProxyForUrl().
In order to address this fix, I added new tests for HttpSystemProxy. I utilized the existing mock
layers for registry and WinHTTP interop that are contained in the WinHttpHandler Unit Tests. It might
seem weird to have tests for the internal HttpSystemProxy class located in the WinHttpHandler folder.
But, we already pull in the source code for WinInetProxyHelper from WinHttpHandler into the
SocketsHttpHandler compilation. Using these Unit Tests with the mocking layers allows us to test a
broad range of scenarios (registry settings and network failures) that are normally difficult to test.
I have a plan, though, to refactor the system proxy code and tests as part of implementing dotnet/corefx#36553.
That will result in the system proxy code and tests ending up in a more logical place.
Fixesdotnet/corefx#33866
Commit migrated from https://github.com/dotnet/corefx/commit/6da9a3b80c2ac56c641fa22157607860f6b9387f
* Fix SocketsHttpHandler response streams to do sync I/O in sync methods
SocketsHttpHandler hands back response Streams for reading response body content. While we encourage developers to use the async Stream APIs, Stream does expose synchronous APIs, yet the current implementations are just wrapping the async ones and doing sync-over-async.
This fixes the response stream synchronous APIs to be sync down to the underlying networking stream.
It also fixes a couple other minor issues, e.g. Flush{Async} on read-only stream should be nops rather than throwing, we should include an error message about a stream being read-only when trying to write to it, etc.
* Address PR feedback
Commit migrated from https://github.com/dotnet/corefx/commit/315fd00cb2465390900209919cdb2bf6bc44ae0e
When the client websocket was going through a proxy, it would sometimes transition to the
'Aborted' state and not the 'Closed' state after a successful closing handshake.
I first opened this bug a year ago but finally was able to get back to it and root cause the
problem. The problem only occured with the managed websocket. The .NET Framework did not have
this problem. And some proxies didn't cause a problem with managed websocket (such as Fiddler).
The root cause is a misinterpretation of RFC 6455, Section 7.1.1. This describes the behavior
of how the websocket's TCP connection should be closed. The most graceful way is to wait for
the server to initiate the close of the socket. In cases where it is taking too long to wait
for the server to start the TCP close, then the client can start the TCP close of the socket.
But no matter how the socket is finally closed, the websocket state should transition to 'Closed'
as soon as a valid closing handshake was performed (i.e. close frames both sent and received).
And this occurs before any logic for closing the TCP connection.
The code in managed websocket has a timer to wait 1 second for the server to start a close. If
the timer finishes, then the managed websocket calls an Abort() method to close the socket. This
ends up transitioning the websocket to an 'Aborted' state which is incorrect. The state should
only be moved to the 'Aborted' state if a closing handshake was not completed.
I added a new test to support this change and moved the LoopbackProxyServer code to Common.
Fixesdotnet/corefx#28027
Commit migrated from https://github.com/dotnet/corefx/commit/0c9684c2c4057908cf697a366a3b1527326e507d
* Allow cookies with spaces in the name
* Apply suggestions from code review
Co-Authored-By: PaitoAnderson <[email protected]>
* Added supporting HttpClient functional cookie test
* Updated UWP cookie class and added additional tests
* Prevent cookies with spaces at the start or end
* Forgot UWP
* Fix could allocate on trim
Co-Authored-By: Ben Adams <[email protected]>
* Better cookie space start/end check
Co-Authored-By: Ben Adams <[email protected]>
* Bring checks into line
Co-Authored-By: Ben Adams <[email protected]>
* Skip test on .net framework
* Skip additional tests on .net framework
* Fix typo
Commit migrated from https://github.com/dotnet/corefx/commit/8a22438a1af870cdd04eaf798b155b8a1ab04cb4
Several of the HttpClient POST scenario tests were failing with HTTP status code 500. This was caused
by the Azure remote endpoint generating OutOfMemoryException at times. The tests are sending
large amounts of request body data to the Echo endpoint. That endpoint will respond back with all
the headers and request body data serialized into a JSON payload. The OOM exceptions were
coming from the Newtonsoft JSON serialization code currently used by the server endpoint.
These tests don't really require that the request body data be sent back. The purpose of the tests is
to verify that the request body payload was correctly sent by the client. We already have an endpoint,
VerifyUpload, that can do that without echo'ing back the large request body data.
This PR modifies the tests to use that endpoint. This should mitigate the OOM exceptions
currently being generated in the server-side code. Additional mitigations/fixes will be done later on the server-side code to improve robustness. But fixing the tests to be more streamlined is goodness and will result in these tests being faster and more stable.
Closesdotnet/corefx#36782
Commit migrated from https://github.com/dotnet/corefx/commit/288ce93787799052705814876d6e5ccb0523797d