Example #1
0
        private void HeaderLinesCallback(IAsyncResult ar)
        {
            String lHeaderLine;

            try
            {
                lHeaderLine = this.DataConnection.EndReadLine(ar);
            }
            catch (ConnectionClosedException)
            {
                Done();
                return;
            }
            catch (SocketException)
            {
                Done();
                return;
            }
            catch (ObjectDisposedException)
            {
                Done();
                return;
            }

            // HTTP Request Type is already known
            String  lHttpMethod  = this.fContext.CurrentRequest.Header.RequestType;
            Boolean lRequireBody = (lHttpMethod == "POST") || (lHttpMethod == "PUT") || (lHttpMethod == "MERGE");

            Boolean lHaveData = true;

            while (lHaveData)
            {
                lHaveData = false;
                if (lHeaderLine == "")
                {
                    // we've got the last line. Process it
                    if (lRequireBody)
                    {
                        Int64 lContentLength;
#if FULLFRAMEWORK
                        if (!Int64.TryParse(fContext.CurrentRequest.Header.GetHeaderValue("Content-Length"), out lContentLength))
#else
                        if (!LongHelper.TryParse(fContext.CurrentRequest.Header.GetHeaderValue("Content-Length"), out lContentLength))
#endif
                        { lContentLength = 0; }

                        if (lContentLength > ((AsyncHttpServer)this.Owner).MaxPostSize)
                        {
                            this.SendInvalidRequest(new Exception("Content-Length too large"));
                            return;
                        }

                        try
                        {
                            ((AsyncHttpServer)this.Owner).TriggerBeforeHaveData(new AsyncHttpRequestEventArgs(this.DataConnection, this.fContext));
                        }
                        catch (Exception ex)
                        {
                            this.SendInvalidRequest(ex);
                            return;
                        }

                        if (this.fContext.ResponseSent)
                        {
                            return;                             // already triggered the required functions.
                        }
                        try
                        {
                            Byte[] lData = new Byte[(Int32)lContentLength];
                            DataConnection.BeginRead(lData, 0, (Int32)lContentLength, WantBodyCallback, lData);
                        }
                        catch (SocketException)
                        {
                            Done();
                        }
                        catch (ObjectDisposedException)
                        {
                            Done();
                        }

                        return;
                    }
                    else
                    {
                        try
                        {
                            this.fOwner.TriggerHttpRequest(new AsyncHttpRequestEventArgs(this.DataConnection, this.fContext));
                            return;
                        }
                        catch (Exception ex)
                        {
                            this.SendInvalidRequest(ex);
                            return;
                        }
                    }
                }

                if (fContext.CurrentRequest.Header.Count >= fContext.CurrentRequest.Header.MaxHeaderLines && fContext.CurrentRequest.Header.MaxHeaderLinesEnabled)
                {
                    SendInvalidRequest();
                    return;
                }

                Int32 lPosition = lHeaderLine.IndexOf(":", StringComparison.Ordinal);
                if (lPosition == -1)
                {
                    SendInvalidRequest();
                    return;
                }

                String lName  = lHeaderLine.Substring(0, lPosition);
                String lValue = null;
                if (lHeaderLine.Length > lPosition + 1)
                {
                    lValue = lHeaderLine.Substring(lPosition + 2);
                }

                fContext.CurrentRequest.Header.SetHeaderValue(lName, lValue);
                lHeaderLine = DataConnection.BufferReadLine();
                if (lHeaderLine != null)
                {
                    lHaveData = true;
                    continue;
                }

                try
                {
                    DataConnection.BeginReadLine(HeaderLinesCallback, null);
                }
                catch (SocketException)
                {
                    Done();
                }
                catch (ObjectDisposedException)
                {
                    Done();
                }
            }
        }