Esempio n. 1
0
        public void TestSetCookieSessionOnly()
        {
            var cookieStore = new CookieStore(database, "cookie_store_unit_test");

            Assert.AreEqual(0, cookieStore.Count);

            // No expires date specified for a cookie v0:
            var cookie1 = new Cookie("whitechoco", "sweet", "/", "mycookie.com");

            Assert.AreEqual(DateTime.MinValue, cookie1.Expires);
            cookieStore.Add(cookie1);

            // No max age specified for a cookie v1:
            var cookie2 = new Cookie("oatmeal_raisin", "sweet", "/", ".mycookie.com")
            {
                Version = 1
            };

            Assert.AreEqual(DateTime.MinValue, cookie2.Expires);
            cookieStore.Add(cookie2);

            Assert.AreEqual(2, cookieStore.Count);
            var cookies = cookieStore.GetCookies(new Uri("http://mycookie.com"));

            CollectionAssert.AreEquivalent(new[] { cookie1, cookie2 }, cookies);

            cookieStore = new CookieStore(database, "cookie_store_unit_test");
            Assert.AreEqual(0, cookieStore.Count);
        }
Esempio n. 2
0
        public void TestDeleteCookie()
        {
            var cookieStore = new CookieStore(database, "cookie_store_unit_test");

            Assert.AreEqual(0, cookieStore.Count);

            var cookie1 = new Cookie("whitechoco", "sweet", "/", "mycookie.com")
            {
                Expires = DateTime.Now.AddSeconds(3600),
            };

            cookieStore.Add(cookie1);

            var cookie2 = new Cookie("oatmeal_raisin", "sweet", "/", "mycookie.com")
            {
                Expires = DateTime.Now.AddSeconds(3600),
            };

            cookieStore.Add(cookie2);

            var cookie3 = new Cookie("darkchoco", "sweet", "/", "mycookie.com")
            {
                Expires = DateTime.Now.AddSeconds(3600),
            };

            cookieStore.Add(cookie3);

            Assert.AreEqual(3, cookieStore.Count);
            var cookies = cookieStore.GetCookies(new Uri("http://mycookie.com"));

            CollectionAssert.Contains(cookies, cookie1);
            CollectionAssert.Contains(cookies, cookie2);
            CollectionAssert.Contains(cookies, cookie3);

            cookieStore.Delete(new Uri("http://mycookie.com"), cookie2.Name);
            Assert.AreEqual(2, cookieStore.Count);
            cookies = cookieStore.GetCookies(new Uri("http://mycookie.com"));
            CollectionAssert.Contains(cookies, cookie1);
            CollectionAssert.Contains(cookies, cookie3);

            cookieStore = new CookieStore(database, "cookie_store_unit_test");
            Assert.AreEqual(2, cookieStore.Count);
            cookies = cookieStore.GetCookies(new Uri("http://mycookie.com"));
            CollectionAssert.Contains(cookies, cookie1);
            CollectionAssert.Contains(cookies, cookie3);

            cookieStore.Delete(new Uri("http://mycookie.com"), cookie1.Name);
            cookieStore.Delete(new Uri("http://mycookie.com"), cookie3.Name);
            Assert.AreEqual(0, cookieStore.Count);

            cookieStore = new CookieStore(database, "cookie_store_unit_test");
            Assert.AreEqual(0, cookieStore.Count);
        }
