示例#1
0
        protected override bool ValidateCookies(CookieCollection request, CookieCollection response)
        {
            foreach (Cookie cookie in request)
              {
            cookie.Expired = true;
            response.Add (cookie);
              }

              return true;
        }
    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.InvariantCultureIgnoreCase)) {
          if (cookie != null)
            cookie.Version = Int32.Parse (pair.GetValue ('=', true));
        }
        else if (pair.StartsWith ("expires", StringComparison.InvariantCultureIgnoreCase)) {
          var buff = new StringBuilder (pair.GetValue ('='), 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" },
            CultureInfo.CreateSpecificCulture ("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.InvariantCultureIgnoreCase)) {
          var max = Int32.Parse (pair.GetValue ('=', true));
          var expires = DateTime.Now.AddSeconds ((double) max);
          if (cookie != null)
            cookie.Expires = expires;
        }
        else if (pair.StartsWith ("path", StringComparison.InvariantCultureIgnoreCase)) {
          if (cookie != null)
            cookie.Path = pair.GetValue ('=');
        }
        else if (pair.StartsWith ("domain", StringComparison.InvariantCultureIgnoreCase)) {
          if (cookie != null)
            cookie.Domain = pair.GetValue ('=');
        }
        else if (pair.StartsWith ("port", StringComparison.InvariantCultureIgnoreCase)) {
          var port = pair.Equals ("port", StringComparison.InvariantCultureIgnoreCase)
                     ? "\"\""
                     : pair.GetValue ('=');

          if (cookie != null)
            cookie.Port = port;
        }
        else if (pair.StartsWith ("comment", StringComparison.InvariantCultureIgnoreCase)) {
          if (cookie != null)
            cookie.Comment = pair.GetValue ('=').UrlDecode ();
        }
        else if (pair.StartsWith ("commenturl", StringComparison.InvariantCultureIgnoreCase)) {
          if (cookie != null)
            cookie.CommentUri = pair.GetValue ('=', true).ToUri ();
        }
        else if (pair.StartsWith ("discard", StringComparison.InvariantCultureIgnoreCase)) {
          if (cookie != null)
            cookie.Discard = true;
        }
        else if (pair.StartsWith ("secure", StringComparison.InvariantCultureIgnoreCase)) {
          if (cookie != null)
            cookie.Secure = true;
        }
        else if (pair.StartsWith ("httponly", StringComparison.InvariantCultureIgnoreCase)) {
          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;
    }
        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(pair.GetValue('=', true));
                    }
                }
                else if (pair.StartsWith("expires", StringComparison.OrdinalIgnoreCase))
                {
                    var buff = new StringBuilder(pair.GetValue('='), 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" },
                            CultureInfo.InvariantCulture,
                            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(pair.GetValue('=', true));
                    var expires = DateTime.Now.AddSeconds((double)max);
                    if (cookie != null)
                    {
                        cookie.Expires = expires;
                    }
                }
                else if (pair.StartsWith("path", StringComparison.OrdinalIgnoreCase))
                {
                    if (cookie != null)
                    {
                        cookie.Path = pair.GetValue('=');
                    }
                }
                else if (pair.StartsWith("domain", StringComparison.OrdinalIgnoreCase))
                {
                    if (cookie != null)
                    {
                        cookie.Domain = pair.GetValue('=');
                    }
                }
                else if (pair.StartsWith("port", StringComparison.OrdinalIgnoreCase))
                {
                    var port = pair.Equals("port", StringComparison.OrdinalIgnoreCase)
                     ? "\"\""
                     : pair.GetValue('=');

                    if (cookie != null)
                    {
                        cookie.Port = port;
                    }
                }
                else if (pair.StartsWith("comment", StringComparison.OrdinalIgnoreCase))
                {
                    if (cookie != null)
                    {
                        cookie.Comment = pair.GetValue('=').UrlDecode();
                    }
                }
                else if (pair.StartsWith("commenturl", StringComparison.OrdinalIgnoreCase))
                {
                    if (cookie != null)
                    {
                        cookie.CommentUri = pair.GetValue('=', 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);
        }
    private static CookieCollection parseRequest (string value)
    {
      var cookies = new CookieCollection ();

      Cookie cookie = null;
      var ver = 0;
      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.InvariantCultureIgnoreCase)) {
          ver = Int32.Parse (pair.GetValue ('=', true));
        }
        else if (pair.StartsWith ("$path", StringComparison.InvariantCultureIgnoreCase)) {
          if (cookie != null)
            cookie.Path = pair.GetValue ('=');
        }
        else if (pair.StartsWith ("$domain", StringComparison.InvariantCultureIgnoreCase)) {
          if (cookie != null)
            cookie.Domain = pair.GetValue ('=');
        }
        else if (pair.StartsWith ("$port", StringComparison.InvariantCultureIgnoreCase)) {
          var port = pair.Equals ("$port", StringComparison.InvariantCultureIgnoreCase)
                     ? "\"\""
                     : pair.GetValue ('=');

          if (cookie != null)
            cookie.Port = port;
        }
        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 (ver != 0)
            cookie.Version = ver;
        }
      }

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

      return cookies;
    }
        private static CookieCollection parseResponse(string value)
        {
            var ret = new CookieCollection();

            Cookie cookie = null;

            var caseInsensitive = StringComparison.InvariantCultureIgnoreCase;
            var pairs           = value.SplitHeaderValue(',', ';').ToList();

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

                var idx = pair.IndexOf('=');
                if (idx == -1)
                {
                    if (cookie == null)
                    {
                        continue;
                    }

                    if (pair.Equals("port", caseInsensitive))
                    {
                        cookie.Port = "\"\"";
                        continue;
                    }

                    if (pair.Equals("discard", caseInsensitive))
                    {
                        cookie.Discard = true;
                        continue;
                    }

                    if (pair.Equals("secure", caseInsensitive))
                    {
                        cookie.Secure = true;
                        continue;
                    }

                    if (pair.Equals("httponly", caseInsensitive))
                    {
                        cookie.HttpOnly = true;
                        continue;
                    }

                    continue;
                }

                if (idx == 0)
                {
                    if (cookie != null)
                    {
                        ret.Add(cookie);
                        cookie = null;
                    }

                    continue;
                }

                var name = pair.Substring(0, idx).TrimEnd(' ');
                var val  = idx < pair.Length - 1
                  ? pair.Substring(idx + 1).TrimStart(' ')
                  : String.Empty;

                if (name.Equals("version", caseInsensitive))
                {
                    if (cookie == null)
                    {
                        continue;
                    }

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

                    cookie.Version = Int32.Parse(val.Unquote());
                    continue;
                }

                if (name.Equals("expires", caseInsensitive))
                {
                    if (val.Length == 0)
                    {
                        continue;
                    }

                    if (i == pairs.Count - 1)
                    {
                        break;
                    }

                    i++;

                    if (cookie == null)
                    {
                        continue;
                    }

                    if (cookie.Expires != DateTime.MinValue)
                    {
                        continue;
                    }

                    var buff = new StringBuilder(val, 32);
                    buff.AppendFormat(", {0}", pairs[i].Trim());

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

                    cookie.Expires = expires.ToLocalTime();
                    continue;
                }

                if (name.Equals("max-age", caseInsensitive))
                {
                    if (cookie == null)
                    {
                        continue;
                    }

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

                    var max     = Int32.Parse(val.Unquote());
                    var expires = DateTime.Now.AddSeconds((double)max);
                    cookie.Expires = expires;

                    continue;
                }

                if (name.Equals("path", caseInsensitive))
                {
                    if (cookie == null)
                    {
                        continue;
                    }

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

                    cookie.Path = val;
                    continue;
                }

                if (name.Equals("domain", caseInsensitive))
                {
                    if (cookie == null)
                    {
                        continue;
                    }

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

                    cookie.Domain = val;
                    continue;
                }

                if (name.Equals("port", caseInsensitive))
                {
                    if (cookie == null)
                    {
                        continue;
                    }

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

                    cookie.Port = val;
                    continue;
                }

                if (name.Equals("comment", caseInsensitive))
                {
                    if (cookie == null)
                    {
                        continue;
                    }

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

                    cookie.Comment = urlDecode(val, Encoding.UTF8);
                    continue;
                }

                if (name.Equals("commenturl", caseInsensitive))
                {
                    if (cookie == null)
                    {
                        continue;
                    }

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

                    cookie.CommentUri = val.Unquote().ToUri();
                    continue;
                }

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

                cookie = new Cookie(name, val);
            }

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

            return(ret);
        }
        private static CookieCollection parseRequest(string value)
        {
            var cookies = new CookieCollection();

            Cookie cookie = null;
            var    ver    = 0;
            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))
                {
                    ver = Int32.Parse(pair.GetValue('=', true));
                }
                else if (pair.StartsWith("$path", StringComparison.OrdinalIgnoreCase))
                {
                    if (cookie != null)
                    {
                        cookie.Path = pair.GetValue('=');
                    }
                }
                else if (pair.StartsWith("$domain", StringComparison.OrdinalIgnoreCase))
                {
                    if (cookie != null)
                    {
                        cookie.Domain = pair.GetValue('=');
                    }
                }
                else if (pair.StartsWith("$port", StringComparison.OrdinalIgnoreCase))
                {
                    var port = pair.Equals("$port", StringComparison.OrdinalIgnoreCase)
                     ? "\"\""
                     : pair.GetValue('=');

                    if (cookie != null)
                    {
                        cookie.Port = port;
                    }
                }
                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 (ver != 0)
                    {
                        cookie.Version = ver;
                    }
                }
            }

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

            return(cookies);
        }
