internal IAsyncResult BeginRead(HttpWebRequest request, byte [] buffer, int offset, int size, AsyncCallback cb, object state) { Stream s = null; lock (this) { if (Data.request != request) { throw new ObjectDisposedException(typeof(NetworkStream).FullName); } if (nstream == null) { return(null); } s = nstream; } IAsyncResult result = null; if (!chunkedRead || (!chunkStream.DataAvailable && chunkStream.WantMore)) { try { result = s.BeginRead(buffer, offset, size, cb, state); cb = null; } catch (Exception) { HandleError(WebExceptionStatus.ReceiveFailure, null, "chunked BeginRead"); throw; } } if (chunkedRead) { WebAsyncResult wr = new WebAsyncResult(cb, state, buffer, offset, size); wr.InnerAsyncResult = result; if (result == null) { // Will be completed from the data in ChunkStream wr.SetCompleted(true, (Exception)null); wr.DoCallback(); } return(wr); } return(result); }
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 IAsyncResult BeginRead(HttpWebRequest request, byte[] buffer, int offset, int size, AsyncCallback cb, object state) { lock (this) { if (Data.request != request) { throw new ObjectDisposedException(typeof(NetworkStream).FullName); } if (nstream == null) { return(null); } } IAsyncResult asyncResult = null; if (!chunkedRead || chunkStream.WantMore) { try { asyncResult = nstream.BeginRead(buffer, offset, size, cb, state); cb = null; } catch (Exception) { HandleError(WebExceptionStatus.ReceiveFailure, null, "chunked BeginRead"); throw; IL_0095 :; } } if (chunkedRead) { WebAsyncResult webAsyncResult = new WebAsyncResult(cb, state, buffer, offset, size); webAsyncResult.InnerAsyncResult = asyncResult; if (asyncResult == null) { webAsyncResult.SetCompleted(synch: true, (Exception)null); webAsyncResult.DoCallback(); } return(webAsyncResult); } return(asyncResult); }
internal void SetWriteStreamError(WebExceptionStatus status) { if (aborted) { return; } WebAsyncResult r = asyncWrite; if (r == null) { r = asyncRead; } if (r != null) { r.SetCompleted(false, new WebException("Error: " + status, status)); r.DoCallback(); } }
internal void SetWriteStreamError (WebExceptionStatus status, Exception exc) { if (Aborted) return; WebAsyncResult r = asyncWrite; if (r == null) r = asyncRead; if (r != null) { string msg; WebException wex; if (exc == null) { msg = "Error: " + status; wex = new WebException (msg, status); } else { msg = String.Format ("Error: {0} ({1})", status, exc.Message); wex = new WebException (msg, exc, status); } r.SetCompleted (false, wex); r.DoCallback (); } }
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; } }
internal IAsyncResult BeginRead (HttpWebRequest request, byte [] buffer, int offset, int size, AsyncCallback cb, object state) { Stream s = null; lock (this) { if (Data.request != request) throw new ObjectDisposedException (typeof (NetworkStream).FullName); if (nstream == null) return null; s = nstream; } IAsyncResult result = null; if (!chunkedRead || (!chunkStream.DataAvailable && chunkStream.WantMore)) { try { result = s.BeginRead (buffer, offset, size, cb, state); cb = null; } catch (Exception) { HandleError (WebExceptionStatus.ReceiveFailure, null, "chunked BeginRead"); throw; } } if (chunkedRead) { WebAsyncResult wr = new WebAsyncResult (cb, state, buffer, offset, size); wr.InnerAsyncResult = result; if (result == null) { // Will be completed from the data in ChunkStream wr.SetCompleted (true, (Exception) null); wr.DoCallback (); } return wr; } return result; }
public override IAsyncResult BeginGetRequestStream (AsyncCallback callback, object state) { if (Aborted) throw new WebException ("The request was canceled.", WebExceptionStatus.RequestCanceled); bool send = !(method == "GET" || method == "CONNECT" || method == "HEAD" || method == "TRACE"); if (method == null || !send) throw new ProtocolViolationException ("Cannot send data when method is: " + method); if (contentLength == -1 && !sendChunked && !allowBuffering && KeepAlive) throw new ProtocolViolationException ("Content-Length not set"); string transferEncoding = TransferEncoding; if (!sendChunked && transferEncoding != null && transferEncoding.Trim () != "") throw new ProtocolViolationException ("SendChunked should be true."); lock (locker) { if (getResponseCalled) throw new InvalidOperationException ("The operation cannot be performed once the request has been submitted."); if (asyncWrite != null) { throw new InvalidOperationException ("Cannot re-call start of asynchronous " + "method while a previous call is still in progress."); } asyncWrite = new WebAsyncResult (this, callback, state); initialMethod = method; if (haveRequest) { if (writeStream != null) { asyncWrite.SetCompleted (true, writeStream); asyncWrite.DoCallback (); return asyncWrite; } } gotRequestStream = true; WebAsyncResult result = asyncWrite; if (!requestSent) { requestSent = true; redirects = 0; servicePoint = GetServicePoint (); abortHandler = servicePoint.SendRequest (this, connectionGroup); } return result; } }
public override void EndWrite(IAsyncResult r) { if (r == null) { throw new ArgumentNullException("r"); } WebAsyncResult result = r as WebAsyncResult; if (result == null) { throw new ArgumentException("Invalid IAsyncResult"); } if (result.EndCalled) { return; } result.EndCalled = true; if (result.AsyncWriteAll) { result.WaitUntilComplete(); if (result.GotException) { throw result.Exception; } return; } if (allowBuffering && !sendChunked) { return; } if (result.GotException) { throw result.Exception; } try { cnc.EndWrite2(request, result.InnerAsyncResult); result.SetCompleted(false, 0); result.DoCallback(); } catch (Exception e) { if (IgnoreIOErrors) { result.SetCompleted(false, 0); } else { result.SetCompleted(false, e); } result.DoCallback(); if (!IgnoreIOErrors) { throw; } } finally { if (sendChunked) { lock (locker) { pendingWrites--; if (pendingWrites == 0) { pending.Set(); } } } } }
void GetResponseAsyncCB2 (WebAsyncResult aread) { if (haveResponse) { Exception saved = saved_exc; if (webResponse != null) { Monitor.Exit (locker); if (saved == null) { aread.SetCompleted (true, webResponse); } else { aread.SetCompleted (true, saved); } aread.DoCallback (); return; } else if (saved != null) { Monitor.Exit (locker); aread.SetCompleted (true, saved); aread.DoCallback (); return; } } if (!requestSent) { requestSent = true; redirects = 0; servicePoint = GetServicePoint (); abortHandler = servicePoint.SendRequest (this, connectionGroup); } Monitor.Exit (locker); }
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; } } } }
public override int EndRead(IAsyncResult r) { WebAsyncResult result = (WebAsyncResult)r; if (result.EndCalled) { int xx = result.NBytes; return((xx >= 0) ? xx : 0); } result.EndCalled = true; if (!result.IsCompleted) { int nbytes = -1; try { nbytes = cnc.EndRead(request, result); } catch (Exception exc) { lock (locker) { pendingReads--; if (pendingReads == 0) { pending.Set(); } } nextReadCalled = true; cnc.Close(true); result.SetCompleted(false, exc); result.DoCallback(); throw; } if (nbytes < 0) { nbytes = 0; read_eof = true; } totalRead += nbytes; result.SetCompleted(false, nbytes + result.NBytes); result.DoCallback(); if (nbytes == 0) { contentLength = totalRead; } } lock (locker) { pendingReads--; if (pendingReads == 0) { pending.Set(); } } if (totalRead >= contentLength && !nextReadCalled) { ReadAll(); } int nb = result.NBytes; return((nb >= 0) ? nb : 0); }
public override void EndWrite(IAsyncResult r) { if (r == null) { throw new ArgumentNullException("r"); } WebAsyncResult webAsyncResult = r as WebAsyncResult; if (webAsyncResult == null) { throw new ArgumentException("Invalid IAsyncResult"); } if (webAsyncResult.EndCalled) { return; } webAsyncResult.EndCalled = true; if (webAsyncResult.AsyncWriteAll) { webAsyncResult.WaitUntilComplete(); if (webAsyncResult.GotException) { throw webAsyncResult.Exception; } return; } else { if (this.allowBuffering && !this.sendChunked) { return; } if (webAsyncResult.GotException) { throw webAsyncResult.Exception; } try { this.cnc.EndWrite2(this.request, webAsyncResult.InnerAsyncResult); webAsyncResult.SetCompleted(false, 0); webAsyncResult.DoCallback(); } catch (Exception e) { webAsyncResult.SetCompleted(false, e); webAsyncResult.DoCallback(); throw; } finally { if (this.sendChunked) { object obj = this.locker; lock (obj) { this.pendingWrites--; if (this.pendingWrites == 0) { this.pending.Set(); } } } } return; } }
public override int EndRead(IAsyncResult r) { WebAsyncResult webAsyncResult = (WebAsyncResult)r; if (webAsyncResult.EndCalled) { int nbytes = webAsyncResult.NBytes; return((nbytes < 0) ? 0 : nbytes); } webAsyncResult.EndCalled = true; if (!webAsyncResult.IsCompleted) { int num = -1; try { num = this.cnc.EndRead(this.request, webAsyncResult); } catch (Exception e) { object obj = this.locker; lock (obj) { this.pendingReads--; if (this.pendingReads == 0) { this.pending.Set(); } } this.nextReadCalled = true; this.cnc.Close(true); webAsyncResult.SetCompleted(false, e); webAsyncResult.DoCallback(); throw; } if (num < 0) { num = 0; this.read_eof = true; } this.totalRead += num; webAsyncResult.SetCompleted(false, num + webAsyncResult.NBytes); webAsyncResult.DoCallback(); if (num == 0) { this.contentLength = this.totalRead; } } object obj2 = this.locker; lock (obj2) { this.pendingReads--; if (this.pendingReads == 0) { this.pending.Set(); } } if (this.totalRead >= this.contentLength && !this.nextReadCalled) { this.ReadAll(); } int nbytes2 = webAsyncResult.NBytes; return((nbytes2 < 0) ? 0 : nbytes2); }
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int size, AsyncCallback cb, object state) { if (request.Aborted) { throw new WebException("The request was canceled.", null, WebExceptionStatus.RequestCanceled); } if (isRead) { throw new NotSupportedException("this stream does not allow writing"); } if (buffer == null) { throw new ArgumentNullException("buffer"); } int num = buffer.Length; if (offset < 0 || num < offset) { throw new ArgumentOutOfRangeException("offset"); } if (size < 0 || num - offset < size) { throw new ArgumentOutOfRangeException("size"); } if (sendChunked) { lock (locker) { pendingWrites++; pending.Reset(); } } WebAsyncResult webAsyncResult = new WebAsyncResult(cb, state); if (!sendChunked) { CheckWriteOverflow(request.ContentLength, totalWritten, size); } if (allowBuffering && !sendChunked) { if (writeBuffer == null) { writeBuffer = new MemoryStream(); } writeBuffer.Write(buffer, offset, size); totalWritten += size; if (request.ContentLength > 0 && totalWritten == request.ContentLength) { try { webAsyncResult.AsyncWriteAll = true; webAsyncResult.InnerAsyncResult = WriteRequestAsync(WriteRequestAsyncCB, webAsyncResult); if (webAsyncResult.InnerAsyncResult != null) { return(webAsyncResult); } if (!webAsyncResult.IsCompleted) { webAsyncResult.SetCompleted(synch: true, 0); } webAsyncResult.DoCallback(); return(webAsyncResult); } catch (Exception e) { webAsyncResult.SetCompleted(synch: true, e); webAsyncResult.DoCallback(); return(webAsyncResult); } } webAsyncResult.SetCompleted(synch: true, 0); webAsyncResult.DoCallback(); return(webAsyncResult); } AsyncCallback cb2 = null; if (cb != null) { cb2 = WriteCallbackWrapper; } if (sendChunked) { WriteRequest(); string s = $"{size:X}\r\n"; byte[] bytes = Encoding.ASCII.GetBytes(s); int num2 = 2 + size + bytes.Length; byte[] array = new byte[num2]; Buffer.BlockCopy(bytes, 0, array, 0, bytes.Length); Buffer.BlockCopy(buffer, offset, array, bytes.Length, size); Buffer.BlockCopy(crlf, 0, array, bytes.Length + size, crlf.Length); buffer = array; offset = 0; size = num2; } webAsyncResult.InnerAsyncResult = cnc.BeginWrite(request, buffer, offset, size, cb2, webAsyncResult); totalWritten += size; return(webAsyncResult); }
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int size, AsyncCallback cb, object state) { if (this.request.Aborted) { throw new WebException("The request was canceled.", null, WebExceptionStatus.RequestCanceled); } if (this.isRead) { throw new NotSupportedException("this stream does not allow writing"); } if (buffer == null) { throw new ArgumentNullException("buffer"); } int num = buffer.Length; if (offset < 0 || num < offset) { throw new ArgumentOutOfRangeException("offset"); } if (size < 0 || num - offset < size) { throw new ArgumentOutOfRangeException("size"); } if (this.sendChunked) { object obj = this.locker; lock (obj) { this.pendingWrites++; this.pending.Reset(); } } WebAsyncResult webAsyncResult = new WebAsyncResult(cb, state); if (!this.sendChunked) { this.CheckWriteOverflow(this.request.ContentLength, this.totalWritten, (long)size); } if (this.allowBuffering && !this.sendChunked) { if (this.writeBuffer == null) { this.writeBuffer = new MemoryStream(); } this.writeBuffer.Write(buffer, offset, size); this.totalWritten += (long)size; if (this.request.ContentLength > 0L && this.totalWritten == this.request.ContentLength) { try { webAsyncResult.AsyncWriteAll = true; webAsyncResult.InnerAsyncResult = this.WriteRequestAsync(new AsyncCallback(this.WriteRequestAsyncCB), webAsyncResult); if (webAsyncResult.InnerAsyncResult == null) { if (!webAsyncResult.IsCompleted) { webAsyncResult.SetCompleted(true, 0); } webAsyncResult.DoCallback(); } } catch (Exception e) { webAsyncResult.SetCompleted(true, e); webAsyncResult.DoCallback(); } } else { webAsyncResult.SetCompleted(true, 0); webAsyncResult.DoCallback(); } return(webAsyncResult); } AsyncCallback cb2 = null; if (cb != null) { cb2 = new AsyncCallback(this.WriteCallbackWrapper); } if (this.sendChunked) { this.WriteRequest(); string s = string.Format("{0:X}\r\n", size); byte[] bytes = Encoding.ASCII.GetBytes(s); int num2 = 2 + size + bytes.Length; byte[] array = new byte[num2]; Buffer.BlockCopy(bytes, 0, array, 0, bytes.Length); Buffer.BlockCopy(buffer, offset, array, bytes.Length, size); Buffer.BlockCopy(WebConnectionStream.crlf, 0, array, bytes.Length + size, WebConnectionStream.crlf.Length); buffer = array; offset = 0; size = num2; } webAsyncResult.InnerAsyncResult = this.cnc.BeginWrite(this.request, buffer, offset, size, cb2, webAsyncResult); this.totalWritten += (long)size; return(webAsyncResult); }
public override int EndRead(IAsyncResult r) { WebAsyncResult webAsyncResult = (WebAsyncResult)r; if (webAsyncResult.EndCalled) { int nBytes = webAsyncResult.NBytes; return((nBytes >= 0) ? nBytes : 0); } webAsyncResult.EndCalled = true; if (!webAsyncResult.IsCompleted) { int num = -1; try { num = cnc.EndRead(request, webAsyncResult); } catch (Exception e) { lock (locker) { pendingReads--; if (pendingReads == 0) { pending.Set(); } } nextReadCalled = true; cnc.Close(sendNext: true); webAsyncResult.SetCompleted(synch: false, e); webAsyncResult.DoCallback(); throw; IL_00bb :; } if (num < 0) { num = 0; read_eof = true; } totalRead += num; webAsyncResult.SetCompleted(synch: false, num + webAsyncResult.NBytes); webAsyncResult.DoCallback(); if (num == 0) { contentLength = totalRead; } } lock (locker) { pendingReads--; if (pendingReads == 0) { pending.Set(); } } if (totalRead >= contentLength && !nextReadCalled) { ReadAll(); } int nBytes2 = webAsyncResult.NBytes; return((nBytes2 >= 0) ? nBytes2 : 0); }
public override IAsyncResult BeginRead(byte[] buffer, int offset, int size, AsyncCallback cb, object state) { if (!isRead) { throw new NotSupportedException("this stream does not allow reading"); } if (buffer == null) { throw new ArgumentNullException("buffer"); } int num = buffer.Length; if (offset < 0 || num < offset) { throw new ArgumentOutOfRangeException("offset"); } if (size < 0 || num - offset < size) { throw new ArgumentOutOfRangeException("size"); } lock (locker) { pendingReads++; pending.Reset(); } WebAsyncResult webAsyncResult = new WebAsyncResult(cb, state, buffer, offset, size); if (totalRead >= contentLength) { webAsyncResult.SetCompleted(synch: true, -1); webAsyncResult.DoCallback(); return(webAsyncResult); } int num2 = readBufferSize - readBufferOffset; if (num2 > 0) { int num3 = (num2 <= size) ? num2 : size; Buffer.BlockCopy(readBuffer, readBufferOffset, buffer, offset, num3); readBufferOffset += num3; offset += num3; size -= num3; totalRead += num3; if (size == 0 || totalRead >= contentLength) { webAsyncResult.SetCompleted(synch: true, num3); webAsyncResult.DoCallback(); return(webAsyncResult); } webAsyncResult.NBytes = num3; } if (cb != null) { cb = ReadCallbackWrapper; } if (contentLength != int.MaxValue && contentLength - totalRead < size) { size = contentLength - totalRead; } if (!read_eof) { webAsyncResult.InnerAsyncResult = cnc.BeginRead(request, buffer, offset, size, cb, webAsyncResult); } else { webAsyncResult.SetCompleted(synch: true, webAsyncResult.NBytes); webAsyncResult.DoCallback(); } return(webAsyncResult); }
internal void SetHeadersAsync (byte[] buffer, WebAsyncResult result) { if (headersSent) return; headers = buffer; long cl = request.ContentLength; string method = request.Method; bool no_writestream = (method == "GET" || method == "CONNECT" || method == "HEAD" || method == "TRACE"); bool webdav = (method == "PROPFIND" || method == "PROPPATCH" || method == "MKCOL" || method == "COPY" || method == "MOVE" || method == "LOCK" || method == "UNLOCK"); if (sendChunked || cl > -1 || no_writestream || webdav) { headersSent = true; try { result.InnerAsyncResult = cnc.BeginWrite (request, headers, 0, headers.Length, new AsyncCallback(SetHeadersCB), result); if (result.InnerAsyncResult == null) { // when does BeginWrite return null? Is the case when the request is aborted? if (!result.IsCompleted) result.SetCompleted (true, 0); result.DoCallback (); } } catch (Exception exc) { result.SetCompleted (true, exc); result.DoCallback (); } } else { result.SetCompleted (true, 0); result.DoCallback (); } }
internal void SetResponseData(WebConnectionData data) { if (aborted) { if (data.stream != null) { data.stream.Close(); } return; } WebException wexc = null; try { webResponse = new HttpWebResponse(actualUri, method, data, cookieContainer); haveResponse = true; } 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")) { lock (locker) { CheckSendError(data); if (saved_exc != null) { wexc = (WebException)saved_exc; } } } WebAsyncResult r = asyncRead; if (r != null) { if (wexc != null) { r.SetCompleted(false, wexc); r.DoCallback(); return; } bool redirected; try { redirected = CheckFinalStatus(r); if (!redirected) { r.SetCompleted(false, webResponse); r.DoCallback(); } else { if (webResponse != null) { webResponse.Close(); webResponse = null; } haveResponse = false; webResponse = null; r.Reset(); servicePoint = GetServicePoint(); abortHandler = servicePoint.SendRequest(this, connectionGroup); } } catch (WebException wexc2) { r.SetCompleted(false, wexc2); r.DoCallback(); return; } catch (Exception ex) { wexc = new WebException(ex.Message, ex, WebExceptionStatus.ProtocolError, null); r.SetCompleted(false, wexc); r.DoCallback(); return; } } }
public override IAsyncResult BeginRead (byte [] buffer, int offset, int size, AsyncCallback cb, object state) { if (!isRead) throw new NotSupportedException ("this stream does not allow reading"); if (buffer == null) throw new ArgumentNullException ("buffer"); int length = buffer.Length; if (offset < 0 || length < offset) throw new ArgumentOutOfRangeException ("offset"); if (size < 0 || (length - offset) < size) throw new ArgumentOutOfRangeException ("size"); lock (locker) { pendingReads++; pending.Reset (); } WebAsyncResult result = new WebAsyncResult (cb, state, buffer, offset, size); if (totalRead >= contentLength) { result.SetCompleted (true, -1); result.DoCallback (); return result; } int remaining = readBufferSize - readBufferOffset; if (remaining > 0) { int copy = (remaining > size) ? size : remaining; Buffer.BlockCopy (readBuffer, readBufferOffset, buffer, offset, copy); readBufferOffset += copy; offset += copy; size -= copy; totalRead += copy; if (size == 0 || totalRead >= contentLength) { result.SetCompleted (true, copy); result.DoCallback (); return result; } result.NBytes = copy; } if (cb != null) cb = cb_wrapper; if (contentLength != Int64.MaxValue && contentLength - totalRead < size) size = (int)(contentLength - totalRead); if (!read_eof) { result.InnerAsyncResult = cnc.BeginRead (request, buffer, offset, size, cb, result); } else { result.SetCompleted (true, result.NBytes); result.DoCallback (); } return result; }
public override IAsyncResult BeginRead(byte [] buffer, int offset, int size, AsyncCallback cb, object state) { if (!isRead) { throw new NotSupportedException("this stream does not allow reading"); } if (buffer == null) { throw new ArgumentNullException("buffer"); } int length = buffer.Length; if (offset < 0 || length < offset) { throw new ArgumentOutOfRangeException("offset"); } if (size < 0 || (length - offset) < size) { throw new ArgumentOutOfRangeException("size"); } lock (locker) { pendingReads++; pending.Reset(); } WebAsyncResult result = new WebAsyncResult(cb, state, buffer, offset, size); if (totalRead >= contentLength) { result.SetCompleted(true, -1); result.DoCallback(); return(result); } int remaining = readBufferSize - readBufferOffset; if (remaining > 0) { int copy = (remaining > size) ? size : remaining; Buffer.BlockCopy(readBuffer, readBufferOffset, buffer, offset, copy); readBufferOffset += copy; offset += copy; size -= copy; totalRead += copy; if (size == 0 || totalRead >= contentLength) { result.SetCompleted(true, copy); result.DoCallback(); return(result); } result.NBytes = copy; } if (cb != null) { cb = cb_wrapper; } if (contentLength != Int32.MaxValue && contentLength - totalRead < size) { size = contentLength - totalRead; } if (!read_eof) { result.InnerAsyncResult = cnc.BeginRead(request, buffer, offset, size, cb, result); } else { result.SetCompleted(true, result.NBytes); result.DoCallback(); } return(result); }
public override IAsyncResult BeginWrite (byte [] buffer, int offset, int size, AsyncCallback cb, object state) { if (request.Aborted) throw new WebException ("The request was canceled.", WebExceptionStatus.RequestCanceled); if (isRead) throw new NotSupportedException ("this stream does not allow writing"); if (buffer == null) throw new ArgumentNullException ("buffer"); int length = buffer.Length; if (offset < 0 || length < offset) throw new ArgumentOutOfRangeException ("offset"); if (size < 0 || (length - offset) < size) throw new ArgumentOutOfRangeException ("size"); if (sendChunked) { lock (locker) { pendingWrites++; pending.Reset (); } } WebAsyncResult result = new WebAsyncResult (cb, state); AsyncCallback callback = new AsyncCallback (WriteAsyncCB); if (sendChunked) { requestWritten = true; string cSize = String.Format ("{0:X}\r\n", size); byte[] head = Encoding.ASCII.GetBytes (cSize); int chunkSize = 2 + size + head.Length; byte[] newBuffer = new byte [chunkSize]; Buffer.BlockCopy (head, 0, newBuffer, 0, head.Length); Buffer.BlockCopy (buffer, offset, newBuffer, head.Length, size); Buffer.BlockCopy (crlf, 0, newBuffer, head.Length + size, crlf.Length); if (allowBuffering) { if (writeBuffer == null) writeBuffer = new MemoryStream (); writeBuffer.Write (buffer, offset, size); totalWritten += size; } buffer = newBuffer; offset = 0; size = chunkSize; } else { CheckWriteOverflow (request.ContentLength, totalWritten, size); if (allowBuffering) { if (writeBuffer == null) writeBuffer = new MemoryStream (); writeBuffer.Write (buffer, offset, size); totalWritten += size; if (request.ContentLength <= 0 || totalWritten < request.ContentLength) { result.SetCompleted (true, 0); result.DoCallback (); return result; } result.AsyncWriteAll = true; requestWritten = true; buffer = writeBuffer.GetBuffer (); offset = 0; size = (int)totalWritten; } } try { result.InnerAsyncResult = cnc.BeginWrite (request, buffer, offset, size, callback, result); if (result.InnerAsyncResult == null) { if (!result.IsCompleted) result.SetCompleted (true, 0); result.DoCallback (); } } catch (Exception) { if (!IgnoreIOErrors) throw; result.SetCompleted (true, 0); result.DoCallback (); } totalWritten += size; return result; }
public override IAsyncResult BeginWrite(byte [] buffer, int offset, int size, AsyncCallback cb, object state) { if (request.Aborted) { throw new WebException("The request was canceled.", null, WebExceptionStatus.RequestCanceled); } if (isRead) { throw new NotSupportedException("this stream does not allow writing"); } if (buffer == null) { throw new ArgumentNullException("buffer"); } int length = buffer.Length; if (offset < 0 || length < offset) { throw new ArgumentOutOfRangeException("offset"); } if (size < 0 || (length - offset) < size) { throw new ArgumentOutOfRangeException("size"); } if (sendChunked) { lock (locker) { pendingWrites++; pending.Reset(); } } WebAsyncResult result = new WebAsyncResult(cb, state); AsyncCallback callback = new AsyncCallback(WriteRequestAsyncCB); if (sendChunked) { requestWritten = true; string cSize = String.Format("{0:X}\r\n", size); byte[] head = Encoding.ASCII.GetBytes(cSize); int chunkSize = 2 + size + head.Length; byte[] newBuffer = new byte [chunkSize]; Buffer.BlockCopy(head, 0, newBuffer, 0, head.Length); Buffer.BlockCopy(buffer, offset, newBuffer, head.Length, size); Buffer.BlockCopy(crlf, 0, newBuffer, head.Length + size, crlf.Length); buffer = newBuffer; offset = 0; size = chunkSize; } else { CheckWriteOverflow(request.ContentLength, totalWritten, size); if (allowBuffering) { if (writeBuffer == null) { writeBuffer = new MemoryStream(); } writeBuffer.Write(buffer, offset, size); totalWritten += size; if (request.ContentLength <= 0 || totalWritten < request.ContentLength) { result.SetCompleted(true, 0); result.DoCallback(); return(result); } result.AsyncWriteAll = true; requestWritten = true; buffer = writeBuffer.GetBuffer(); offset = 0; size = (int)totalWritten; } } try { result.InnerAsyncResult = cnc.BeginWrite(request, buffer, offset, size, callback, result); if (result.InnerAsyncResult == null) { if (!result.IsCompleted) { result.SetCompleted(true, 0); } result.DoCallback(); } } catch (Exception) { if (!IgnoreIOErrors) { throw; } result.SetCompleted(true, 0); result.DoCallback(); } totalWritten += size; return(result); }
public override IAsyncResult BeginWrite(byte [] buffer, int offset, int size, AsyncCallback cb, object state) { if (isRead) { throw new NotSupportedException("this stream does not allow writing"); } if (buffer == null) { throw new ArgumentNullException("buffer"); } int length = buffer.Length; if (size < 0 || offset < 0 || length < offset || length - offset < size) { throw new ArgumentOutOfRangeException(); } if (sendChunked) { lock (locker) { pendingWrites++; pending.Reset(); } } WebAsyncResult result = new WebAsyncResult(cb, state); if (allowBuffering) { if (max_buffer_size >= 0) { long avail = max_buffer_size - writeBuffer.Length; if (size > avail) { if (requestWritten) { throw new ProtocolViolationException( "The number of bytes to be written is greater than " + "the specified ContentLength."); } } } writeBuffer.Write(buffer, offset, size); if (!sendChunked) { result.SetCompleted(true, 0); result.DoCallback(); return(result); } } AsyncCallback callback = null; if (cb != null) { callback = new AsyncCallback(WriteCallbackWrapper); } if (sendChunked) { WriteRequest(); string cSize = String.Format("{0:X}\r\n", size); byte [] head = Encoding.ASCII.GetBytes(cSize); int chunkSize = 2 + size + head.Length; byte [] newBuffer = new byte [chunkSize]; Buffer.BlockCopy(head, 0, newBuffer, 0, head.Length); Buffer.BlockCopy(buffer, offset, newBuffer, head.Length, size); Buffer.BlockCopy(crlf, 0, newBuffer, head.Length + size, crlf.Length); buffer = newBuffer; offset = 0; size = chunkSize; } result.InnerAsyncResult = cnc.BeginWrite(buffer, offset, size, callback, result); return(result); }