Esempio n. 3
0
        public void TestSetCookiePersistent()
        {
            var cookieStore = new CookieStore(database, "cookie_store_unit_test");

            Assert.AreEqual(0, cookieStore.Count);

            var cookie1 = new Cookie("whitechoco", "sweet", "/", "mycookie.com")
            {
                Comment    = "yummy",
                CommentUri = new Uri("http://www.mycookie.com"),
                Expires    = DateTime.Now.AddSeconds(60),
            };

            cookieStore.Add(cookie1);

            var cookie2 = new Cookie("darkchoco", "sweet", "/", "mycookie.com")
            {
                Comment    = "yummy",
                CommentUri = new Uri("http://www.mycookie.com"),
                Expires    = DateTime.Now.AddSeconds(60)
            };

            cookieStore.Add(cookie2);

            var cookies = cookieStore.GetCookies(new Uri("http://mycookie.com"));

            Assert.AreEqual(2, cookieStore.Count);
            CollectionAssert.AreEquivalent(new[] { cookie1, cookie2 }, cookies);

            // Set cookie with same name, domain, and path
            // with one of the previously set cookies:
            var cookie3 = new Cookie("darkchoco", "bitter sweet", "/", "mycookie.com")
            {
                Comment    = "yummy",
                CommentUri = new Uri("http://www.mycookie.com"),
                Expires    = DateTime.Now.AddSeconds(60)
            };

            cookieStore.Add(cookie3);
            Assert.AreEqual(2, cookieStore.Count);
            cookies = cookieStore.GetCookies(new Uri("http://mycookie.com"));
            CollectionAssert.AreEquivalent(new[] { cookie1, cookie3 }, cookies);

            cookieStore = new CookieStore(database, "cookie_store_unit_test");
            cookies     = cookieStore.GetCookies(new Uri("http://mycookie.com"));
            Assert.AreEqual(2, cookieStore.Count);
            CollectionAssert.AreEquivalent(new[] { cookie1, cookie3 }, cookies);
        }
        public void SetCookie(Cookie cookie)
        {
            var nc = ToNativeCookie(cookie);

            //cookieManager.CookieStore.Add(new URI(nc.Domain), nc);
            CookieStore.Add(new URI(nc.Domain), nc);
        }
        protected override HttpResponseMessage ProcessResponse(HttpResponseMessage response, CancellationToken cancellationToken)
        {
            int retryCount;

            do
            {
                if (Authenticator != null && response.StatusCode == HttpStatusCode.Unauthorized)
                {
                    retryCount = _retryMessages.GetOrAdd(response, 0);
                    if (retryCount >= 5)
                    {
                        // Multiple concurrent requests means that the Nc can sometimes get out of order
                        // so try again, but within reason.
                        break;
                    }

                    _retryMessages.TryUpdate(response, retryCount + 1, retryCount);
                    var newRequest = new HttpRequestMessage(response.RequestMessage.Method, response.RequestMessage.RequestUri);
                    foreach (var header in response.RequestMessage.Headers)
                    {
                        if (header.Key != "Authorization")
                        {
                            newRequest.Headers.Add(header.Key, header.Value);
                        }
                    }

                    newRequest.Content = response.RequestMessage.Content;
                    var challengeResponse = Authenticator.ResponseFromChallenge(response);
                    if (challengeResponse != null)
                    {
                        newRequest.Headers.Add("Authorization", challengeResponse);
                        return(ProcessResponse(SendAsync(newRequest, cancellationToken).Result, cancellationToken));
                    }
                }
            }  while(false);

            var hasSetCookie = response.Headers.Contains("Set-Cookie");

            if (hasSetCookie)
            {
                var cookie = default(Cookie);
                if (CookieParser.TryParse(response.Headers.GetValues("Set-Cookie").ElementAt(0), response.RequestMessage.RequestUri.Host,
                                          out cookie))
                {
                    lock (_locker) {
                        try {
                            _cookieStore.Add(cookie);
                        } catch (CookieException e) {
                            var headerValue = new SecureLogString(response.Headers.GetValues("Set-Cookie").ElementAt(0),
                                                                  LogMessageSensitivity.Insecure);
                            Log.To.Sync.W("DefaultAuthHandler",
                                          $"Invalid cookie string received from remote: {headerValue}", e);
                        }
                    }
                }
            }

            _retryMessages.TryRemove(response, out retryCount);
            return(response);
        }
        private void AddRequestHeaders(HttpRequestMessage request)
        {
            foreach (var requestHeaderKey in RequestHeaders.Keys)
            {
                if (requestHeaderKey.ToLowerInvariant() == "cookie")
                {
                    Cookie cookie;
                    var    cookieStr = RequestHeaders[requestHeaderKey];
                    if (!CookieParser.TryParse(cookieStr, request.RequestUri.Host, out cookie))
                    {
                        Log.To.Sync.W(Tag, "Invalid cookie string received, {0}",
                                      new SecureLogString(cookieStr, LogMessageSensitivity.Insecure));
                    }
                    else
                    {
                        try {
                            CookieStore.Add(cookie);
                        } catch (CookieException e) {
                            var headerValue = new SecureLogString(cookieStr, LogMessageSensitivity.Insecure);
                            Log.To.Sync.W(Tag, $"Invalid cookie string received, {headerValue}", e);
                        }
                    }

                    request.Headers.Add("Cookie", CookieStore.GetCookieHeader(request.RequestUri));
                    continue;
                }


                request.Headers.Add(requestHeaderKey, RequestHeaders.Get(requestHeaderKey));
            }
        }
 public void SetCookies(IEnumerable <Cookie> cookies)
 {
     foreach (var nc in cookies.Select(ToNativeCookie))
     {
         //cookieManager.CookieStore.Add(new URI(nc.Domain), nc);
         CookieStore.Add(new URI(nc.Domain), nc);
     }
 }
        public void SaveFromResponse(Square.OkHttp3.HttpUrl p0, IList <Square.OkHttp3.Cookie> p1)
        {
            foreach (var cookie in p1)
            {
                var nc = new HttpCookie(cookie.Name(), cookie.Value());
                nc.Domain = cookie.Domain();
                nc.Path   = cookie.Path();
                nc.Secure = cookie.Secure();

                CookieStore.Add(p0.Uri(), nc);
            }
        }
