public override int Read(byte [] buffer, int offset, int size) { if (!isRead) { throw new NotSupportedException("this stream does not allow reading"); } if (totalRead >= contentLength) { return(0); } AsyncCallback cb = new AsyncCallback(ReadCallbackWrapper); WebAsyncResult res = (WebAsyncResult)BeginRead(buffer, offset, size, cb, null); if (!res.IsCompleted && !res.WaitUntilComplete(request.ReadWriteTimeout, false)) { nextReadCalled = true; cnc.Close(true); throw new WebException("The operation has timed out.", WebExceptionStatus.Timeout); } return(EndRead(res)); }
static void PrepareSharingNtlm(WebConnection cnc, HttpWebRequest request) { if (!cnc.NtlmAuthenticated) { return; } bool needs_reset = false; NetworkCredential cnc_cred = cnc.NtlmCredential; NetworkCredential req_cred = request.Credentials.GetCredential(request.RequestUri, "NTLM"); if (cnc_cred.Domain != req_cred.Domain || cnc_cred.UserName != req_cred.UserName || cnc_cred.Password != req_cred.Password) { needs_reset = true; } #if NET_1_1 if (!needs_reset) { bool req_sharing = request.UnsafeAuthenticatedConnectionSharing; bool cnc_sharing = cnc.UnsafeAuthenticatedConnectionSharing; needs_reset = (req_sharing == false || req_sharing != cnc_sharing); } #endif if (needs_reset) { cnc.Close(false); // closes the authenticated connection cnc.ResetNtlm(); } }
private static void PrepareSharingNtlm(WebConnection cnc, HttpWebRequest request) { if (!cnc.NtlmAuthenticated) { return; } bool flag = false; NetworkCredential ntlmCredential = cnc.NtlmCredential; NetworkCredential credential = request.Credentials.GetCredential(request.RequestUri, "NTLM"); if (ntlmCredential.Domain != credential.Domain || ntlmCredential.UserName != credential.UserName || ntlmCredential.Password != credential.Password) { flag = true; } if (!flag) { bool unsafeAuthenticatedConnectionSharing = request.UnsafeAuthenticatedConnectionSharing; bool unsafeAuthenticatedConnectionSharing2 = cnc.UnsafeAuthenticatedConnectionSharing; flag = (!unsafeAuthenticatedConnectionSharing || unsafeAuthenticatedConnectionSharing != unsafeAuthenticatedConnectionSharing2); } if (flag) { cnc.Close(false); cnc.ResetNtlm(); } }
static void PrepareSharingNtlm(WebConnection cnc, HttpWebRequest request) { if (!cnc.NtlmAuthenticated) { return; } bool needs_reset = false; NetworkCredential cnc_cred = cnc.NtlmCredential; bool isProxy = (request.Proxy != null && !request.Proxy.IsBypassed(request.RequestUri)); ICredentials req_icreds = (!isProxy) ? request.Credentials : request.Proxy.Credentials; NetworkCredential req_cred = (req_icreds != null) ? req_icreds.GetCredential(request.RequestUri, "NTLM") : null; if (cnc_cred == null || req_cred == null || cnc_cred.Domain != req_cred.Domain || cnc_cred.UserName != req_cred.UserName || cnc_cred.Password != req_cred.Password) { needs_reset = true; } if (!needs_reset) { bool req_sharing = request.UnsafeAuthenticatedConnectionSharing; bool cnc_sharing = cnc.UnsafeAuthenticatedConnectionSharing; needs_reset = (req_sharing == false || req_sharing != cnc_sharing); } if (needs_reset) { cnc.Close(false); // closes the authenticated connection cnc.ResetNtlm(); } }
public override int Read(byte[] buffer, int offset, int size) { AsyncCallback cb = ReadCallbackWrapper; WebAsyncResult webAsyncResult = (WebAsyncResult)BeginRead(buffer, offset, size, cb, null); if (!webAsyncResult.IsCompleted && !webAsyncResult.WaitUntilComplete(ReadTimeout, exitContext: false)) { nextReadCalled = true; cnc.Close(sendNext: true); throw new WebException("The operation has timed out.", WebExceptionStatus.Timeout); } return(EndRead(webAsyncResult)); }
public override int Read(byte [] buffer, int offset, int size) { AsyncCallback cb = cb_wrapper; WebAsyncResult res = (WebAsyncResult)BeginRead(buffer, offset, size, cb, null); if (!res.IsCompleted && !res.WaitUntilComplete(ReadTimeout, false)) { nextReadCalled = true; cnc.Close(true); throw new WebException("The operation has timed out.", WebExceptionStatus.Timeout); } return(EndRead(res)); }
public void Close() { ArrayList obj = this.connections; lock (obj) { int count = this.connections.Count; for (int i = 0; i < count; i++) { WeakReference weakReference = (WeakReference)this.connections[i]; WebConnection webConnection = weakReference.Target as WebConnection; if (webConnection != null) { webConnection.Close(false); } } this.connections.Clear(); } }
public void Close() { //TODO: what do we do with the queue? Empty it out and abort the requests? //TODO: abort requests or wait for them to finish lock (connections) { WeakReference cncRef = null; int end = connections.Count; ArrayList removed = null; for (int i = 0; i < end; i++) { cncRef = (WeakReference)connections [i]; WebConnection cnc = cncRef.Target as WebConnection; if (cnc != null) { cnc.Close(false); } } connections.Clear(); } }
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); }
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); }
static void PrepareSharingNtlm (WebConnection cnc, HttpWebRequest request) { if (!cnc.NtlmAuthenticated) return; bool needs_reset = false; NetworkCredential cnc_cred = cnc.NtlmCredential; bool isProxy = (request.Proxy != null && !request.Proxy.IsBypassed (request.RequestUri)); ICredentials req_icreds = (!isProxy) ? request.Credentials : request.Proxy.Credentials; NetworkCredential req_cred = (req_icreds != null) ? req_icreds.GetCredential (request.RequestUri, "NTLM") : null; if (cnc_cred == null || req_cred == null || cnc_cred.Domain != req_cred.Domain || cnc_cred.UserName != req_cred.UserName || cnc_cred.Password != req_cred.Password) { needs_reset = true; } if (!needs_reset) { bool req_sharing = request.UnsafeAuthenticatedConnectionSharing; bool cnc_sharing = cnc.UnsafeAuthenticatedConnectionSharing; needs_reset = (req_sharing == false || req_sharing != cnc_sharing); } if (needs_reset) { cnc.Close (false); // closes the authenticated connection cnc.ResetNtlm (); } }
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); }
static void PrepareSharingNtlm (WebConnection cnc, HttpWebRequest request) { if (!cnc.NtlmAuthenticated) return; bool needs_reset = false; NetworkCredential cnc_cred = cnc.NtlmCredential; NetworkCredential req_cred = request.Credentials.GetCredential (request.RequestUri, "NTLM"); if (cnc_cred == null || req_cred == null || cnc_cred.Domain != req_cred.Domain || cnc_cred.UserName != req_cred.UserName || cnc_cred.Password != req_cred.Password) { needs_reset = true; } #if NET_1_1 if (!needs_reset) { bool req_sharing = request.UnsafeAuthenticatedConnectionSharing; bool cnc_sharing = cnc.UnsafeAuthenticatedConnectionSharing; needs_reset = (req_sharing == false || req_sharing != cnc_sharing); } #endif if (needs_reset) { cnc.Close (false); // closes the authenticated connection cnc.ResetNtlm (); } }