While researching dotnet/corefx#3292, I discovered that POST'ing zero length content throws an exception. This was because we were calling WinHttpWriteData() and passing in a zero length for the buffer size. This returns an error back from WinHTTP.
We don't need to call WinHTTP APIs to write zero length data. We simply need to return a completed task back to the caller.
Added tests to verify the fix.
Commit migrated from https://github.com/dotnet/corefx/commit/0776e741430e83fe72bf4be544701f0eeddce042
CurlHandler calls to HttpContext.CopyToAsync with a stream that CopyToAsync should write to asynchronously. If, however, the call to CopyToAsync writes to it synchronously as part of the synchronous call to CopyToAsync, CurlHandler will deadlock.
As a precaution, we ensure that the CopyToAsync call is asynchronous from the perspective of CurlHandler.
Commit migrated from https://github.com/dotnet/corefx/commit/ebad2d3426af24201526263401fb010a78ee60bd
WinHttpHandler was hanging due to blocking on the WinHTTP status callback thread. The blocking was coming from a task continuation resuming on the same thread and subsequently calling a blocking Stream.Write method in a custom HttpContent class being written onto the request stream.
Blocking is not allowed on the WinHTTP callback thread. So any task continuations need to occur on a different thread. The fix is to create the TaskCompletionSources to use RunContinuationsAsynchronously. This allows them to unblock the WinHTTP thread. Additionally, I added some missing ConfigureAwait(false) settings to some await'd operations.
Added a new test which uses a custom HttpContent class that uses a blocking Write() call when serializing onto the request stream.
Fixdotnet/corefx#4077
Commit migrated from https://github.com/dotnet/corefx/commit/babb7c6c65bf1e02dab667cbda80f0e24af21ed8
A change was made to DNX to no longer restore project.jsons
that are in nested folders. This caused my batch update to miss
a number of tests and reference assembly projects.
This fixes the stale lock files, but restore is still broken.
[tfs-changeset: 1540448]
Commit migrated from https://github.com/dotnet/corefx/commit/3622cccc1b22fa1947a72d5126b59864e9d2fd0d
We need a new DNX to understand the latest packages.
We also need to move all "dotnet" projects to use dotnetX.X (generations)
now that the packages no longer have "dotnet" assets.
The new version of DNX dropped support for the aspnetcore
so I needed to update a few projects/packages that depended
on old package versions that had this moniker.
[tfs-changeset: 1540388]
Commit migrated from https://github.com/dotnet/corefx/commit/38f8d7a3ca1ee819c2b8eabb1c07ee4e99683c97
An instance of HttpContentAsyncStream is passed to HttpContent.CopyToAsync. Currently HttpContentAsyncStream is both readable and writable, but reading is only meant for internal usage by CurlHandler's libcurl callbacks. This change codifies that, such that CanRead now returns false and the public Read* methods throw NotSupportedException. Internal reading is done via ReadAsyncInternal.
Commit migrated from https://github.com/dotnet/corefx/commit/c0fbe3c8e4d50bbf57acd330de8eac136d59c013
WCF ends up trying to use Write as part of HttpContent.CopyToAsync implementations. While that should be addressed separately to use WriteAsync, it's possible others are also using Write instead of WriteAsync, and today we throw NotSupportedException from Write. So as to let this at least work, I'm implementing Write to delegate to WriteAsync.
I also noticed we were missing a FlushAsync implementation, so I've added one.
Commit migrated from https://github.com/dotnet/corefx/commit/b1bc8d48484f4104d0ec2c440544620b920b605f
All projects that previously used dotnet have been updated to specify the appropriate generation of the surface area they depend on or in the case of desktop inbox contracts, the generation that includes the set of platforms they can support.
Now all packages contain all generations of API that they have ever shipped.
In doing this I needed to change how we determine package version. Before we ensured all assemblies would have the same version and used that. Now we choose the max.
Additionally I needed to change how we chose which asset to use for netcore50 when the dotnet asset was obscured by a placeholder for a previous netcore release (eg System.Runtime will have placeholders for Win8 since it was inbox there). To do this I wrote a task that uses nuget to evaluate the package assets with and without the placeholders. In this way it chooses the "best" dotnet implementation and reference assembly to use for netcore50.
This new model made my old MSBuild-based validation impossible to carry forward, so I wrote better validation in a task that actually uses Nuget's asset resolution algorithm. This uncovered many existing issues in packages that I have cleaned up. The validation algorithm could use some polish but its working now. Perf is not great, package build went from 15s to ~2 minutes. I'll work on this some more by making it incremental (input: nuspec, output: marker created on success). Profiling shows that most of the time is spent in calling the nuget APIs. I'll see if I can reduce some of this with caching for things that don't change (eg: RID graph). Ultimately I think its a reasonable tradeoff for the types of bugs I can find this way.
[tfs-changeset: 1539216]
Commit migrated from https://github.com/dotnet/corefx/commit/0931c347eb836c9da94d0d5ff9ad7a14403b9738
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
It's common for the HttpContent to synchronously give back a Task from CopyToAsync and for CopyToAsync have synchronously initiated a WriteAsync. In such cases, the previous commits would have made that asynchronous by scheduling it in a Task, which means that it's more likely a subsequent ReadAsync from a libcurl callback would end up having the ReadAsync complete asynchronously, which is expensive due to the pausing infrastructure.
This change removes the asynchronous scheduling of CopyToAsync, making it more likely that synchronously available data will be available by the first ReadAsync call.
Commit migrated from https://github.com/dotnet/corefx/commit/52aace9364de641030affa8c1c6a5d24526a65ae
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
CurlHandler is explicitly checking to ensure that the request URI scheme is either http or https. HttpClient already does this check before getting to this point, WinHttpHandler doesn't do the duplicative check, and even if a bad scheme sneaks in, our subsequent usage of libcurl will enforce that only http/htps are used. Replacing the check/exception with an assert.
Commit migrated from https://github.com/dotnet/corefx/commit/287baeadf7348f0c671a8bcd07842977935de215
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
CurlResponseStream.VerifyInvariants has an assert that assumes that if the stream has been disposed, it must have also completed. But since this stream is exposed publically, it's possible for consuming code to Dispose of the stream before the request has finished, violating the assert. In such a situation, a subsequent attempt to transfer data to the response stream will fail and cause the request to be aborted as desired, cleaning up resources (there are probably some opportunities here for some optimizations to handle this case, but that can be explored later.)
This commit just removes the bad assert.
Commit migrated from https://github.com/dotnet/corefx/commit/cfc3f755af4302973d7455fdb86182822c467bdb