* Ensure that CancellationToken is propagated to built-in request content types
We don't currently expose the overload that would allow CancellationToken to be accessed by HttpContent-derived types in general, but we can at least ensure that when using the built-in content types, the CancellationToken provided to SendAsync is appropriately threaded through. We were doing this previously in only a few cases, where we knew that the previous overload wouldn't be overridden (namely internal types and sealed types), but we can enable the 90% scenario as well by doing a type check at run-time.
* Disable cancellation test for UAP handler
Commit migrated from https://github.com/dotnet/corefx/commit/450f49a1a80663529b31d3defafbd5e59822a16a
SocketsHttpHandler doesn't own the proxy its given; the lifetime of that proxy is up to the developer supplying it. We may also end up using a global proxy instance if the developer hasn't explicitly supplied one. However, SocketsHttpHandler's internal HttpConnectionPoolManager was disposing of this proxy instance on its disposal. For proxies that implement IDisposable, as our global default proxy does, that can be very problematic; on Windows, our default proxy has a SafeHandle, and once its disposed, every subsequent request to the proxy will fail (an exception gets thrown and then internally eaten). The fix is simply to stop disposing a proxy instance we don't own.
While investigating this, I also made a few style tweaks to WinInetProxyHelper, removing private property setters, so as to better be able to see that certain values were readonly and never changed after construction.
Commit migrated from https://github.com/dotnet/corefx/commit/c6a849b8d24fc48bb0bbdb9f9165752a4a19459b
* Making Exceptions serializable
* Remove EventLog serialization exceptions from tests since they won't work on non-Windows platforms
* Fix build and add internal exceptions as well
* Add parameter-less constructor to RethrowException
* Adding PR Feedback
* Fix allConfigurations build
Commit migrated from https://github.com/dotnet/corefx/commit/7075587321a1b6b59248b1a6c7ea04fc803eab96
* 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
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
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
* Clean up lots of string.Format usage
This PR does a few things:
- Adds new SR.Format overloads, in particular to support accepting an IFormatProvider as the first argument.
- Fixes a bunch of calls to string.Format that should have been using SR.Format. This not only helps in AOT builds where we want to be able to more cleanly remove resource strings, it enables easier auditing of the remaining string.Format uses to determine whether there are more appropriate implementations.
- Replaces some string.Format uses, in particular in asserts, with easier to read and maintain string interpolation
- Replaces some string.Format uses with string concatenation where it's simple and cheaper
- Fixes a bunch of SR.Format(SR.Something) calls where the SR.Format part is extraneous and should just be removed.
- Replaces a bunch of Debug.Assert(false, ...) calls with Debug.Fail(...). This isn't directly related to string.Format, but I started this cleanup as part of cleaning up asserts that were using string.Format, and then ended up accidentally merging the chain into the same commit, and decided it was simple enough that I should just leave it rather than trying to separate it out again.
* Address PR feedback
* Fix breaks due to lower netstandard builds
Commit migrated from https://github.com/dotnet/corefx/commit/a6f76f4f620cbe74821c6445af3f13e048361658
* Auto-generate the reference assemblies
* Add back type forwards.
* Revert "Add back type forwards."
This reverts commit dotnet/corefx@3acf74aabb.
* Add back TypeForwardedTo in .Forward.cs files
* Add new type forward files to the csproj files
* Add back if-defs and move TestUtilities comment from ref to source
* Remove duplicate definitions, delete unnecessary manual files,
* Fix build breaks
* Add back missing AsyncMethodBuilderAttr to S.Runtime ref
* Fix typo between XmlDocument and XPath.XDocument
* Add type forward fild missed from previous commit
* Fix ECDiffieHellmanCngPublicKey internal ctor for netfx 4.6.1
* Remove "this type was moved to comments" from the type forwards.
* Add back the Xml and Debugger* attributes to Xml.ReaderWriter
* Add back struct layout attributes
* Add back attributes missing (highlighted from api compat)
* Add missing toolbox item attribute in s.data.common.
* Add missing struct layout attributes and remove unnecessary additions
* Add back InterfaceTypeAttribute
Commit migrated from https://github.com/dotnet/corefx/commit/16689aa0a933517dc45465fc6edb6223222414a6
* Finish (mostly) erradicating StringBuilder marshaling from corefx
This should finish my quest of removing StringBuilder marshaling from coreclr and corefx, which I've strived to do because it often adds unnecessary overhead and often is done incorrectly because of intricacies in how the marshaling works; while not using it often leads to `unsafe` code at call sites, there's much less magic, and it affords the ability to optimize more if desired.
There are still three remaining [Out] StringBuilder's in Interop.Odbc.cs, which I've not removed because tests are limited and the call graph to these is non-trivial with StringBuilders passed through. There may also be a few straggling DllImports I missed while scouring interop that's not following our guidelines of being in src\Common\.
Other than those, the remaining StringBuilder usage I found in DllImports was for [In] only, and in those cases it's reasonable, as the call sites are building up StringBuilders and then passing them off to the call sites; converting those to use `char*` or similar could actually make them more expensive. I did, however, ensure they were properly annotated as [In], in order to make the intent clear and avoid potential marshaling costs for the unnecessary [Out] direction.
Where I was touching a function in one of these stray interop files that was duplicated elsewhere, I also consolidated it to a centralized location, but I've not in this PR done the cleanup work for the rest of the files.
* Address PR feedback
* Address further feedback
Commit migrated from https://github.com/dotnet/corefx/commit/c77984de34e3334734d53c09d5dac7c7471e972d
* Removing Reference assets from packages which contain runtime-specific dependencies when targeting Desktop. (dotnet/corefx#33254)
* Removing Reference assets from packages which contain runtime-specific dependencies when targeting Desktop.
* Adding validation target to catch other instances of packages that need the property
* Fixing target to check all exclude compiles. Undoing changes on two packages that are not required any more.
* Update package index to contain the right versions
* Fixing packaging build errors
Commit migrated from https://github.com/dotnet/corefx/commit/6832425094123c858b9e5da02469c98ca8c200c4
* TLS 1.3 connections have been tested on Ubuntu 18.10 (OpenSSL 1.1.1).
* There's no public version of Windows with TLS 1.3 enabled.
* There's no public version of macOS with TLS 1.3 enabled out of the box.
When making use of SslProtocols.None (system default) TLS 1.3 is already working on Ubuntu 18.10, this change mainly makes it so that EncryptionPolicy.None is better handled, and that we can restrict-to and report TLS 1.3.
Commit migrated from https://github.com/dotnet/corefx/commit/d30157d9e48cf73e267994e45953722d18ccd4c6
* Change netfx to target .NET 4.7.1
* Fix CoreFx.Private.TestUtilities to not reference System.Runtime.InteropServices.RuntimeInformation which is now inbox in netfx
* Fix StackTrace.Tests to be built against netstandard and run against netfx inbox
* Fix ValueTuple configurations to support net461 and add netfx placeholder configuration since in net471 it is now inbox
* Fix projects' package configurations that had netfx (net461) to still support net461 and netfx as a buildconfigurations
* Disable failing tests when running on .NET 4.7.1
* Make netfx target net472 and merge with master
* Update ActiveIssue comments to issue number only
* Workaround packages tests due to not installed targeting pack
* Match NSSupportPackage version to CLI's
* Remove netstandard support lib bin placing to runtime and testhost for netfx, fix reference paths to match new package
* Run UpdateVSConfigurations after merging conflicts with the newly updated SDK projects
* Add net461 package configuration to System.CodeDom
* Add net461 package configuration to System.Configuration.ConfigurationManager
* Add net461 package configuration to System.Security.Cryptography.Xml
* Add net461 package configuration to System.Security.Permissions
* Add net461 package configuration to System.ServiceModel.Syndication
Commit migrated from https://github.com/dotnet/corefx/commit/faad2e63b35bed10fe18195987a413308b97eded
This was a regression from .NET Core 2.0 due to PR dotnet/corefx#28105.
Proxy authentication using system default proxy settings that involve a PAC file
(either autodiscovery or explicit PAC file) cause a NullReferenceException in WinHttpHandler in the
CheckResponseForAuthentication() method.
This problem is only discovered when using an authenticating proxy server (any auth scheme) that is
discovered using PAC files. This is considered the "system default" proxy. When this occurs, the
handler's Proxy property is null and dereferencing it caused the exception.
Due to the problems described in dotnet/corefx#6997, the uri of the proxy can't be determined yet since it is only
known to WinHTTP. Fixing dotnet/corefx#6997 is complicated and impacts performance. However, in most cases, as
long as the credentials are a NetworkCredential object, knowing the uri of the proxy is needed.
I tested this manually using the steps I described in dotnet/corefx#30191. I did not add any tests to this PR since
they can't be run in CI. However, I am working on a task that will eventually add Enterprise-Scenario
testing like this (PAC files, authenticating proxies, etc.) to our systems.
Fixesdotnet/corefx#30191
Contributes to dotnet/corefx#6997
Commit migrated from https://github.com/dotnet/corefx/commit/69c2e2072574805589becac0c08590dc5e39b65e
Fixed a few bugs in both WinHttpHandler and SocketsHttpHandler regarding the behavior of
'Accept-Encoding' request headers and automatic decompression of response body content.
On Windows 7, native WinHTTP doesn't support automatic decompression. That also means it doesn't automatically append 'Accept-Encoding' request headers. WinHttpHandler was relying on WinHTTP to add those headers. But it wasn't happening on Windows 7. So, now WinHttpHandler will make sure those headers are added if not already present. WinHttpHandler was also always decompressing content even when the encodings in the .AutomaticDecompression property didn't match.
SocketsHttpHandler had a different bug. It would make sure that the 'Accept-Encoding' headers were
present when .AutomaticDecompression was being used. But it wouldn't check for existing headers and duplicate encodings would be present in the outbound 'Accept-Encoding' request header. I.e., it would end up with "Accept-Encoding: gzip, gzip".
In addition to fixing these bugs, I added more tests to both Http Functional and WinHttpHandler Unit
tests (which can simulate running on Windows 7 using the existing mocks).
Fixesdotnet/corefx#29747Fixesdotnet/corefx#26991
Commit migrated from https://github.com/dotnet/corefx/commit/40ce4cb20270f731d7d826291e1edce8d1ce2c37
Add EventSource tracing to WinHttpHandler
Removed the custom OutputDebugString-style tracing from WinHttpHandler and replaced it with
NetEventSource. Didn't define any new event id's but tried to use "Info" vs. "Error"
in more places.
Due to the unique way the WinHttpHandler class gets built into two different binaries,
I needed to add a new EventSource name/GUID for the standalone System.Net.Http.WinHttpHandler.dll
binary. The logging output is similar but will show up in either "Microsoft-System-Net-Http" or
"Microsoft-System-Net-Http-WinHttpHandler" EventSource's depending on which binary is being used,
i.e. whether "new HttpClientHandler()" or "new WinHttpHandler()" was used.
For those curious about how the new GUID for "Microsoft-System-Net-Http-WinHttpHandler" was done, it
was not generated randomly. EventSource GUID's are generated based on the string name for the
EventSource according to a well-defined formula. Doing a construct like this will return back the
information:
```c#
Type esType = typeof(WinHttpHandler).GetTypeInfo().Assembly.GetType("System.Net.NetEventSource", true, false);
Console.WriteLine($"Name={EventSource.GetName(esType)}, Guid={EventSource.GetGuid(esType).ToString()}");
```
Closesdotnet/corefx#7856
Commit migrated from https://github.com/dotnet/corefx/commit/11222269ae3bc45cd414dcb973f2d92f1bad73f8
* Clean dead code in System.Net
* More cleaning
* Clean dead code for HttpListener
* Clean dead code for Mail
* Clean dead code in Http
* Clean dead code for NetworkInformation
* Clean more dead code
* Various fixes
* Build fix
* Restore field
* var -> string
Commit migrated from https://github.com/dotnet/corefx/commit/71cc1ae8ba02b7b71878cb69a50c667c8ed0f50f
This PR will reduce the calls to WinHTTP GetProxyForUrl() if a previous call recently discovered
no PAC file within a certain timeframe. We call this API when "Automatically detect settings"
is turned on. The default configuration on Windows has this proxy setting always turned on. This
is ironic given that most network environments don't have a PAC file on the network. So, it's
important to try to optimize this common case on customers' networks.
Initial testing shows at least a 2x speed improvement with these changes.
I did not use any locking mechanisms since those would drag down performance. In the worst case,
any race condition would mean we might call GetProxyForUrl() an extra time. But since we're only
filtering on ERROR_WINHTTP_AUTODETECTION_FAILED, it is unlikely to actually change over time.
That particular error means that the WPAD protocol was able to complete on the network, but the
answer discovered is that there is no PAC file registered on the network.
Fixesdotnet/corefx#28543
Commit migrated from https://github.com/dotnet/corefx/commit/712d3065cef7f28eae96a578b8bd73870e9b2cd5
By default, .NET Core defaults to allowing TLSv1, TLSv1.1, and TLSv1.2. However, whereas .NET Framework allows a developer to opt-in to SSLv2/SSLv3, .NET Core currently blocks it. This means developers are unable to use .NET Core when they need to communicate with an endpoint they don't control and that's unfortunately limited to one of these protocols, which blocks adoption of .NET Core.
This commit stops blocking these protocols. They're still disabled by default, and compiling against them still results in a deprecation warning, but if a developer chooses to ignore the warning and explicitly sets SslProtocols, they are at least able to use the protocols, subject to whatever the underlying operating system allows (e.g. Windows stops supporting SSLv2 as of v1607).
Commit migrated from https://github.com/dotnet/corefx/commit/3cc6fda0c6abe76eea222c165ebdf816e83df564
This PR addresses some error handling problems in WinHttpHandler as well as
improves the error messages generated for WinHttpException.
This was root caused to a bug in WinHttp where it can't do a GET request with a
request body using chunked encoding. However, this exception was masked due to
an error handling bug caused by an inconsistency in how WinHttp associates
context values for callbacks during the WinHttpSendRequest API call. This PR now
fixes the error handling around synchronous errors being returned from
WinHttpSendRequest. The root WinHttp bug itself can't be fixed. So, doing a GET request
will still throw an exception, but it will be a catchable HttpRequestException.
Another bug was discovered while investigating dotnet/corefx#26278. WinHttpCloseHandle should only be
called once and should never race between threads. This is normally protected when
the request handle is closed via SafeHandle Dispose(). But there was a place in the
callback method where we called WinHttpCloseHandle directly against the raw handle.
Finally, the error messages for WinHttpExceptions have been improved to show the
error number and the probable WinHttp API call that generated the error. This will
save diagnostic time.
I also moved the WinHttpException source code back from Common. It was originally shared
between WinHttpHandler and ClientWebSocket. But then ClientWebSocket moved away from
WinHttp implementation. So, it makes more sense for this class to be under WinHttpHandler.
Example:
Original WinHttpException message: "The parameter is incorrect"
New WinHttpException message: "Error 87 calling WinHttpSendRequest, 'The parameter is incorrect'."
Closesdotnet/corefx#28156
Contributes to dotnet/corefx#26278
Commit migrated from https://github.com/dotnet/corefx/commit/ce4ebdfbf2080a38855ab8df5c9bb553db8724af
When choosing what authentication scheme to use, WinHttpHandler picks the most secure scheme supported by the server. When the client does not support this scheme it closes the connection. This fix updates the authentication logic to instead pick the most secure scheme offered by the server that is also supported by the client.
This change also adds a test of the relevant behavior.
Fixes: dotnet/corefx#27672
Commit migrated from https://github.com/dotnet/corefx/commit/6d031a04f3df8fadd5b82d9c23d904182eea94e4