Esempio n. 9
0
        Response SendLoginCheckRequest()
        {
            Request Req = LoginCheckRequest.GetClone();

            Req.Cookie.RemoveAll();
            Req.SetCookie(Cookies);
            Req.SetSource("LoginCheck");
            Response Res = Req.Send();

            Cookies.Add(Req, Res);
            return(Res);
        }
Esempio n. 10
0
        public Response PlayStep()
        {
            Response Res = CurrentRequestBeingPlayed.Send();

            Cookies.Add(CurrentRequestBeingPlayed, Res);
            if (PreviousAssociationToOneBeingPlayed != null && PreviousAssociationToOneBeingPlayed.ReplayAssociation != null && PreviousAssociationToOneBeingPlayed.ReplayAssociation.DestinationLog != null)
            {
                CurrentAssociationBeingPlayed.ReplayAssociation = new LogAssociation(LogAssociationType.Unknown, RefererAssociationType.None, IronHtml.UrlInHtmlMatch.None, LogAssociationMatchLevel.Other, PreviousAssociationToOneBeingPlayed.ReplayAssociation.DestinationLog, new Session(CurrentRequestBeingPlayed, Res));
            }
            else
            {
                CurrentAssociationBeingPlayed.ReplayAssociation = new LogAssociation(LogAssociationType.Unknown, RefererAssociationType.None, IronHtml.UrlInHtmlMatch.None, LogAssociationMatchLevel.Other, null, new Session(CurrentRequestBeingPlayed, Res));
            }
            CurrentPlayIndex++;
            return(Res);
        }
