示例#1
0
        void OnReadInternal(IAsyncResult ares)
        {
            timer.Change(Timeout.Infinite, Timeout.Infinite);
            int nread = -1;

            try
            {
                nread = stream.EndRead(ares);
                ms.Write(buffer, 0, nread);
                if (ms.Length > 32768)
                {
                    SendError("Bad request", 400);
                    Close(true);
                    return;
                }
            }
            catch
            {
                if (ms != null && ms.Length > 0)
                {
                    SendError();
                }
                if (sock != null)
                {
                    CloseSocket();
                    Unbind();
                }
                return;
            }

            if (nread == 0)
            {
                //if (ms.Length > 0)
                //	SendError (); // Why bother?
                CloseSocket();
                Unbind();
                return;
            }

            if (ProcessInput(ms))
            {
                if (!context.HaveError)
                {
                    context.Request.FinishInitialization();
                }

                if (context.HaveError)
                {
                    SendError();
                    Close(true);
                    return;
                }

                if (!epl.BindContext(context))
                {
                    SendError("Invalid host", 400);
                    Close(true);
                    return;
                }
                HttpListener listener = context.Listener;
                if (last_listener != listener)
                {
                    RemoveConnection();
                    listener.AddConnection(this);
                    last_listener = listener;
                }

                context_bound = true;
                listener.RegisterContext(context);
                return;
            }
            stream.BeginRead(buffer, 0, BufferSize, onread_cb, this);
        }
        private void OnReadInternal(IAsyncResult asyncResult)
        {
            _timer.Change(Timeout.Infinite, Timeout.Infinite);
            var nread = -1;

            try {
                nread = _stream.EndRead(asyncResult);
                _requestBuffer.Write(_buffer, 0, nread);
                if (_requestBuffer.Length > 32768)
                {
                    SendError();
                    Close(true);

                    return;
                }
            } catch {
                if (_requestBuffer != null && _requestBuffer.Length > 0)
                {
                    SendError();
                }

                if (_socket != null)
                {
                    CloseSocket();
                    Unbind();
                }

                return;
            }

            if (nread == 0)
            {
                //if (_requestBuffer.Length > 0)
                //	SendError (); // Why bother?
                CloseSocket();
                Unbind();

                return;
            }

            if (ProcessInput(_requestBuffer.GetBuffer()))
            {
                if (!_context.HaveError)
                {
                    _context.Request.FinishInitialization();
                }

                if (_context.HaveError)
                {
                    SendError();
                    Close(true);

                    return;
                }

                if (!_epListener.BindContext(_context))
                {
                    SendError("Invalid host", 400);
                    Close(true);

                    return;
                }

                var listener = _context.Listener;
                if (_lastListener != listener)
                {
                    RemoveConnection();
                    listener.AddConnection(this);
                    _lastListener = listener;
                }

                listener.RegisterContext(_context);
                _contextWasBound = true;

                return;
            }

            _stream.BeginRead(_buffer, 0, BufferSize, OnRead, this);
        }