コード例 #1
0
        internal ServiceStackHttpHandlerFactoryInstance(EndpointHostInstance endpointHost)
        {
            _endpointHost = endpointHost;

            //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;
            AutoRedirectsDirs = 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 = _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);

            var httpHandlers = _endpointHost.Config.CustomHttpHandlers;

            httpHandlers.TryGetValue(HttpStatusCode.Forbidden, out ForbiddenHttpHandler);
            if (ForbiddenHttpHandler == null)
            {
                ForbiddenHttpHandler = new ForbiddenHttpHandler
                {
                    IsIntegratedPipeline = IsIntegratedPipeline,
                    WebHostPhysicalPath = WebHostPhysicalPath,
                    WebHostRootFileNames = WebHostRootFileNames,
                    ApplicationBaseUrl = ApplicationBaseUrl,
                    DefaultRootFileName = DefaultRootFileName,
                    DefaultHandler = debugDefaultHandler,
                };
            }

            httpHandlers.TryGetValue(HttpStatusCode.NotFound, out NotFoundHttpHandler);
            if (NotFoundHttpHandler == null)
            {
                NotFoundHttpHandler = new NotFoundHttpHandler
                {
                    IsIntegratedPipeline = IsIntegratedPipeline,
                    WebHostPhysicalPath = WebHostPhysicalPath,
                    WebHostRootFileNames = WebHostRootFileNames,
                    ApplicationBaseUrl = ApplicationBaseUrl,
                    DefaultRootFileName = DefaultRootFileName,
                    DefaultHandler = debugDefaultHandler,
                };
            }

            var rawHandlers = _endpointHost.Config.RawHttpHandlers;
            rawHandlers.Add(ReturnRequestInfo);
            rawHandlers.Add(MiniProfilerHandler.MatchesRequest);
            RawHttpHandlers = rawHandlers.ToArray();
        }
コード例 #2
0
 internal static IDisposable SetThreadSpecificHost(EndpointHostInstance useInstance)
 {
     _threadSpecificInstance = new ServiceStackHttpHandlerFactoryInstance(useInstance);
     return new ThreadCleanup();
 }