Example #1
0
        public void Ctor_SetsCorrectProperties()
        {
            // Arrange, Act
            var handler = new RedirectHttpHandler(targetUrl: "~/foo", permanent: true, isReusable: false);

            // Assert
            PAssert.IsTrue(() => handler.TargetUrl == "~/foo");
            PAssert.IsTrue(() => handler.Permanent);
            PAssert.IsTrue(() => handler.IsReusable == false);
        }
Example #2
0
        public void GetHttpHandler_ReturnsItself()
        {
            // Arrange
            var handler = new RedirectHttpHandler(targetUrl: "~/foo", permanent: true, isReusable: false);

            // Act
            var httpHandler = handler.GetHttpHandler(new Mock <RequestContext>().Object);

            // Assert
            PAssert.IsTrue(() => handler == httpHandler);
        }
 //- @GetHandler -//
 /// <summary>
 /// Runs the catchall processor; this only runs if the HTTP handler selection process did not successfully find an appropriate handler. If null is returned, the PassThrougHttpHandler is used.
 /// </summary>
 /// <param name="context">The HttpContext object.</param>
 /// <param name="requestType">Type of the request.</param>
 /// <param name="virtualPath">The virtual path.</param>
 /// <param name="path">The actual path.</param>
 /// <param name="parameterArray">The optional parameter array.</param>
 /// <returns>Instance of HTTP handler to use</returns>
 public override System.Web.IHttpHandler GetHandler(System.Web.HttpContext context, String requestType, String virtualPath, String path, params Object[] parameterArray)
 {
     String text = String.Empty;
     if (!Collection.IsNullOrEmpty(parameterArray))
     {
         text = parameterArray[0] as String;
     }
     //+
     RedirectHttpHandler redirectHttpHandler = new RedirectHttpHandler();
     if (!String.IsNullOrEmpty(text))
     {
         redirectHttpHandler.ParameterMap.Add("Destination", text);
     }
     //+
     return redirectHttpHandler;
 }