示例#7
0
        private static CookieCollection parseRequest(string value)
        {
            CookieCollection cookieCollection = new CookieCollection();
            Cookie           cookie           = null;
            int num = 0;

            string[] array = splitCookieHeaderValue(value);
            for (int i = 0; i < array.Length; i++)
            {
                string text = array[i].Trim();
                if (text.Length == 0)
                {
                    continue;
                }
                if (text.StartsWith("$version", StringComparison.InvariantCultureIgnoreCase))
                {
                    num = int.Parse(text.GetValue('=', unquote: true));
                    continue;
                }
                if (text.StartsWith("$path", StringComparison.InvariantCultureIgnoreCase))
                {
                    if (cookie != null)
                    {
                        cookie.Path = text.GetValue('=');
                    }
                    continue;
                }
                if (text.StartsWith("$domain", StringComparison.InvariantCultureIgnoreCase))
                {
                    if (cookie != null)
                    {
                        cookie.Domain = text.GetValue('=');
                    }
                    continue;
                }
                if (text.StartsWith("$port", StringComparison.InvariantCultureIgnoreCase))
                {
                    string port = ((!text.Equals("$port", StringComparison.InvariantCultureIgnoreCase)) ? text.GetValue('=') : "\"\"");
                    if (cookie != null)
                    {
                        cookie.Port = port;
                    }
                    continue;
                }
                if (cookie != null)
                {
                    cookieCollection.Add(cookie);
                }
                string value2 = string.Empty;
                int    num2   = text.IndexOf('=');
                string name;
                if (num2 == -1)
                {
                    name = text;
                }
                else if (num2 == text.Length - 1)
                {
                    name = text.Substring(0, num2).TrimEnd(' ');
                }
                else
                {
                    name   = text.Substring(0, num2).TrimEnd(' ');
                    value2 = text.Substring(num2 + 1).TrimStart(' ');
                }
                cookie = new Cookie(name, value2);
                if (num != 0)
                {
                    cookie.Version = num;
                }
            }
            if (cookie != null)
            {
                cookieCollection.Add(cookie);
            }
            return(cookieCollection);
        }
        private static CookieCollection parseRequest(string value)
        {
            var ret = new CookieCollection();

            Cookie cookie = null;
            var    ver    = 0;

            var caseInsensitive = StringComparison.InvariantCultureIgnoreCase;
            var pairs           = value.SplitHeaderValue(',', ';').ToList();

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

                var idx = pair.IndexOf('=');
                if (idx == -1)
                {
                    if (cookie == null)
                    {
                        continue;
                    }

                    if (pair.Equals("$port", caseInsensitive))
                    {
                        cookie.Port = "\"\"";
                        continue;
                    }

                    continue;
                }

                if (idx == 0)
                {
                    if (cookie != null)
                    {
                        ret.Add(cookie);
                        cookie = null;
                    }

                    continue;
                }

                var name = pair.Substring(0, idx).TrimEnd(' ');
                var val  = idx < pair.Length - 1
                  ? pair.Substring(idx + 1).TrimStart(' ')
                  : String.Empty;

                if (name.Equals("$version", caseInsensitive))
                {
                    ver = val.Length > 0 ? Int32.Parse(val.Unquote()) : 0;
                    continue;
                }

                if (name.Equals("$path", caseInsensitive))
                {
                    if (cookie == null)
                    {
                        continue;
                    }

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

                    cookie.Path = val;
                    continue;
                }

                if (name.Equals("$domain", caseInsensitive))
                {
                    if (cookie == null)
                    {
                        continue;
                    }

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

                    cookie.Domain = val;
                    continue;
                }

                if (name.Equals("$port", caseInsensitive))
                {
                    if (cookie == null)
                    {
                        continue;
                    }

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

                    cookie.Port = val;
                    continue;
                }

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

                cookie = new Cookie(name, val);

                if (ver != 0)
                {
                    cookie.Version = ver;
                }
            }

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

            return(ret);
        }
