internal HttpLimitedMultiProcessor(HttpService reference)
        {
            _reference = reference;

            _internalPreManipulators  = new HttpManipulatorCollection <RoutedContext>();
            _internalPostManipulators = new HttpManipulatorCollection <RoutedContext>();

            _preManipulators  = new HttpManipulatorCollection <RoutedContext>();
            _postManipulators = new HttpManipulatorCollection <RoutedContext>();

            _ops = new HttpProcessorOperations(_reference);
        }
Esempio n. 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;
             */
        }
 public HttpRouter(HttpService reference)
 {
     _reference    = reference;
     _manipulators = new HttpManipulatorCollection <HttpContext>();
 }