Exemplo n.º 1
0
        private static void Parse(Accept accept, string acceptString)
        {
            var accepts = acceptString.Split(',');

            foreach (var s in accepts.Where(x => !string.IsNullOrWhiteSpace(x)))
            {
                var acceptParts = s.Split(';');
                var mimePart = acceptParts[0];
                var args = acceptParts.Length > 1 ? acceptParts.Skip(1) : new string[0];

                var mimeParts = mimePart.Split('/');

                var mime = new MimeType
                {
                    Type = mimeParts[0],
                    Subtype = mimeParts.Length > 1 ? mimeParts[1] : "*"
                };

                var arguments = args.Select(x => x.Split('='))
                    .Where(x => x.Length == 2)
                    .ToDictionary(x => x[0].Trim(), x => x[1].Trim());

                mime.Q = arguments.ContainsKey("q") ? arguments["q"].AsDecimal() : 1;

                accept.MimeTypes.Add(mime);
            }
        }
Exemplo n.º 2
0
        public static Accept GetFromHeaders(Hashtable httpHeaders)
        {
            var accept = new Accept();
            if (!httpHeaders.ContainsKey("accept"))
            {
                accept.MimeTypes.Add(MimeType.Any);

                return accept;
            }

            var acceptString = httpHeaders["accept"] as string;

            Parse(accept, acceptString);

            return accept;
        }