/// <summary>Entry point for ASP.NET.</summary>
        ///
        /// <param name="context">       An instance of the <see cref="T:System.Web.HttpContext" /> class that provides references to intrinsic server objects (for example, Request, Response, Session, and
        /// Server) used to service HTTP requests.
        /// </param>
        /// <param name="requestType">   The HTTP data transfer method (GET or POST) that the client uses.</param>
        /// <param name="url">           The <see cref="P:System.Web.HttpRequest.RawUrl" /> of the requested resource.</param>
        /// <param name="pathTranslated">The <see cref="P:System.Web.HttpRequest.PhysicalApplicationPath" /> to the requested resource.</param>
        ///
        /// <returns>The handler.</returns>
        public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
        {
            DebugLastHandlerArgs = requestType + "|" + url + "|" + pathTranslated;
            var httpReq = new HttpRequestWrapper(pathTranslated, context.Request);
            foreach (var rawHttpHandler in RawHttpHandlers)
            {
                var reqInfo = rawHttpHandler(httpReq);
                if (reqInfo != null) return reqInfo;
            }

            var mode = EndpointHost.Config.NServiceKitHandlerFactoryPath;
            var pathInfo = context.Request.GetPathInfo();

            //WebDev Server auto requests '/default.aspx' so recorrect path to different default document
            if (mode == null && (url == "/default.aspx" || url == "/Default.aspx"))
                pathInfo = "/";

            //Default Request /
            if (string.IsNullOrEmpty(pathInfo) || pathInfo == "/")
            {
                //Exception calling context.Request.Url on Apache+mod_mono
                if (ApplicationBaseUrl == null)
                {
                    var absoluteUrl = Env.IsMono ? url.ToParentPath() : context.Request.GetApplicationUrl();
                    SetApplicationBaseUrl(absoluteUrl);
                }

                //e.g. CatchAllHandler to Process Markdown files
                var catchAllHandler = GetCatchAllHandlerIfAny(httpReq.HttpMethod, pathInfo, httpReq.GetPhysicalPath());
                if (catchAllHandler != null) return catchAllHandler;

                return ServeDefaultHandler ? DefaultHttpHandler : NonRootModeDefaultHttpHandler;
            }

            if (mode != null && pathInfo.EndsWith(mode))
            {
                var requestPath = context.Request.Path.ToLower();
                if (requestPath == "/" + mode
                    || requestPath == mode
                    || requestPath == mode + "/")
                {
                    if (context.Request.PhysicalPath != WebHostPhysicalPath
                        || !File.Exists(Path.Combine(context.Request.PhysicalPath, DefaultRootFileName ?? "")))
                    {
                        return new IndexPageHttpHandler();
                    }
                }

                var okToServe = ShouldAllow(context.Request.FilePath);
                return okToServe ? DefaultHttpHandler : ForbiddenHttpHandler;
            }

            return GetHandlerForPathInfo(
                httpReq.HttpMethod, pathInfo, context.Request.FilePath, pathTranslated)
                   ?? NotFoundHttpHandler;
        }
Example #2
0
        /// <summary>Entry point for ASP.NET.</summary>
        ///
        /// <param name="context">       An instance of the <see cref="T:System.Web.HttpContext" /> class that provides references to intrinsic server objects (for example, Request, Response, Session, and
        /// Server) used to service HTTP requests.
        /// </param>
        /// <param name="requestType">   The HTTP data transfer method (GET or POST) that the client uses.</param>
        /// <param name="url">           The <see cref="P:System.Web.HttpRequest.RawUrl" /> of the requested resource.</param>
        /// <param name="pathTranslated">The <see cref="P:System.Web.HttpRequest.PhysicalApplicationPath" /> to the requested resource.</param>
        ///
        /// <returns>The handler.</returns>
        public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
        {
            DebugLastHandlerArgs = requestType + "|" + url + "|" + pathTranslated;
            var httpReq = new HttpRequestWrapper(pathTranslated, context.Request);

            foreach (var rawHttpHandler in RawHttpHandlers)
            {
                var reqInfo = rawHttpHandler(httpReq);
                if (reqInfo != null)
                {
                    return(reqInfo);
                }
            }

            var mode     = EndpointHost.Config.NServiceKitHandlerFactoryPath;
            var pathInfo = context.Request.GetPathInfo();

            //WebDev Server auto requests '/default.aspx' so recorrect path to different default document
            if (mode == null && (url == "/default.aspx" || url == "/Default.aspx"))
            {
                pathInfo = "/";
            }

            //Default Request /
            if (string.IsNullOrEmpty(pathInfo) || pathInfo == "/")
            {
                //Exception calling context.Request.Url on Apache+mod_mono
                if (ApplicationBaseUrl == null)
                {
                    var absoluteUrl = Env.IsMono ? url.ToParentPath() : context.Request.GetApplicationUrl();
                    SetApplicationBaseUrl(absoluteUrl);
                }

                //e.g. CatchAllHandler to Process Markdown files
                var catchAllHandler = GetCatchAllHandlerIfAny(httpReq.HttpMethod, pathInfo, httpReq.GetPhysicalPath());
                if (catchAllHandler != null)
                {
                    return(catchAllHandler);
                }

                return(ServeDefaultHandler ? DefaultHttpHandler : NonRootModeDefaultHttpHandler);
            }

            if (mode != null && pathInfo.EndsWith(mode))
            {
                var requestPath = context.Request.Path.ToLower();
                if (requestPath == "/" + mode ||
                    requestPath == mode ||
                    requestPath == mode + "/")
                {
                    if (context.Request.PhysicalPath != WebHostPhysicalPath ||
                        !File.Exists(Path.Combine(context.Request.PhysicalPath, DefaultRootFileName ?? "")))
                    {
                        return(new IndexPageHttpHandler());
                    }
                }

                var okToServe = ShouldAllow(context.Request.FilePath);
                return(okToServe ? DefaultHttpHandler : ForbiddenHttpHandler);
            }

            return(GetHandlerForPathInfo(
                       httpReq.HttpMethod, pathInfo, context.Request.FilePath, pathTranslated)
                   ?? NotFoundHttpHandler);
        }