We need you! We're working hard on the next version of Developer Fusion -
Let us know what you think we should be up to!
- namespace System.Net
- {
-
- using System.Collections;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading;
- using System.Security;
- using System.Globalization;
- using System.Net.Configuration;
-
- internal enum ReadState
- {
- Start,
- StatusLine,
-
- Headers,
-
- Data
-
- }
-
- internal enum DataParseStatus
- {
- NeedMoreData = 0,
-
- ContinueParsing,
-
- Done,
-
- Invalid,
-
- DataTooBig
-
- }
-
- internal enum WriteBufferState
- {
- Disabled,
- Headers,
- Buffer,
- Playback
- }
-
-
- internal enum WebParseErrorSection
- {
- Generic,
- ResponseHeader,
- ResponseStatusLine,
- ResponseBody
- }
-
-
- internal enum WebParseErrorCode
- {
- Generic,
- InvalidHeaderName,
- InvalidContentLength,
- IncompleteHeaderLine,
- CrLfError,
- InvalidChunkFormat,
- UnexpectedServerResponse
- }
-
-
- struct WebParseError
- {
- public WebParseErrorSection Section;
- public WebParseErrorCode Code;
- }
-
-
- struct TunnelStateObject
- {
- internal TunnelStateObject(HttpWebRequest r, Connection c)
- {
- Connection = c;
- OriginalRequest = r;
- }
-
- internal Connection Connection;
- internal HttpWebRequest OriginalRequest;
- }
-
-
- internal struct BufferChunkBytes : IReadChunkBytes
- {
-
- public byte[] Buffer;
- public int Offset;
- public int Count;
-
- public int NextByte {
- get {
- if (Count != 0) {
- Count--;
- return (int)Buffer[Offset++];
- }
- return -1;
- }
- set {
- Count++;
- Offset--;
- Buffer[Offset] = (byte)value;
- }
- }
- }
-
-
-
-
-
-
- internal class ConnectionReturnResult
- {
-
- private static readonly WaitCallback s_InvokeConnectionCallback = new WaitCallback(InvokeConnectionCallback);
-
- private struct RequestContext
- {
- internal HttpWebRequest Request;
- internal object CoreResponse;
-
- internal RequestContext(HttpWebRequest request, object coreResponse)
- {
- Request = request;
- CoreResponse = coreResponse;
- }
- }
-
- private List<RequestContext> m_Context;
-
- internal ConnectionReturnResult()
- {
- m_Context = new List<RequestContext>(5);
- }
-
- internal ConnectionReturnResult(int capacity)
- {
- m_Context = new List<RequestContext>(capacity);
- }
-
- internal bool IsNotEmpty {
- get { return m_Context.Count != 0; }
- }
-
- static internal void Add(ref ConnectionReturnResult returnResult, HttpWebRequest request, CoreResponseData coreResponseData)
- {
- if (coreResponseData == null)
- throw new InternalException();
-
- if (returnResult == null) {
- returnResult = new ConnectionReturnResult();
- }
-
- #if DEBUG
-
- for (int j = 0; j < returnResult.m_Context.Count; ++j)
- if ((object)returnResult.m_Context[j].Request == (object)request)
- throw new InternalException();
- #endif
-
- returnResult.m_Context.Add(new RequestContext(request, coreResponseData));
- }
-
- static internal void AddExceptionRange(ref ConnectionReturnResult returnResult, HttpWebRequest[] requests, Exception exception)
- {
- AddExceptionRange(ref returnResult, requests, exception, exception);
- }
- static internal void AddExceptionRange(ref ConnectionReturnResult returnResult, HttpWebRequest[] requests, Exception exception, Exception firstRequestException)
- {
-
-
- if (exception == null)
- throw new InternalException();
-
- if (returnResult == null) {
- returnResult = new ConnectionReturnResult(requests.Length);
- }
-
-
- for (int i = 0; i < requests.Length; ++i) {
- #if DEBUG
-
- for (int j = 0; j < returnResult.m_Context.Count; ++j)
- if ((object)returnResult.m_Context[j].Request == (object)requests[i])
- throw new InternalException();
- #endif
-
- if (i == 0)
- returnResult.m_Context.Add(new RequestContext(requests[i], firstRequestException));
- else
- returnResult.m_Context.Add(new RequestContext(requests[i], exception));
- }
- }
-
- static internal void SetResponses(ConnectionReturnResult returnResult)
- {
- if (returnResult == null) {
- return;
- }
-
- GlobalLog.Print("ConnectionReturnResult#" + ValidationHelper.HashString(returnResult) + "::SetResponses() count=" + returnResult.m_Context.Count.ToString());
- for (int i = 0; i < returnResult.m_Context.Count; i++) {
- try {
- HttpWebRequest request = returnResult.m_Context[i].Request;
- #if DEBUG
- CoreResponseData coreResponseData = returnResult.m_Context[i].CoreResponse as CoreResponseData;
- if (coreResponseData == null)
- GlobalLog.DebugRemoveRequest(request);
- #endif
- request.SetAndOrProcessResponse(returnResult.m_Context[i].CoreResponse);
- }
- catch (Exception e) {
-
-
-
- GlobalLog.Print("ConnectionReturnResult#" + ValidationHelper.HashString(returnResult) + "::Exception" + e);
- returnResult.m_Context.RemoveRange(0, (i + 1));
- if (returnResult.m_Context.Count > 0) {
- ThreadPool.UnsafeQueueUserWorkItem(s_InvokeConnectionCallback, returnResult);
- }
- throw;
- }
- }
-
- returnResult.m_Context.Clear();
- }
-
- private static void InvokeConnectionCallback(object objectReturnResult)
- {
- ConnectionReturnResult returnResult = (ConnectionReturnResult)objectReturnResult;
- SetResponses(returnResult);
- }
- }
-
-
-
-
-
- internal class Connection : PooledStream
- {
-
-
-
-
-
-
-
-
- [ThreadStatic()]
- private static int t_SyncReadNesting;
-
- private const int CRLFSize = 2;
- private const long c_InvalidContentLength = -2l;
-
-
-
- private class StatusLineValues
- {
- internal int MajorVersion;
- internal int MinorVersion;
- internal int StatusCode;
- internal string StatusDescription;
- }
-
-
-
-
- private WebExceptionStatus m_Error;
- internal Exception m_InnerException;
-
-
- internal int m_IISVersion = -1;
-
- private byte[] m_ReadBuffer;
- private int m_BytesRead;
- private int m_BytesScanned;
- private int m_TotalResponseHeadersLength;
- private int m_MaximumResponseHeadersLength;
- private long m_MaximumUnauthorizedUploadLength;
- private CoreResponseData m_ResponseData;
- private ReadState m_ReadState;
- private StatusLineValues m_StatusLineValues;
- private int m_StatusState;
- private ArrayList m_WaitList;
- private ArrayList m_WriteList;
- private IAsyncResult m_LastAsyncResult;
- private TimerThread.Timer m_RecycleTimer;
- private WebParseError m_ParseError;
- private bool m_AtLeastOneResponseReceived;
-
- private static readonly WaitCallback m_PostReceiveDelegate = new WaitCallback(PostReceiveWrapper);
- private static readonly AsyncCallback m_ReadCallback = new AsyncCallback(ReadCallbackWrapper);
- private static readonly AsyncCallback m_TunnelCallback = new AsyncCallback(TunnelThroughProxyWrapper);
- private static byte[] s_NullBuffer = new byte[0];
-
-
-
-
-
-
-
-
- private HttpAbortDelegate m_AbortDelegate;
- private ConnectionGroup m_ConnectionGroup;
-
- private UnlockConnectionDelegate m_ConnectionUnlock;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- private DateTime m_IdleSinceUtc;
- private HttpWebRequest m_LockedRequest;
- private HttpWebRequest m_CurrentRequest;
-
- private bool m_CanPipeline;
- private bool m_Free = true;
- private bool m_Idle = true;
- private bool m_KeepAlive = true;
- private bool m_Pipelining;
- private bool m_ReadDone;
- private bool m_WriteDone;
- private bool m_RemovedFromConnectionList;
-
-
- private bool m_IsPipelinePaused;
- private static int s_MaxPipelinedCount = 10;
- private static int s_MinPipelinedCount = 5;
-
-
- internal override ServicePoint ServicePoint {
- get { return ConnectionGroup.ServicePoint; }
- }
-
- private ConnectionGroup ConnectionGroup {
- get { return m_ConnectionGroup; }
- }
-
-
-
-
-
-
- internal HttpWebRequest LockedRequest {
- get { return m_LockedRequest; }
- set {
- HttpWebRequest myLock = m_LockedRequest;
-
- GlobalLog.Print("Connection#" + ValidationHelper.HashString(this) + "::LockedRequest_set() old#" + ((myLock != null) ? myLock.GetHashCode().ToString() : "null") + " new#" + ((value != null) ? value.GetHashCode().ToString() : "null"));
-
- if ((object)value == (object)myLock) {
- if (value != null && (object)value.UnlockConnectionDelegate != (object)m_ConnectionUnlock) {
- throw new InternalException();
- }
- return;
- }
-
- object myDelegate = myLock == null ? null : myLock.UnlockConnectionDelegate;
- if (myDelegate != null && (value != null || (object)m_ConnectionUnlock != (object)myDelegate))
- throw new InternalException();
-
- if (value == null) {
- m_LockedRequest = null;
- myLock.UnlockConnectionDelegate = null;
- return;
- }
-
- UnlockConnectionDelegate chkDelegate = value.UnlockConnectionDelegate;
-
-
-
- if ((object)chkDelegate != null) {
- if ((object)chkDelegate == (object)m_ConnectionUnlock)
- throw new InternalException();
-
- GlobalLog.Print("Connection#" + ValidationHelper.HashString(this) + "::LockedRequest_set() Unlocking old request Connection");
- chkDelegate();
- }
-
- value.UnlockConnectionDelegate = m_ConnectionUnlock;
- m_LockedRequest = value;
- }
- }
-
-
- /// <devdoc>
- /// <para>
- /// Delegate called when the request is finished using this Connection
- /// exclusively. Called in Abort cases and after NTLM authenticaiton completes.
- /// </para>
- /// </devdoc>
- private void UnlockRequest()
- {
- GlobalLog.Print("Connection#" + ValidationHelper.HashString(this) + "::UnlockRequest() LockedRequest#" + ValidationHelper.HashString(LockedRequest));
- LockedRequest = null;
-
- if (ConnectionGroup != null) {
- GlobalLog.Print("Connection#" + ValidationHelper.HashString(this) + "::UnlockRequest() - forcing call to ConnectionGoneIdle()");
- ConnectionGroup.ConnectionGoneIdle();
- }
- }
-
-
-
- internal Connection(ConnectionGroup connectionGroup) : base(null)
- {
-
-
-
-
- m_MaximumUnauthorizedUploadLength = SettingsSectionInternal.Section.MaximumUnauthorizedUploadLength;
- if (m_MaximumUnauthorizedUploadLength > 0) {
- m_MaximumUnauthorizedUploadLength *= 1024;
- }
- m_ResponseData = new CoreResponseData();
- m_ConnectionGroup = connectionGroup;
- m_ReadBuffer = new byte[4096];
-
- m_ReadState = ReadState.Start;
- m_WaitList = new ArrayList();
- m_WriteList = new ArrayList();
- m_AbortDelegate = new HttpAbortDelegate(AbortOrDisassociate);
- m_ConnectionUnlock = new UnlockConnectionDelegate(UnlockRequest);
-
-
- m_StatusLineValues = new StatusLineValues();
- m_RecycleTimer = ConnectionGroup.ServicePoint.ConnectionLeaseTimerQueue.CreateTimer();
-
- ConnectionGroup.Associate(this);
- m_ReadDone = true;
- m_WriteDone = true;
- m_Error = WebExceptionStatus.Success;
- }
-
- ~Connection()
- {
- #if DEBUG
- GlobalLog.SetThreadSource(ThreadKinds.Finalization);
- using (GlobalLog.SetThreadKind(ThreadKinds.System | ThreadKinds.Async)) {
- #endif
-
- try {
- AbortSocket(true);
- }
- catch (SocketException) {
- }
- catch (ObjectDisposedException) {
- }
-
- #if DEBUG
- }
- #endif
- }
-
- internal int BusyCount {
- get { return (m_ReadDone ? 0 : 1) + 2 * (m_WaitList.Count + m_WriteList.Count); }
- }
-
- internal int IISVersion {
- get { return m_IISVersion; }
- }
-
- internal bool AtLeastOneResponseReceived {
- get { return m_AtLeastOneResponseReceived; }
- }
-
-
-
- internal bool SubmitRequest(HttpWebRequest request)
- {
- GlobalLog.Enter("Connection#" + ValidationHelper.HashString(this) + "::SubmitRequest", "request#" + ValidationHelper.HashString(request));
- GlobalLog.ThreadContract(ThreadKinds.Unknown, "Connection#" + ValidationHelper.HashString(thi