示例#9
0
        private static CookieCollection parseResponse(string value)
        {
            CookieCollection cookieCollection = new CookieCollection();
            Cookie           cookie           = null;

            string[] array = splitCookieHeaderValue(value);
            for (int i = 0; i < array.Length; i++)
            {
                string text = array[i].Trim();
                if (text.Length == 0)
                {
                    continue;
                }
                if (text.StartsWith("version", StringComparison.InvariantCultureIgnoreCase))
                {
                    if (cookie != null)
                    {
                        cookie.Version = int.Parse(text.GetValue('=', unquote: true));
                    }
                    continue;
                }
                if (text.StartsWith("expires", StringComparison.InvariantCultureIgnoreCase))
                {
                    StringBuilder stringBuilder = new StringBuilder(text.GetValue('='), 32);
                    if (i < array.Length - 1)
                    {
                        stringBuilder.AppendFormat(", {0}", array[++i].Trim());
                    }
                    if (!DateTime.TryParseExact(stringBuilder.ToString(), new string[2] {
                        "ddd, dd'-'MMM'-'yyyy HH':'mm':'ss 'GMT'", "r"
                    }, CultureInfo.CreateSpecificCulture("en-US"), DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal, out var result))
                    {
                        result = DateTime.Now;
                    }
                    if (cookie != null && cookie.Expires == DateTime.MinValue)
                    {
                        cookie.Expires = result.ToLocalTime();
                    }
                    continue;
                }
                if (text.StartsWith("max-age", StringComparison.InvariantCultureIgnoreCase))
                {
                    int      num     = int.Parse(text.GetValue('=', unquote: true));
                    DateTime expires = DateTime.Now.AddSeconds(num);
                    if (cookie != null)
                    {
                        cookie.Expires = expires;
                    }
                    continue;
                }
                if (text.StartsWith("path", StringComparison.InvariantCultureIgnoreCase))
                {
                    if (cookie != null)
                    {
                        cookie.Path = text.GetValue('=');
                    }
                    continue;
                }
                if (text.StartsWith("domain", StringComparison.InvariantCultureIgnoreCase))
                {
                    if (cookie != null)
                    {
                        cookie.Domain = text.GetValue('=');
                    }
                    continue;
                }
                if (text.StartsWith("port", StringComparison.InvariantCultureIgnoreCase))
                {
                    string port = ((!text.Equals("port", StringComparison.InvariantCultureIgnoreCase)) ? text.GetValue('=') : "\"\"");
                    if (cookie != null)
                    {
                        cookie.Port = port;
                    }
                    continue;
                }
                if (text.StartsWith("comment", StringComparison.InvariantCultureIgnoreCase))
                {
                    if (cookie != null)
                    {
                        cookie.Comment = text.GetValue('=').UrlDecode();
                    }
                    continue;
                }
                if (text.StartsWith("commenturl", StringComparison.InvariantCultureIgnoreCase))
                {
                    if (cookie != null)
                    {
                        cookie.CommentUri = text.GetValue('=', unquote: true).ToUri();
                    }
                    continue;
                }
                if (text.StartsWith("discard", StringComparison.InvariantCultureIgnoreCase))
                {
                    if (cookie != null)
                    {
                        cookie.Discard = true;
                    }
                    continue;
                }
                if (text.StartsWith("secure", StringComparison.InvariantCultureIgnoreCase))
                {
                    if (cookie != null)
                    {
                        cookie.Secure = true;
                    }
                    continue;
                }
                if (text.StartsWith("httponly", StringComparison.InvariantCultureIgnoreCase))
                {
                    if (cookie != null)
                    {
                        cookie.HttpOnly = true;
                    }
                    continue;
                }
                if (cookie != null)
                {
                    cookieCollection.Add(cookie);
                }
                string value2 = string.Empty;
                int    num2   = text.IndexOf('=');
                string name;
                if (num2 == -1)
                {
                    name = text;
                }
                else if (num2 == text.Length - 1)
                {
                    name = text.Substring(0, num2).TrimEnd(' ');
                }
                else
                {
                    name   = text.Substring(0, num2).TrimEnd(' ');
                    value2 = text.Substring(num2 + 1).TrimStart(' ');
                }
                cookie = new Cookie(name, value2);
            }
            if (cookie != null)
            {
                cookieCollection.Add(cookie);
            }
            return(cookieCollection);
        }
