コード例 #1
0
        internal void AddHeader(string header)
        {
            int colon = header.IndexOf(':');

            if (colon == -1 || colon == 0)
            {
                context.ErrorMessage = "Bad Request";

                context.ErrorStatus = 400;

                return;
            }

            string name = header.Substring(0, colon).Trim();

            string val = header.Substring(colon + 1).Trim();

            string lower = name.ToLower(CultureInfo.InvariantCulture);

            headers.SetInternal(name, val);

            switch (lower)
            {
            case "accept-language":

                user_languages = val.Split(',');     // yes, only split with a ','

                break;

            case "accept":

                accept_types = val.Split(',');     // yes, only split with a ','

                break;

            case "content-length":

                try
                {
                    //TODO: max. content_length?
                    content_length = Int64.Parse(val.Trim());

                    if (content_length < 0)
                    {
                        context.ErrorMessage = "Invalid Content-Length.";
                    }

                    cl_set = true;
                }
                catch
                {
                    context.ErrorMessage = "Invalid Content-Length.";
                }

                break;

            case "referer":
                try
                {
                    referrer = new Uri(val);
                }
                catch
                {
                    referrer = new Uri("http://someone.is.screwing.with.the.headers.com/");
                }
                break;

            case "cookie":
                if (cookies == null)
                {
                    cookies = new CookieCollection();
                }

                string[] cookieStrings = val.Split(new char[] { ',', ';' });

                Cookie current = null;

                int version = 0;

                foreach (string cookieString in cookieStrings)
                {
                    string str = cookieString.Trim();

                    if (str.Length == 0)
                    {
                        continue;
                    }

                    if (str.StartsWith("$Version"))
                    {
                        version = Int32.Parse(Unquote(str.Substring(str.IndexOf('=') + 1)));
                    }
                    else if (str.StartsWith("$Path"))
                    {
                        if (current != null)
                        {
                            current.Path = str.Substring(str.IndexOf('=') + 1).Trim();
                        }
                    }
                    else if (str.StartsWith("$Domain"))
                    {
                        if (current != null)
                        {
                            current.Domain = str.Substring(str.IndexOf('=') + 1).Trim();
                        }
                    }
                    else if (str.StartsWith("$Port"))
                    {
                        if (current != null)
                        {
                            current.Port = str.Substring(str.IndexOf('=') + 1).Trim();
                        }
                    }
                    else
                    {
                        if (current != null)
                        {
                            cookies.Add(current);
                        }
                        current = new Cookie();

                        int idx = str.IndexOf('=');

                        if (idx > 0)
                        {
                            current.Name = str.Substring(0, idx).Trim();

                            current.Value = str.Substring(idx + 1).Trim();
                        }
                        else
                        {
                            current.Name = str.Trim();

                            current.Value = String.Empty;
                        }
                        current.Version = version;
                    }
                }
                if (current != null)
                {
                    cookies.Add(current);
                }
                break;
            }
        }