Commit Graph
1944 Commits
Author SHA1 Message Date
Nikita Potapenko 28f63d6f4e Fix typo (dotnet/corefx#36107)
.Net -> .NET

Commit migrated from https://github.com/dotnet/corefx/commit/d29ae79fb13b4df9078400ac49b460e011f96404
2019-03-17 15:43:21 -07:00
Caesar Chen 52de487b17 Http trailer support for HTTP/1.x (dotnet/corefx#35337)
* http trailer for http/1.1

* address feedback

* address feedback

* pr feedback

* stop blocking context

* fix framework test failiure

* address feedback

* seperate tests

* address some feedback

* more feedback from review

* correct ReadAsync

* feedback from review

* handle short ReadAsync()

* more feedback


Commit migrated from https://github.com/dotnet/corefx/commit/ac9c746c44a5c0decf551fe33fb2acbdc895d021
2019-03-16 15:56:39 -04:00
Marco Rossignoli 76dd5a6434 Optimize recursion of System.Net.Http.HttpRuleParser.GetExpressionLength (dotnet/corefx#35959)
Optimize recursion of System.Net.Http.HttpRuleParser.GetExpressionLength


Commit migrated from https://github.com/dotnet/corefx/commit/4636de3b8595ad10eaebf00cbeed715684e7fd1b
2019-03-14 10:39:24 -07:00
David Shulman 94d443763c Disable some proxy tests (dotnet/corefx#36037)
Disable some problematic proxy related tests.

Contributes to issue dotnet/corefx#32809

Commit migrated from https://github.com/dotnet/corefx/commit/b97e1b696cb9bf6bf288b4fd28481a2026b0dd7a
2019-03-14 09:48:55 -04:00
Stephen Toub e7e11b10e1 Remove remaining usage of Contract.* (dotnet/corefx#36002)
Only remaining use of System.Diagnostics.Contracts in corefx is tests for the library itself, and usage of the [Pure] attribute.

Commit migrated from https://github.com/dotnet/corefx/commit/940ecee40b64ab93c92f87a7e8909771c013f1a5
2019-03-14 09:47:04 -04:00
David Shulman dd2c47fc44 Clean up some tests and move to new Azure endpoint (dotnet/corefx#36018)
This PR changes the Azure test endpoint for HTTP/1.1 and WebSocket tests to use
Azure App Service instead of the classic Azure Cloud Service endpoint. This now
matches the HTTP/2.0 endpoint architecture.

We are deprecating the use of Azure Cloud Service endpoints because they are hard
to deploy and maintain. Azure App Service, on the other hand, provides a lot of benefits
including built in production/staging slots, TLS certificate handling and easier
integration with Azure DevOps deployment models.

There are a few downsides to Azure App Service which are known feature limitations.
Since it uses ARR (reverse proxy), it causes websocket connections to be proxied.
This results in some behavior changes for some edge condition tests we have. For example,
when a websocket handshake fails (due to subprotocol mismatch for example), the client
side doesn't see a TCP disconnect. Instead, due to the reverse proxy, we end up getting
an HTTP status code (like 500). Either way, it is a websocket handshake failure. So, I've
updated a few tests to be less brittle for that. I also opened another issue dotnet/corefx#36016 to
track moving a few websocket tests to the loopback websocket server which doesn't yet
have full capability.

I also converted an HTTP statusline test to use the loopback server.

Commit migrated from https://github.com/dotnet/corefx/commit/689e58ec123d8c730d48d1c35c8f7ecbba6c22cf
2019-03-13 20:57:55 -07:00
Stephen Toub 440cb8cf44 Disable tests failing on WSL (dotnet/corefx#36017)
This is on Windows 10 Version 10.0.17763 Build 17763.

Commit migrated from https://github.com/dotnet/corefx/commit/cf5c3b0282c07ab13da79f04ee83d39f20bac431
2019-03-13 21:09:42 -04:00
Liudmila Molkova 8f347c6f90 Inject W3C headers in HTTP diagnostics handler (dotnet/corefx#35882)
* Inject W3C headers in corefx Htpp Diagnostics HAndler

* Inject W3C headers if activity format is W3C

* fix style and indentation

* review: fix issue


Commit migrated from https://github.com/dotnet/corefx/commit/7314001fd1ebfb25b500774d1551170a17c7a209
2019-03-11 15:15:08 -07:00
Stephen Toub dce7647bbc Clean up lots of string.Format / SR.Format usage (dotnet/corefx#35777)
* 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
2019-03-06 12:03:00 -08:00
Viktor Hofer f8a8530d0f Project msbuild syntax cleanup (dotnet/corefx#35686)
* Remove license header from non-shipping files

* Remove obsolete Project attributes

* Remove xml header and remove DefaultTargets


Commit migrated from https://github.com/dotnet/corefx/commit/c390ce7df50252e11f5d322276e9d19e046d1332
2019-03-03 18:55:25 +01:00
Geoff Kizer 8aab5832e0 Merge pull request dotnet/corefx#35623 from MarcoRossignoli/fixuseragentparsing
Fix bug: UserAgent malformed parsing

Commit migrated from https://github.com/dotnet/corefx/commit/cb54ad963cc655d4c783033a2ab3dc3576d0dc65
2019-03-01 15:40:58 -08:00
Marco Rossignoli 357812cea2 continue parsing after nested call
Commit migrated from https://github.com/dotnet/corefx/commit/b2865f5e4dcda0a3745b96d704aebec82ab22d60
2019-03-01 23:11:29 +01:00
Marco Rossignoli 68952502ce add some tests
Commit migrated from https://github.com/dotnet/corefx/commit/12fc04b6f8c20d901f9c89a1b8d8a540fa22dd0d
2019-02-28 16:13:24 +01:00
Ahson Khan 4fed6ab881 Auto-generate the reference assemblies and separate type forwards (dotnet/corefx#35557)
* 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
2019-02-27 19:39:21 -08:00
Marco Rossignoli 2f1d983a55 fix UserAgent parsing
Commit migrated from https://github.com/dotnet/corefx/commit/840d29c05243f5afe31e43e2415d6dce5b3c71fd
2019-02-27 12:00:17 +01:00
Tomas Weinfurt a41f64dab6 enable GetAsync_AllowedSSLVersion_Succeeds for SocketHttpHandler (dotnet/corefx#35605)
* enable GetAsync_AllowedSSLVersion_Succeeds for SocketHttpHandler and skip Curl on RH6


Commit migrated from https://github.com/dotnet/corefx/commit/f44142b855330ca9274827a230b8ad9a3ccda357
2019-02-26 22:41:05 -08:00
Tomas Weinfurt 19fc9cb9ed improve Ssl3 detection (dotnet/corefx#35574)
* improve sslv3 detection

* updates to pass all tests

* correct catch


Commit migrated from https://github.com/dotnet/corefx/commit/0fa22d4b6753c47b6139980255e932341353179e
2019-02-25 23:02:20 -08:00
Stephen Toub 6751207cca Remove Task{CompletionSource} allocations from Http2Stream (dotnet/corefx#35522)
* Remove Task{CompletionSource} allocations from Http2Stream

This change makes the Http2Stream instance itself awaitable via ValueTask, such that we don't need to allocate any additional TaskCompletionSource or Task objects for such internal await'ing, regardless of how many times the Http2Connection or Http2ReadStream needs to wait for headers or response data to arrive.

* Address PR feedback


Commit migrated from https://github.com/dotnet/corefx/commit/aacff5212204ee54e3b3e0172fcf364c3fe253e3
2019-02-23 05:00:31 -05:00
Jeremy Barton 46ae7ef229 Add support for OCSP on Linux, overhaul Linux X509Chain processing
This change moves a lot of the chain building work from managed code into the native shim, largely to cut down on the number of P/Invokes required to set up the chain builder.

Once a chain has been built to a point where only one issuer will be considered, if revocation was requested and a CRL is not available, attempt an OCSP request if the certificate indicates the CA has an OCSP endpoint.

Based on CA/Browser Forum's requirements this expects CRL for all intermediates and only attempts OCSP for the end-entity certificate.

"Conforming" OCSP requests are opportunistically cached on the basis that local filesystem re-reads are more reliable (and faster) than doing a live request to the CA.

Commit migrated from https://github.com/dotnet/corefx/commit/0fbbb68a3f7be82d26e4b2dff5c25c192e3a2023
2019-02-22 12:51:22 -08:00
Geoff Kizer d97476b640 Merge pull request dotnet/corefx#35485 from geoffkizer/http2streamstate
add StreamState and rework HTTP2 stream state handling

Commit migrated from https://github.com/dotnet/corefx/commit/cab8d6bbfe49c7571f682234622f4ea3bfcf5010
2019-02-21 16:03:00 -08:00
Geoff Kizer 3a5f3c641e address PR feedback
Commit migrated from https://github.com/dotnet/corefx/commit/333fabaffeabf3b4de5d6ba45e54b2078f57adcf
2019-02-21 13:52:02 -08:00
Geoff Kizer 294a0381c5 add StreamState and rework HTTP2 stream state handling
cleanup


Commit migrated from https://github.com/dotnet/corefx/commit/1eba83637d47fde09c8e1748b0ebb4ca42da5ca4
2019-02-21 03:59:36 -08:00
Geoff Kizer fcd7836937 don't dispose the HTTP2 connection writer lock, to avoid race (dotnet/corefx#35468)
* don't dispose the writer lock, to avoid race

* Update Http2Connection.cs


Commit migrated from https://github.com/dotnet/corefx/commit/c4c8f72cd8d18d6ec00afea6644ae2077d442869
2019-02-20 21:23:19 -05:00
Stephen Toub d66ba991e0 Simplify ConnectHelper.ConnectAsync to just return Stream (dotnet/corefx#35411)
* Simplify ConnectHelper.ConnectAsync to just return Stream

SocketsHttpHandler's ConnectHelper.ConnectAsync currently returns a tuple of (Socket, Stream).  We can instead just have it return the Stream and allow the call site to fish out the Socket from the Stream if it can.  This is slightly more efficient memory-wise, in that the state machine involved will be smaller, but it also allows for a potential extensibility point that's simpler (abstracting out the ConnectAsync as a callback) and potentially allows us to in the future get a Socket in more cases, e.g. any connection helper handing back a NetworkStream.

Unfortunately, NetworkStream.Socket is protected rather than public.  Until such time that there's a public property for this, there are two options here:
1. Use reflection to create a delegate we can then use to get at the property; "Socket" is part of the exposed surface area, so the only dependency here is on documented surface area.
2. Create a custom NetworkStream-derived type that exposes the protected as a public.

I started with (2), which works fine for our own internal needs currently, but shifted to (1), as it's more flexible.

* Don't use reflection


Commit migrated from https://github.com/dotnet/corefx/commit/d9cdbc3cca0f75e5d7692f9ae03978dd0ed9b2e9
2019-02-20 14:38:31 -05:00
Geoff Kizer 1546a5a5fe Merge pull request dotnet/corefx#35432 from geoffkizer/windowupdatethreshold
defer sending WINDOW_UDPATE frames until a minimum threshold has been met

Commit migrated from https://github.com/dotnet/corefx/commit/852602a3cebc2bfa14199cf6c39368bad1903c6c
2019-02-20 11:26:12 -08:00
Geoff Kizer eb18055ef3 PR feedback
Commit migrated from https://github.com/dotnet/corefx/commit/8b4d2f58cd89efbe0d0b2a487d5ce96353c83150
2019-02-19 19:54:30 -08:00
Stephen Toub 58d340f4de Make Http2Connection.SendFrameAsync return ValueTask (dotnet/corefx#35426)
This method frequently completes synchronously, and in such cases we can avoid the task allocation.

Commit migrated from https://github.com/dotnet/corefx/commit/52993e52f9d3ab30da54884f7c5e0cdea2518e28
2019-02-19 20:27:49 -05:00
Geoff Kizer c839ef5edd defer sending WINDOW_UDPATE frames until a minimum threshold has been met
Commit migrated from https://github.com/dotnet/corefx/commit/99f8d99738f78b6791e2b30b9fababa9f8daa643
2019-02-19 16:02:55 -08:00
David Shulman ad957976ed Improve HTTP2 connection diagnostics in SocketsHttpHandler (dotnet/corefx#35423)
Modified the top-level exception being returned (HttpRequestException) to always include
the inner exception.

Contributes to dotnet/corefx#35422

Commit migrated from https://github.com/dotnet/corefx/commit/fcf46bea903c78accd9586fa2fb355ade70c485e
2019-02-19 17:33:43 -05:00
David Shulman 6167d604af Fix Negotiate/SPNEGO Kerberos to NTLM fallback on Linux (dotnet/corefx#35383)
This PR adds support to our Linux gssapi layer so that we properly handle the Negotiate
protocol (SPNEGO) being able to to fall back from Kerberos to NTLM.

Windows Negotiate SSPI implements the SPNEGO protocol. However, it has built-in error
handling for dealing with quick fallback from Kerberos to NTLM in error cases such as where
the client is not on a domain/Kerberos realm, the client is using a local credential, the
client is talking to a non-domain server, etc.

However, the Linux implementation of SPNEGO doesn't handle those error cases very well.
So, I added the proper retry logic when generating the initial context token. Now, we will
be able to fall back from SPNEGO-wrapped Kerberos to SPNEGO-wrapped NTLM protocol blobs.

A similar bug exists in libcurl which is why Curl and CurlHandler are unable to connect to
Negotiate servers when not part of the Kerberos realm. I am considering proposing my fixes
to the libcurl repo as well.

I added more tests to support this PR and did manual testing. I added some additional
EventSource tracing as well.

Contributes to dotnet/corefx#34878


Commit migrated from https://github.com/dotnet/corefx/commit/1fd3ba126538c2fed76d2279db26dc7ba5ceed41
2019-02-19 09:14:44 -08:00
Stephen Toub facab81dc9 Use Environment.TickCount instead of DateTime{Offset}.UtcNow in SocketsHttpHandler (dotnet/corefx#35401)
We don't need the accuracy of UtcNow nor the absolute time when getting a time stamp in order to determine whether a connection should be retired; rather, we only need an approximate difference in time.  This means the resolution of TickCount is fine, and the worst that happens if a connection happens to be alive for more than its ~49 day period is we may allow the connection to then live even longer than we otherwise would.  In exchange, we get a much cheaper mechanism (~20x on Windows) mechanism for computing the connection age.

Commit migrated from https://github.com/dotnet/corefx/commit/dca39727b02ceb4413d0fde11467965099ab4a2f
2019-02-19 00:38:07 -05:00
Stephen Toub a7cd5b22b1 Address PR feedback and additional cleanup
Commit migrated from https://github.com/dotnet/corefx/commit/c0f24cf4136f22e508e1b9289062e541bad6af88
2019-02-15 15:39:49 -05:00
Stephen Toub 1bd66ccdd9 Remove uap config from System.Net.Http UnitTests
These tests are compiling in internal sources and running tests against them directly.  The only configuration we need is netcoreapp.


Commit migrated from https://github.com/dotnet/corefx/commit/f9d1057321c17d8692e829b311c925660e23f8db
2019-02-15 13:05:31 -05:00
Stephen Toub 2aa4ad5d3a Use StaticTable for writing known headers
Commit migrated from https://github.com/dotnet/corefx/commit/65a101d6968bac28f343ed2e352cf54f8e122f21
2019-02-15 13:05:30 -05:00
Geoff Kizer 375fb2ae80 Implement static table for writing some headers
Commit migrated from https://github.com/dotnet/corefx/commit/1b0b62a3a6fa0ea0fa301b0bac9b9c350ad6c14c
2019-02-15 13:05:30 -05:00
Stephen Toub 718fb2d08f Don't pool Http2Connection buffers
Http2Connection keeps these buffers for a really long time; we don't want it to starve the pool.  It also leads to lifetime issues, as the buffers need to be (but currently aren't) released back to the pool, yet work can end up accessing the ArrayBuffer after the connection is logically disposed.


Commit migrated from https://github.com/dotnet/corefx/commit/847f64fa812362e75dfcbd0bdcf41541921d607c
2019-02-15 13:05:30 -05:00
Stephen Toub ce92a4bafe Avoid per-request HeaderCallback delegate allocation
Commit migrated from https://github.com/dotnet/corefx/commit/14ef1be63af22bc610223eab337bdab99e6af4a8
2019-02-15 13:05:28 -05:00
Stephen Toub 69bbcb1cf4 Avoid lock object allocation in CreditManager
Commit migrated from https://github.com/dotnet/corefx/commit/701102c5c0282b3d29edc76aca12d985af8ff70f
2019-02-15 13:05:28 -05:00
Stephen Toub 65270f3717 Avoid syncobj object allocation in Http2Connection
Just use one of the other readonly objects.


Commit migrated from https://github.com/dotnet/corefx/commit/d74108c671abffa3d314b011b7269806eedb7df7
2019-02-15 13:05:28 -05:00
David Shulman 819f9791e4 Add simple http Windows auth tests using loopback server (dotnet/corefx#35280)
I added this test to help get familiar with the new GenericLoopbackServer
test pattern as well as use the new SkipTestException() ability.

The test is not an end-to-end test. It simply validates that the client is able
to begin the multi-leg Windows auth handshake and send the initial Authorization
header.

In addition, once https://github.com/dotnet/core-eng/issues/5135 is completed,
the 'IsNtlmInstalled' condition will always be true for the CI machines. Thus,
we will get better test coverage for Windows auth scenarios.

Contributes to dotnet/corefx#34878

Commit migrated from https://github.com/dotnet/corefx/commit/48bcedf786f264aa15adcb55b161ecc0cb2b2172
2019-02-12 21:12:03 -05:00
Geoff Kizer c88fd24fd9 Merge pull request dotnet/corefx#34345 from geoffkizer/genericloopbackserver
Add GenericLoopbackServer for HttpClient testing

Commit migrated from https://github.com/dotnet/corefx/commit/3de6ad8c1233ccf9c781d42a0cdb88b0aeb24cb7
2019-02-12 12:54:50 -08:00
Tomas Weinfurt c5aa04cff7 enable SendAsync_RequestVersion20_ResponseVersion20IfHttp2Supported on Unix (dotnet/corefx#35201)
* enable SendAsync_RequestVersion20_ResponseVersion20IfHttp2Supported on Unix

* use IsWinHttpHandler to skip platform test on older windows

* reorder using statement


Commit migrated from https://github.com/dotnet/corefx/commit/caba0ef9ea05f66aaae814e89848acc98c23f327
2019-02-12 11:45:13 -08:00
Geoff Kizer 2cf7954e1a fix netfx build
Commit migrated from https://github.com/dotnet/corefx/commit/0919bf7fde56a88f8cb2598d3b3709e6a75d279d
2019-02-12 04:00:04 -08:00
David Shulman 327b35cd6d Fix HttpClient EKU tests (dotnet/corefx#35240)
A set of HttpClient tests related to ClientCertificates were always failing on my machine:

System.Net.Http.Functional.Tests.SocketsHttpHandler_HttpClientHandler_ClientCertificates_Test
* method="Manual_SendClientCertificateWithClientAuthEKUToRemoteServer_OK"
* method="Manual_SendClientCertificateWithNoEKUToRemoteServer_OK"
* method="Manual_SendClientCertificateWithServerAuthEKUToRemoteServer_Forbidden"

These tests would generate an exception:
>System.Net.Http.HttpRequestException: An error occurred while sending the request. --->
 System.IO.IOException: The remote party requested renegotiation when AllowRenegotiation was set to false.

Similar to the tests failing and fixed in PR dotnet/corefx#35225, the CI machines don't have the test
root certificate installed. So, the tests weren't running in CI and the failures weren't noticed.

The root cause of the test failures is due to using SocketsHttpHandler. It is always setting
`SslStream.AllowRenegotiation` to false when first attempting to connect via HTTP/2. This is fine
if the connection is truely HTTP/2 since HTTP/2 doesn't support TLS renegotiation. However, if the
remote endpoint doesn't support HTTP/2 and thus connects with HTTP/1.1, then the SslStream should
not have AllowRenegotiation set to false. Client certificate scenarios frequently have the server
ask for the client certificate later in the connection process. So, TLS renegotiation is used. Opened
new issue dotnet/corefx#35239 to fix this in SocketsHttpHandler (or SslStream).

As part of fixing these tests, I refactored them into a single Theory test. But I had to workaround
complications due to using RemoteInvoke. I added a new RemoteInvoke overload to handling passing
an additional parameter.

Commit migrated from https://github.com/dotnet/corefx/commit/0c2ccd172157e0ebf1957e1bbcd5f07786ad6e3d
2019-02-11 17:27:04 -08:00
Tomas Weinfurt a49a1fa554 basic support for PooledConnectionLifetime for HTTP2 (dotnet/corefx#35087)
* basic support for PooledConnectionLifetime

* rework Http2Connection expiration

* feedback from review

* add separate LifetimeExpired() and re-factor expiration checks.

* feedback from review


Commit migrated from https://github.com/dotnet/corefx/commit/45f4518def7d416eb7bd481d118209de2e703429
2019-02-11 13:56:40 -08:00
David Shulman d609d95d6b Disable AuthProxy__ValidCreds_ProxySendsRequestToServer test on Nano (dotnet/corefx#35221)
The System.Net.Http AuthProxy__ValidCreds_ProxySendsRequestToServer test is
frequently failing on Nano.  It is highly likely a test bug and not a product bug.

Contributes to dotnet/corefx#32809

Commit migrated from https://github.com/dotnet/corefx/commit/d0ddc1c8202f1c9e2b9913a533c3aba3202d1f65
2019-02-10 14:32:31 -08:00
David Shulman 8ddab057fd Add NTLM feature detection and more tests (dotnet/corefx#35200)
This PR works toward the goal of our Enterprise Scenarios. I added logic in the Unix
PAL gssapi layer to detect whether the NTLM plugin is installed. This is useful both
in the product code (to return better exception messages) and the test code (to
skip NTLM tests if needed).

Refactored some of the Common System.Net.Configuration logic for tests. Added new
environment variable logic to separate between domain-joined and standalone server
environments.

Add new tests to validate NTLM end-to-end connectivity. Opened some new issues
discovered along with way regarding HTTP/2 and Windows authentication.

To run these new tests simply set the 3 environment variables to point to the
standalone windows server environment.  For example, on Linux:

```
export COREFX_WINDOWSSERVER_HTTPHOST=somewindowsmachine.contoso.com
export COREFX_NET_SERVER_USERNAME=someusername
export COREFX_NET_SERVER_PASSWORD=somepassword
```

I have already deployed a standalone Windows server for these tests but
we're not ready yet to have the tests always run in CI.

Contributes to dotnet/corefx#34878 


Commit migrated from https://github.com/dotnet/corefx/commit/fe6f55b496489dbd02cdd207c0b1f9112b560763
2019-02-10 08:24:06 -08:00
Stephen Toub e8314907d2 Reduce memory allocation in Http2Stream (dotnet/corefx#35179)
* Reduce memory allocation in Http2Stream

- Pool response buffers via ArrayPool
- Don't allocate a separate sync object
- Shrink the size of a few types

* Address PR feedback


Commit migrated from https://github.com/dotnet/corefx/commit/24aa561da2d0632dc284bc230b1147ace848d22b
2019-02-09 19:32:15 -05:00
Stephen Toub 8ec5a72b49 Remove a lot of allocation from SslStream's handshake (dotnet/corefx#35091)
* Remove a lot of allocation from SslStream's handshake

This makes a sizeable dent in the allocation incurred as part of SslStream's handshake (AuthenticateAsClient/Server{Async}), though mainly on Windows; many of these changes also impact Unix, but on Unix there's also substantially more allocation, so the relevant percentage impact is less.

Main changes:
- X509Certificate2.RawData.  This was being accessed by SslStream in order to store the byte[] away in case the RemoteCertificate property was accessed, in which case it would use it to instantiate an X509Certificate2.  That, however, was resulting in lots of sizeable byte[]s being allocated.  We can instead just instantiate an X509Certificate2 from the original, and while that entails a few additional small allocations, it's a sizeable reduction, and we'd eventually need the cert anyway if the property is accessed.
- X509ChainPolicy.  Every time Reset was called, it would allocate several new collections, but they often weren't used.  Made them lazy.
- X509 object finalization.  A bunch of X509-related objects were being left for finalization.  Those are now aggressively disposed.
- ChainPal.GetChainStatusInformation: every call was P/Invoke'ing to get a string error message for a handful of prechosen error codes... we can just get the strings once upfront and avoid allocating them each time.
- OidCollections.  A bunch of these are created but not actually populated, and each time one is created it was allocating a new List.  Changed to just have it be built around an array.
- SecPkgContext classes.  These have been converted to blittable structs, avoiding the associated allocations and marshaling costs, e.g. SecPkgContext_ApplicationProtocol would end up allocating a byte[] along with the instance itself.
- PtrToStructure.  When QueryContextAttributes was being called, in many cases PtrToStructure was being used.  This has been cleaned up, to avoid the marshaling and just use spans / casting.
- SecurityBuffer[] arrays.  Lots of code was written in terms of SecurityBuffer[], which meant lots of operations allocating these.  However, they were never larger than three items.  We now just stack allocate spans for them.
- Overloads taking both SecurityBuffer and SecurityBuffer[].  I believe because of the arrays, there were some cases where methods would take both a SecurityBuffer argument and a SecurityBuffer[] argument, with one and only one non-null.  Now that we're just passing around allocation-free spans, this has been cleaned up.
- GCHandles and GCHandle[].  Multiple methods were allocating arrays of GCHandles, and then one GCHandle.Alloc (not a managed allocation, but also not free) per SecurityBuffer.  Now that we're known to be limited to at most three, we just used fixed (even if we don't actually have three), avoiding the need for the arrays and for the GCHandle.Alloc, which is more expensive than fixed.
- SecBuffer[] arrays.  Several places were allocating a SecBuffer[]; we can just stackalloc a struct.  These are all limited by the number of SecurityBuffers passed around, which is inherently small (<= 3 in the current code).
- SecureChannel.EnsurePrivateKey.  It was always forcing a certificate's Thumbpring into existence, even though it was rarely needed.
- SecureChannel.AcquireClientCredentials.  It was always forcing a List into existence, even if it wasn't needed.
- SslAuthenticationOptions.  This internal type had several fields for delegates that were never used.  Removed.
- Span.ToArray.  In several places utilize span to create an array from a pointer/length rather than allocating the array and Marshal.Copy'ing.  This is mostly for readability.
- Also cleaned up a few things, like using ?.Dispose across System.Net.Security.
- Protocol version strings.  On Unix we were getting the protocol version constant string from the native string, allocating a managed string for it, and then comparing it against the set of known constant strings.  We don't need to do that.

There's still more that can be done.  On Windows, the two big remaining sources of allocation are for byte[]s created for data to send/receive in the handshake, and that will hopefully be addressed by work to combine SslStream/SslStreamInternal/SslState and to rewrite them to use async/await, and the allocation of the actual SecurityBuffers, which should be fairly straightforward to convert to structs (albeit with some care, as they're mutated as they're passed around).  On Unix, there's a ton more allocation, in particular as part of X509Chain building, and that should be investigated separately as part of an effort to improve performance there.

* Move SecurityBuffer to be Windows-only and make it a struct

* Rename several files

* Fix netstandard build of SqlClient


Commit migrated from https://github.com/dotnet/corefx/commit/3c30357d51b96339021b4ee52f38445c31248bb2
2019-02-08 15:09:50 -05:00
Geoff Kizer 20be2ff6d4 rename HttpCookieProtocolTests => HttpClientHandlerTest.Cookies
Commit migrated from https://github.com/dotnet/corefx/commit/47e35192ff1f830b27e2ed5fa14d7d7645a27e95
2019-02-07 17:06:48 -08:00