示例#1
0
        public void SetCookies(CookieCollection cookies)
        {
            if (cookies == null || cookies.Count == 0)
                return;

            var buff = new StringBuilder(64);
            foreach (System.Net.Cookie cookie in cookies) //.Sorted)
                if (!cookie.Expired)
                    buff.AppendFormat("{0}; ", cookie.ToString());

            var len = buff.Length;
            if (len > 2)
            {
                buff.Length = len - 2;
                Headers["Cookie"] = buff.ToString();
            }
        }
示例#2
0
        private static CookieCollection ParseResponse(string value)
        {
            var cookies = new CookieCollection();

            Cookie cookie = null;
            var    pairs  = SplitCookieHeaderValue(value);

            for (var i = 0; i < pairs.Length; i++)
            {
                var pair = pairs[i].Trim();
                if (pair.Length == 0)
                {
                    continue;
                }

                if (pair.StartsWith("version", StringComparison.OrdinalIgnoreCase))
                {
                    if (cookie != null)
                    {
                        cookie.Version = Int32.Parse(GetValue(pair, '=', true));
                    }
                }
                else if (pair.StartsWith("expires", StringComparison.OrdinalIgnoreCase))
                {
                    var buff = new StringBuilder(GetValue(pair, '='), 32);
                    if (i < pairs.Length - 1)
                    {
                        buff.AppendFormat(", {0}", pairs[++i].Trim());
                    }

                    DateTime expires;
                    if (!DateTime.TryParseExact(
                            buff.ToString(),
                            new[] { "ddd, dd'-'MMM'-'yyyy HH':'mm':'ss 'GMT'", "r" },
                            new CultureInfo("en-US"),
                            DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal,
                            out expires))
                    {
                        expires = DateTime.Now;
                    }

                    if (cookie != null && cookie.Expires == DateTime.MinValue)
                    {
                        cookie.Expires = expires.ToLocalTime();
                    }
                }
                else if (pair.StartsWith("max-age", StringComparison.OrdinalIgnoreCase))
                {
                    var max     = Int32.Parse(GetValue(pair, '=', true));
                    var expires = DateTime.Now.AddSeconds(max);
                    if (cookie != null)
                    {
                        cookie.Expires = expires;
                    }
                }
                else if (pair.StartsWith("path", StringComparison.OrdinalIgnoreCase))
                {
                    if (cookie != null)
                    {
                        cookie.Path = GetValue(pair, '=');
                    }
                }
                else if (pair.StartsWith("domain", StringComparison.OrdinalIgnoreCase))
                {
                    if (cookie != null)
                    {
                        cookie.Domain = GetValue(pair, '=');
                    }
                }
                else if (pair.StartsWith("port", StringComparison.OrdinalIgnoreCase))
                {
                    var port = pair.Equals("port", StringComparison.OrdinalIgnoreCase)
                        ? "\"\""
                        : GetValue(pair, '=');

                    if (cookie != null)
                    {
                        cookie.Port = port;
                    }
                }
                else if (pair.StartsWith("comment", StringComparison.OrdinalIgnoreCase))
                {
                    if (cookie != null)
                    {
                        cookie.Comment = System.Net.WebUtility.UrlDecode(GetValue(pair, '='));
                    }
                }
                else if (pair.StartsWith("commenturl", StringComparison.OrdinalIgnoreCase))
                {
                    if (cookie != null)
                    {
                        cookie.CommentUri = GetValue(pair, '=', true).ToUri();
                    }
                }
                else if (pair.StartsWith("discard", StringComparison.OrdinalIgnoreCase))
                {
                    if (cookie != null)
                    {
                        cookie.Discard = true;
                    }
                }
                else if (pair.StartsWith("secure", StringComparison.OrdinalIgnoreCase))
                {
                    if (cookie != null)
                    {
                        cookie.Secure = true;
                    }
                }
                else if (pair.StartsWith("httponly", StringComparison.OrdinalIgnoreCase))
                {
                    if (cookie != null)
                    {
                        cookie.HttpOnly = true;
                    }
                }
                else
                {
                    if (cookie != null)
                    {
                        cookies.Add(cookie);
                    }

                    string name;
                    string val = String.Empty;

                    var pos = pair.IndexOf('=');
                    if (pos == -1)
                    {
                        name = pair;
                    }
                    else if (pos == pair.Length - 1)
                    {
                        name = pair.Substring(0, pos).TrimEnd(' ');
                    }
                    else
                    {
                        name = pair.Substring(0, pos).TrimEnd(' ');
                        val  = pair.Substring(pos + 1).TrimStart(' ');
                    }

                    cookie = new Cookie(name, val);
                }
            }

            if (cookie != null)
            {
                cookies.Add(cookie);
            }

            return(cookies);
        }
