This test wasn't actually running because it was incorrectly annotated as a [Theory] but without any data. When changed to be a [Fact], it failed because the test depends on the Socket's listening backlog being filled so that a subsequent connect will time out, but Listen(1) doesn't actually force the OS to use a queue of max size of 1, its just advisory. The fix is to flood the server with requests that don't get accepted, filling the listen queue.
Commit migrated from https://github.com/dotnet/corefx/commit/883a32630a21c83bb91735cebf1d87c76691bd7d
If something unexpectedly goes wrong in our worker loop, and in particular when queueing a replacement worker, we may end up leaving a request sitting in the request queue with no one to process it. This commit ensures that if something does go horribly wrong, the agent is left in a state where subsequent incoming requests can still kick off another worker, and in the meantime any pending requests in the queue are failed, so that code blocking on them doesn't simply hang.
As I was looking at this and some of our dispose usage, I also cleaned up a few places that could be simplified using the C# 6 ?. syntax.
Commit migrated from https://github.com/dotnet/corefx/commit/ad4afcf61aa62aa85c26b2c058953fe18328ffd4
The CurlHandler work is sometimes hitting brief pauses unnecessary, where it finishes processing the last active request and then calls curl_multi_wait to wait for more work to do. This is purely wasteful and can be skipped when we know there aren't any more active operations.
Commit migrated from https://github.com/dotnet/corefx/commit/8075d3a1b54121b9f656e033fe6308de05eb7381
Now that we're allowing the multi handle to live for as long as the CurlHandler, the DNS cache timeout starts to matter much more. libcurl will cache the results of DNS lookups, and by default will timeout entries after 1 minute. Our Windows implementation defaults to 10 minutes, so I'm extending our Unix implementation to match that.
Commit migrated from https://github.com/dotnet/corefx/commit/184792734ebb89d95425e37c7d1631a84e05e624
Today each time we spin up a worker in CurlHandler, we create a new SafeCurlMultiHandle, use it for the lifetime of that worker, and then when the worker goes away, the multi handle is torn down. This was done for simplicity of resource management, but it also leads to significant overheads due to everything that libcurl caches associated with the multi handle, e.g. when the multi handle goes away, so does it connection cache, its DNS cache, etc. This commit addresses that by extending the lifetime of the multi handle to match that of the CurlHandler. We still lazily create the handle when the first worker is spun up, but then we keep it alive until the handler is disposed. As before, we ensure that as long as a worker is running, the multi handle will be kept alive, even if the handler is disposed.
Commit migrated from https://github.com/dotnet/corefx/commit/8d911b76c2ba05eb1ef22cebd6fef5cad2a21893
There were a handful of test projects that just didn't have .builds
files and some other cases where the data in the .builds file did not
match what was in the UnsupportedPlatforms metadata.
Some of the projects said the tests were unsupported on FreeBSD and
nowhere else. Given that these tests should most likely be supported on
FreeBSD (given that OSX and Linux both supported them), I've not moved
that logic into the .builds files.
If there ends up being a specific set of tests that the FreeBSD port
team wants to disable long term, they can add .builds metadata as they
see fit.
Commit migrated from https://github.com/dotnet/corefx/commit/836c6f12e1a05749d7d5bf27d02bd28f98eb76aa
CurlHandler was explicitly setting the Content-Length header, which overrides libcurl's default handling of Content-Length. This means that Content-Length was being sent even when it shouldn't have been, such as when redirecting a POST to a GET. This commit changes how we handle Content-Length. Rather than explicitly setting the header ourselves, we let libcurl do it, and we tell it the length of the input via the INFILESIZE and POSTFIELDSIZE options.
A few other issues addressed as well in the act of fixing this:
- A SafeHandle wasn't being disposed of in the case of a redirect.
- 301 wasn't being considered a redirect status code for the purposes of credential handling.
- Some code could have been a bit cleaner.
Commit migrated from https://github.com/dotnet/corefx/commit/864f0926754fbb781b81b543f972e069f6295a0c
Add CurlHandlers.SslProtocols option
With libcurl on the OpenSSL backend (the recommended configuration), we can provide full-fidelity for this option. With libcurl on another SSL backend, we can allow choosing all three TLS protcols or just one TLS protocol, but we can't support selecting two of the three.
Commit migrated from https://github.com/dotnet/corefx/commit/bd79e80f25923e0f2e29e07aba60b03070596bb4
- Perf. We were creating a SafeHandle for the SSL context, but it didn't own the underlying handle, and we were then just passing it back to two native functions. We can skip the SafeHandle altogether, saving on the allocation and marshaling costs for all SSL connections.
- Error handling. For robustness, we should be ensuring that any problems related to getting the state from the GCHandle result in returning an error code rather than propagating an exception, which will likely crash.
- Naming. The name of the callback method is no longer correct, as it's use for more than just cert verification.
Commit migrated from https://github.com/dotnet/corefx/commit/b736f46255000a40219f3c5be864782cdb450d59
The HttpContent ReadAsStringAsync and ReadAsByteArrayAsync methods ensure that the data is loaded into the buffer, via an async operation, and then process that buffer synchronously. This refactors that synchronous processing into its own method, and changes ReadAs methods to use a simple async method that does the await on LoadIntoBufferAsync and then calls these methods. This avoids any task-related allocations if the data is already buffered. It also makes those synchronous methods callable from the GetString/ByteArrayAsync directly, such that these methods can avoid an extra layer of continuations, as they already do their own buffering.
Commit migrated from https://github.com/dotnet/corefx/commit/1334af155c3892a45e42505a85a1eaf194344572
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