This is a large PR and addresses work item dotnet/corefx#3000. I apologize for not being able to structure it in a series of smaller commits.
The bulk of this PR is switching WinHttpHandler to use the async pattern of calling WinHTTP APIs. This involved a significant restructuring of the code. As part of that, I split up the large WinHttpHandler class into multiple, smaller, helper classes.
While the intent of using async WinHTTP calls is better performance and cancel-ability, this PR has not been fully optimized yet. Due to the schedule of RC1 code complete next week, it's important that this PR get in and get mileage on it in the next few weeks.
There are several TODO items that will be done in later PR's. There is still work being tracked in dotnet/corefx#2165 for various follow-up items. For example, re-conciling the best SafeHandle pattern to use for WinHTTP between WebSockets.Client and WinHttpHandler.
I'm currently using a simple Debug.WriteLine for verbose debug logging which can be turned on/off via environment variable. I plan to switch this to System.Net ETW trace loggers in the next commit and/or PR. But the logging has proved very useful in diagnosing issues.
Please provide feedback. Depending on the feedback, some of it will be marked as follow-up items for future PRs.
Commit migrated from https://github.com/dotnet/corefx/commit/d8e5371a5d0f9f4bb3aa85127cb78adf89dde1fb
When CURLOPT_HTTPAUTH is set to 'any', libcurl will always try to make more than one call to figure out the
right authentication scheme needed by the server. This breaks the behavior when PreAuthenticate is set to true.
The fix is to iterate over available schemes and set the auth-scheme flags to only those that are available.
Also, while updating the CredentialCache, we ignore ArgumentException (raised when the cache contains the given url and scheme pair)
Throw exception when network credentials are different for auth schemes
Commit migrated from https://github.com/dotnet/corefx/commit/17fdc686de07b9f9711956eea25e6c0c9042cd17
To get a request content stream, CurlHandler currently uses the
HttpContent's ReadAsStreamAsync, and when libcurl needs to rewind the
stream, CurlHandler seeks on the stream.
Unfortunately, it turns out ReadAsStreamAsync isn't actually intended to
be used by handlers in this manner, and as such there are some
deficiencies that lead to correctness problems. For example, some of the
WCF HttpContent-derived types return non-seekable streams from
CreateContentReadStreamAsync and thus from ReadAsStreamAsync. This means
that, using ReadAsStreamAsync, if we need to resend a request, we need to
call ReadAsStreamAsync again. But ReadAsStreamAsync doesn't do the same
_contentConsumed check that SerializeToStreamAsync does. That means if the
content is actually a StreamContent, whose CreateContentReadStreamAsync
just does ```return Task.FromResult(new
ReadOnlyStream(this._content));```, and if the stream is non-seekable,
then we'll end up getting back the "new" stream but that's already had its
content consumed, and we won't know. We can't even try to do an object
reference comparison, because StreamContent is wrapping the returned
stream in a new ReadOnlyStream instance each time. The recommended
approach for handlers is to use HttpContent.CopyToAsync.
CopyToAsync is a push-based model, letting the HttpContent write to a
target stream. CurlHandler/libcurl have a pull-based model, with
CurlHandler ReadAsync'ing from the request content based on callback from
libcurl. To straddle this divide, this commit adds a specialized
push/pull adapter stream that let's CurlHandler effectively read from the
pushing source. If/when libcurl asks to rewind, the stream resets itself
by doing a new CopyToAsync operation.
This revised approach does incur more costs, however care has been taken
to try to minimize those overheads as much as possible, and the change is
necessary for correctness.
Commit migrated from https://github.com/dotnet/corefx/commit/3678490e2d53d9a9fca6c7e50daadd7152b836f7
Our common tests folder has gotten a bit unwieldy in its organization. This change simply moves files around to better map to how src\Common\src is organized. Files are now stored in a path based on functionality/namespace.
I mostly kept groups of functionality together and did not attempt to reorganize at that level, e.g. I moved the src\Common\tests\System.Xml.XPath\ folder to be src\Common\tests\System\Xml\XPath\ and did not further reorganize all of the folders/files it contains. Such cleanup can be done separately on each individual area.
Some of the files also need to be cleaned up internally, e.g. the namespace (or lack thereof), so I put such things in a path that I thought made sense even if it differs from the current namespace in the file; I wanted this change to be relatively simple and just be about moving files around to clean up the main hierarchy.
Commit migrated from https://github.com/dotnet/corefx/commit/9a5546c439f7700d3462564678aa4206c7fbf4f1
Ported more HTTP related tests. These tests focus mostly on POST
scenarios mixed with authentication, pre-authentication, and the
"rewind-ability" of content if it needs to be re-POST'd due to AUTH
challeges by the server.
Added a new HttpTestServers2 class to Common/tests/System/Net. This is a
point-in-time thing as we improve the test infrastructure capability. In
particular, we are moving away from the use of the "httpbin.org" server.
Our new networking test server that we are bringing up has more features
than httpbin.org. It can support all the main HTTP verbs when doing "echo"
of request headers. It includes a "Content-MD5" HTTP response header. This
contains a hash of the response body. This makes it easier to validate the
returned payloads. The "httpbin.org" server also has a bug where it does
not properly echo chunked upload headers which was causing issues when
trying to port these tests.
I did not change other networking tests still using "httpbin.org" and the
Common HttpTestServers class. I will be doing that later as we finalize
the test infrastructure.
Commit migrated from https://github.com/dotnet/corefx/commit/f14225a7355bdae2468bb3c28562da0a0976f206
Array.Copy is implemented to work with any System.Array, not just T[]. When Array.Copy(sourceArr, destArray, length) is used, it delegates to Copy(sourceArr, sourceIndex, destArray, destIndex, length), and as such needs to query for the lower-bound of the supplied arrays, since although a T[] in C# always has a lower-bound of 0, an arbitrary Array may not. Given this, when calling Array.Copy, instead of calling Array.Copy(src, dest, length), it's slightly more efficient to just call Array.Copy(src, 0, dest, 0, length), avoiding the need for the GetLowerBound calls made while delegating to the 5-arg version.
I noticed one occurrence of this in some code I was looking at, and while fixing that, I fixed all of them across corefx\src.
Commit migrated from https://github.com/dotnet/corefx/commit/979026e435672e611e3f6e0647c873aa96ccc684
This commit fixes the following issues in CurlHandler w.r.t following URI redirection
* Ensure that the credentials are nullified at the right place. We should not set username and/or password to null unnecessarily. libcurl differentiates between unset UserNames/Passwords and null user names/passwords
* Ensure that libcurl follows only HTTPS uri if the original request was for a HTTPS URL.
* Ensure that libcurl follows only a HTTPS or a HTTP uri if the original request was for a HTTP URL.
* Ensure redirect results in a proper response code
* For all requests, limiting protocols to http and https
Commit migrated from https://github.com/dotnet/corefx/commit/7c59e04ce8b26887c386fc3045882196a3142b36
* Add more tests ported from the internal ToF tests.
* Cleanup existing tests to be consistent in style and format as it has
drifted due to many different people working on the tests.
* Disposing the HttpClient object will dispose the handler as well so we
don't need multiple 'using' statements unless we are explicitly testing
dispose ordering semantics.
Commit migrated from https://github.com/dotnet/corefx/commit/ad55c1d5685afc4009cb2481fcc7c5e4f4a551f8
`build.cmd /p:LockDependencies=true` was the start of this change, but some errors with xunit build numbers led to all xunit upgrades being reset back to what they had in master.
project.json changes:
* System.Net.Security\src Switch off of explicit beta versioning, use the same baseline as everyone else. The S.S.C.Algorithms explicit upgrade is no longer required.
* System.Net.Requests\tests didn't build. Looking at the project.lock.json change, it appeared that a System.Net.Http reference was lost, restored it.
* System.Net.Http\tests didn't build. Looking at the project.lock.json change, it appeared that a System.IO.FileSystem reference was lost, restored it.
* System.Net.WebSockets.Client\src didn't build. Looking at the project.lock.json change, it appeared that a System.Collections (4.0.10) reference was lost, restored it.
* System.Diagnostics.TextWriterTraceListener\tests needed the runtime reference to load the correct System.Diagnostics.TraceSource binary.
Commit migrated from https://github.com/dotnet/corefx/commit/cec3a0f2db542bfa9d9c56bf988d966eacc35ad9
Added more http tests to open source from the original internal ToF tests. Some of
the tests should be refactored in the future to avoid Assert'ing multiple
logical things.
Fixed up test namespaces for System.Net.Http and
System.Net.Http.WinHttpHandler.
Commit migrated from https://github.com/dotnet/corefx/commit/e5a017cb6ca01e99b1f1462298bc0cbf60c06ee0
The corefx build used an older beta5 version of DNX which hardcoded the path to bash in dnu, breaking the build on FreeBSD.
Updating DNX to the latest "stable" beta7 release fixes this.
After updating I noticed a few test projects didn't build anymore as they were missing some dependencies in project.json, added those.
Fixesdotnet/corefx#3173
Commit migrated from https://github.com/dotnet/corefx/commit/6df77fdbb6cf8e0d35a3074bb325f7810085daa3
When receiving the body of an HTTP response, libcurl provides the data
received via a callback that's invoked synchronously from the thread
pumping libcurl's processing. This callback can't return until it's
handed off all of the available data, and thus the current implementation
of CurlHandler blocks that thread until a reader has consumed the data.
This is both expensive and dangerous. It's expensive because we end up
blocking a thread until some reader comes along, and it's dangerous
because this can easily lead to deadlock. A libcurl multi handle may be
used by only one thread at a time, which means that if the handle is being
used by a thread while blocked on the write callback, no other thread can
access the multi handle, which means no other callbacks for other
concurrent requests can be invoked. If multiple operations were queued,
and then the data for them arrived in an order that didn't exactly match
the order of readers consuming the data, deadlock would ensue.
This change makes it so that we no longer block the callback, taking
advantage of a pausing mechanism libcurl provides that allows one to pause
a connection until it's needed.
I rewrote the CurlResponseStream to manage this. Now when the callback
has data to write, it attempts to transfer it to the stream. If there's a
reader already waiting for the data (represented by a task stored on the
stream), the callback simply transfers its data to the reader. If the
callback has too much data than would fit in the reader, the remaining
data is buffered on the stream. If instead the callback finds that either
there isn't a waiting task or that there's already some remaining data
buffered from a previous callback, it instead pauses the connection, such
that no more callbacks will be invoked until we unpause. From the reader
side, a reader who tries to read data from the stream can pull data from
the remaining buffer if there is any, or can register a task with the
stream that will be filled with data at some point when callback has data
available.
As part of debugging some issues with this change, I added a verbose
tracing mechanism that's enabled by compiling the library with the
CURLHANDLER_VERBOSE constant (the same that we use for enabling libcurl's
verbose diagnostic output). I left the tracing in as I found it valuable
in diagnosing issues with the library, but we can remove it later if
desired.
I also added a test that deadlocks before the change and that completes
successfully after.
Commit migrated from https://github.com/dotnet/corefx/commit/308efaba2833e5e146319f74a2281e12ef3e5519
The stable versioned packages use an older codebase currently than the latest pre-release.
That codebase is still using SecureString in System.Private.Networking, but the dependency
was omitted since crypto is still pre-release but networking had to be marked stable for UWP.
This missing dependency was causing tests to fail.
Rather than just list the dependency I am moving these tests back to pre-release packages
for the System.Net contracts, since System.Net packages are under active development
it is better to have the tests running against latest than the stable bits.
Once we have newer versions published we'll update these tests to use those.
Commit migrated from https://github.com/dotnet/corefx/commit/5d9f9d4c6ea450366c7b70af6e6006a401284d14
This change modifies the build to consume the stable version where available.
This disables building the dowlevel packages since we shipped them once as stable and we're done.
From here on out, we'll only build the latest version (except for a servicing event, which would
be handled by syncing to a hash).
I can't rev the package versions in the normal way yet (by modifying the assembly version of the contents)
because that turned out to be too big of a problem to tackle in this change. Instead I added a
parameter to the task that calculates package versions to do the rev for anything that was
stable, but only when building the package itself, not when calculating the dependencies.
This has the downside of not permitting a dependency on a incremented pre-release version if we've
shipped a stable version, but that is a small point in time problem until I can rev the assembly
versions.
[tfs-changeset: 1508664]
Commit migrated from https://github.com/dotnet/corefx/commit/ee1ec2891c7c65b46fa9dfb078636be503bb2a60
- Avoid unnecessary allocations associated with the use of
ToUpperInvariant().
- Update tests to be more explicit about the ToUpperInvariant()
behavior.
- Minor cleanup (readonly field, remove unused usings & trailing
whitespace).
Commit migrated from https://github.com/dotnet/corefx/commit/a75896bd169b6481543d1a99d9d110a1c0339987
A PR from last month changed how GetHashCode() works. This ended up
breaking tests against the .NET Native build of System.Net.Http which
shares the same source code as CoreFx. So, reverting the change in
GetHashCode() and porting additional ToF tests to GitHub.
Commit migrated from https://github.com/dotnet/corefx/commit/2dcf6471f00954b86663f26f5ce45d8c3485017b
This change sets up the layout for having multiple test projects for
WinHttpHandler. It follows the current guidlines as well as working
within the BUILDTOOLS requirements of having ".Tests.csproj" for the
names of the projects.
Also, fixed up some cross-platform definitions in the solutions files
for Http and WinHttpHandler.
After this PR is done, we will be migrating a large set of http
functional tests to GitHub
Commit migrated from https://github.com/dotnet/corefx/commit/42d81c41175ab8832032a2a387b8dc58365604a9
This change sets up the layout for having multiple test projects for Http. It follows the current guidlines as well as working within the BUILDTOOLS requirements of having ".Tests.csproj" for the names of the projects.
Commit migrated from https://github.com/dotnet/corefx/commit/e7b68608db14327ec55fc90c0f5aee3ed72fab36
An include for a Common file was using ..\..\Common instead of $(CommonPath). Another had a link but not under Common. And in the test project, all of the production code files were also being flattened into a single ProductionCode folder.
Commit migrated from https://github.com/dotnet/corefx/commit/3df107322881298bacafe36704a6b0d96ea86755