private static void IOCompleted(HttpRequestStreamAsyncResult asyncResult, uint errorCode, uint numBytes)
            {
                if (NetEventSource.IsEnabled)
                {
                    NetEventSource.Info(null, $"asyncResult: {asyncResult} errorCode:0x {errorCode.ToString("x8")} numBytes: {numBytes}");
                }
                object result = null;

                try
                {
                    if (errorCode != Interop.HttpApi.ERROR_SUCCESS && errorCode != Interop.HttpApi.ERROR_HANDLE_EOF)
                    {
                        asyncResult.ErrorCode = (int)errorCode;
                        result = new HttpListenerException((int)errorCode);
                    }
                    else
                    {
                        result = numBytes;
                        if (NetEventSource.IsEnabled)
                        {
                            NetEventSource.DumpBuffer(asyncResult, (IntPtr)asyncResult._pPinnedBuffer, (int)numBytes);
                        }
                    }
                    if (NetEventSource.IsEnabled)
                    {
                        NetEventSource.Info(null, $"asyncResult: {asyncResult} calling Complete()");
                    }
                }
                catch (Exception e)
                {
                    result = e;
                }
                asyncResult.InvokeCallback(result);
            }
Example #2
0
            private static void IOCompleted(HttpRequestStreamAsyncResult asyncResult, uint errorCode, uint numBytes)
            {
                GlobalLog.Print("HttpRequestStreamAsyncResult#" + ValidationHelper.HashString(asyncResult) + "::Callback() errorCode:0x" + errorCode.ToString("x8") + " numBytes:" + numBytes);
                object result = null;

                try {
                    if (errorCode != UnsafeNclNativeMethods.ErrorCodes.ERROR_SUCCESS && errorCode != UnsafeNclNativeMethods.ErrorCodes.ERROR_HANDLE_EOF)
                    {
                        asyncResult.ErrorCode = (int)errorCode;
                        result = new HttpListenerException((int)errorCode);
                    }
                    else
                    {
                        result = numBytes;
                        if (Logging.On)
                        {
                            Logging.Dump(Logging.HttpListener, asyncResult, "Callback", (IntPtr)asyncResult.m_pPinnedBuffer, (int)numBytes);
                        }
                    }
                    GlobalLog.Print("HttpRequestStreamAsyncResult#" + ValidationHelper.HashString(asyncResult) + "::Callback() calling Complete()");
                }
                catch (Exception e) {
                    result = e;
                }
                asyncResult.InvokeCallback(result);
            }
Example #3
0
            private static unsafe void Callback(uint errorCode, uint numBytes, NativeOverlapped *nativeOverlapped)
            {
                Overlapped callbackOverlapped            = Overlapped.Unpack(nativeOverlapped);
                HttpRequestStreamAsyncResult asyncResult = callbackOverlapped.AsyncResult as HttpRequestStreamAsyncResult;

                GlobalLog.Print("HttpRequestStreamAsyncResult#" + ValidationHelper.HashString(asyncResult) + "::Callback() errorCode:0x" + errorCode.ToString("x8") + " numBytes:" + numBytes + " nativeOverlapped:0x" + ((IntPtr)nativeOverlapped).ToString("x8"));

                IOCompleted(asyncResult, errorCode, numBytes);
            }
            private static unsafe void Callback(uint errorCode, uint numBytes, NativeOverlapped *nativeOverlapped)
            {
                HttpRequestStreamAsyncResult asyncResult = (HttpRequestStreamAsyncResult)ThreadPoolBoundHandle.GetNativeOverlappedState(nativeOverlapped);

                if (NetEventSource.IsEnabled)
                {
                    NetEventSource.Info(null, $"asyncResult: {asyncResult} errorCode:0x {errorCode.ToString("x8")} numBytes: {numBytes} nativeOverlapped:0x {((IntPtr)nativeOverlapped).ToString("x8")}");
                }

                IOCompleted(asyncResult, errorCode, numBytes);
            }
