/// <summary>An IHttpRequest extension method that gets base URL.</summary>
        ///
        /// <param name="httpReq">The httpReq to act on.</param>
        ///
        /// <returns>The base URL.</returns>
        public static string GetBaseUrl(this IHttpRequest httpReq)
        {
            var baseUrl = NServiceKitHttpHandlerFactory.GetBaseUrl();

            if (baseUrl != null)
            {
                return(baseUrl);
            }

            var handlerPath = EndpointHost.Config.NServiceKitHandlerFactoryPath;

            if (handlerPath != null)
            {
                var pos = httpReq.AbsoluteUri.IndexOf(handlerPath, StringComparison.InvariantCultureIgnoreCase);
                if (pos >= 0)
                {
                    baseUrl = httpReq.AbsoluteUri.Substring(0, pos + handlerPath.Length);
                    return(baseUrl);
                }
                return("/" + handlerPath);
            }

            return("/"); //Can't infer Absolute Uri, fallback to root relative path
        }
        private IHttpHandler GetHandlerForPathParts(string[] pathParts)
        {
            var pathController = string.Intern(pathParts[0].ToLower());

            if (pathParts.Length == 1)
            {
                if (pathController == "metadata")
                {
                    return(new IndexMetadataHandler());
                }

                return(null);
            }

            var pathAction = string.Intern(pathParts[1].ToLower());

            if (pathAction == "wsdl")
            {
                if (pathController == "soap11")
                {
                    return(new Soap11WsdlMetadataHandler());
                }
                if (pathController == "soap12")
                {
                    return(new Soap12WsdlMetadataHandler());
                }
            }

            if (pathAction != "metadata")
            {
                return(null);
            }

            switch (pathController)
            {
            case "json":
                return(new JsonMetadataHandler());

            case "xml":
                return(new XmlMetadataHandler());

            case "jsv":
                return(new JsvMetadataHandler());

            case "soap11":
                return(new Soap11MetadataHandler());

            case "soap12":
                return(new Soap12MetadataHandler());

            case "types":

                if (EndpointHost.Config == null ||
                    EndpointHost.Config.MetadataTypesConfig == null)
                {
                    return(null);
                }

                if (EndpointHost.Config.MetadataTypesConfig.BaseUrl == null)
                {
                    EndpointHost.Config.MetadataTypesConfig.BaseUrl = NServiceKitHttpHandlerFactory.GetBaseUrl();
                }

                return(new MetadataTypesHandler {
                    Config = EndpointHost.AppHost.Config.MetadataTypesConfig
                });

            case "operations":

                return(new ActionHandler((httpReq, httpRes) =>
                                         EndpointHost.Config.HasAccessToMetadata(httpReq, httpRes)
                            ? EndpointHost.Metadata.GetOperationDtos()
                            : null, "Operations"));

            default:
                string contentType;
                if (EndpointHost.ContentTypeFilter
                    .ContentTypeFormats.TryGetValue(pathController, out contentType))
                {
                    var format = Common.Web.ContentType.GetContentFormat(contentType);
                    return(new CustomMetadataHandler(contentType, format));
                }
                break;
            }
            return(null);
        }