Пример #1
0
        protected override void Route(HttpContext task)
        {
            //apply manipulation first
            _manipulators.Manipulate(task);

            //prepare route
            string route = "";

            if (task.Request.Version == HttpVersion.HTTP_2_0)
            {
                route = HttpInternalController.HTTP_VERSION;
            }
            else if (!task.Request.Headers.Has("Host"))
            {
                route = HttpInternalController.BAD_REQUEST;
            }
            else if (task.Request.Method == HttpMethod.TRACE && (_reference.ServiceConfiguration.AllowTracing == TracingMode.DISABLED || _reference.ServiceConfiguration.AllowTracing == TracingMode.AUTO))
            {
                route = HttpInternalController.TRACE;
            }
            else if (task.Request.Method == HttpMethod.OPTIONS && (_reference.ServiceConfiguration.AllowOptions == OptionsMode.DISABLED || _reference.ServiceConfiguration.AllowOptions == OptionsMode.AUTO))
            {
                route = HttpInternalController.OPTIONS;
            }
            else if (task.Request.Method == HttpMethod.HEAD)
            {
                route = @"GET" + task.Request.Path;
            }
            else
            {
                route = task.Request.Method.ToString() + task.Request.Path;
            }

            //determining routing target
            RoutingEntry entry = _reference.RoutingEngine.GetEntry(route);

            //if no such entry --> EXCEPTION
            if (entry == null)
            {
                entry = _reference.RoutingEngine.GetEntry(HttpInternalController.PATH_NOT_FOUND);
            }

            //pack routed context
            RoutedContext routedContext = new RoutedContext(task, entry);

            Forward(entry.ProcessingGroup, routedContext);
        }
Пример #2
0
        public void ProcessHttpRequest(RoutedContext context, HttpManipulatorCollection <RoutedContext> internalPreManipulators, HttpManipulatorCollection <RoutedContext> preManipulators, HttpManipulatorCollection <RoutedContext> internalPostManipulators, HttpManipulatorCollection <RoutedContext> postManipulators)
        {
            try
            {
                //execute pre manipulators first
                internalPreManipulators.Manipulate(context);
                preManipulators.Manipulate(context);

                //extract routing information
                RoutingEntry   entry      = context.RoutingEntry;
                MethodInfo     method     = entry.MethodInfo;
                HttpController controller = entry.HttpController;

                //prepare parameter list
                IList <string> variables = ExtractPathVariables(entry.Path, context.Context.Request.Path);

                object[] parameters = new object[1 + variables.Count]; //prepare parameter list for invocation
                parameters[0] = context.Context;                       //the first parameter is always the context object
                                                                       //followed by the extracted URL variables:
                for (int i = 0; i < variables.Count; i++)
                {
                    parameters[i + 1] = variables[i];
                }

                //invoke method
                method.Invoke(controller, parameters);

                //post processing
                internalPostManipulators.Manipulate(context);
                postManipulators.Manipulate(context);
            }
            catch (HttpRequestException hre) //this might never be the case, as HttpRequestException is always an inner exeception
            {
                if (!String.IsNullOrWhiteSpace(hre.ErrorMessage))
                {
                    context.Context.Response.Payload.Write(hre.ErrorMessage);
                    if (!String.IsNullOrWhiteSpace(hre.ContentType))
                    {
                        context.Context.Response.Headers.Set("Content-Type", hre.ContentType);
                    }
                    else
                    {
                        context.Context.Response.Headers.Set("Content-Type", MimeType.TEXT_PLAN);
                    }
                }
                context.Context.Response.Status = hre.Status;
            }
            catch (Exception e)
            {
                if (e.InnerException is HttpRequestException)
                {
                    HttpRequestException hre = (HttpRequestException)e.InnerException;
                    if (!String.IsNullOrWhiteSpace(hre.ErrorMessage))
                    {
                        context.Context.Response.Payload.Write(hre.ErrorMessage);
                        if (!String.IsNullOrWhiteSpace(hre.ContentType))
                        {
                            context.Context.Response.Headers.Set("Content-Type", hre.ContentType);
                        }
                        else
                        {
                            context.Context.Response.Headers.Set("Content-Type", MimeType.TEXT_PLAN);
                        }
                    }
                    context.Context.Response.Status = hre.Status;
                }
                else
                {
                    if (e.InnerException != null && !String.IsNullOrEmpty(e.InnerException.Message))
                    {
                        context.Context.Response.Payload.Write(e.InnerException.Message);
                    }
                    else
                    {
                        context.Context.Response.Payload.Write(e.Message);
                    }

                    context.Context.Response.Headers.Set("Content-Type", MimeType.TEXT_PLAN);
                    context.Context.Response.Status = HttpStatus.InternalServerError;
                }
            }


            /*
             * //invoke method:
             * HttpContext httpContext = (HttpContext)
             *
             * /*
             * if (httpContext == null)
             * {
             *  httpContext = context.Context;
             *  httpContext.Response.Status = HttpStatus.InternalServerError;
             *  //TODO: set Content with error
             * }
             *
             * return httpContext;
             */
        }