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;
        }
Exemplo n.º 3
0
        internal void ParseRequest()
        {
            var request = Input.StreamReadLine(HttpServer.TIMEOUT);
            var tokens = request.Split(' ');
            if (tokens.Length != 3)
            {
                throw new Exception("invalid http request line");
            }

            Method = new HttpMethod(tokens[0].ToUpper());
            URL = GetUri(tokens[1]);
            HttpProtocolVersion = tokens[2];
            var query = HttpUtility.ParseQueryString(URL.Query);
            Query = query.AllKeys.ToDictionary(x => x, x => query[x]);

            this.HttpHeaders = HeadersHelper.ReadHeaders(this.Input, HttpServer.TIMEOUT);

            Accept = Accept.GetFromHeaders(this.HttpHeaders);
        }