Copying between streams represents one of the larger sources of memory allocations and copying in HttpClient. HttpClient has its own stream-to-stream copy implementation, but there are ways we can significantly improve it based on typical usage within HttpClient. This commit does the following:
- Previously it would special case when both the source and destination stream were MemoryStreams, but even in that case it would allocate a temporary buffer and do a Read/Write loop to copy between them, and even then it's relatively rare to copy from a MemoryStream to a MemoryStream. Instead, if the source stream is a MemoryStream and we can get its buffer, we can do a single write to the destination (regardless of what kind of stream it is), avoiding the need for multiple read/writes and for the temporary buffer. If we can't get its buffer but its length is known to be less than or equal to the requested buffer size and it's positioned at the beginning, we can simply ToArray the source and still do a single write to the target.
- If the destination is the special LimitMemoryStream, used for buffering output, and if that LimitMemoryStream was pre-sized based on the response having a Content-Length, then rather than allocating a temporary buffer and repeatedly copying from the source stream into that buffer and copying from the buffer into the memory stream, we can instead give the destination's buffer directly to be read into by the source, avoiding the temporary buffer and the extra copies. This is a very common case.
- If the source does not need to be disposed after the copy, rather than doing our own copy loop, we can instead ask the source to use its CopyToAsync implementation to copy to the destination stream. Worst case this will be exactly what we were previously doing, but best case the source has its own CopyToAsync override that's more efficient.
- When constructing a LimitMemoryStream, if the Content-Length isn't known, we were explicitly passing a 0 capacity to the base, which causes it to allocate a new zero-length array; I've added a ctor to avoid that.
Also avoids some unnecessary array allocations and copies while getting data from buffered content.
Commit migrated from https://github.com/dotnet/corefx/commit/d2502ce05032ae83c5675e2cd4b27ca2fb6706fa
HttpClient on Unix has a thread dedicated to multiplexing the processing of any number of concurrent requests on that client. When a request comes in and that worker thread isn't running, a worker is spun up, and that worker will service any number of requests that come in while it's up. Then, when requests quiesce and there's no more processing to be done, the worker goes away. If more work later arrives, a new worker is spun up.
This is done to avoid burning a thread per HttpClient when there's no work to be done on that client. It works well for bursts of activity as well as for sustained concurrent usage. However, if the client is used for a sequence of requests, one after the other, there's a good chance that the worker will go away between requests, requiring a new worker to be spun up for each. This has non-trivial costs, both in terms of CPU processing and in terms of managed and native resource allocation/deallocation.
This commit enables a worker to briefly pause before exiting, blocking for a short period of time for new work to arrive rather than simply exiting.
Commit migrated from https://github.com/dotnet/corefx/commit/d3fc3d151da4d1573372b2ab82c7b2aa52eaa5d8
This commit addresses just the HttpClient class and not the rest of the library, but those can follow. When HttpClient was initially written, async/await didn't exist, and it was written to use ContinueWith. By switching to async/await, we can both improve the readability and maintainability of the code and improve performance.
Commit migrated from https://github.com/dotnet/corefx/commit/3860eab0d26527b0e5b931ef4084d40f2886ccc7
More low-hanging fruit. When building up its output, MultipartContent is already using a StringBuilder, but it's creating temporary intermediate strings which it then writes to the StringBuilder, rather than just adding the constituent pieces to the StringBuilder directly. And when computing the length of the output, it's again generating lots of intermediate strings, when it could instead just measure the size of each of the constituent pieces.
Commit migrated from https://github.com/dotnet/corefx/commit/47e9bbeab66b38b21e512243b5ed16590156d35b
When we get called back by libcurl to verify the server's certificate, we try to take a fast path where we use OpenSSL's certificate verification rather than doing our own, which is more comprehensive but also slower. However, when we added that fast path, we inadvertently ended up not setting the Channel Binding Token in that case, only doing so in the fallback path. This fixes that, moving the configuring of the CBT up to before we ask OpenSSL to verify the cert.
Commit migrated from https://github.com/dotnet/corefx/commit/bb26ba8b5375e76d00db8b371abb6e273e7b4019
Today CurlChannelBinding creates the result of ToString when the token is set into it. Getting the description of the binding is rare compared to setting a token into the binding, so just create the string when it's requested.
Also a bit of miscellaneous cleanup in related code.
Commit migrated from https://github.com/dotnet/corefx/commit/49c31a87c41c326788162b7aa148b01f1d89c796
This SafeHandle is only needed when using HttpContent-derived types that care about TransportContext, which is not a common case. Lazily-allocate it.
Just as an example, before this change, it was being allocated during ~50 tests. After this change, it's allocated just once, in the test explicitly verifying the binding.
Commit migrated from https://github.com/dotnet/corefx/commit/900a3664cf4c574e26144b3cf46db2bcf93fe394
This test failed once, and I expect it was just due to a network blip that caused the request to fail, but since we're not currently checking the http status code, I can't be sure. As such, I'm just adding another assert for the expected status code, and if this repros again, we'll know whether that's the issue.
Commit migrated from https://github.com/dotnet/corefx/commit/50b2ce5699eb7034a033b3d125ad62bf03b23458
Currently the shim exposes a single method to get just the information that was needed when this was written. Every time we need to get additional version info, we need to change the existing signature (which won't version well) or add additional similar signatures (will which be confusing). Instead, I've changed it to have more fine-grained entrypoints, each exposing a particular piece of info. We may end up making a few more interop calls on first-use, but the overhead is negligable.
With that scheme, I've then exposed additional entrypoints for getting version information, in order to provide better tracing and exception error messages.
This is then also used in tests to only run tests when certain features are available.
Commit migrated from https://github.com/dotnet/corefx/commit/29b0dc7b810776178bf42851064c397e47c4d0b8
Setting VERIFYPEER to 0 not only tells libcurl/OpenSSL not to do verification of the peer, but also to ignore any failures from our callback.
Also add a check for revocation checking and throw a PNSE if the backend doesn't support us registering the necessary callback.
Commit migrated from https://github.com/dotnet/corefx/commit/e3a4845289edf4ffa3f4da41efff52e3f6363c6c
I made a previous change to decrement assembly versions to enable our packages to be consumed
in desktop and redistribute netcore50 assemblies: https://github.com/dotnet/corefx/pull/6902
That caused a lot of runtime breaks because folks who were built against old packages would
get ref-def mismatches at runtime. Rather than keep everything broken until the whole stack
updates I'm moving the implementation assemblies back to the old versions. This will let folks
move to the new packages with the lower versioned ref-assemblies. Once everyone is on the new
packages we can undo this change.
Change is going directly into TFS to make tonight's build. I will deal with the fallout during mirror.
[tfs-changeset: 1588328]
Commit migrated from https://github.com/dotnet/corefx/commit/0f268ed1079c182d71cdf49cde3f339a01d67863
- Disable libcurl's cert verification when we're going to do it all ourselves
- Disable application policy for the single-cert manual case
- Avoid allocating a List when a simple array suffices
- Avoid an array allocation when a stack handle is invalid
- Dispose of a chain when Build fails in client callback
- Dispose of chain and certificate in client callback
- Avoid explicitly throwing an exception across an interop boundary when an invalid SSL pointer is provided
- Use named return constants in client callback for readability
- Dispose of certificates retrieved from store
- Remove some unnecessary code in GetRequestCertificateAuthorities
Commit migrated from https://github.com/dotnet/corefx/commit/027eb1e0dbac9bfa1c9b925fcf84e0566372d916
Adds implementations for:
- ClientCertificates
- ServerCertificateValidationCallback
- CheckCertificateRevocationList
I've explicitly not added these to the contract, as we're not ready for that yet on other platforms, and I've not added tests (though I've done some testing manually, such as verifying that I could run tweaked versions of the WinHttp server callback tests against this successfully).
Commit migrated from https://github.com/dotnet/corefx/commit/8376a43a5c425cc78dfb94c53b6f100eb5146aca
We removed the lifting from the lineup so tests should reference the
latest packages in order to ensure an implementation is available.
The latest pacakges exclude implementation dependencies so I needed to
add these to projects that were depending on them coming in transitively.
Commit migrated from https://github.com/dotnet/corefx/commit/bef9061fd1238adf47b864d8924d121e4134465b
The latest drop of packages no longer includes lifting entries in the
lineup. This causes errors when restoring for a concrete TFM and no
implementation assembly is in the packages.
I addressed this issue two ways:
1. In places where we were targeting a concrete TFM but packaging as
NETStandard, just target NETStandard instead.
2. In places where we were targeting a concrete TFM but that packages
were missing the implementation on that TFM, update the reference to
newer package version where implementation is available.
Commit migrated from https://github.com/dotnet/corefx/commit/dfb30176c6056f5b3427515c4444a9dc31b9ba4f
Microsoft.CSharp, System.IO.Compression, and System.Net.Http are all
reference assemblies in the desktop and mono/xamarin targeting packs.
As such these won't be referenced automatically. Ensure we add a
reference when installing the package.
Commit migrated from https://github.com/dotnet/corefx/commit/eedef71bc2f4fe040ce10a5a59d385b036d3375f
We were getting an assert if we had an authenticating proxy but didn't have any credentials to send. After the initial 407 was received from the proxy, we then tried to set credentials for a retry. But since we didn't have any, we triggered an assert in SetWinHttpCredential.
The fix is to check for null credentials after getting the 407 response. If there are no credentials, then we end the retry loop with the final 407 status code.
Commit migrated from https://github.com/dotnet/corefx/commit/89966ef37a1269471069c2ee14cdb6af51d8a707