Esempio n. 11
0
        public void TestSetCookieNameDomainPath()
        {
            var cookieStore = new CookieStore(database, "cookie_store_unit_test");

            Assert.AreEqual(0, cookieStore.Count);
            var cookie1 = new Cookie("cookie1", "sweet", "/", "mycookie.com");

            cookieStore.Add(cookie1);
            var cookie2 = new Cookie("cookie1", "sweet", "", "mycookie.com");

            cookieStore.Add(cookie2);
            var cookie3 = new Cookie("cookie1", "sweet", "/path", "mycookie.com");

            cookieStore.Add(cookie3);
            var cookie4 = new Cookie("cookie1", "sweet", "/path/", "mycookie.com");

            cookieStore.Add(cookie4);
            var cookie5 = new Cookie("cookie1", "sweet", "/", "www.mycookie.com");

            cookieStore.Add(cookie5);
            var cookie6 = new Cookie("cookie7", "sweet", "/", "www.mycookie.com");

            cookieStore.Add(cookie6);

            Assert.AreEqual(6, cookieStore.Count);
            var cookies = cookieStore.GetCookies(new Uri("http://www.mycookie.com/path/"));

            Assert.AreEqual(6, cookies.Count);
            CollectionAssert.Contains(cookies, cookie1);
            CollectionAssert.Contains(cookies, cookie2);
            CollectionAssert.Contains(cookies, cookie3);
            CollectionAssert.Contains(cookies, cookie4);
            CollectionAssert.Contains(cookies, cookie5);
            CollectionAssert.Contains(cookies, cookie6);

            var cookie8 = new Cookie("cookie1", "bitter. sweet", "/", "mycookie.com");

            cookieStore.Add(cookie8);

            Assert.AreEqual(6, cookieStore.Count);
            cookies = cookieStore.GetCookies(new Uri("http://www.mycookie.com/path/"));
            Assert.AreEqual(6, cookies.Count);
            CollectionAssert.DoesNotContain(cookies, cookie1);
            CollectionAssert.Contains(cookies, cookie8);
        }
Esempio n. 12
0
        public void TestSaveCookieStore()
        {
            var cookieStore = new CookieStore(GetCookiesDirectory().FullName);

            Assert.AreEqual(0, cookieStore.Count);

            var name     = "foo";
            var value    = "bar";
            var uri      = new Uri("http://foo.com/baz");
            var domain   = uri.Host;
            var path     = uri.PathAndQuery;
            var httpOnly = false;
            var isSecure = false;
            var expires  = DateTime.Now.Add(TimeSpan.FromDays(1));

            var cookie = new Cookie(name, value);

            cookie.Path     = path;
            cookie.Domain   = domain;
            cookie.HttpOnly = httpOnly;
            cookie.Secure   = isSecure;
            cookie.Expires  = expires;

            cookieStore.Add(cookie);
            cookieStore.Save();

            cookieStore = new CookieStore(GetCookiesDirectory().FullName);
            Assert.AreEqual(1, cookieStore.Count);

            var cookies = cookieStore.GetCookies(uri);

            Assert.AreEqual(1, cookies.Count);
            Assert.AreEqual(name, cookies[0].Name);
            Assert.AreEqual(value, cookies[0].Value);
            Assert.AreEqual(path, cookies[0].Path);
            Assert.AreEqual(domain, cookies[0].Domain);
            Assert.AreEqual(expires, cookies[0].Expires);
        }
Esempio n. 13
0
        public void TestCookieExpires()
        {
            var cookieStore = new CookieStore(database, "cookie_store_unit_test");

            Assert.AreEqual(0, cookieStore.Count);

            var cookie1 = new Cookie("whitechoco", "sweet", "/", ".mycookie.com")
            {
                Expires = DateTime.Now.AddSeconds(1),
                Version = 1
            };

            cookieStore.Add(cookie1);

            Assert.AreEqual(1, cookieStore.Count);
            var cookies = cookieStore.GetCookies(new Uri("http://mycookie.com"));

            Assert.AreEqual(1, cookies.Count);
            Assert.AreEqual(cookie1, cookies[0]);

            Sleep(1500);

            Assert.AreEqual(0, cookieStore.Count);
        }
 public void AddCookies(CookieCollection cookies)
 {
     cookieStore.Add(cookies);
 }