示例#10
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;
            }
        }
示例#11
0
        static CookieCollection ParseRequest(string value)
        {
            var cookies = new CookieCollection();

            Cookie cookie  = null;
            int    version = 0;

            string [] pairs = Split(value).ToArray();
            for (int i = 0; i < pairs.Length; i++)
            {
                string pair = pairs [i].Trim();
                if (pair.IsEmpty())
                {
                    continue;
                }

                if (pair.StartsWith("$version", StringComparison.InvariantCultureIgnoreCase))
                {
                    version = Int32.Parse(pair.GetValueInternal("=").Trim('"'));
                }
                else if (pair.StartsWith("$path", StringComparison.InvariantCultureIgnoreCase))
                {
                    if (!cookie.IsNull())
                    {
                        cookie.Path = pair.GetValueInternal("=");
                    }
                }
                else if (pair.StartsWith("$domain", StringComparison.InvariantCultureIgnoreCase))
                {
                    if (!cookie.IsNull())
                    {
                        cookie.Domain = pair.GetValueInternal("=");
                    }
                }
                else if (pair.StartsWith("$port", StringComparison.InvariantCultureIgnoreCase))
                {
                    var port = pair.Equals("$port", StringComparison.InvariantCultureIgnoreCase)
                                                 ? "\"\""
                                                 : pair.GetValueInternal("=");

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

                    string name;
                    string val = String.Empty;
                    int    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 (version != 0)
                    {
                        cookie.Version = version;
                    }
                }
            }

            if (!cookie.IsNull())
            {
                cookies.Add(cookie);
            }

            return(cookies);
        }
