public async Task ProcessRequestAsync()
        {
            try
            {
                var httpRequest = await this.ReadRequest();

                if (httpRequest != null)
                {
                    string sessionId = this.SetRequestSession(httpRequest);

                    var httpResponse = httpHandler.Handle(httpRequest);

                    this.SetResponseSession(httpResponse, sessionId);

                    await this.PrepareResponse(httpResponse);
                }
            }

            catch (BadRequestException e)
            {
                await this.PrepareResponse(new HtmlResult($"<h1>400 Bad Request</h1><p>{e.Message}</p>",
                                                          HttpResponseStatusCode.BadRequest));
            }
            catch (Exception e)
            {
                await this.PrepareResponse(new HtmlResult($"<h1>500 Internal Server Error</h1><p>{e.Message}</p>",
                                                          HttpResponseStatusCode.InternalServerError));
            }

            this.client.Shutdown(SocketShutdown.Both);
        }
示例#2
0
        public async Task ProcessRequestAsync()
        {
            IHttpResponse response;

            try
            {
                IHttpRequest request = await ReadRequest();

                if (request == null)
                {
                    response = new HttpResponse(HttpStatusCode.BadRequest);
                }
                else
                {
                    string sessionId;

                    if (request.Cookies.TryGetValue(HttpSessionStorage.SessionCookieKey, out HttpCookie cookie))
                    {
                        sessionId = cookie.Value;
                    }
                    else
                    {
                        sessionId = Guid.NewGuid().ToString();
                    }

                    request.Session = HttpSessionStorage.GetSession(sessionId);

                    if (request.Path.Contains("/Resources") || request.Path.Contains('.'))
                    {
                        response = _resourceHandler.Handle(request);
                    }
                    else
                    {
                        response = _controllerHandler.Handle(request);
                    }

                    response.Cookies.Add(new HttpCookie(HttpSessionStorage.SessionCookieKey, sessionId, isHttpOnly: true));
                }
            }
            catch (ArgumentException e)
            {
                Console.WriteLine("Bad Request Error\n{0}\n{1}\n", e.Message, e.StackTrace);
                response = new HttpResponse(HttpStatusCode.BadRequest);
            }
            catch (Exception e)
            {
                Console.WriteLine("Internal Server Error\n{0}\n{1}\n", e.Message, e.StackTrace);
                response = new HttpResponse(HttpStatusCode.InternalServerError);
            }

            await SendResponse(response);

            _clientSocket.Dispose();
        }
示例#3
0
        public async Task ProcessRequestAsync()
        {
            IHttpRequest httpRequest = await ReadRequest();

            if (httpRequest != null)
            {
                string sessionId = SetRequestSession(httpRequest);

                IHttpResponse httpResponse = _handler.Handle(httpRequest);

                SetResponseSession(httpResponse, sessionId);

                await PrepareResponse(httpResponse);
            }

            _client.Shutdown(SocketShutdown.Both);
        }
示例#4
0
 public async Task ProcessRequestAsync(HttpContext context)
 {
     await _handler.Handle(context);
 }
示例#5
0
        protected void SendResponse()
        {
            EndUploads ();

            _Logger.DebugFormat ("Data received: {0} bytes", SocketAsyncManager.TotalReceived);
            bool closeConnectionAfterSend = true;

            if (_request != null)
            {
                Handler = InitHandler ();
                if (Handler != null)
                {
                    _Logger.InfoFormat ("{0} /{1} {2}", _request.Method, _request.Resource, _request.Protocol);
                    HttpResponse response = Handler.Handle (_request);
                    if (response != null)
                    {
                        byte[] responseData = response.Data;

                        if (_httpServer.Mode == ServerMode.FastCGI)
                            responseData = _fcgiInterpreter.TranslateToFCGIResponse (responseData);

                        SocketAsyncManager.SendAsync (responseData, 0, responseData.Length);
                        SocketAsyncManager.Clear ();
                        _request = null;
                        closeConnectionAfterSend = !response.ConnectionKeepAlive;
                    } else
                    {
                        byte[] h404 = Encoding.Default.GetBytes (string.Format ("HTTP/1.1 {0}\r\n\r\n", HttpResponseStatus._404.ToResponseHeaderText ()));
                        if (_httpServer.Mode == ServerMode.FastCGI)
                            h404 = _fcgiInterpreter.TranslateToFCGIResponse (h404);

                        SocketAsyncManager.SendAsync (h404, 0, h404.Length, true);
                    }
                }
            }

            SocketAsyncManager.ShouldCloseConnection = closeConnectionAfterSend;
        }
示例#6
0
 public void Handle(IHttpContext context)
 {
     _handler.Handle(context);
 }