Example #4
0
        static ServiceStackHttpHandlerFactory()
        {
            //MONO doesn't implement this property
            var pi = typeof(HttpRuntime).GetProperty("UsingIntegratedPipeline");

            if (pi != null)
            {
                IsIntegratedPipeline = (bool)pi.GetGetMethod().Invoke(null, new object[0]);
            }

            if (EndpointHost.Config == null)
            {
                throw new ConfigurationErrorsException(
                          "ServiceStack: AppHost does not exist or has not been initialized. "
                          + "Make sure you have created an AppHost and started it with 'new AppHost().Init();' in your Global.asax Application_Start()",
                          new ArgumentNullException("EndpointHost.Config"));
            }

            var isAspNetHost = HttpListenerBase.Instance == null || HttpContext.Current != null;

            WebHostPhysicalPath = EndpointHost.Config.WebHostPhysicalPath;

            //Apache+mod_mono treats path="servicestack*" as path="*" so takes over root path, so we need to serve matching resources
            var hostedAtRootPath = EndpointHost.Config.ServiceStackHandlerFactoryPath == null;

            //DefaultHttpHandler not supported in IntegratedPipeline mode
            if (!IsIntegratedPipeline && isAspNetHost && !hostedAtRootPath && !Env.IsMono)
            {
                DefaultHttpHandler = new DefaultHttpHandler();
            }

            ServeDefaultHandler = hostedAtRootPath || Env.IsMono;
            if (ServeDefaultHandler)
            {
                foreach (var filePath in Directory.GetFiles(WebHostPhysicalPath))
                {
                    var fileNameLower = Path.GetFileName(filePath).ToLower();
                    if (DefaultRootFileName == null && EndpointHost.Config.DefaultDocuments.Contains(fileNameLower))
                    {
                        //Can't serve Default.aspx pages when hostedAtRootPath so ignore and allow for next default document
                        if (!(hostedAtRootPath && fileNameLower.EndsWith(".aspx")))
                        {
                            DefaultRootFileName = fileNameLower;
                            ((StaticFileHandler)StaticFileHandler).SetDefaultFile(filePath);

                            if (DefaultHttpHandler == null)
                            {
                                DefaultHttpHandler = new RedirectHttpHandler {
                                    RelativeUrl = DefaultRootFileName
                                }
                            }
                            ;
                        }
                    }
                    WebHostRootFileNames.Add(Path.GetFileName(fileNameLower));
                }
                foreach (var dirName in Directory.GetDirectories(WebHostPhysicalPath))
                {
                    var dirNameLower = Path.GetFileName(dirName).ToLower();
                    WebHostRootFileNames.Add(Path.GetFileName(dirNameLower));
                }
            }

            if (!string.IsNullOrEmpty(EndpointHost.Config.DefaultRedirectPath))
            {
                DefaultHttpHandler = new RedirectHttpHandler {
                    RelativeUrl = EndpointHost.Config.DefaultRedirectPath
                }
            }
            ;

            if (DefaultHttpHandler == null && !string.IsNullOrEmpty(EndpointHost.Config.MetadataRedirectPath))
            {
                DefaultHttpHandler = new RedirectHttpHandler {
                    RelativeUrl = EndpointHost.Config.MetadataRedirectPath
                }
            }
            ;

            if (!string.IsNullOrEmpty(EndpointHost.Config.MetadataRedirectPath))
            {
                NonRootModeDefaultHttpHandler = new RedirectHttpHandler {
                    RelativeUrl = EndpointHost.Config.MetadataRedirectPath
                }
            }
            ;

            if (DefaultHttpHandler == null)
            {
                DefaultHttpHandler = NotFoundHttpHandler;
            }

            var defaultRedirectHanlder = DefaultHttpHandler as RedirectHttpHandler;
            var debugDefaultHandler    = defaultRedirectHanlder != null
                                ? defaultRedirectHanlder.RelativeUrl
                                : typeof(DefaultHttpHandler).Name;

            SetApplicationBaseUrl(EndpointHost.Config.WebHostUrl);

            ForbiddenHttpHandler = new ForbiddenHttpHandler {
                IsIntegratedPipeline = IsIntegratedPipeline,
                WebHostPhysicalPath  = WebHostPhysicalPath,
                WebHostRootFileNames = WebHostRootFileNames,
                ApplicationBaseUrl   = ApplicationBaseUrl,
                DefaultRootFileName  = DefaultRootFileName,
                DefaultHandler       = debugDefaultHandler,
            };

            NotFoundHttpHandler = string.IsNullOrEmpty(EndpointHost.Config.NotFoundRedirectPath)
                                ? (IHttpHandler) new NotFoundHttpHandler {
                IsIntegratedPipeline = IsIntegratedPipeline,
                WebHostPhysicalPath  = WebHostPhysicalPath,
                WebHostRootFileNames = WebHostRootFileNames,
                ApplicationBaseUrl   = ApplicationBaseUrl,
                DefaultRootFileName  = DefaultRootFileName,
                DefaultHandler       = debugDefaultHandler,
            }
                                : new RedirectHttpHandler {
                RelativeUrl = EndpointHost.Config.NotFoundRedirectPath
            };
        }
        static HttpHandlerFactory()
        {
            //MONO doesn't implement this property
            var pi = typeof(HttpRuntime).GetProperty("UsingIntegratedPipeline");

            if (pi != null)
            {
                IsIntegratedPipeline = (bool)pi.GetGetMethod().Invoke(null, new object[0]);
            }

            var appHost = HostContext.AppHost;
            var config  = appHost.Config;

            var isAspNetHost = HostContext.IsAspNetHost;

            WebHostPhysicalPath   = appHost.VirtualPathProvider.RootDirectory.RealPath;
            HostAutoRedirectsDirs = isAspNetHost && !Env.IsMono;

            //Apache+mod_mono treats path="servicestack*" as path="*" so takes over root path, so we need to serve matching resources
            var hostedAtRootPath = config.HandlerFactoryPath == null;

            //DefaultHttpHandler not supported in IntegratedPipeline mode
            if (!IsIntegratedPipeline && isAspNetHost && !hostedAtRootPath && !Env.IsMono)
            {
                DefaultHttpHandler = new DefaultHttpHandler();
            }

            var rootFiles = appHost.VirtualPathProvider.GetRootFiles().ToList();

            foreach (var file in rootFiles)
            {
                var fileNameLower = file.Name.ToLower();
                if (DefaultRootFileName == null && config.DefaultDocuments.Contains(fileNameLower))
                {
                    //Can't serve Default.aspx pages so ignore and allow for next default document
                    if (!fileNameLower.EndsWith(".aspx"))
                    {
                        DefaultRootFileName = fileNameLower;
                        StaticFileHandler.SetDefaultFile(file.VirtualPath, file.ReadAllBytes(), file.LastModified);

                        if (DefaultHttpHandler == null)
                        {
                            DefaultHttpHandler = new StaticFileHandler {
                                VirtualNode = file
                            }
                        }
                        ;
                    }
                }
                WebHostRootFileNames.Add(fileNameLower);
            }

            foreach (var dir in appHost.VirtualPathProvider.GetRootDirectories())
            {
                WebHostRootFileNames.Add(dir.Name.ToLower());
            }

            if (!string.IsNullOrEmpty(config.DefaultRedirectPath))
            {
                DefaultHttpHandler = new RedirectHttpHandler {
                    RelativeUrl = config.DefaultRedirectPath
                }
            }
            ;

            if (DefaultHttpHandler == null && !string.IsNullOrEmpty(config.MetadataRedirectPath))
            {
                DefaultHttpHandler = new RedirectHttpHandler {
                    RelativeUrl = config.MetadataRedirectPath
                }
            }
            ;

            if (!string.IsNullOrEmpty(config.MetadataRedirectPath))
            {
                NonRootModeDefaultHttpHandler = new RedirectHttpHandler {
                    RelativeUrl = config.MetadataRedirectPath
                }
            }
            ;

            if (DefaultHttpHandler == null)
            {
                DefaultHttpHandler = NotFoundHttpHandler;
            }

            var defaultRedirectHanlder = DefaultHttpHandler as RedirectHttpHandler;
            var debugDefaultHandler    = defaultRedirectHanlder != null
                ? defaultRedirectHanlder.RelativeUrl
                : typeof(DefaultHttpHandler).GetOperationName();

            SetApplicationBaseUrl(config.WebHostUrl);

            ForbiddenHttpHandler = appHost.GetCustomErrorHttpHandler(HttpStatusCode.Forbidden);
            if (ForbiddenHttpHandler == null)
            {
                ForbiddenHttpHandler = new ForbiddenHttpHandler
                {
                    IsIntegratedPipeline = IsIntegratedPipeline,
                    WebHostPhysicalPath  = WebHostPhysicalPath,
                    WebHostRootFileNames = WebHostRootFileNames,
                    WebHostUrl           = config.WebHostUrl,
                    DefaultRootFileName  = DefaultRootFileName,
                    DefaultHandler       = debugDefaultHandler,
                };
            }

            NotFoundHttpHandler = appHost.GetCustomErrorHttpHandler(HttpStatusCode.NotFound);
            if (NotFoundHttpHandler == null)
            {
                NotFoundHttpHandler = new NotFoundHttpHandler
                {
                    IsIntegratedPipeline = IsIntegratedPipeline,
                    WebHostPhysicalPath  = WebHostPhysicalPath,
                    WebHostRootFileNames = WebHostRootFileNames,
                    WebHostUrl           = config.WebHostUrl,
                    DefaultRootFileName  = DefaultRootFileName,
                    DefaultHandler       = debugDefaultHandler,
                };
            }
        }
        static ServiceStackHttpHandlerFactory()
        {
            //MONO doesn't implement this property
            var pi = typeof(HttpRuntime).GetProperty("UsingIntegratedPipeline");

            if (pi != null)
            {
                IsIntegratedPipeline = (bool)pi.GetGetMethod().Invoke(null, new object[0]);
            }

            var isAspNetHost = HttpListenerBase.Instance == null || HttpContext.Current != null;

            WebHostPhysicalPath = isAspNetHost
                                ? "~".MapHostAbsolutePath()
                                : "~".MapAbsolutePath();

            //DefaultHttpHandler not supported in IntegratedPipeline mode
            if (!IsIntegratedPipeline && isAspNetHost && !Env.IsMono)
            {
                DefaultHttpHandler = new DefaultHttpHandler();
            }

            //Apache+mod_mono treats path="servicestack*" as path="*" so takes over root path, so we need to serve matching resources
            var hostedAtRootPath = EndpointHost.Config.ServiceStackHandlerFactoryPath == null;

            ServeDefaultHandler = hostedAtRootPath || Env.IsMono;
            if (ServeDefaultHandler)
            {
                foreach (var fileName in Directory.GetFiles(WebHostPhysicalPath))
                {
                    var fileNameLower = Path.GetFileName(fileName).ToLower();
                    if (DefaultRootFileName == null && EndpointHost.Config.DefaultDocuments.Contains(fileNameLower))
                    {
                        DefaultRootFileName = fileNameLower;
                        if (DefaultHttpHandler == null)
                        {
                            DefaultHttpHandler = new RedirectHttpHandler {
                                RelativeUrl = DefaultRootFileName
                            }
                        }
                        ;
                    }
                    WebHostRootFileNames.Add(Path.GetFileName(fileNameLower));
                }
                foreach (var dirName in Directory.GetDirectories(WebHostPhysicalPath))
                {
                    var dirNameLower = Path.GetFileName(dirName).ToLower();
                    WebHostRootFileNames.Add(Path.GetFileName(dirNameLower));
                }
            }

            if (!string.IsNullOrEmpty(EndpointHost.Config.DefaultRedirectPath))
            {
                DefaultHttpHandler = new RedirectHttpHandler {
                    RelativeUrl = EndpointHost.Config.DefaultRedirectPath
                }
            }
            ;

            if (DefaultHttpHandler == null && !string.IsNullOrEmpty(EndpointHost.Config.MetadataRedirectPath))
            {
                DefaultHttpHandler = new RedirectHttpHandler {
                    RelativeUrl = EndpointHost.Config.MetadataRedirectPath
                }
            }
            ;

            if (!string.IsNullOrEmpty(EndpointHost.Config.MetadataRedirectPath))
            {
                NonRootModeDefaultHttpHandler = new RedirectHttpHandler {
                    RelativeUrl = EndpointHost.Config.MetadataRedirectPath
                }
            }
            ;

            if (DefaultHttpHandler == null)
            {
                DefaultHttpHandler = NotFoundHttpHandler;
            }

            var defaultRedirectHanlder = DefaultHttpHandler as RedirectHttpHandler;
            var debugDefaultHandler    = defaultRedirectHanlder != null
                                ? defaultRedirectHanlder.RelativeUrl
                                : typeof(DefaultHttpHandler).Name;

            ForbiddenHttpHandler = new ForbiddenHttpHandler
            {
                IsIntegratedPipeline = IsIntegratedPipeline,
                WebHostPhysicalPath  = WebHostPhysicalPath,
                WebHostRootFileNames = WebHostRootFileNames,
                ApplicationBaseUrl   = ApplicationBaseUrl,
                DefaultRootFileName  = DefaultRootFileName,
                DefaultHandler       = debugDefaultHandler,
            };

            NotFoundHttpHandler = string.IsNullOrEmpty(EndpointHost.Config.NotFoundRedirectPath)
                                ? (IHttpHandler) new NotFoundHttpHandler
            {
                IsIntegratedPipeline = IsIntegratedPipeline,
                WebHostPhysicalPath  = WebHostPhysicalPath,
                WebHostRootFileNames = WebHostRootFileNames,
                ApplicationBaseUrl   = ApplicationBaseUrl,
                DefaultRootFileName  = DefaultRootFileName,
                DefaultHandler       = debugDefaultHandler,
            }
                                : new RedirectHttpHandler {
                RelativeUrl = EndpointHost.Config.NotFoundRedirectPath
            };
        }
        internal static void Init()
        {
            var appHost = HostContext.AppHost;

            WebHostPhysicalPath = appHost.VirtualFileSources.RootDirectory.RealPath;

            //Apache+mod_mono treats path="servicestack*" as path="*" so takes over root path, so we need to serve matching resources
            var hostedAtRootPath = appHost.Config.HandlerFactoryPath.IsNullOrEmpty();

//#if !NETSTANDARD2_0
//            //DefaultHttpHandler not supported in IntegratedPipeline mode
//            if (!Platform.IsIntegratedPipeline && HostContext.IsAspNetHost && !hostedAtRootPath && !Env.IsMono)
//                DefaultHttpHandler = new DefaultHttpHandler();
//#endif
            var rootFiles = appHost.VirtualFileSources.GetRootFiles();

            DefaultRootFileName = null;
            foreach (var file in rootFiles)
            {
                var fileNameLower = file.Name.ToLowerInvariant();
                if (DefaultRootFileName == null && appHost.Config.DefaultDocuments.Contains(fileNameLower))
                {
                    //Can't serve Default.aspx pages so ignore and allow for next default document
                    if (!fileNameLower.EndsWith(".aspx"))
                    {
                        DefaultRootFileName = file.Name;
                        StaticFileHandler.SetDefaultFile(file.VirtualPath, file.ReadAllBytes(), file.LastModified);

                        if (DefaultHttpHandler == null)
                        {
                            DefaultHttpHandler = new StaticFileHandler(file);
                        }
                    }
                }
            }

            if (!appHost.Config.DefaultRedirectPath.IsNullOrEmpty())
            {
                DefaultHttpHandler = new RedirectHttpHandler {
                    RelativeUrl = appHost.Config.DefaultRedirectPath
                }
            }
            ;

            var metadataHandler = new RedirectHttpHandler();

            if (!appHost.Config.MetadataRedirectPath.IsNullOrEmpty())
            {
                metadataHandler.RelativeUrl = appHost.Config.MetadataRedirectPath;
            }
            else
            {
                metadataHandler.RelativeUrl = "metadata";
            }
            if (hostedAtRootPath)
            {
                if (DefaultHttpHandler == null)
                {
                    DefaultHttpHandler = metadataHandler;
                }
            }
            else
            {
                DefaultHttpHandler = metadataHandler;
            }

            var defaultRedirectHanlder = DefaultHttpHandler as RedirectHttpHandler;
            var debugDefaultHandler    = defaultRedirectHanlder?.RelativeUrl;

            ForbiddenHttpHandler = appHost.GetCustomErrorHttpHandler(HttpStatusCode.Forbidden) ?? new ForbiddenHttpHandler
            {
                WebHostPhysicalPath = WebHostPhysicalPath,
                WebHostUrl          = appHost.Config.WebHostUrl,
                DefaultRootFileName = DefaultRootFileName,
                DefaultHandler      = debugDefaultHandler,
            };

            NotFoundHttpHandler = appHost.GetCustomErrorHttpHandler(HttpStatusCode.NotFound) ?? new NotFoundHttpHandler
            {
                WebHostPhysicalPath = WebHostPhysicalPath,
                WebHostUrl          = appHost.Config.WebHostUrl,
                DefaultRootFileName = DefaultRootFileName,
                DefaultHandler      = debugDefaultHandler,
            };
        }
