Example #1
0
        public static string GetResponseContentType(IRequest httpReq)
        {
            var specifiedContentType = GetQueryStringContentType(httpReq);

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

            var serverDefaultContentType = "application/json";

            var    acceptContentTypes = httpReq.AcceptTypes;
            string defaultContentType = null;

            if (HasAnyOfContentTypes(httpReq, FormUrlEncoded, MultiPartFormData))
            {
                defaultContentType = serverDefaultContentType;
            }

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

            if (acceptContentTypes != null)
            {
                foreach (var acceptsType in acceptContentTypes)
                {
                    var contentType = HttpResultFactory.GetRealContentType(acceptsType);
                    acceptsAnything = acceptsAnything || contentType == "*/*";
                }

                if (acceptsAnything)
                {
                    if (hasDefaultContentType)
                    {
                        return(defaultContentType);
                    }
                    if (serverDefaultContentType != null)
                    {
                        return(serverDefaultContentType);
                    }
                }
            }

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

            //We could also send a '406 Not Acceptable', but this is allowed also
            return(serverDefaultContentType);
        }
Example #2
0
        private static string GetResponseContentType(IRequest httpReq)
        {
            var specifiedContentType = GetQueryStringContentType(httpReq);

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

            var serverDefaultContentType = "application/json";

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

            if (HasAnyOfContentTypes(httpReq, FormUrlEncoded, MultiPartFormData))
            {
                defaultContentType = serverDefaultContentType;
            }

            var preferredContentTypes = new string[] {};

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

            if (acceptContentTypes != null)
            {
                var hasPreferredContentTypes = new bool[preferredContentTypes.Length];
                foreach (var acceptsType in acceptContentTypes)
                {
                    var contentType = HttpResultFactory.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 (serverDefaultContentType != null)
                    {
                        return(serverDefaultContentType);
                    }
                }
            }

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

            //We could also send a '406 Not Acceptable', but this is allowed also
            return(serverDefaultContentType);
        }