When the stream returned by WebClient.OpenWrite() / OpenWriteAsync() /
OpenWriteTaskAsync(), we need to call GetResponse() and read the full
response.
Unfortunately, there is no async API available for this, so this API should
not be used in async applications.
* Merged WebConnection.EndWrite() and EndWrite2().
* WebConnectionStream.BeginWrite(): Use the same async-write
callback for both chunked and non-chunked; remove the single-use
small WriteRequestAsync() and inline it here to make the code
more readable.
* WebConnectionStream.WriteRequestAsyncCB(): Now called for all
async writes, call WebConnection.EndWrite() here.
The code is now more readable and easier to understand. This is
the first part of a series of cleanups, which should eventually
lead towards full async and 100-continue.
Revert "Revert "Merge pull request #829 from symform/httpwebreq-async-ssl""
This reverts commit 56d9a057eb, which was a
revert of 35899c07ff, thus putting 35889 back.
* ServicePoint now uses an idle timer to close idle connections.
* WebConnectionGroup has been mostly rewritten to use a linked
list of open connections and their idle times.
This fixes both the problem that we were leaking sockets and
that we were not reusing existing connections.
* ServicePoint now correctly keeps track of the number of open
connections.
* ServicePointManager now uses the correct default idle time
of 100 seconds (previously 15 minutes).
* WebConnection uses the new WebConnectionGroup's busy logic
to correctly flag connections as busy / reusable.
More details:
* HttpWebRequest now writes the HTTP request headers asynchronously. This will avoid blocking a thread pool thread until the headers are written.
* SslClientStream was doing the TLS negotiations in a blocking manner. Converted the negotiation method to an async state machine where all operations are done asynchronously.
* The test case identified in #15451 now completes quickly with less than 15 threads created, vs. 100s of threads used before this fix.
Future work if needed:
* I did not convert SslServerStream but the same pattern could be used here.
* SendAlert is still blocking. I thought it was not worth converting it to non-blocking.
Modulo the GUID in the assembly and the GUID embedded in some
field initializers that use the assembly GUID for their name, the
assemblies are idential before and after the patch.
When we get an error from the server before writing an entire
unbuffered response, the server might close the connection leading to
write errors. Ignore those and return the actual HTTP status code from
the original server error.
* HttpWebRequest.cs:
* WebConnectionStream.cs: if there's an error writing the headers from
a buffered request, propagate the error instead of hiding it and
causing a different one later (ie, ObjectDisposedException instead of
the original WebException). This happened when posting without
Content-Length set to an HTTPS url without validating the server
certificate.
svn path=/trunk/mcs/; revision=139885
* WebConnectionStream.cs: when sending a 0-length POST, ignore further
writes instead of throwing.
Fixes bug #524049.
svn path=/trunk/mcs/; revision=138447
* WebClient.cs: set the content length when uploading values. This
allows reusing the connection.
* WebConnectionStream.cs: SetHeaders() takes ownership of the buffer
and does not need to copy it any more. WriteRequest() wasn't always
setting 'requestWritten' to true.
* HttpWebRequest.cs: change in SetHeaders call.
svn path=/trunk/mcs/; revision=137497
Modified CanRead and CanWrite to return false when instance is disposed.
* HttpWebRequestTest.cs: Enabled tests, and removed test that is now
covered by two separate CanTimeout tests for request and response
stream.
* HttpWebResponseStreamTest.cs: Enabled tests.
svn path=/trunk/mcs/; revision=137450
* WebConnection.cs: provide better error message when there's an error
writing.
* WebConnectionStream.cs: don't nullify the buffer until
CheckFinalStatus() decides that there are no more authentication
retries or redirects.
svn path=/trunk/mcs/; revision=137332
* HttpWebRequest.cs: avoid duplicating Accept-Encoding if GetHeaders()
is called twice.
* WebConnectionStream.cs: don't send a 0 content length for non-write
requests.
svn path=/trunk/mcs/; revision=137233
* WebConnectionStream.cs: when buffering the response, we have to set
the internal length and regenerate the headers.
svn path=/trunk/mcs/; revision=137043
* HttpWebRequest.cs: don't send Content-Length header when SendChunked
is enabled.
* WebConnectionStream.cs: send the headers immediately when possible.
When writing the request before a Close(), if there was an exception
it was lost.
svn path=/trunk/mcs/; revision=137019
* WebAsyncResult.cs: new field that tells us if the request is being
written automatically because we reached the content length.
* WebConnection.cs:
* WebConnectionStream.cs: make sure we only access the NetworkStream
while we own it. Automatically write the request when we reach
ContentLength number of bytes. Kill the write buffer as soon as
possible. Don't use the buffer at all when chunked encoding is
enabled.
Fixes bugs #515931 and #510642.
svn path=/trunk/mcs/; revision=136900
* WebConnectionStream.cs: close the underlying socket if there's an
exception when closing the stream. Fixes bug #514996.
svn path=/trunk/mcs/; revision=136572
* WebConnectionStream.cs: Keep count of all bytes written, and perform
write overflow check in both buffered and non-buffered writing (if
not using chunked transfer encoding). Fixes bug #510642.
svn path=/trunk/mcs/; revision=136570
zero-length string literal. Fixed exception argument name to match
MS.
* WebConnectionStream.cs: Throw a WebException that wraps an
IOException when closing the stream before all bytes are written.
svn path=/trunk/mcs/; revision=136543
* WebConnectionStream.cs: if the buffer has been killed, return -1 for
its length. Fixes regression in System.Runtime.Remoting.
svn path=/trunk/mcs/; revision=123449
* HttpConnection.cs: we were not writing the headers for 0-length
responses.
* WebConnectionStream.cs: for 204/304 responses with an explicit
content-length of 0, release the socket only once.
Fixes bug #465638.
svn path=/trunk/mcs/; revision=123250
* WebConnectionStream.cs: when there's a NTLM authentication in
progress, don't let the connection be used by the next request if we
have the entire response in the input buffer.
svn path=/trunk/mcs/; revision=117511
* HttpWebRequest.cs: don't call ProxyRequest twice.
* WebConnectionData.cs: remove unused fields.
* WebConnection.cs:
* WebConnectionStream.cs: when setting the input buffer, check to see
if the entire response is already there and free up the request slot.
Fixes bug #423156.
* ResponseStream.cs:
* HttpListenerResponse.cs: buffer the headers so that they are sent
together with the data on the first Write (if any). The fix above
didn't work with our HttpListener without this.
svn path=/trunk/mcs/; revision=116868