Exemplo n.º 1
0
        public static RequestAttributes GetAttributes(this IRequest request)
        {
            if (HostContext.DebugMode &&
                request.QueryString != null)    //Mock<IHttpRequest>
            {
                var simulate = request.QueryString["simulate"];
                if (simulate != null)
                {
                    return(ToRequestAttributes(simulate.Split(',')));
                }
            }

            var portRestrictions = RequestAttributes.None;

            portRestrictions |= ContentFormat.GetRequestAttribute(request.Verb);
            portRestrictions |= request.IsSecureConnection ? RequestAttributes.Secure : RequestAttributes.InSecure;

            if (request.UserHostAddress != null)
            {
                var isIpv4Address = request.UserHostAddress.IndexOf('.') != -1 &&
                                    request.UserHostAddress.IndexOf("::", StringComparison.InvariantCulture) == -1;

                string ipAddressNumber = null;
                if (isIpv4Address)
                {
                    ipAddressNumber = request.UserHostAddress.SplitOnFirst(":")[0];
                }
                else
                {
                    if (request.UserHostAddress.Contains("]:"))
                    {
                        ipAddressNumber = request.UserHostAddress.SplitOnLast(":")[0];
                    }
                    else
                    {
                        ipAddressNumber = request.UserHostAddress.LastIndexOf("%", StringComparison.InvariantCulture) > 0 ?
                                          request.UserHostAddress.SplitOnLast(":")[0] :
                                          request.UserHostAddress;
                    }
                }

                try
                {
                    ipAddressNumber = ipAddressNumber.SplitOnFirst(',')[0];
                    var ipAddress = ipAddressNumber.StartsWith("::1")
                        ? IPAddress.IPv6Loopback
                        : IPAddress.Parse(ipAddressNumber);
                    portRestrictions |= GetAttributes(ipAddress);
                }
                catch (Exception ex)
                {
                    throw new ArgumentException("Could not parse Ipv{0} Address: {1} / {2}"
                                                .Fmt((isIpv4Address ? 4 : 6), request.UserHostAddress, ipAddressNumber), ex);
                }
            }

            return(portRestrictions);
        }
        public static string GetResponseContentType(this IRequest httpReq)
        {
            var specifiedContentType = GetQueryStringContentType(httpReq);

            if (!IsNullOrEmpty(specifiedContentType))
            {
                return(specifiedContentType);
            }

            var acceptContentTypes = httpReq.AcceptTypes;
            var defaultContentType = httpReq.ContentType;

            if (httpReq.HasAnyOfContentTypes(MimeTypes.FormUrlEncoded, MimeTypes.MultiPartFormData))
            {
                defaultContentType = HostContext.Config.DefaultContentType;
            }

            var customContentTypes    = HostContext.ContentTypes.ContentTypeFormats.Values;
            var preferredContentTypes = HostContext.Config.PreferredContentTypesArray;

            var acceptsAnything       = false;
            var hasDefaultContentType = !IsNullOrEmpty(defaultContentType);

            if (acceptContentTypes != null)
            {
                var hasPreferredContentTypes = new bool[preferredContentTypes.Length];
                foreach (var acceptsType in acceptContentTypes)
                {
                    var contentType = ContentFormat.GetRealContentType(acceptsType);
                    acceptsAnything = acceptsAnything || contentType == "*/*";

                    for (var i = 0; i < preferredContentTypes.Length; i++)
                    {
                        if (hasPreferredContentTypes[i])
                        {
                            continue;
                        }
                        var preferredContentType = preferredContentTypes[i];
                        hasPreferredContentTypes[i] = contentType.StartsWith(preferredContentType);

                        //Prefer Request.ContentType if it is also a preferredContentType
                        if (hasPreferredContentTypes[i] && preferredContentType == defaultContentType)
                        {
                            return(preferredContentType);
                        }
                    }
                }

                for (var i = 0; i < preferredContentTypes.Length; i++)
                {
                    if (hasPreferredContentTypes[i])
                    {
                        return(preferredContentTypes[i]);
                    }
                }

                if (acceptsAnything)
                {
                    if (hasDefaultContentType)
                    {
                        return(defaultContentType);
                    }
                    if (HostContext.Config.DefaultContentType != null)
                    {
                        return(HostContext.Config.DefaultContentType);
                    }
                }

                foreach (var contentType in acceptContentTypes)
                {
                    foreach (var customContentType in customContentTypes)
                    {
                        if (contentType.StartsWith(customContentType, StringComparison.OrdinalIgnoreCase))
                        {
                            return(customContentType);
                        }
                    }
                }
            }

            if (httpReq.ContentType.MatchesContentType(MimeTypes.Soap12))
            {
                return(MimeTypes.Soap12);
            }

            if (acceptContentTypes == null && httpReq.ContentType == MimeTypes.Soap11)
            {
                return(MimeTypes.Soap11);
            }

            //We could also send a '406 Not Acceptable', but this is allowed also
            return(HostContext.Config.DefaultContentType);
        }