示例#3
0
        internal void AddHeader(string header)
        {
            var colon = header.IndexOf(':');

            if (colon == -1 || colon == 0)
            {
                _context.ErrorMessage = "Bad Request";
                _context.ErrorStatus  = 400;
                return;
            }

            var name  = header.Substring(0, colon).Trim();
            var val   = header.Substring(colon + 1).Trim();
            var lower = name.ToLowerInvariant();

            Headers.Set(name, val);
            switch (lower)
            {
            case "accept-language":
                UserLanguages = val.Split(',');     // yes, only split with a ','
                break;

            case "accept":
                AcceptTypes = val.Split(',');     // yes, only split with a ','
                break;

            case "content-length":
                try
                {
                    //TODO: max. content_length?
                    ContentLength64 = long.Parse(val.Trim());
                    if (ContentLength64 < 0)
                    {
                        _context.ErrorMessage = "Invalid Content-Length.";
                    }
                    _clSet = true;
                }
                catch
                {
                    _context.ErrorMessage = "Invalid Content-Length.";
                }

                break;

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

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

                var    cookieStrings = val.Split(',', ';');
                Cookie current       = null;
                var    version       = 0;
                foreach (var cookieString in cookieStrings)
                {
                    var str = cookieString.Trim();
                    if (str.Length == 0)
                    {
                        continue;
                    }
                    if (str.StartsWith("$Version"))
                    {
                        version = int.Parse(str.Substring(str.IndexOf('=') + 1).Unquote());
                    }
                    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();
                        var 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;
            }
        }
示例#4
0
        private static CookieCollection ParseRequest(string value)
        {
            var cookies = new CookieCollection();

            Cookie cookie = null;
            var    ver    = 0;
            var    pairs  = SplitCookieHeaderValue(value);

            foreach (var t in pairs)
            {
                var pair = t.Trim();
                if (pair.Length == 0)
                {
                    continue;
                }

                if (pair.StartsWith("$version", StringComparison.OrdinalIgnoreCase))
                {
                    ver = int.Parse(GetValue(pair, '=', true));
                }
                else if (pair.StartsWith("$path", StringComparison.OrdinalIgnoreCase))
                {
                    if (cookie != null)
                    {
                        cookie.Path = GetValue(pair, '=');
                    }
                }
                else if (pair.StartsWith("$domain", StringComparison.OrdinalIgnoreCase))
                {
                    if (cookie != null)
                    {
                        cookie.Domain = GetValue(pair, '=');
                    }
                }
                else if (pair.StartsWith("$port", StringComparison.OrdinalIgnoreCase))
                {
                    var port = pair.Equals("$port", StringComparison.OrdinalIgnoreCase)
                        ? "\"\""
                        : GetValue(pair, '=');

                    if (cookie != null)
                    {
                        cookie.Port = port;
                    }
                }
                else
                {
                    if (cookie != null)
                    {
                        cookies.Add(cookie);
                    }

                    string name;
                    var    val = String.Empty;

                    var pos = pair.IndexOf('=');
                    if (pos == -1)
                    {
                        name = pair;
                    }
                    else if (pos == pair.Length - 1)
                    {
                        name = pair.Substring(0, pos).TrimEnd(' ');
                    }
                    else
                    {
                        name = pair.Substring(0, pos).TrimEnd(' ');
                        val  = pair.Substring(pos + 1).TrimStart(' ');
                    }

                    cookie = new Cookie(name, val);
                    if (ver != 0)
                    {
                        cookie.Version = ver;
                    }
                }
            }

            if (cookie != null)
            {
                cookies.Add(cookie);
            }

            return(cookies);
        }