Example #5
0
        public override int EndRead(IAsyncResult asyncResult)
        {
            if (Logging.On)
            {
                Logging.Enter(Logging.HttpListener, this, "EndRead", "");
            }
            GlobalLog.Print("HttpRequestStream#" + ValidationHelper.HashString(this) + "::EndRead() asyncResult#" + ValidationHelper.HashString(asyncResult));
            if (asyncResult == null)
            {
                throw new ArgumentNullException("asyncResult");
            }
            HttpRequestStreamAsyncResult castedAsyncResult = asyncResult as HttpRequestStreamAsyncResult;

            if (castedAsyncResult == null || castedAsyncResult.AsyncObject != this)
            {
                throw new ArgumentException(SR.GetString(SR.net_io_invalidasyncresult), "asyncResult");
            }
            if (castedAsyncResult.EndCalled)
            {
                throw new InvalidOperationException(SR.GetString(SR.net_io_invalidendcall, "EndRead"));
            }
            castedAsyncResult.EndCalled = true;
            // wait & then check for errors
            object    returnValue = castedAsyncResult.InternalWaitForCompletion();
            Exception exception   = returnValue as Exception;

            if (exception != null)
            {
                GlobalLog.Print("HttpRequestStream#" + ValidationHelper.HashString(this) + "::EndRead() rethrowing exception:" + exception);
                if (Logging.On)
                {
                    Logging.Exception(Logging.HttpListener, this, "EndRead", exception);
                }
                throw exception;
            }
            //


            uint dataRead = (uint)returnValue;

            UpdateAfterRead((uint)castedAsyncResult.ErrorCode, dataRead);
            GlobalLog.Print("HttpRequestStream#" + ValidationHelper.HashString(this) + "::EndRead() returning returnValue:" + ValidationHelper.ToString(returnValue));
            if (Logging.On)
            {
                Logging.Exit(Logging.HttpListener, this, "EndRead", "");
            }

            return((int)dataRead + (int)castedAsyncResult.m_dataAlreadyRead);
        }
        public override int EndRead(IAsyncResult asyncResult)
        {
            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Enter(this);
                NetEventSource.Info(this, $"asyncResult: {asyncResult}");
            }
            if (asyncResult == null)
            {
                throw new ArgumentNullException(nameof(asyncResult));
            }
            HttpRequestStreamAsyncResult castedAsyncResult = asyncResult as HttpRequestStreamAsyncResult;

            if (castedAsyncResult == null || castedAsyncResult.AsyncObject != this)
            {
                throw new ArgumentException(SR.net_io_invalidasyncresult, nameof(asyncResult));
            }
            if (castedAsyncResult.EndCalled)
            {
                throw new InvalidOperationException(SR.Format(SR.net_io_invalidendcall, nameof(EndRead)));
            }
            castedAsyncResult.EndCalled = true;
            // wait & then check for errors
            object    returnValue = castedAsyncResult.InternalWaitForCompletion();
            Exception exception   = returnValue as Exception;

            if (exception != null)
            {
                if (NetEventSource.IsEnabled)
                {
                    NetEventSource.Info(this, "Rethrowing exception:" + exception);
                    NetEventSource.Error(this, exception.ToString());
                }
                throw exception;
            }

            uint dataRead = (uint)returnValue;

            UpdateAfterRead((uint)castedAsyncResult.ErrorCode, dataRead);
            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Info(this, $"returnValue:{returnValue}");
                NetEventSource.Exit(this);
            }

            return((int)dataRead + (int)castedAsyncResult._dataAlreadyRead);
        }
