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

            var headers = Headers;
            foreach (var cookie in cookies.Sorted)
                headers.Add("Set-Cookie", cookie.ToClientString());
        }
コード例 #2
0
        private static CookieCollection parseResponse(string value)
        {
            var cookies = new CookieCollection ();

              Cookie cookie = null;
              var pairs = splitCookieHeaderValue (value);
              for (int 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.GetValueInternal ("=").Trim ('"'));
            }
            else if (pair.StartsWith ("expires", StringComparison.InvariantCultureIgnoreCase)) {
              var buffer = new StringBuilder (pair.GetValueInternal ("="), 32);
              if (i < pairs.Length - 1)
            buffer.AppendFormat (", {0}", pairs [++i].Trim ());

              DateTime expires;
              if (!DateTime.TryParseExact (
            buffer.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.GetValueInternal ("=").Trim ('"'));
              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.GetValueInternal ("=");
            }
            else if (pair.StartsWith ("domain", StringComparison.InvariantCultureIgnoreCase)) {
              if (cookie != null)
            cookie.Domain = pair.GetValueInternal ("=");
            }
            else if (pair.StartsWith ("port", StringComparison.InvariantCultureIgnoreCase)) {
              var port = pair.Equals ("port", StringComparison.InvariantCultureIgnoreCase)
                     ? "\"\""
                     : pair.GetValueInternal ("=");

              if (cookie != null)
            cookie.Port = port;
            }
            else if (pair.StartsWith ("comment", StringComparison.InvariantCultureIgnoreCase)) {
              if (cookie != null)
            cookie.Comment = pair.GetValueInternal ("=").UrlDecode ();
            }
            else if (pair.StartsWith ("commenturl", StringComparison.InvariantCultureIgnoreCase)) {
              if (cookie != null)
            cookie.CommentUri = pair.GetValueInternal ("=").Trim ('"').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;
        }
コード例 #3
0
 private void init()
 {
     _compression = CompressionMethod.None;
     _cookies = new CookieCollection();
     _forConn = new object();
     _forEvent = new object();
     _forSend = new object();
     _messageEventQueue = new Queue<MessageEventArgs>();
     _forMessageEventQueue = ((ICollection)_messageEventQueue).SyncRoot;
     _readyState = WebSocketState.Connecting;
 }
コード例 #4
0
        private static CookieCollection parseRequest(string value)
        {
            var cookies = new CookieCollection ();

              Cookie cookie = null;
              var version = 0;
              var pairs = splitCookieHeaderValue (value);
              for (int i = 0; i < pairs.Length; i++) {
            var pair = pairs [i].Trim ();
            if (pair.Length == 0)
              continue;

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

              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 (version != 0)
            cookie.Version = version;
            }
              }

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

              return cookies;
        }
コード例 #5
0
 internal void SetOrRemove(CookieCollection cookies)
 {
     foreach (Cookie cookie in cookies)
     SetOrRemove (cookie);
 }
コード例 #6
0
        /// <summary>
        /// Adds the specified <paramref name="cookies"/> to the collection.
        /// </summary>
        /// <param name="cookies">
        /// A <see cref="CookieCollection"/> that contains the cookies to add.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="cookies"/> is <see langword="null"/>.
        /// </exception>
        public void Add(CookieCollection cookies)
        {
            if (cookies == null)
            throw new ArgumentNullException ("cookies");

              foreach (Cookie cookie in cookies)
            Add (cookie);
        }
コード例 #7
0
        private static CookieCollection parseResponse(string value)
        {
            var cookies = new CookieCollection();

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

            for (int 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 = int.Parse(pair.GetValueInternal("=").Trim('"'));
                    }
                }
                else if (pair.StartsWith("expires", StringComparison.OrdinalIgnoreCase))
                {
                    var buffer = new StringBuilder(pair.GetValueInternal("="), 32);
                    if (i < pairs.Length - 1)
                    {
                        buffer.AppendFormat(", {0}", pairs[++i].Trim());
                    }

                    DateTime expires;
                    if (!DateTime.TryParseExact(
                            buffer.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     = int.Parse(pair.GetValueInternal("=").Trim('"'));
                    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.GetValueInternal("=");
                    }
                }
                else if (pair.StartsWith("domain", StringComparison.OrdinalIgnoreCase))
                {
                    if (cookie != null)
                    {
                        cookie.Domain = pair.GetValueInternal("=");
                    }
                }
                else if (pair.StartsWith("port", StringComparison.OrdinalIgnoreCase))
                {
                    var port = pair.Equals("port", StringComparison.OrdinalIgnoreCase)
                               ? "\"\""
                               : pair.GetValueInternal("=");

                    if (cookie != null)
                    {
                        cookie.Port = port;
                    }
                }
                else if (pair.StartsWith("comment", StringComparison.OrdinalIgnoreCase))
                {
                    if (cookie != null)
                    {
                        cookie.Comment = pair.GetValueInternal("=").UrlDecode();
                    }
                }
                else if (pair.StartsWith("commenturl", StringComparison.OrdinalIgnoreCase))
                {
                    if (cookie != null)
                    {
                        cookie.CommentUri = pair.GetValueInternal("=").Trim('"').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);
        }
コード例 #8
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.ToLowerInvariant();

            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 "content-type":
            {
                var contents = val.Split(';');
                foreach (var content in contents)
                {
                    var tmp = content.Trim();
                    if (tmp.StartsWith("charset"))
                    {
                        var charset = tmp.GetValue("=");
                        if (charset != null && charset.Length > 0)
                        {
                            try
                            {
                                // Support upnp/dlna devices - CONTENT-TYPE: text/xml ; charset="utf-8"\r\n
                                charset = charset.Trim('"');
                                var index = charset.IndexOf('"');
                                if (index != -1)
                                {
                                    charset = charset.Substring(0, index);
                                }

                                content_encoding = Encoding.GetEncoding(charset);
                            }
                            catch
                            {
                                context.ErrorMessage = "Invalid Content-Type header: " + charset;
                            }
                        }

                        break;
                    }
                }
            }
            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;
            }
        }