Exemplo n.º 3
0
        private IHttpHandler GetHandlerForPathParts(string[] pathParts)
        {
            var pathController = pathParts[0].ToLowerInvariant();

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

                return(null);
            }

            var pathAction = pathParts[1].ToLowerInvariant();

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

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

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

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

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

#if !NETSTANDARD2_0
            case "soap11":
                return(new Soap11MetadataHandler());

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

            case "operations":
                return(new CustomResponseHandler((httpReq, httpRes) =>
                                                 HostContext.AppHost.HasAccessToMetadata(httpReq, httpRes)
                            ? HostContext.Metadata.GetOperationDtos()
                            : null, "Operations"));

            default:
                string contentType;
                if (HostContext.ContentTypes
                    .ContentTypeFormats.TryGetValue(pathController, out contentType))
                {
                    var format = ContentFormat.GetContentFormat(contentType);
                    return(new CustomMetadataHandler(contentType, format));
                }
                break;
            }
            return(null);
        }
Exemplo n.º 4
0
        public virtual IServiceStackHandler ProcessRequest(string httpMethod, string pathInfo, string filePath)
        {
            string metadata = HostContext.Config.MetadataRedirectPath.IsNullOrEmpty()
                ? "/metadata"
                : HostContext.Config.MetadataRedirectPath;

            if (pathInfo.TrimEnd('/').Equals(metadata, StringComparison.OrdinalIgnoreCase))
            {
                if (pathInfo.Length == metadata.Length)
                {
                    return(new IndexMetadataHandler());
                }
                else
                {
                    return new RedirectHttpHandler {
                               RelativeUrl = metadata
                    }
                };
            }

            var pathArray = pathInfo.ToLower().Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

            if (pathArray.Length != 2)
            {
                return(null);
            }

            switch (pathArray[0])
            {
            case "json":
                return(pathArray[1] == "metadata" ? new JsonMetadataHandler() : null);

            case "xml":
                return(pathArray[1] == "metadata" ? new XmlMetadataHandler() : null);

            case "jsv":
                return(pathArray[1] == "metadata" ? new JsvMetadataHandler() : null);

#if !NETSTANDARD2_0
            case "soap11":
                return(pathArray[1] == "metadata"
                        ? new Soap11MetadataHandler() as IServiceStackHandler
                        : (pathArray[1] == "wsdl" ? new Soap11WsdlMetadataHandler() : null));

            case "soap12":
                return(pathArray[1] == "metadata"
                        ? new Soap12MetadataHandler() as IServiceStackHandler
                        : (pathArray[1] == "wsdl" ? new Soap12WsdlMetadataHandler() : null));
#endif

            case "operations":
                return(new CustomResponseHandler((httpReq, httpRes) =>
                                                 HostContext.AppHost.HasAccessToMetadata(httpReq, httpRes)
                            ? HostContext.Metadata.GetOperationDtos()
                            : null, "Operations"));

            default:
                string contentType;
                if (HostContext.ContentTypes.ContentTypeFormats.TryGetValue(pathArray[0], out contentType) && pathArray[1] == "metadata")
                {
                    var format = ContentFormat.GetContentFormat(contentType);

                    return(new CustomMetadataHandler(contentType, format));
                }
                return(null);
            }
        }
