예제 #1
0
        /// <summary>
        /// Handles when the results are sent.  This even works with non-blocking long-polling!
        /// </summary>
        void WebConnection_ResultsSent()
        {
            WebConnection.ResultsSent -= new GenericVoid(WebConnection_ResultsSent);

            if (null != Content)
            {
                Content.Dispose();
                Content = null;
            }

            if (!WebServer.KeepAlive)
                Close();
            else
            {
                /*if (BufferBytesRead > 0)
                {
                    // If there is a complete header in the buffer, use it before switching to the ReadingHeader mode
                    HeaderEnd = Array<byte>.IndexOf(
                        Buffer,
                        HeaderEndMarker,
                        0,
                        BufferBytesRead);

                    if (-1 != HeaderEnd)
                    {
                        WebConnectionIOState = WebConnectionIOState.ParsingHeader;
                        DoIO = NoOp;
                        HandleHeader();

                        return;
                    }
                }*/

                WebConnectionIOState = WebConnectionIOState.ReadingHeader;
                DoIO = ReadHeader;
                //StartReadingSocket();
            }
        }
예제 #2
0
        /// <summary>
        /// Sets up the socket reader to read the Content of the HTTP request
        /// </summary>
        private void SetUpReadingContent()
        {
            ContentLength = long.Parse(WebConnection.Headers["CONTENT-LENGTH"]);

            if (ContentLength <= WebServer.MaxInMemoryContentSize)
                Content = new AsyncWebConnectionContent.InMemory(ContentLength);
            else if (ContentLength <= WebServer.MaxContentSize)
                Content = new AsyncWebConnectionContent.OnDisk();
            else
            {
                Close(WebResults.FromString(Status._413_Request_Entity_Too_Large, "Too much data, max size: " + WebServer.MaxContentSize.ToString()));
                return;
            }

            // TODO: get rid of this
            /*if (BufferBytesRead > 4)
                if (Encoding.UTF8.GetString(Buffer).StartsWith("POST"))
                    System.Diagnostics.Debugger.Break();*/

            // Give the reader the additional read bytes

            if (BufferBytesRead >= ContentLength)
            {
                byte[] toCopy = new byte[ContentLength];
                Array.Copy(Buffer, 0, toCopy, 0, toCopy.Length);

                Content.TakeBytes(toCopy);

                if (BufferBytesRead == ContentLength)
                    BufferBytesRead = 0;
                else
                {
                    byte[] oldBuffer = Buffer;
                    Buffer = new byte[oldBuffer.Length];

                    Array.Copy(oldBuffer, ContentLength, Buffer, 0, BufferBytesRead - ContentLength);

                    BufferBytesRead = Convert.ToInt32(Convert.ToInt64(BufferBytesRead) - ContentLength);
                }

                DoIO = NoOp;
                WebConnectionIOState = WebConnectionIOState.PerformingRequest;

                PerformRequest();
            }
            else if (0 == BufferBytesRead)
            {
                ReadStartTime = DateTime.UtcNow;

                WebConnectionIOState = WebConnectionIOState.ReadingContent;
                DoIO = ReadContent;

                //StartReadingSocket();
            }
            else // Buffer has less bytes then what's needed
            {
                byte[] toCopy = new byte[BufferBytesRead];

                Array.Copy(Buffer, 0, toCopy, 0, BufferBytesRead);
                Content.TakeBytes(toCopy);

                BufferBytesRead = 0;

                ReadStartTime = DateTime.UtcNow;

                WebConnectionIOState = WebConnectionIOState.ReadingContent;
                DoIO = ReadContent;

                //StartReadingSocket();
            }
        }