private static void processAccepted(Socket socket, EndPointListener listener)
        {
            HttpConnection conn = null;

            try {
                conn = new HttpConnection(socket, listener);
                lock (listener._unregisteredSync)
                    listener._unregistered[conn] = conn;

                conn.BeginReadRequest();
            } catch {
                if (conn != null)
                {
                    conn.Close(true);
                    return;
                }

                socket.Close();
            }
        }
Beispiel #2
0
        internal void SendError()
        {
            try {
                _response.StatusCode  = _errorStatusCode;
                _response.ContentType = "text/html";

                var content = createErrorContent(
                    _errorStatusCode,
                    _response.StatusDescription,
                    _errorMessage
                    );

                var enc    = Encoding.UTF8;
                var entity = enc.GetBytes(content);
                _response.ContentEncoding = enc;
                _response.ContentLength64 = entity.LongLength;

                _response.Close(entity, true);
            }
            catch {
                _connection.Close(true);
            }
        }
Beispiel #3
0
        private static void onRead(IAsyncResult asyncResult)
        {
            //Discarded unreachable code: IL_00cb
            HttpConnection httpConnection = (HttpConnection)asyncResult.AsyncState;

            if (httpConnection._socket == null)
            {
                return;
            }
            lock (httpConnection._sync)
            {
                if (httpConnection._socket == null)
                {
                    return;
                }
                int num = -1;
                try
                {
                    httpConnection._timer.Change(-1, -1);
                    num = httpConnection._stream.EndRead(asyncResult);
                    httpConnection._requestBuffer.Write(httpConnection._buffer, 0, num);
                    if (httpConnection._requestBuffer.Length > 32768)
                    {
                        httpConnection.SendError("Bad request", 400);
                        httpConnection.Close(force: true);
                        return;
                    }
                }
                catch
                {
                    if (httpConnection._requestBuffer != null && httpConnection._requestBuffer.Length > 0)
                    {
                        httpConnection.SendError();
                    }
                    httpConnection.close();
                    return;
                }
                if (num <= 0)
                {
                    httpConnection.close();
                }
                else if (httpConnection.processInput(httpConnection._requestBuffer.GetBuffer()))
                {
                    if (!httpConnection._context.HasError)
                    {
                        httpConnection._context.Request.FinishInitialization();
                    }
                    if (httpConnection._context.HasError)
                    {
                        httpConnection.SendError();
                        httpConnection.Close(force: true);
                        return;
                    }
                    if (!httpConnection._listener.BindContext(httpConnection._context))
                    {
                        httpConnection.SendError("Invalid host", 400);
                        httpConnection.Close(force: true);
                        return;
                    }
                    HttpListener listener = httpConnection._context.Listener;
                    if (httpConnection._lastListener != listener)
                    {
                        httpConnection.removeConnection();
                        listener.AddConnection(httpConnection);
                        httpConnection._lastListener = listener;
                    }
                    httpConnection._contextWasBound = true;
                    listener.RegisterContext(httpConnection._context);
                }
                else
                {
                    httpConnection._stream.BeginRead(httpConnection._buffer, 0, 8192, onRead, httpConnection);
                }
            }
        }