Example #7
0
        public override int EndRead(IAsyncResult asyncResult)
        {
            if (Logging.On)
            {
                Logging.Enter(Logging.HttpListener, this, "EndRead", "");
            }
            if (asyncResult == null)
            {
                throw new ArgumentNullException("asyncResult");
            }
            HttpRequestStreamAsyncResult result = asyncResult as HttpRequestStreamAsyncResult;

            if ((result == null) || (result.AsyncObject != this))
            {
                throw new ArgumentException(SR.GetString("net_io_invalidasyncresult"), "asyncResult");
            }
            if (result.EndCalled)
            {
                throw new InvalidOperationException(SR.GetString("net_io_invalidendcall", new object[] { "EndRead" }));
            }
            result.EndCalled = true;
            object    obj2 = result.InternalWaitForCompletion();
            Exception e    = obj2 as Exception;

            if (e != null)
            {
                if (Logging.On)
                {
                    Logging.Exception(Logging.HttpListener, this, "EndRead", e);
                }
                throw e;
            }
            uint dataRead = (uint)obj2;

            this.UpdateAfterRead((uint)result.ErrorCode, dataRead);
            if (Logging.On)
            {
                Logging.Exit(Logging.HttpListener, this, "EndRead", "");
            }
            return((int)(dataRead + result.m_dataAlreadyRead));
        }
        public override IAsyncResult BeginRead(byte[] buffer, int offset, int size, AsyncCallback callback, object state)
        {
            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Enter(this);
            }
            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Info(this, "buffer.Length:" + buffer.Length + " size:" + size + " offset:" + offset);
            }
            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }
            if (offset < 0 || offset > buffer.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(offset));
            }
            if (size < 0 || size > buffer.Length - offset)
            {
                throw new ArgumentOutOfRangeException(nameof(size));
            }
            if (size == 0 || _closed)
            {
                if (NetEventSource.IsEnabled)
                {
                    NetEventSource.Exit(this);
                }
                HttpRequestStreamAsyncResult result = new HttpRequestStreamAsyncResult(this, state, callback);
                result.InvokeCallback((uint)0);
                return(result);
            }

            HttpRequestStreamAsyncResult asyncResult = null;

            uint dataRead = 0;

            if (_dataChunkIndex != -1)
            {
                dataRead = Interop.HttpApi.GetChunks(_httpContext.Request.RequestBuffer, _httpContext.Request.OriginalBlobAddress, ref _dataChunkIndex, ref _dataChunkOffset, buffer, offset, size);
                if (_dataChunkIndex != -1 && dataRead == size)
                {
                    asyncResult = new HttpRequestStreamAsyncResult(_httpContext.RequestQueueBoundHandle, this, state, callback, buffer, offset, (uint)size, 0);
                    asyncResult.InvokeCallback(dataRead);
                }
            }

            if (_dataChunkIndex == -1 && dataRead < size)
            {
                if (NetEventSource.IsEnabled)
                {
                    NetEventSource.Info(this, "size:" + size + " offset:" + offset);
                }
                uint statusCode = 0;
                offset += (int)dataRead;
                size   -= (int)dataRead;

                //the http.sys team recommends that we limit the size to 128kb
                if (size > MaxReadSize)
                {
                    size = MaxReadSize;
                }

                asyncResult = new HttpRequestStreamAsyncResult(_httpContext.RequestQueueBoundHandle, this, state, callback, buffer, offset, (uint)size, dataRead);
                uint bytesReturned;

                try
                {
                    fixed(byte *pBuffer = buffer)
                    {
                        // issue unmanaged blocking call
                        if (NetEventSource.IsEnabled)
                        {
                            NetEventSource.Info(this, "Calling Interop.HttpApi.HttpReceiveRequestEntityBody");
                        }

                        uint flags = 0;

                        if (!_inOpaqueMode)
                        {
                            flags = (uint)Interop.HttpApi.HTTP_FLAGS.HTTP_RECEIVE_REQUEST_FLAG_COPY_BODY;
                        }

                        statusCode =
                            Interop.HttpApi.HttpReceiveRequestEntityBody(
                                _httpContext.RequestQueueHandle,
                                _httpContext.RequestId,
                                flags,
                                asyncResult._pPinnedBuffer,
                                (uint)size,
                                out bytesReturned,
                                asyncResult._pOverlapped);

                        if (NetEventSource.IsEnabled)
                        {
                            NetEventSource.Info(this, "Call to Interop.HttpApi.HttpReceiveRequestEntityBody returned:" + statusCode + " dataRead:" + dataRead);
                        }
                    }
                }
                catch (Exception e)
                {
                    if (NetEventSource.IsEnabled)
                    {
                        NetEventSource.Error(this, e.ToString());
                    }
                    asyncResult.InternalCleanup();
                    throw;
                }

                if (statusCode != Interop.HttpApi.ERROR_SUCCESS && statusCode != Interop.HttpApi.ERROR_IO_PENDING)
                {
                    asyncResult.InternalCleanup();
                    if (statusCode == Interop.HttpApi.ERROR_HANDLE_EOF)
                    {
                        asyncResult = new HttpRequestStreamAsyncResult(this, state, callback, dataRead);
                        asyncResult.InvokeCallback((uint)0);
                    }
                    else
                    {
                        Exception exception = new HttpListenerException((int)statusCode);
                        if (NetEventSource.IsEnabled)
                        {
                            NetEventSource.Error(this, exception.ToString());
                        }
                        asyncResult.InternalCleanup();
                        throw exception;
                    }
                }
                else if (statusCode == Interop.HttpApi.ERROR_SUCCESS &&
                         HttpListener.SkipIOCPCallbackOnSuccess)
                {
                    // IO operation completed synchronously - callback won't be called to signal completion.
                    asyncResult.IOCompleted(statusCode, bytesReturned);
                }
            }
            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Exit(this);
            }
            return(asyncResult);
        }
 private static void IOCompleted(HttpRequestStreamAsyncResult asyncResult, uint errorCode, uint numBytes)
 {
     GlobalLog.Print("HttpRequestStreamAsyncResult#" + ValidationHelper.HashString(asyncResult) + "::Callback() errorCode:0x" + errorCode.ToString("x8") + " numBytes:" + numBytes);
     object result = null;
     try {
         if (errorCode != UnsafeNclNativeMethods.ErrorCodes.ERROR_SUCCESS && errorCode != UnsafeNclNativeMethods.ErrorCodes.ERROR_HANDLE_EOF) {
             asyncResult.ErrorCode = (int)errorCode;
             result = new HttpListenerException((int)errorCode);
         }
         else {
             result = numBytes;
             if(Logging.On)Logging.Dump(Logging.HttpListener, asyncResult, "Callback", (IntPtr)asyncResult.m_pPinnedBuffer, (int)numBytes);
         }
         GlobalLog.Print("HttpRequestStreamAsyncResult#" + ValidationHelper.HashString(asyncResult) + "::Callback() calling Complete()");
     }
     catch (Exception e) {
         result = e;
     }
     asyncResult.InvokeCallback(result);
 }
        public override IAsyncResult BeginRead(byte[] buffer, int offset, int size, AsyncCallback callback, object state) {
            if(Logging.On) Logging.Enter(Logging.HttpListener, this, "BeginRead", "");
            GlobalLog.Print("HttpRequestStream#" + ValidationHelper.HashString(this) + "::BeginRead() buffer.Length:" + buffer.Length + " size:" + size + " offset:" + offset);
            if (buffer==null) {
                throw new ArgumentNullException("buffer");
            }
            if (offset<0 || offset>buffer.Length) {
                throw new ArgumentOutOfRangeException("offset");
            }
            if (size<0 || size>buffer.Length-offset) {
                throw new ArgumentOutOfRangeException("size");
            }
            if (size==0 || m_Closed) {
                if(Logging.On)Logging.Exit(Logging.HttpListener, this, "BeginRead", "");
                HttpRequestStreamAsyncResult result = new HttpRequestStreamAsyncResult(this, state, callback);
                result.InvokeCallback((uint) 0);
                return result;
            }
            
            HttpRequestStreamAsyncResult asyncResult = null;

            uint dataRead = 0;
            if (m_DataChunkIndex != -1) {
                dataRead = UnsafeNclNativeMethods.HttpApi.GetChunks(m_HttpContext.Request.RequestBuffer, m_HttpContext.Request.OriginalBlobAddress, ref m_DataChunkIndex, ref m_DataChunkOffset, buffer, offset, size);
                if (m_DataChunkIndex != -1 && dataRead == size) {
                    asyncResult = new HttpRequestStreamAsyncResult(this, state, callback, buffer, offset, (uint)size, 0);
                    asyncResult.InvokeCallback(dataRead);
                }
            }

            if (m_DataChunkIndex == -1 && dataRead < size) {
                GlobalLog.Print("HttpRequestStream#" + ValidationHelper.HashString(this) + "::BeginRead() size:" + size + " offset:" + offset);
                uint statusCode = 0;
                offset += (int)dataRead;
                size -= (int)dataRead;

                //the http.sys team recommends that we limit the size to 128kb
                if (size > MaxReadSize) {
                    size = MaxReadSize;
                }

                asyncResult = new HttpRequestStreamAsyncResult(this, state, callback, buffer, offset, (uint)size, dataRead);
                uint bytesReturned;

                try {
                    fixed (byte* pBuffer = buffer) {
                        // issue unmanaged blocking call
                        GlobalLog.Print("HttpRequestStream#" + ValidationHelper.HashString(this) + "::BeginRead() calling UnsafeNclNativeMethods.HttpApi.HttpReceiveRequestEntityBody");

                        m_HttpContext.EnsureBoundHandle();
                        uint flags = 0;

                        if (!m_InOpaqueMode)
                        {
                            flags = (uint)UnsafeNclNativeMethods.HttpApi.HTTP_FLAGS.HTTP_RECEIVE_REQUEST_FLAG_COPY_BODY;
                        }

                        statusCode =
                            UnsafeNclNativeMethods.HttpApi.HttpReceiveRequestEntityBody(
                                m_HttpContext.RequestQueueHandle,
                                m_HttpContext.RequestId,
                                flags,
                                asyncResult.m_pPinnedBuffer,
                                (uint)size,
                                out bytesReturned,
                                asyncResult.m_pOverlapped);

                        GlobalLog.Print("HttpRequestStream#" + ValidationHelper.HashString(this) + "::BeginRead() call to UnsafeNclNativeMethods.HttpApi.HttpReceiveRequestEntityBody returned:" + statusCode + " dataRead:" + dataRead);
                    }
                }
                catch (Exception e) {
                    if (Logging.On) Logging.Exception(Logging.HttpListener, this, "BeginRead", e);
                    asyncResult.InternalCleanup();
                    throw;
                }

                if (statusCode != UnsafeNclNativeMethods.ErrorCodes.ERROR_SUCCESS && statusCode != UnsafeNclNativeMethods.ErrorCodes.ERROR_IO_PENDING) {
                    asyncResult.InternalCleanup();
                    if (statusCode == UnsafeNclNativeMethods.ErrorCodes.ERROR_HANDLE_EOF) {
                        asyncResult = new HttpRequestStreamAsyncResult(this, state, callback, dataRead);
                        asyncResult.InvokeCallback((uint)0);
                    }
                    else {
                        Exception exception = new HttpListenerException((int)statusCode);
                        if (Logging.On) Logging.Exception(Logging.HttpListener, this, "BeginRead", exception);
                        asyncResult.InternalCleanup();
                        throw exception;
                    }
                }
                else if (statusCode == UnsafeNclNativeMethods.ErrorCodes.ERROR_SUCCESS &&
                         HttpListener.SkipIOCPCallbackOnSuccess)
                {
                    // IO operation completed synchronously - callback won't be called to signal completion.
                    asyncResult.IOCompleted(statusCode, bytesReturned);
                }
            }
            if(Logging.On)Logging.Exit(Logging.HttpListener, this, "BeginRead", "");
            return asyncResult;
        }
