示例#1
0
        internal HeaderCollection(NameValueCollection collection) : base(collection)
        {
            AccessControlRequestHeaders = TryGet("Access-Control-Request-Headers");
            AccessControlRequestMethod = TryGet("Access-Control-Request-Method");
            Authorization = TryGet("Authorization");
            Host = TryGet("Host");
            Origin = TryGet("Origin");
            Referrer = TryGet("Referrer");
            UserAgent = TryGet("User-Agent");

            ContentLength = GetContentLength();
            ContentEncoding = TryGet("Content-Encoding");

            var contentTypes = new AcceptValueCollection(TryGet("Content-Type"));
            ContentType = contentTypes.GetPreferredName();
            SetContentCharset(contentTypes);
            SetContentLanguage();

            SetAcceptValues();
            SetAcceptCharsets();
            SetAcceptEncodings();
            SetAcceptLanguages();
            SetLinks();
        }
        private string GetAcceptedMediaType(IHttpRequest request, string acceptValue)
        {
            if (String.IsNullOrWhiteSpace(acceptValue))
            {
                return null;
            }

            string acceptedMediaType = new AcceptValueCollection(acceptValue).GetPreferredName();

            return IsValidAcceptedMediaType(request, ref acceptedMediaType) ? acceptedMediaType : null;
        }
示例#3
0
        private void SetAcceptLanguages()
        {
            var acceptLanguages = new AcceptValueCollection(TryGet("Accept-Language"));
            AcceptLanguage = acceptLanguages.GetPreferredName();
            AcceptLanguages = acceptLanguages.AcceptedNames.ToArray();

            if (String.IsNullOrEmpty(AcceptLanguage))
            {
                return;
            }

            if (AcceptLanguage == "*")
            {
                AcceptLanguageCulture = Thread.CurrentThread.CurrentCulture;
                return;
            }

            try
            {
                AcceptLanguageCulture = new CultureInfo(AcceptLanguage);
            }
            catch (Exception)
            {
                throw new HttpResponseException(HttpStatusCode.NotAcceptable, Resources.Global.NonAcceptedContentLanguage);
            }
        }
示例#4
0
 private void SetAcceptEncodings()
 {
     var acceptEncodings = new AcceptValueCollection(TryGet("Accept-Encoding"));
     AcceptEncoding = acceptEncodings.GetPreferredName();
     AcceptEncodings = acceptEncodings.AcceptedNames.ToArray();
 }
示例#5
0
        private void SetAcceptCharsets()
        {
            var acceptCharsets = new AcceptValueCollection(TryGet("Accept-Charset"));
            string acceptCharset = acceptCharsets.GetPreferredName();           

            if (!String.IsNullOrEmpty(acceptCharset) && acceptCharset.IndexOf('*') < 0)
            {
                AcceptCharsets = acceptCharsets.AcceptedNames.ToArray();
            }
            else
            {
                acceptCharset = Utf8Charset;
                AcceptCharsets = new[] { Utf8Charset };
            }

            try
            {
                AcceptCharsetEncoding = Encoding.GetEncoding(acceptCharset);
                AcceptCharset = AcceptCharsetEncoding.WebName;
            }
            catch (Exception)
            {
                throw new HttpResponseException(HttpStatusCode.NotAcceptable, Resources.Global.NonAcceptedContentCharset);
            }
        }
示例#6
0
        private void SetAcceptValues()
        {
            var acceptValues = new AcceptValueCollection(TryGet("Accept"));
            AcceptType = acceptValues.GetPreferredName();
            AcceptTypes = acceptValues.AcceptedNames.ToArray();

            AcceptValue? acceptedTypeValue = acceptValues.GetPreferredValue();
            AcceptVersion = acceptedTypeValue.HasValue ? acceptedTypeValue.Value.Version : 0;
        }
示例#7
0
        private void SetContentCharset(AcceptValueCollection contentTypes)
        {
            string contentCharset;

            if (!String.IsNullOrEmpty(ContentType) && ContentType.IndexOf('*') < 0)
            {
                contentCharset = contentTypes.GetPreferredValues(ContentType)
                                     .Where(v => v.Charset != null)
                                     .Select(v => v.Charset).FirstOrDefault() ?? Utf8Charset;
            }
            else
            {
                contentCharset = Utf8Charset;
            }

            try
            {
                ContentCharsetEncoding = Encoding.GetEncoding(contentCharset);
                ContentCharset = ContentCharsetEncoding.WebName;
            }
            catch (Exception)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest, Resources.Global.UnsupportedContentCharset);
            }
        }