public override void Abort () { if (Interlocked.CompareExchange (ref aborted, 1, 0) == 1) return; if (haveResponse && finished_reading) return; haveResponse = true; if (abortHandler != null) { try { abortHandler (this, EventArgs.Empty); } catch (Exception) {} abortHandler = null; } if (asyncWrite != null) { WebAsyncResult r = asyncWrite; if (!r.IsCompleted) { try { WebException wexc = new WebException ("Aborted.", WebExceptionStatus.RequestCanceled); r.SetCompleted (false, wexc); r.DoCallback (); } catch {} } asyncWrite = null; } if (asyncRead != null) { WebAsyncResult r = asyncRead; if (!r.IsCompleted) { try { WebException wexc = new WebException ("Aborted.", WebExceptionStatus.RequestCanceled); r.SetCompleted (false, wexc); r.DoCallback (); } catch {} } asyncRead = null; } if (writeStream != null) { try { writeStream.Close (); writeStream = null; } catch {} } if (webResponse != null) { try { webResponse.Close (); webResponse = null; } catch {} } }
internal void ReadAll() { WebConnectionStream wce = stream as WebConnectionStream; if (wce == null) { return; } try { wce.ReadAll(); } catch {} }
internal void ReadAll() { WebConnectionStream webConnectionStream = stream as WebConnectionStream; if (webConnectionStream != null) { try { webConnectionStream.ReadAll(); } catch { } } }
internal void ReadAll() { WebConnectionStream webConnectionStream = this.stream as WebConnectionStream; if (webConnectionStream == null) { return; } try { webConnectionStream.ReadAll(); } catch { } }
public override void Abort() { haveResponse = true; aborted = true; if (asyncWrite != null) { WebAsyncResult r = asyncWrite; WebException wexc = new WebException("Aborted.", WebExceptionStatus.RequestCanceled); r.SetCompleted(false, wexc); r.DoCallback(); asyncWrite = null; } if (asyncRead != null) { WebAsyncResult r = asyncRead; WebException wexc = new WebException("Aborted.", WebExceptionStatus.RequestCanceled); r.SetCompleted(false, wexc); r.DoCallback(); asyncRead = null; } if (abortHandler != null) { try { abortHandler(this, EventArgs.Empty); } catch (Exception) {} abortHandler = null; } if (writeStream != null) { try { writeStream.Close(); writeStream = null; } catch {} } if (webResponse != null) { try { webResponse.Close(); webResponse = null; } catch {} } }
void HandleNtlmAuth (WebAsyncResult r) { WebConnectionStream wce = webResponse.GetResponseStream () as WebConnectionStream; if (wce != null) { WebConnection cnc = wce.Connection; cnc.PriorityRequest = this; bool isProxy = (proxy != null && !proxy.IsBypassed (actualUri)); ICredentials creds = (!isProxy) ? credentials : proxy.Credentials; if (creds != null) { cnc.NtlmCredential = creds.GetCredential (requestUri, "NTLM"); cnc.UnsafeAuthenticatedConnectionSharing = unsafe_auth_blah; } } r.Reset (); finished_reading = false; haveResponse = false; webResponse.ReadAll (); webResponse = null; }
internal void SetWriteStream(WebConnectionStream stream) { if (aborted) { return; } writeStream = stream; if (bodyBuffer != null) { webHeaders.RemoveInternal("Transfer-Encoding"); contentLength = bodyBufferLength; writeStream.SendChunked = false; } SendRequestHeaders(); haveRequest = true; if (bodyBuffer != null) { // The body has been written and buffered. The request "user" // won't write it again, so we must do it. writeStream.Write(bodyBuffer, 0, bodyBufferLength); bodyBuffer = null; writeStream.Close(); } else if (method == "PUT" || method == "POST" || method == "OPTIONS") { if (getResponseCalled && !writeStream.RequestWritten) { writeStream.WriteRequest(); } } if (asyncWrite != null) { asyncWrite.SetCompleted(false, stream); asyncWrite.DoCallback(); asyncWrite = null; } }
internal void SetWriteStream (WebConnectionStream stream) { if (Aborted) return; writeStream = stream; if (bodyBuffer != null) { webHeaders.RemoveInternal ("Transfer-Encoding"); contentLength = bodyBufferLength; writeStream.SendChunked = false; } SendRequestHeaders (false); haveRequest = true; if (bodyBuffer != null) { // The body has been written and buffered. The request "user" // won't write it again, so we must do it. if (ntlm_auth_state != NtlmAuthState.Challenge) { writeStream.Write (bodyBuffer, 0, bodyBufferLength); bodyBuffer = null; writeStream.Close (); } } else if (method != "HEAD" && method != "GET" && method != "MKCOL" && method != "CONNECT" && method != "TRACE") { if (getResponseCalled && !writeStream.RequestWritten) writeStream.WriteRequest (); } if (asyncWrite != null) { asyncWrite.SetCompleted (false, stream); asyncWrite.DoCallback (); asyncWrite = null; } }
// Returns true if redirected bool CheckFinalStatus(WebAsyncResult result) { if (result.GotException) { throw result.Exception; } Exception throwMe = result.Exception; bodyBuffer = null; HttpWebResponse resp = result.Response; WebExceptionStatus protoError = WebExceptionStatus.ProtocolError; HttpStatusCode code = 0; if (throwMe == null && webResponse != null) { code = webResponse.StatusCode; if (!authCompleted && ((code == HttpStatusCode.Unauthorized && credentials != null) || (ProxyQuery && code == HttpStatusCode.ProxyAuthenticationRequired))) { if (!usedPreAuth && CheckAuthorization(webResponse, code)) { // Keep the written body, so it can be rewritten in the retry if (InternalAllowBuffering) { bodyBuffer = writeStream.WriteBuffer; bodyBufferLength = writeStream.WriteBufferLength; webResponse.Close(); return(true); } else if (method != "PUT" && method != "POST") { webResponse.Close(); return(true); } writeStream.InternalClose(); writeStream = null; webResponse.Close(); webResponse = null; throw new WebException("This request requires buffering " + "of data for authentication or " + "redirection to be sucessful."); } } if ((int)code >= 400) { string err = String.Format("The remote server returned an error: ({0}) {1}.", (int)code, webResponse.StatusDescription); throwMe = new WebException(err, null, protoError, webResponse); webResponse.ReadAll(); } else if ((int)code == 304 && allowAutoRedirect) { string err = String.Format("The remote server returned an error: ({0}) {1}.", (int)code, webResponse.StatusDescription); throwMe = new WebException(err, null, protoError, webResponse); } else if ((int)code >= 300 && allowAutoRedirect && redirects > maxAutoRedirect) { throwMe = new WebException("Max. redirections exceeded.", null, protoError, webResponse); webResponse.ReadAll(); } } if (throwMe == null) { bool b = false; int c = (int)code; if (allowAutoRedirect && c >= 300) { if (InternalAllowBuffering && writeStream.WriteBufferLength > 0) { bodyBuffer = writeStream.WriteBuffer; bodyBufferLength = writeStream.WriteBufferLength; } b = Redirect(result, code); } if (resp != null && c >= 300 && c != 304) { resp.ReadAll(); } return(b); } if (writeStream != null) { writeStream.InternalClose(); writeStream = null; } webResponse = null; throw throwMe; }
static void ReadDone(IAsyncResult result) { WebConnection cnc = (WebConnection)result.AsyncState; WebConnectionData data = cnc.Data; Stream ns = cnc.nstream; if (ns == null) { cnc.Close(true); return; } int nread = -1; try { nread = ns.EndRead(result); } catch (Exception e) { cnc.HandleError(WebExceptionStatus.ReceiveFailure, e, "ReadDone1"); return; } if (nread == 0) { cnc.HandleError(WebExceptionStatus.ReceiveFailure, null, "ReadDone2"); return; } if (nread < 0) { cnc.HandleError(WebExceptionStatus.ServerProtocolViolation, null, "ReadDone3"); return; } int pos = -1; nread += cnc.position; if (cnc.readState == ReadState.None) { Exception exc = null; try { pos = cnc.GetResponse(cnc.buffer, nread); } catch (Exception e) { exc = e; } if (exc != null) { cnc.HandleError(WebExceptionStatus.ServerProtocolViolation, exc, "ReadDone4"); return; } } if (cnc.readState != ReadState.Content) { int est = nread * 2; int max = (est < cnc.buffer.Length) ? cnc.buffer.Length : est; byte [] newBuffer = new byte [max]; Buffer.BlockCopy(cnc.buffer, 0, newBuffer, 0, nread); cnc.buffer = newBuffer; cnc.position = nread; cnc.readState = ReadState.None; InitRead(cnc); return; } cnc.position = 0; WebConnectionStream stream = new WebConnectionStream(cnc); string contentType = data.Headers ["Transfer-Encoding"]; cnc.chunkedRead = (contentType != null && contentType.ToLower().IndexOf("chunked") != -1); if (!cnc.chunkedRead) { stream.ReadBuffer = cnc.buffer; stream.ReadBufferOffset = pos; stream.ReadBufferSize = nread; stream.CheckResponseInBuffer(); } else if (cnc.chunkStream == null) { try { cnc.chunkStream = new ChunkStream(cnc.buffer, pos, nread, data.Headers); } catch (Exception e) { cnc.HandleError(WebExceptionStatus.ServerProtocolViolation, e, "ReadDone5"); return; } } else { cnc.chunkStream.ResetBuffer(); try { cnc.chunkStream.Write(cnc.buffer, pos, nread); } catch (Exception e) { cnc.HandleError(WebExceptionStatus.ServerProtocolViolation, e, "ReadDone6"); return; } } data.stream = stream; if (!ExpectContent(data.StatusCode) || data.request.Method == "HEAD") { stream.ForceCompletion(); } data.request.SetResponseData(data); }
// Returns true if redirected bool CheckFinalStatus (WebAsyncResult result) { if (result.GotException) { bodyBuffer = null; throw result.Exception; } Exception throwMe = result.Exception; HttpWebResponse resp = result.Response; WebExceptionStatus protoError = WebExceptionStatus.ProtocolError; HttpStatusCode code = 0; if (throwMe == null && webResponse != null) { code = webResponse.StatusCode; if (!authCompleted && ((code == HttpStatusCode.Unauthorized && credentials != null) || (ProxyQuery && code == HttpStatusCode.ProxyAuthenticationRequired))) { if (!usedPreAuth && CheckAuthorization (webResponse, code)) { // Keep the written body, so it can be rewritten in the retry if (InternalAllowBuffering) { // NTLM: This is to avoid sending data in the 'challenge' request // We save it in the first request (first 401), don't send anything // in the challenge request and send it in the response request along // with the buffers kept form the first request. if (ntlm_auth_state != NtlmAuthState.Response) { bodyBuffer = writeStream.WriteBuffer; bodyBufferLength = writeStream.WriteBufferLength; } return true; } else if (method != "PUT" && method != "POST") { bodyBuffer = null; return true; } if (!ThrowOnError) return false; writeStream.InternalClose (); writeStream = null; webResponse.Close (); webResponse = null; bodyBuffer = null; throw new WebException ("This request requires buffering " + "of data for authentication or " + "redirection to be sucessful."); } } bodyBuffer = null; if ((int) code >= 400) { string err = String.Format ("The remote server returned an error: ({0}) {1}.", (int) code, webResponse.StatusDescription); throwMe = new WebException (err, null, protoError, webResponse); webResponse.ReadAll (); } else if ((int) code == 304 && allowAutoRedirect) { string err = String.Format ("The remote server returned an error: ({0}) {1}.", (int) code, webResponse.StatusDescription); throwMe = new WebException (err, null, protoError, webResponse); } else if ((int) code >= 300 && allowAutoRedirect && redirects >= maxAutoRedirect) { throwMe = new WebException ("Max. redirections exceeded.", null, protoError, webResponse); webResponse.ReadAll (); } } bodyBuffer = null; if (throwMe == null) { bool b = false; int c = (int) code; if (allowAutoRedirect && c >= 300) { if (InternalAllowBuffering && writeStream.WriteBufferLength > 0) { bodyBuffer = writeStream.WriteBuffer; bodyBufferLength = writeStream.WriteBufferLength; } b = Redirect (result, code); if (b && ntlm_auth_state != 0) ntlm_auth_state = 0; } if (resp != null && c >= 300 && c != 304) resp.ReadAll (); return b; } if (!ThrowOnError) return false; if (writeStream != null) { writeStream.InternalClose (); writeStream = null; } webResponse = null; throw throwMe; }
internal void SetWriteStream (WebConnectionStream stream) { if (Aborted) return; writeStream = stream; if (bodyBuffer != null) { webHeaders.RemoveInternal ("Transfer-Encoding"); contentLength = bodyBufferLength; writeStream.SendChunked = false; } try { var result = writeStream.SetHeadersAsync (false, SetWriteStreamCB, null); if (result == null) SetWriteStreamCB (null); } catch (Exception exc) { SetWriteStreamErrorCB (exc); } }
static void ReadDone(IAsyncResult result) { WebConnection cnc = (WebConnection)result.AsyncState; WebConnectionData data = cnc.Data; Stream ns = cnc.nstream; if (ns == null) { cnc.Close(true); return; } int nread = -1; try { nread = ns.EndRead(result); } catch (ObjectDisposedException) { return; } catch (Exception e) { if (e.InnerException is ObjectDisposedException) { return; } cnc.HandleError(WebExceptionStatus.ReceiveFailure, e, "ReadDone1"); return; } if (nread == 0) { cnc.HandleError(WebExceptionStatus.ReceiveFailure, null, "ReadDone2"); return; } if (nread < 0) { cnc.HandleError(WebExceptionStatus.ServerProtocolViolation, null, "ReadDone3"); return; } int pos = -1; nread += cnc.position; if (data.ReadState == ReadState.None) { Exception exc = null; try { pos = GetResponse(data, cnc.sPoint, cnc.buffer, nread); } catch (Exception e) { exc = e; } if (exc != null || pos == -1) { cnc.HandleError(WebExceptionStatus.ServerProtocolViolation, exc, "ReadDone4"); return; } } if (data.ReadState == ReadState.Aborted) { cnc.HandleError(WebExceptionStatus.RequestCanceled, null, "ReadDone"); return; } if (data.ReadState != ReadState.Content) { int est = nread * 2; int max = (est < cnc.buffer.Length) ? cnc.buffer.Length : est; byte [] newBuffer = new byte [max]; Buffer.BlockCopy(cnc.buffer, 0, newBuffer, 0, nread); cnc.buffer = newBuffer; cnc.position = nread; data.ReadState = ReadState.None; InitRead(cnc); return; } cnc.position = 0; WebConnectionStream stream = new WebConnectionStream(cnc, data); bool expect_content = ExpectContent(data.StatusCode, data.request.Method); string tencoding = null; if (expect_content) { tencoding = data.Headers ["Transfer-Encoding"]; } cnc.chunkedRead = (tencoding != null && tencoding.IndexOf("chunked", StringComparison.OrdinalIgnoreCase) != -1); if (!cnc.chunkedRead) { stream.ReadBuffer = cnc.buffer; stream.ReadBufferOffset = pos; stream.ReadBufferSize = nread; try { stream.CheckResponseInBuffer(); } catch (Exception e) { cnc.HandleError(WebExceptionStatus.ReceiveFailure, e, "ReadDone7"); } } else if (cnc.chunkStream == null) { try { cnc.chunkStream = new ChunkStream(cnc.buffer, pos, nread, data.Headers); } catch (Exception e) { cnc.HandleError(WebExceptionStatus.ServerProtocolViolation, e, "ReadDone5"); return; } } else { cnc.chunkStream.ResetBuffer(); try { cnc.chunkStream.Write(cnc.buffer, pos, nread); } catch (Exception e) { cnc.HandleError(WebExceptionStatus.ServerProtocolViolation, e, "ReadDone6"); return; } } data.stream = stream; if (!expect_content) { stream.ForceCompletion(); } data.request.SetResponseData(data); }
// Returns true if redirected bool CheckFinalStatus (WebAsyncResult result) { if (result.GotException) throw result.Exception; Exception throwMe = result.Exception; bodyBuffer = null; HttpWebResponse resp = result.Response; WebExceptionStatus protoError = WebExceptionStatus.ProtocolError; HttpStatusCode code = 0; if (throwMe == null && webResponse != null) { code = webResponse.StatusCode; if (!authCompleted && ((code == HttpStatusCode.Unauthorized && credentials != null) || (ProxyQuery && code == HttpStatusCode.ProxyAuthenticationRequired))) { if (!usedPreAuth && CheckAuthorization (webResponse, code)) { // Keep the written body, so it can be rewritten in the retry if (InternalAllowBuffering) { bodyBuffer = writeStream.WriteBuffer; bodyBufferLength = writeStream.WriteBufferLength; return true; } else if (method != "PUT" && method != "POST") { return true; } writeStream.InternalClose (); writeStream = null; webResponse.Close (); webResponse = null; throw new WebException ("This request requires buffering " + "of data for authentication or " + "redirection to be sucessful."); } } if ((int) code >= 400) { string err = String.Format ("The remote server returned an error: ({0}) {1}.", (int) code, webResponse.StatusDescription); throwMe = new WebException (err, null, protoError, webResponse); webResponse.ReadAll (); } else if ((int) code == 304 && allowAutoRedirect) { string err = String.Format ("The remote server returned an error: ({0}) {1}.", (int) code, webResponse.StatusDescription); throwMe = new WebException (err, null, protoError, webResponse); } else if ((int) code >= 300 && allowAutoRedirect && redirects >= maxAutoRedirect) { throwMe = new WebException ("Max. redirections exceeded.", null, protoError, webResponse); webResponse.ReadAll (); } } if (throwMe == null) { bool b = false; int c = (int) code; if (allowAutoRedirect && c >= 300) { if (InternalAllowBuffering && writeStream.WriteBufferLength > 0) { bodyBuffer = writeStream.WriteBuffer; bodyBufferLength = writeStream.WriteBufferLength; } b = Redirect (result, code); } if (resp != null && c >= 300 && c != 304) resp.ReadAll (); return b; } if (writeStream != null) { writeStream.InternalClose (); writeStream = null; } webResponse = null; throw throwMe; }
// Returns true if redirected bool CheckFinalStatus (WebAsyncResult result) { if (result.GotException) { bodyBuffer = null; throw result.Exception; } Exception throwMe = result.Exception; HttpWebResponse resp = result.Response; WebExceptionStatus protoError = WebExceptionStatus.ProtocolError; HttpStatusCode code = 0; if (throwMe == null && webResponse != null) { code = webResponse.StatusCode; if ((!auth_state.IsCompleted && code == HttpStatusCode.Unauthorized && credentials != null) || (ProxyQuery && !proxy_auth_state.IsCompleted && code == HttpStatusCode.ProxyAuthenticationRequired)) { if (!usedPreAuth && CheckAuthorization (webResponse, code)) { // Keep the written body, so it can be rewritten in the retry if (MethodWithBuffer) { if (AllowWriteStreamBuffering) { if (writeStream.WriteBufferLength > 0) { bodyBuffer = writeStream.WriteBuffer; bodyBufferLength = writeStream.WriteBufferLength; } return true; } // // Buffering is not allowed but we have alternative way to get same content (we // need to resent it due to NTLM Authentication). // if (ResendContentFactory != null) { using (var ms = new MemoryStream ()) { ResendContentFactory (ms); bodyBuffer = ms.ToArray (); bodyBufferLength = bodyBuffer.Length; } return true; } } else if (method != "PUT" && method != "POST") { bodyBuffer = null; return true; } if (!ThrowOnError) return false; writeStream.InternalClose (); writeStream = null; webResponse.Close (); webResponse = null; bodyBuffer = null; throw new WebException ("This request requires buffering " + "of data for authentication or " + "redirection to be sucessful."); } } bodyBuffer = null; if ((int) code >= 400) { string err = String.Format ("The remote server returned an error: ({0}) {1}.", (int) code, webResponse.StatusDescription); throwMe = new WebException (err, null, protoError, webResponse); webResponse.ReadAll (); } else if ((int) code == 304 && allowAutoRedirect) { string err = String.Format ("The remote server returned an error: ({0}) {1}.", (int) code, webResponse.StatusDescription); throwMe = new WebException (err, null, protoError, webResponse); } else if ((int) code >= 300 && allowAutoRedirect && redirects >= maxAutoRedirect) { throwMe = new WebException ("Max. redirections exceeded.", null, protoError, webResponse); webResponse.ReadAll (); } } bodyBuffer = null; if (throwMe == null) { bool b = false; int c = (int) code; if (allowAutoRedirect && c >= 300) { b = Redirect (result, code, webResponse); if (InternalAllowBuffering && writeStream.WriteBufferLength > 0) { bodyBuffer = writeStream.WriteBuffer; bodyBufferLength = writeStream.WriteBufferLength; } if (b && !unsafe_auth_blah) { auth_state.Reset (); proxy_auth_state.Reset (); } } if (resp != null && c >= 300 && c != 304) resp.ReadAll (); return b; } if (!ThrowOnError) return false; if (writeStream != null) { writeStream.InternalClose (); writeStream = null; } webResponse = null; throw throwMe; }
internal void SetWriteStream (WebConnectionStream stream) { if (Aborted) return; writeStream = stream; if (bodyBuffer != null) { webHeaders.RemoveInternal ("Transfer-Encoding"); contentLength = bodyBufferLength; writeStream.SendChunked = false; } byte[] requestHeaders = GetRequestHeaders (); WebAsyncResult result = new WebAsyncResult (new AsyncCallback (SetWriteStreamCB), null); writeStream.SetHeadersAsync (requestHeaders, result); }
static void ReadDone (IAsyncResult result) { WebConnection cnc = (WebConnection) result.AsyncState; WebConnectionData data = cnc.Data; Stream ns = cnc.nstream; if (ns == null) { cnc.Close (true); return; } int nread = -1; try { nread = ns.EndRead (result); } catch (ObjectDisposedException) { return; } catch (Exception e) { if (e.InnerException is ObjectDisposedException) return; cnc.HandleError (WebExceptionStatus.ReceiveFailure, e, "ReadDone1"); return; } if (nread == 0) { cnc.HandleError (WebExceptionStatus.ReceiveFailure, null, "ReadDone2"); return; } if (nread < 0) { cnc.HandleError (WebExceptionStatus.ServerProtocolViolation, null, "ReadDone3"); return; } int pos = -1; nread += cnc.position; if (cnc.readState == ReadState.None) { Exception exc = null; try { pos = cnc.GetResponse (cnc.buffer, nread); } catch (Exception e) { exc = e; } if (exc != null || pos == -1) { cnc.HandleError (WebExceptionStatus.ServerProtocolViolation, exc, "ReadDone4"); return; } } if (cnc.readState != ReadState.Content) { int est = nread * 2; int max = (est < cnc.buffer.Length) ? cnc.buffer.Length : est; byte [] newBuffer = new byte [max]; Buffer.BlockCopy (cnc.buffer, 0, newBuffer, 0, nread); cnc.buffer = newBuffer; cnc.position = nread; cnc.readState = ReadState.None; InitRead (cnc); return; } cnc.position = 0; WebConnectionStream stream = new WebConnectionStream (cnc); bool expect_content = ExpectContent (data.StatusCode, data.request.Method); string tencoding = null; if (expect_content) tencoding = data.Headers ["Transfer-Encoding"]; cnc.chunkedRead = (tencoding != null && tencoding.IndexOf ("chunked", StringComparison.OrdinalIgnoreCase) != -1); if (!cnc.chunkedRead) { stream.ReadBuffer = cnc.buffer; stream.ReadBufferOffset = pos; stream.ReadBufferSize = nread; try { stream.CheckResponseInBuffer (); } catch (Exception e) { cnc.HandleError (WebExceptionStatus.ReceiveFailure, e, "ReadDone7"); } } else if (cnc.chunkStream == null) { try { cnc.chunkStream = new ChunkStream (cnc.buffer, pos, nread, data.Headers); } catch (Exception e) { cnc.HandleError (WebExceptionStatus.ServerProtocolViolation, e, "ReadDone5"); return; } } else { cnc.chunkStream.ResetBuffer (); try { cnc.chunkStream.Write (cnc.buffer, pos, nread); } catch (Exception e) { cnc.HandleError (WebExceptionStatus.ServerProtocolViolation, e, "ReadDone6"); return; } } data.stream = stream; if (!expect_content) stream.ForceCompletion (); data.request.SetResponseData (data); }
internal void SetResponseData (WebConnectionData data) { lock (locker) { if (Aborted) { if (data.stream != null) data.stream.Close (); return; } WebException wexc = null; try { webResponse = new HttpWebResponse (actualUri, method, data, cookieContainer); } catch (Exception e) { wexc = new WebException (e.Message, e, WebExceptionStatus.ProtocolError, null); if (data.stream != null) data.stream.Close (); } if (wexc == null && (method == "POST" || method == "PUT")) { CheckSendError (data); if (saved_exc != null) wexc = (WebException) saved_exc; } WebAsyncResult r = asyncRead; bool forced = false; if (r == null && webResponse != null) { // This is a forced completion (302, 204)... forced = true; r = new WebAsyncResult (null, null); r.SetCompleted (false, webResponse); } if (r != null) { if (wexc != null) { haveResponse = true; if (!r.IsCompleted) r.SetCompleted (false, wexc); r.DoCallback (); return; } bool redirected; try { redirected = CheckFinalStatus (r); if (!redirected) { if (ntlm_auth_state != NtlmAuthState.None && authCompleted && webResponse != null && (int)webResponse.StatusCode < 400) { WebConnectionStream wce = webResponse.GetResponseStream () as WebConnectionStream; if (wce != null) { WebConnection cnc = wce.Connection; cnc.NtlmAuthenticated = true; } } // clear internal buffer so that it does not // hold possible big buffer (bug #397627) if (writeStream != null) writeStream.KillBuffer (); haveResponse = true; r.SetCompleted (false, webResponse); r.DoCallback (); } else { if (webResponse != null) { if (ntlm_auth_state != NtlmAuthState.None) { HandleNtlmAuth (r); return; } webResponse.Close (); } finished_reading = false; haveResponse = false; webResponse = null; r.Reset (); servicePoint = GetServicePoint (); abortHandler = servicePoint.SendRequest (this, connectionGroup); } } catch (WebException wexc2) { if (forced) { saved_exc = wexc2; haveResponse = true; } r.SetCompleted (false, wexc2); r.DoCallback (); return; } catch (Exception ex) { wexc = new WebException (ex.Message, ex, WebExceptionStatus.ProtocolError, null); if (forced) { saved_exc = wexc; haveResponse = true; } r.SetCompleted (false, wexc); r.DoCallback (); return; } } } }
private static void ReadDone(IAsyncResult result) { WebConnection webConnection = (WebConnection)result.AsyncState; WebConnectionData data = webConnection.Data; Stream stream = webConnection.nstream; if (stream == null) { webConnection.Close(sendNext: true); return; } int num = -1; try { num = stream.EndRead(result); } catch (Exception e) { webConnection.HandleError(WebExceptionStatus.ReceiveFailure, e, "ReadDone1"); return; IL_004c :; } if (num == 0) { webConnection.HandleError(WebExceptionStatus.ReceiveFailure, null, "ReadDone2"); return; } if (num < 0) { webConnection.HandleError(WebExceptionStatus.ServerProtocolViolation, null, "ReadDone3"); return; } int num2 = -1; num += webConnection.position; if (webConnection.readState == ReadState.None) { Exception ex = null; try { num2 = webConnection.GetResponse(webConnection.buffer, num); } catch (Exception ex2) { ex = ex2; } if (ex != null) { webConnection.HandleError(WebExceptionStatus.ServerProtocolViolation, ex, "ReadDone4"); return; } } if (webConnection.readState != ReadState.Content) { int num3 = num * 2; int num4 = (num3 >= webConnection.buffer.Length) ? num3 : webConnection.buffer.Length; byte[] dst = new byte[num4]; Buffer.BlockCopy(webConnection.buffer, 0, dst, 0, num); webConnection.buffer = dst; webConnection.position = num; webConnection.readState = ReadState.None; InitRead(webConnection); return; } webConnection.position = 0; WebConnectionStream webConnectionStream = new WebConnectionStream(webConnection); string text = data.Headers["Transfer-Encoding"]; webConnection.chunkedRead = (text != null && text.ToLower().IndexOf("chunked") != -1); if (!webConnection.chunkedRead) { webConnectionStream.ReadBuffer = webConnection.buffer; webConnectionStream.ReadBufferOffset = num2; webConnectionStream.ReadBufferSize = num; webConnectionStream.CheckResponseInBuffer(); } else if (webConnection.chunkStream == null) { try { webConnection.chunkStream = new ChunkStream(webConnection.buffer, num2, num, data.Headers); } catch (Exception e2) { webConnection.HandleError(WebExceptionStatus.ServerProtocolViolation, e2, "ReadDone5"); return; IL_01ef :; } } else { webConnection.chunkStream.ResetBuffer(); try { webConnection.chunkStream.Write(webConnection.buffer, num2, num); } catch (Exception e3) { webConnection.HandleError(WebExceptionStatus.ServerProtocolViolation, e3, "ReadDone6"); return; IL_0233 :; } } data.stream = webConnectionStream; if (!ExpectContent(data.StatusCode) || data.request.Method == "HEAD") { webConnectionStream.ForceCompletion(); } data.request.SetResponseData(data); }
internal void SetWriteStream (WebConnectionStream stream) { if (Aborted) return; writeStream = stream; if (bodyBuffer != null) { webHeaders.RemoveInternal ("Transfer-Encoding"); contentLength = bodyBufferLength; writeStream.SendChunked = false; } writeStream.SetHeadersAsync (false, result => { if (result.GotException) { SetWriteStreamError (result.Exception); return; } haveRequest = true; SetWriteStreamInner (inner => { if (inner.GotException) { SetWriteStreamError (inner.Exception); return; } if (asyncWrite != null) { asyncWrite.SetCompleted (inner.CompletedSynchronously, writeStream); asyncWrite.DoCallback (); asyncWrite = null; } }); }); }