Example #11
0
 private static void IOCompleted(HttpRequestStreamAsyncResult asyncResult, uint errorCode, uint numBytes)
 {
     if (NetEventSource.IsEnabled) NetEventSource.Info(null, $"asyncResult: {asyncResult} errorCode:0x {errorCode.ToString("x8")} numBytes: {numBytes}");
     object result = null;
     try
     {
         if (errorCode != Interop.HttpApi.ERROR_SUCCESS && errorCode != Interop.HttpApi.ERROR_HANDLE_EOF)
         {
             asyncResult.ErrorCode = (int)errorCode;
             result = new HttpListenerException((int)errorCode);
         }
         else
         {
             result = numBytes;
             if (NetEventSource.IsEnabled) NetEventSource.DumpBuffer(asyncResult, (IntPtr)asyncResult._pPinnedBuffer, (int)numBytes);
         }
         if (NetEventSource.IsEnabled) NetEventSource.Info(null, $"asyncResult: {asyncResult} calling Complete()");
     }
     catch (Exception e)
     {
         result = e;
     }
     asyncResult.InvokeCallback(result);
 }
Example #12
0
        public override IAsyncResult BeginRead(byte[] buffer, int offset, int size, AsyncCallback callback, object state)
        {
            if (NetEventSource.IsEnabled) NetEventSource.Enter(this);
            if (NetEventSource.IsEnabled) NetEventSource.Info(this, "buffer.Length:" + buffer.Length + " size:" + size + " offset:" + offset);
            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }
            if (offset < 0 || offset > buffer.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(offset));
            }
            if (size < 0 || size > buffer.Length - offset)
            {
                throw new ArgumentOutOfRangeException(nameof(size));
            }
            if (size == 0 || _closed)
            {
                if (NetEventSource.IsEnabled) NetEventSource.Exit(this);
                HttpRequestStreamAsyncResult result = new HttpRequestStreamAsyncResult(this, state, callback);
                result.InvokeCallback((uint)0);
                return result;
            }

            HttpRequestStreamAsyncResult asyncResult = null;

            uint dataRead = 0;
            if (_dataChunkIndex != -1)
            {
                dataRead = Interop.HttpApi.GetChunks(_httpContext.Request.RequestBuffer, _httpContext.Request.OriginalBlobAddress, ref _dataChunkIndex, ref _dataChunkOffset, buffer, offset, size);
                if (_dataChunkIndex != -1 && dataRead == size)
                {
                    asyncResult = new HttpRequestStreamAsyncResult(_httpContext.RequestQueueBoundHandle, this, state, callback, buffer, offset, (uint)size, 0);
                    asyncResult.InvokeCallback(dataRead);
                }
            }

            if (_dataChunkIndex == -1 && dataRead < size)
            {
                if (NetEventSource.IsEnabled) NetEventSource.Info(this, "size:" + size + " offset:" + offset);
                uint statusCode = 0;
                offset += (int)dataRead;
                size -= (int)dataRead;

                //the http.sys team recommends that we limit the size to 128kb
                if (size > MaxReadSize)
                {
                    size = MaxReadSize;
                }

                asyncResult = new HttpRequestStreamAsyncResult(_httpContext.RequestQueueBoundHandle, this, state, callback, buffer, offset, (uint)size, dataRead);
                uint bytesReturned;

                try
                {
                    fixed (byte* pBuffer = buffer)
                    {
                        // issue unmanaged blocking call
                        if (NetEventSource.IsEnabled) NetEventSource.Info(this, "Calling Interop.HttpApi.HttpReceiveRequestEntityBody");

                        uint flags = 0;

                        if (!_inOpaqueMode)
                        {
                            flags = (uint)Interop.HttpApi.HTTP_FLAGS.HTTP_RECEIVE_REQUEST_FLAG_COPY_BODY;
                        }

                        statusCode =
                            Interop.HttpApi.HttpReceiveRequestEntityBody(
                                _httpContext.RequestQueueHandle,
                                _httpContext.RequestId,
                                flags,
                                asyncResult._pPinnedBuffer,
                                (uint)size,
                                out bytesReturned,
                                asyncResult._pOverlapped);

                        if (NetEventSource.IsEnabled) NetEventSource.Info(this, "Call to Interop.HttpApi.HttpReceiveRequestEntityBody returned:" + statusCode + " dataRead:" + dataRead);
                    }
                }
                catch (Exception e)
                {
                    if (NetEventSource.IsEnabled) NetEventSource.Error(this, e.ToString());
                    asyncResult.InternalCleanup();
                    throw;
                }

                if (statusCode != Interop.HttpApi.ERROR_SUCCESS && statusCode != Interop.HttpApi.ERROR_IO_PENDING)
                {
                    asyncResult.InternalCleanup();
                    if (statusCode == Interop.HttpApi.ERROR_HANDLE_EOF)
                    {
                        asyncResult = new HttpRequestStreamAsyncResult(this, state, callback, dataRead);
                        asyncResult.InvokeCallback((uint)0);
                    }
                    else
                    {
                        Exception exception = new HttpListenerException((int)statusCode);
                        if (NetEventSource.IsEnabled) NetEventSource.Error(this, exception.ToString());
                        asyncResult.InternalCleanup();
                        throw exception;
                    }
                }
                else if (statusCode == Interop.HttpApi.ERROR_SUCCESS &&
                         HttpListener.SkipIOCPCallbackOnSuccess)
                {
                    // IO operation completed synchronously - callback won't be called to signal completion.
                    asyncResult.IOCompleted(statusCode, bytesReturned);
                }
            }
            if (NetEventSource.IsEnabled) NetEventSource.Exit(this);
            return asyncResult;
        }
 public override unsafe IAsyncResult BeginRead(byte[] buffer, int offset, int size, AsyncCallback callback, object state)
 {
     if (Logging.On)
     {
         Logging.Enter(Logging.HttpListener, this, "BeginRead", "");
     }
     if (buffer == null)
     {
         throw new ArgumentNullException("buffer");
     }
     if ((offset < 0) || (offset > buffer.Length))
     {
         throw new ArgumentOutOfRangeException("offset");
     }
     if ((size < 0) || (size > (buffer.Length - offset)))
     {
         throw new ArgumentOutOfRangeException("size");
     }
     if ((size == 0) || this.m_Closed)
     {
         if (Logging.On)
         {
             Logging.Exit(Logging.HttpListener, this, "BeginRead", "");
         }
         HttpRequestStreamAsyncResult result = new HttpRequestStreamAsyncResult(this, state, callback);
         result.InvokeCallback(0);
         return result;
     }
     HttpRequestStreamAsyncResult result2 = null;
     uint num = 0;
     if (this.m_DataChunkIndex != -1)
     {
         num = UnsafeNclNativeMethods.HttpApi.GetChunks(this.m_HttpContext.Request.RequestBuffer, this.m_HttpContext.Request.OriginalBlobAddress, ref this.m_DataChunkIndex, ref this.m_DataChunkOffset, buffer, offset, size);
         if ((this.m_DataChunkIndex != -1) && (num == size))
         {
             result2 = new HttpRequestStreamAsyncResult(this, state, callback, buffer, offset, (uint) size, 0);
             result2.InvokeCallback(num);
         }
     }
     if ((this.m_DataChunkIndex == -1) && (num < size))
     {
         uint num2 = 0;
         offset += (int) num;
         size -= (int) num;
         if (size > 0x20000)
         {
             size = 0x20000;
         }
         result2 = new HttpRequestStreamAsyncResult(this, state, callback, buffer, offset, (uint) size, num);
         try
         {
             byte[] buffer2 = buffer;
             if (buffer2 != null)
             {
                 int length = buffer2.Length;
             }
             this.m_HttpContext.EnsureBoundHandle();
             num2 = UnsafeNclNativeMethods.HttpApi.HttpReceiveRequestEntityBody(this.m_HttpContext.RequestQueueHandle, this.m_HttpContext.RequestId, 1, result2.m_pPinnedBuffer, (uint) size, null, result2.m_pOverlapped);
         }
         catch (Exception exception)
         {
             if (Logging.On)
             {
                 Logging.Exception(Logging.HttpListener, this, "BeginRead", exception);
             }
             result2.InternalCleanup();
             throw;
         }
         switch (num2)
         {
             case 0:
             case 0x3e5:
                 goto Label_0217;
         }
         if (num2 == 0x26)
         {
             result2.m_pOverlapped.InternalLow = IntPtr.Zero;
         }
         result2.InternalCleanup();
         if (num2 != 0x26)
         {
             Exception e = new HttpListenerException((int) num2);
             if (Logging.On)
             {
                 Logging.Exception(Logging.HttpListener, this, "BeginRead", e);
             }
             result2.InternalCleanup();
             throw e;
         }
         result2 = new HttpRequestStreamAsyncResult(this, state, callback, num);
         result2.InvokeCallback(0);
     }
 Label_0217:
     if (Logging.On)
     {
         Logging.Exit(Logging.HttpListener, this, "BeginRead", "");
     }
     return result2;
 }
