Ejemplo n.º 1
0
        private void ParseCookies(string val)
        {
            if (_cookies == null)
            {
                _cookies = new CookieList();
            }

            var cookieStrings = val.SplitByAny(';', ',')
                                .Where(x => !string.IsNullOrEmpty(x));
            Cookie current = null;
            var    version = 0;

            foreach (var str in cookieStrings)
            {
                if (str.StartsWith("$Version"))
                {
                    version = int.Parse(str.Substring(str.IndexOf('=') + 1).Unquote(), CultureInfo.InvariantCulture);
                }
                else if (str.StartsWith("$Path") && current != null)
                {
                    current.Path = str.Substring(str.IndexOf('=') + 1).Trim();
                }
                else if (str.StartsWith("$Domain") && current != null)
                {
                    current.Domain = str.Substring(str.IndexOf('=') + 1).Trim();
                }
                else if (str.StartsWith("$Port") && 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);
            }
        }
Ejemplo n.º 2
0
        public TestRequest(HttpRequestMessage clientRequest)
        {
            _content = Validate.NotNull(nameof(clientRequest), clientRequest).Content;

            var headers = new NameValueCollection();

            foreach (var pair in clientRequest.Headers)
            {
                var values = pair.Value.ToArray();
                switch (values.Length)
                {
                case 0:
                    headers.Add(pair.Key, string.Empty);
                    break;

                case 1:
                    headers.Add(pair.Key, values[0]);
                    break;

                default:
                    foreach (var value in values)
                    {
                        headers.Add(pair.Key, value);
                    }

                    break;
                }

                switch (pair.Key)
                {
                case HttpHeaderNames.Cookie:
                    Cookies = CookieList.Parse(string.Join(",", values));
                    break;
                }
            }

            Headers = headers;
            if (Cookies == null)
            {
                Cookies = new CookieList();
            }

            ProtocolVersion = clientRequest.Version;
            KeepAlive       = !(clientRequest.Headers.ConnectionClose ?? true);
            RawUrl          = clientRequest.RequestUri.PathAndQuery;
            QueryString     = UrlEncodedDataParser.Parse(clientRequest.RequestUri.Query, true, true);
            HttpMethod      = clientRequest.Method.ToString();
            HttpVerb        = HttpMethodToVerb(clientRequest.Method);
            Url             = clientRequest.RequestUri;
            HasEntityBody   = _content != null;
            ContentEncoding = Encoding.GetEncoding(_content?.Headers.ContentType?.CharSet ?? Encoding.UTF8.WebName);
            RemoteEndPoint  = new IPEndPoint(IPAddress.Loopback, 9999);
            UserAgent       = clientRequest.Headers.UserAgent?.ToString();
            LocalEndPoint   = new IPEndPoint(IPAddress.Loopback, 8080);
            ContentType     = _content?.Headers.ContentType?.MediaType;
        }
        private void UserWantsToCreateCookie(object obj)
        {
            var neu = new Cookie()
            {
                Name = "NEU", Herstellung = DateTime.Now, Form = Form.Stern
            };

            context.Cookies.Add(neu);
            CookieList.Add(neu);
            SelectedCookie = neu;
        }