Exemplo n.º 1
0
 private int SetCookieParams(Controller.RequestContext rc, HttpServletRequest req)
 {
     Cookie[] cookies = req.GetCookies();
     if (cookies != null)
     {
         foreach (Cookie cookie in cookies)
         {
             rc.Cookies()[cookie.GetName()] = cookie;
         }
         return(cookies.Length);
     }
     return(0);
 }
Exemplo n.º 2
0
        /// <exception cref="Javax.Servlet.ServletException"/>
        /// <exception cref="System.IO.IOException"/>
        protected override void Service(HttpServletRequest req, HttpServletResponse res)
        {
            res.SetCharacterEncoding("UTF-8");
            string uri = HtmlQuoting.QuoteHtmlChars(req.GetRequestURI());

            if (uri == null)
            {
                uri = "/";
            }
            if (devMode && uri.Equals("/__stop"))
            {
                // quick hack to restart servers in dev mode without OS commands
                res.SetStatus(res.ScNoContent);
                Log.Info("dev mode restart requested");
                PrepareToExit();
                return;
            }
            // if they provide a redirectPath go there instead of going to
            // "/" so that filters can differentiate the webapps.
            if (uri.Equals("/"))
            {
                string redirectPath = webApp.GetRedirectPath();
                if (redirectPath != null && !redirectPath.IsEmpty())
                {
                    res.SendRedirect(redirectPath);
                    return;
                }
            }
            string method = req.GetMethod();

            if (method.Equals("OPTIONS"))
            {
                DoOptions(req, res);
                return;
            }
            if (method.Equals("TRACE"))
            {
                DoTrace(req, res);
                return;
            }
            if (method.Equals("HEAD"))
            {
                DoGet(req, res);
                // default to bad request
                return;
            }
            string pathInfo = req.GetPathInfo();

            if (pathInfo == null)
            {
                pathInfo = "/";
            }
            Controller.RequestContext rc = injector.GetInstance <Controller.RequestContext>();
            if (SetCookieParams(rc, req) > 0)
            {
                Cookie ec = rc.Cookies()[ErrorCookie];
                if (ec != null)
                {
                    rc.SetStatus(System.Convert.ToInt32(rc.Cookies()[StatusCookie].GetValue()));
                    RemoveErrorCookies(res, uri);
                    rc.Set(Params.ErrorDetails, ec.GetValue());
                    Render(typeof(ErrorPage));
                    return;
                }
            }
            rc.prefix = webApp.Name();
            Router.Dest dest = null;
            try
            {
                dest = router.Resolve(method, pathInfo);
            }
            catch (WebAppException e)
            {
                rc.error = e;
                if (!e.Message.Contains("not found"))
                {
                    rc.SetStatus(res.ScInternalServerError);
                    Render(typeof(ErrorPage));
                    return;
                }
            }
            if (dest == null)
            {
                rc.SetStatus(res.ScNotFound);
                Render(typeof(ErrorPage));
                return;
            }
            rc.devMode = devMode;
            SetMoreParams(rc, pathInfo, dest);
            Controller controller = injector.GetInstance(dest.controllerClass);

            try
            {
                // TODO: support args converted from /path/:arg1/...
                dest.action.Invoke(controller, (object[])null);
                if (!rc.rendered)
                {
                    if (dest.defaultViewClass != null)
                    {
                        Render(dest.defaultViewClass);
                    }
                    else
                    {
                        if (rc.status == 200)
                        {
                            throw new InvalidOperationException("No view rendered for 200");
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Log.Error("error handling URI: " + uri, e);
                // Page could be half rendered (but still not flushed). So redirect.
                RedirectToErrorPage(res, e, uri, devMode);
            }
        }