示例#12
0
        private static CookieCollection parseResponse(string value)
        {
            DateTime         now;
            string           str;
            CookieCollection cookieCollections = new CookieCollection();
            Cookie           localTime         = null;

            string[] strArrays = CookieCollection.splitCookieHeaderValue(value);
            for (int i = 0; i < (int)strArrays.Length; i++)
            {
                string str1 = strArrays[i].Trim();
                if (str1.Length != 0)
                {
                    if (str1.StartsWith("version", StringComparison.InvariantCultureIgnoreCase))
                    {
                        if (localTime != null)
                        {
                            localTime.Version = int.Parse(str1.GetValue('=', true));
                        }
                    }
                    else if (str1.StartsWith("expires", StringComparison.InvariantCultureIgnoreCase))
                    {
                        StringBuilder stringBuilder = new StringBuilder(str1.GetValue('='), 32);
                        if (i < (int)strArrays.Length - 1)
                        {
                            int num = i + 1;
                            i = num;
                            stringBuilder.AppendFormat(", {0}", strArrays[num].Trim());
                        }
                        if (!DateTime.TryParseExact(stringBuilder.ToString(), new string[] { "ddd, dd'-'MMM'-'yyyy HH':'mm':'ss 'GMT'", "r" }, CultureInfo.CreateSpecificCulture("en-US"), DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal, out now))
                        {
                            now = DateTime.Now;
                        }
                        if ((localTime == null ? false : localTime.Expires == DateTime.MinValue))
                        {
                            localTime.Expires = now.ToLocalTime();
                        }
                    }
                    else if (str1.StartsWith("max-age", StringComparison.InvariantCultureIgnoreCase))
                    {
                        int      num1     = int.Parse(str1.GetValue('=', true));
                        DateTime dateTime = DateTime.Now.AddSeconds((double)num1);
                        if (localTime != null)
                        {
                            localTime.Expires = dateTime;
                        }
                    }
                    else if (str1.StartsWith("path", StringComparison.InvariantCultureIgnoreCase))
                    {
                        if (localTime != null)
                        {
                            localTime.Path = str1.GetValue('=');
                        }
                    }
                    else if (str1.StartsWith("domain", StringComparison.InvariantCultureIgnoreCase))
                    {
                        if (localTime != null)
                        {
                            localTime.Domain = str1.GetValue('=');
                        }
                    }
                    else if (str1.StartsWith("port", StringComparison.InvariantCultureIgnoreCase))
                    {
                        string str2 = (str1.Equals("port", StringComparison.InvariantCultureIgnoreCase) ? "\"\"" : str1.GetValue('='));
                        if (localTime != null)
                        {
                            localTime.Port = str2;
                        }
                    }
                    else if (str1.StartsWith("comment", StringComparison.InvariantCultureIgnoreCase))
                    {
                        if (localTime != null)
                        {
                            localTime.Comment = str1.GetValue('=').UrlDecode();
                        }
                    }
                    else if (str1.StartsWith("commenturl", StringComparison.InvariantCultureIgnoreCase))
                    {
                        if (localTime != null)
                        {
                            localTime.CommentUri = str1.GetValue('=', true).ToUri();
                        }
                    }
                    else if (str1.StartsWith("discard", StringComparison.InvariantCultureIgnoreCase))
                    {
                        if (localTime != null)
                        {
                            localTime.Discard = true;
                        }
                    }
                    else if (str1.StartsWith("secure", StringComparison.InvariantCultureIgnoreCase))
                    {
                        if (localTime != null)
                        {
                            localTime.Secure = true;
                        }
                    }
                    else if (!str1.StartsWith("httponly", StringComparison.InvariantCultureIgnoreCase))
                    {
                        if (localTime != null)
                        {
                            cookieCollections.Add(localTime);
                        }
                        string empty = string.Empty;
                        int    num2  = str1.IndexOf('=');
                        if (num2 == -1)
                        {
                            str = str1;
                        }
                        else if (num2 != str1.Length - 1)
                        {
                            str   = str1.Substring(0, num2).TrimEnd(new char[] { ' ' });
                            empty = str1.Substring(num2 + 1).TrimStart(new char[] { ' ' });
                        }
                        else
                        {
                            str = str1.Substring(0, num2).TrimEnd(new char[] { ' ' });
                        }
                        localTime = new Cookie(str, empty);
                    }
                    else if (localTime != null)
                    {
                        localTime.HttpOnly = true;
                    }
                }
            }
            if (localTime != null)
            {
                cookieCollections.Add(localTime);
            }
            return(cookieCollections);
        }