Exemplo n.º 5
0
        private static IHttpHandler GetHandlerForPathParts(string[] pathParts)
        {
            var pathController = string.Intern(pathParts[0].ToLower());

            if (pathParts.Length == 1)
            {
                if (pathController == "soap11")
                {
                    return(new Soap11MessageSyncReplyHttpHandler());
                }
                if (pathController == "soap12")
                {
                    return(new Soap12MessageSyncReplyHttpHandler());
                }

                return(null);
            }

            var pathAction  = string.Intern(pathParts[1].ToLower());
            var requestName = pathParts.Length > 2 ? pathParts[2] : null;
            var isReply     = pathAction == "syncreply" || pathAction == "reply";
            var isOneWay    = pathAction == "asynconeway" || pathAction == "oneway";

            switch (pathController)
            {
            case "json":
                if (isReply)
                {
                    return new JsonSyncReplyHandler {
                               RequestName = requestName
                    }
                }
                ;
                if (isOneWay)
                {
                    return new JsonAsyncOneWayHandler {
                               RequestName = requestName
                    }
                }
                ;
                break;

            case "xml":
                if (isReply)
                {
                    return new XmlSyncReplyHandler {
                               RequestName = requestName
                    }
                }
                ;
                if (isOneWay)
                {
                    return new XmlAsyncOneWayHandler {
                               RequestName = requestName
                    }
                }
                ;
                break;

            case "jsv":
                if (isReply)
                {
                    return new JsvSyncReplyHandler {
                               RequestName = requestName
                    }
                }
                ;
                if (isOneWay)
                {
                    return new JsvAsyncOneWayHandler {
                               RequestName = requestName
                    }
                }
                ;
                break;

            default:
                string contentType;
                if (EndpointHost.ContentTypes.ContentTypeFormats.TryGetValue(pathController, out contentType))
                {
                    var feature = ContentFormat.ToFeature(contentType);

                    if (feature == Feature.None)
                    {
                        feature = Feature.CustomFormat;
                    }

                    if (isReply)
                    {
                        return new GenericHandler(contentType, EndpointAttributes.Reply, feature)
                               {
                                   RequestName = requestName
                               }
                    }
                    ;
                    if (isOneWay)
                    {
                        return new GenericHandler(contentType, EndpointAttributes.OneWay, feature)
                               {
                                   RequestName = requestName
                               }
                    }
                    ;
                }
                break;
            }

            return(null);
        }
    }
}
Exemplo n.º 6
0
        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 (HostContext.Config == null ||
                    HostContext.Config.MetadataTypesConfig == null)
                {
                    return(null);
                }

                if (HostContext.Config.MetadataTypesConfig.BaseUrl == null)
                {
                    HostContext.Config.MetadataTypesConfig.BaseUrl = HttpHandlerFactory.GetBaseUrl();
                }

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

            case "operations":

                return(new CustomResponseHandler((httpReq, httpRes) =>
                                                 HostContext.AppHost.HasAccessToMetadata(httpReq, httpRes)
                            ? HostContext.Metadata.GetOperationDtos()
                            : null, "Operations"));

            default:
                string contentType;
                if (HostContext.ContentTypes
                    .ContentTypeFormats.TryGetValue(pathController, out contentType))
                {
                    var format = ContentFormat.GetContentFormat(contentType);
                    return(new CustomMetadataHandler(contentType, format));
                }
                break;
            }
            return(null);
        }