Example #14
0
        public override unsafe IAsyncResult BeginRead(byte[] buffer, int offset, int size, AsyncCallback callback, object state)
        {
            if (Logging.On)
            {
                Logging.Enter(Logging.HttpListener, this, "BeginRead", "");
            }
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }
            if ((offset < 0) || (offset > buffer.Length))
            {
                throw new ArgumentOutOfRangeException("offset");
            }
            if ((size < 0) || (size > (buffer.Length - offset)))
            {
                throw new ArgumentOutOfRangeException("size");
            }
            if ((size == 0) || this.m_Closed)
            {
                if (Logging.On)
                {
                    Logging.Exit(Logging.HttpListener, this, "BeginRead", "");
                }
                HttpRequestStreamAsyncResult result = new HttpRequestStreamAsyncResult(this, state, callback);
                result.InvokeCallback(0);
                return(result);
            }
            HttpRequestStreamAsyncResult result2 = null;
            uint num = 0;

            if (this.m_DataChunkIndex != -1)
            {
                num = UnsafeNclNativeMethods.HttpApi.GetChunks(this.m_HttpContext.Request.RequestBuffer, this.m_HttpContext.Request.OriginalBlobAddress, ref this.m_DataChunkIndex, ref this.m_DataChunkOffset, buffer, offset, size);
                if ((this.m_DataChunkIndex != -1) && (num == size))
                {
                    result2 = new HttpRequestStreamAsyncResult(this, state, callback, buffer, offset, (uint)size, 0);
                    result2.InvokeCallback(num);
                }
            }
            if ((this.m_DataChunkIndex == -1) && (num < size))
            {
                uint num2 = 0;
                offset += (int)num;
                size   -= (int)num;
                if (size > 0x20000)
                {
                    size = 0x20000;
                }
                result2 = new HttpRequestStreamAsyncResult(this, state, callback, buffer, offset, (uint)size, num);
                try
                {
                    byte[] buffer2 = buffer;
                    if (buffer2 != null)
                    {
                        int length = buffer2.Length;
                    }
                    this.m_HttpContext.EnsureBoundHandle();
                    num2 = UnsafeNclNativeMethods.HttpApi.HttpReceiveRequestEntityBody(this.m_HttpContext.RequestQueueHandle, this.m_HttpContext.RequestId, 1, result2.m_pPinnedBuffer, (uint)size, null, result2.m_pOverlapped);
                }
                catch (Exception exception)
                {
                    if (Logging.On)
                    {
                        Logging.Exception(Logging.HttpListener, this, "BeginRead", exception);
                    }
                    result2.InternalCleanup();
                    throw;
                }
                switch (num2)
                {
                case 0:
                case 0x3e5:
                    goto Label_0217;
                }
                if (num2 == 0x26)
                {
                    result2.m_pOverlapped.InternalLow = IntPtr.Zero;
                }
                result2.InternalCleanup();
                if (num2 != 0x26)
                {
                    Exception e = new HttpListenerException((int)num2);
                    if (Logging.On)
                    {
                        Logging.Exception(Logging.HttpListener, this, "BeginRead", e);
                    }
                    result2.InternalCleanup();
                    throw e;
                }
                result2 = new HttpRequestStreamAsyncResult(this, state, callback, num);
                result2.InvokeCallback(0);
            }