示例#13
0
        private static CookieCollection parseRequest(string value)
        {
            string           str;
            CookieCollection cookieCollections = new CookieCollection();
            Cookie           cookie            = null;
            int num = 0;

            string[] strArrays = CookieCollection.splitCookieHeaderValue(value);
            for (int i = 0; i < (int)strArrays.Length; i++)
            {
                string str1 = strArrays[i].Trim();
                if (str1.Length != 0)
                {
                    if (str1.StartsWith("$version", StringComparison.InvariantCultureIgnoreCase))
                    {
                        num = int.Parse(str1.GetValue('=', true));
                    }
                    else if (str1.StartsWith("$path", StringComparison.InvariantCultureIgnoreCase))
                    {
                        if (cookie != null)
                        {
                            cookie.Path = str1.GetValue('=');
                        }
                    }
                    else if (str1.StartsWith("$domain", StringComparison.InvariantCultureIgnoreCase))
                    {
                        if (cookie != null)
                        {
                            cookie.Domain = str1.GetValue('=');
                        }
                    }
                    else if (!str1.StartsWith("$port", StringComparison.InvariantCultureIgnoreCase))
                    {
                        if (cookie != null)
                        {
                            cookieCollections.Add(cookie);
                        }
                        string empty = string.Empty;
                        int    num1  = str1.IndexOf('=');
                        if (num1 == -1)
                        {
                            str = str1;
                        }
                        else if (num1 != str1.Length - 1)
                        {
                            str   = str1.Substring(0, num1).TrimEnd(new char[] { ' ' });
                            empty = str1.Substring(num1 + 1).TrimStart(new char[] { ' ' });
                        }
                        else
                        {
                            str = str1.Substring(0, num1).TrimEnd(new char[] { ' ' });
                        }
                        cookie = new Cookie(str, empty);
                        if (num != 0)
                        {
                            cookie.Version = num;
                        }
                    }
                    else
                    {
                        string str2 = (str1.Equals("$port", StringComparison.InvariantCultureIgnoreCase) ? "\"\"" : str1.GetValue('='));
                        if (cookie != null)
                        {
                            cookie.Port = str2;
                        }
                    }
                }
            }
            if (cookie != null)
            {
                cookieCollections.Add(cookie);
            }
            return(cookieCollections);
        }