Example #8
0
        internal static void Init()
        {
            try
            {
#if !NETSTANDARD2_0
                //MONO doesn't implement this property
                var pi = typeof(HttpRuntime).GetProperty("UsingIntegratedPipeline");
                if (pi != null)
                {
                    IsIntegratedPipeline = (bool)pi.GetGetMethod().Invoke(null, TypeConstants.EmptyObjectArray);
                }
#endif
                var appHost = HostContext.AppHost;
                var config  = appHost.Config;

                var isAspNetHost = HostContext.IsAspNetHost;
                WebHostPhysicalPath = appHost.VirtualFileSources.RootDirectory.RealPath;

                //Apache+mod_mono treats path="servicestack*" as path="*" so takes over root path, so we need to serve matching resources
                var hostedAtRootPath = config.HandlerFactoryPath == null;

                //DefaultHttpHandler not supported in IntegratedPipeline mode
                if (!IsIntegratedPipeline && isAspNetHost && !hostedAtRootPath && !Env.IsMono)
                {
                    DefaultHttpHandler = new DefaultHttpHandler();
                }

                var rootFiles = appHost.VirtualFileSources.GetRootFiles().ToList();
                foreach (var file in rootFiles)
                {
                    var fileNameLower = file.Name.ToLowerInvariant();
                    if (DefaultRootFileName == null && config.DefaultDocuments.Contains(fileNameLower))
                    {
                        //Can't serve Default.aspx pages so ignore and allow for next default document
                        if (!fileNameLower.EndsWith(".aspx"))
                        {
                            DefaultRootFileName = fileNameLower;
                            StaticFileHandler.SetDefaultFile(file.VirtualPath, file.ReadAllBytes(), file.LastModified);

                            if (DefaultHttpHandler == null)
                            {
                                DefaultHttpHandler = new StaticFileHandler(file);
                            }
                        }
                    }
                }

                if (!string.IsNullOrEmpty(config.DefaultRedirectPath))
                {
                    DefaultHttpHandler = new RedirectHttpHandler {
                        RelativeUrl = config.DefaultRedirectPath
                    };
                    NonRootModeDefaultHttpHandler = new RedirectHttpHandler {
                        RelativeUrl = config.DefaultRedirectPath
                    };
                }

                if (DefaultHttpHandler == null && !string.IsNullOrEmpty(config.MetadataRedirectPath))
                {
                    DefaultHttpHandler = new RedirectHttpHandler {
                        RelativeUrl = config.MetadataRedirectPath
                    };
                    NonRootModeDefaultHttpHandler = new RedirectHttpHandler {
                        RelativeUrl = config.MetadataRedirectPath
                    };
                }

                if (DefaultHttpHandler == null)
                {
                    DefaultHttpHandler = NotFoundHttpHandler;
                }

                var defaultRedirectHanlder = DefaultHttpHandler as RedirectHttpHandler;
                var debugDefaultHandler    = defaultRedirectHanlder != null
                    ? defaultRedirectHanlder.RelativeUrl
                    : typeof(DefaultHttpHandler).GetOperationName();

                ForbiddenHttpHandler = appHost.GetCustomErrorHttpHandler(HttpStatusCode.Forbidden);
                if (ForbiddenHttpHandler == null)
                {
                    ForbiddenHttpHandler = new ForbiddenHttpHandler
                    {
                        IsIntegratedPipeline = IsIntegratedPipeline,
                        WebHostPhysicalPath  = WebHostPhysicalPath,
                        WebHostUrl           = config.WebHostUrl,
                        DefaultRootFileName  = DefaultRootFileName,
                        DefaultHandler       = debugDefaultHandler,
                    };
                }

                NotFoundHttpHandler = appHost.GetCustomErrorHttpHandler(HttpStatusCode.NotFound);
                if (NotFoundHttpHandler == null)
                {
                    NotFoundHttpHandler = new NotFoundHttpHandler
                    {
                        IsIntegratedPipeline = IsIntegratedPipeline,
                        WebHostPhysicalPath  = WebHostPhysicalPath,
                        WebHostUrl           = config.WebHostUrl,
                        DefaultRootFileName  = DefaultRootFileName,
                        DefaultHandler       = debugDefaultHandler,
                    };
                }
            }
            catch (Exception ex)
            {
                HostContext.AppHost.OnStartupException(ex);
            }
        }