Label_0217:
            if (Logging.On)
            {
                Logging.Exit(Logging.HttpListener, this, "BeginRead", "");
            }
            return(result2);
        }
Example #15
0
        public override IAsyncResult BeginRead(byte[] buffer, int offset, int size, AsyncCallback callback, object state)
        {
            if (Logging.On)
            {
                Logging.Enter(Logging.HttpListener, this, "BeginRead", "");
            }
            GlobalLog.Print("HttpRequestStream#" + ValidationHelper.HashString(this) + "::BeginRead() buffer.Length:" + buffer.Length + " size:" + size + " offset:" + offset);
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }
            if (offset < 0 || offset > buffer.Length)
            {
                throw new ArgumentOutOfRangeException("offset");
            }
            if (size < 0 || size > buffer.Length - offset)
            {
                throw new ArgumentOutOfRangeException("size");
            }
            if (size == 0 || m_Closed)
            {
                if (Logging.On)
                {
                    Logging.Exit(Logging.HttpListener, this, "BeginRead", "");
                }
                HttpRequestStreamAsyncResult result = new HttpRequestStreamAsyncResult(this, state, callback);
                result.InvokeCallback((uint)0);
                return(result);
            }

            HttpRequestStreamAsyncResult asyncResult = null;

            uint dataRead = 0;

            if (m_DataChunkIndex != -1)
            {
                dataRead = UnsafeNclNativeMethods.HttpApi.GetChunks(m_HttpContext.Request.RequestBuffer, m_HttpContext.Request.OriginalBlobAddress, ref m_DataChunkIndex, ref m_DataChunkOffset, buffer, offset, size);
                if (m_DataChunkIndex != -1 && dataRead == size)
                {
                    asyncResult = new HttpRequestStreamAsyncResult(this, state, callback, buffer, offset, (uint)size, 0);
                    asyncResult.InvokeCallback(dataRead);
                }
            }

            if (m_DataChunkIndex == -1 && dataRead < size)
            {
                GlobalLog.Print("HttpRequestStream#" + ValidationHelper.HashString(this) + "::BeginRead() size:" + size + " offset:" + offset);
                uint statusCode = 0;
                offset += (int)dataRead;
                size   -= (int)dataRead;

                //the http.sys team recommends that we limit the size to 128kb
                if (size > MaxReadSize)
                {
                    size = MaxReadSize;
                }

                asyncResult = new HttpRequestStreamAsyncResult(this, state, callback, buffer, offset, (uint)size, dataRead);
                uint bytesReturned;

                try
                {
                    fixed(byte *pBuffer = buffer)
                    {
                        // issue unmanaged blocking call
                        GlobalLog.Print("HttpRequestStream#" + ValidationHelper.HashString(this) + "::BeginRead() calling UnsafeNclNativeMethods.HttpApi.HttpReceiveRequestEntityBody");

                        m_HttpContext.EnsureBoundHandle();
                        uint flags = 0;

                        if (!m_InOpaqueMode)
                        {
                            flags = (uint)UnsafeNclNativeMethods.HttpApi.HTTP_FLAGS.HTTP_RECEIVE_REQUEST_FLAG_COPY_BODY;
                        }

                        statusCode =
                            UnsafeNclNativeMethods.HttpApi.HttpReceiveRequestEntityBody(
                                m_HttpContext.RequestQueueHandle,
                                m_HttpContext.RequestId,
                                flags,
                                asyncResult.m_pPinnedBuffer,
                                (uint)size,
                                out bytesReturned,
                                asyncResult.m_pOverlapped);

                        GlobalLog.Print("HttpRequestStream#" + ValidationHelper.HashString(this) + "::BeginRead() call to UnsafeNclNativeMethods.HttpApi.HttpReceiveRequestEntityBody returned:" + statusCode + " dataRead:" + dataRead);
                    }
                }
                catch (Exception e) {
                    if (Logging.On)
                    {
                        Logging.Exception(Logging.HttpListener, this, "BeginRead", e);
                    }
                    asyncResult.InternalCleanup();
                    throw;
                }

                if (statusCode != UnsafeNclNativeMethods.ErrorCodes.ERROR_SUCCESS && statusCode != UnsafeNclNativeMethods.ErrorCodes.ERROR_IO_PENDING)
                {
                    asyncResult.InternalCleanup();
                    if (statusCode == UnsafeNclNativeMethods.ErrorCodes.ERROR_HANDLE_EOF)
                    {
                        asyncResult = new HttpRequestStreamAsyncResult(this, state, callback, dataRead);
                        asyncResult.InvokeCallback((uint)0);
                    }
                    else
                    {
                        Exception exception = new HttpListenerException((int)statusCode);
                        if (Logging.On)
                        {
                            Logging.Exception(Logging.HttpListener, this, "BeginRead", exception);
                        }
                        asyncResult.InternalCleanup();
                        throw exception;
                    }
                }
                else if (statusCode == UnsafeNclNativeMethods.ErrorCodes.ERROR_SUCCESS &&
                         HttpListener.SkipIOCPCallbackOnSuccess)
                {
                    // IO operation completed synchronously - callback won't be called to signal completion.
                    asyncResult.IOCompleted(statusCode, bytesReturned);
                }
            }
            if (Logging.On)
            {
                Logging.Exit(Logging.HttpListener, this, "BeginRead", "");
            }
            return(asyncResult);
        }