Exemplo n.º 1
0
 public HttpContextBuilder(HttpContext httpContext, RequestContextBuilder requestContextBuilder, ResponseContextBuilder responseContextBuilder, ServerContext serverContext)
 {
     _httpContext = httpContext;
     _requestContextBuilder = requestContextBuilder;
     _responseContextBuilder = responseContextBuilder;
     _serverContext = serverContext;
 }
        internal void Execute(HttpContext httpContext)
        {
            var requestedRoute = httpContext.Request.RequestedRoute;
            var method = requestedRoute.Route.ControllerType.GetMethod(requestedRoute.MethodName);

            var instance = (Controller)requestedRoute.Route.ControllerType.GetConstructor(new Type[] { }).Invoke(new object[] { });
            instance.InitializeController(httpContext, new JsonSerializer(), new ModelFactory());

            method.Invoke(instance, null);
        }
        internal void ProcessRequest()
        {
            try
            {
                // Get Request information
                _httpContext = CreateHttpContext();

                if (_serverContext.RequestFilter != null)
                {
                    _serverContext.RequestFilter.FilterRequest(_httpContext.Request, _httpContext.Response);
                }

                // Interrupt execution of this request if response is sent
                if (_httpContext.Response.IsSent)
                    return;

                // Create reponse
                if (_httpContext.Response.HttpStatusCode == null)
                {
                    CreateResponse();
                }

                // Interrupt execution of this request if response is sent
                if (_httpContext.Response.IsSent)
                    return;

                // IResponseFilter ?
                if (_serverContext.ResponseFilter != null)
                {
                    _serverContext.ResponseFilter.FilterResponse(_httpContext.Response, _httpContext.Request);
                }

                // Interrupt execution of this request if response is sent
                if (_httpContext.Response.IsSent)
                    return;

                // Send response
                SendResponse();

            }
            catch (Exception e)
            {
                try
                {
                    _responseHandler.SendInternalServerError(_httpContext.Socket, e);
                }
                catch (Exception innerException)
                {
                    throw new Exception("Error in error handler ", innerException);
                }
                throw; // Re throw error to error handler after trying to send to client
            }
        }
        internal RequestResponseHandler Create(Socket socket)
        {
            var httpContext = new HttpContext(new RequestContext(), new ResponseContext(), socket);

            var requestContextBuilder = new RequestContextBuilder(_serverContext);
            var responseContextBuilder = new ResponseContextBuilder();
            var httpApplicationHandler = new HttpApplicationHandler();
            var responseHandler = new ResponseHandler(_serverContext, new ResponseHeaderBuilder());

            var httpContextBuilder = new HttpContextBuilder(httpContext, requestContextBuilder, responseContextBuilder, _serverContext);

            return new RequestResponseHandler(httpContextBuilder, httpApplicationHandler, responseHandler, _serverContext);
        }
        internal void SendResponse(HttpContext httpContext)
        {
            var response = httpContext.Response;

            var responseLineAndHeaders = _responseHeaderBuilder.BuildResponseLineAndHeaders(response);

            if(httpContext.Socket.Poll(5000000, SelectMode.SelectWrite))
            {
                httpContext.Socket.Send(Encoding.UTF8.GetBytes(responseLineAndHeaders), responseLineAndHeaders.Length, SocketFlags.None);

                if (response.IsFile)
                {
                    _serverContext.FileAndDirectoryService.SendFile(_serverContext, httpContext);
                    return;
                }

                if (response.Data != null && response.Data != string.Empty)
                {
                    httpContext.Socket.Send(Encoding.UTF8.GetBytes(response.Data), response.Data.Length, SocketFlags.None);
                }
            }
        }
Exemplo n.º 6
0
 public void InitializeController(HttpContext httpContext, IJsonSerializer jsonSerializer, ModelFactory modelFactory)
 {
     HttpContext = httpContext;
     JsonSerializer = jsonSerializer;
     _modelFactory = modelFactory;
 }