示例#1
0
        internal static void Set(HTTPResponse response)
        {
            if (response == null)
            {
                return;
            }
            object locker = CookieJar.Locker;

            lock (locker)
            {
                try
                {
                    CookieJar.Maintain();
                    List <Cookie> list         = new List <Cookie>();
                    List <string> headerValues = response.GetHeaderValues("set-cookie");
                    if (headerValues != null)
                    {
                        foreach (string current in headerValues)
                        {
                            try
                            {
                                Cookie cookie = Cookie.Parse(current, response.baseRequest.CurrentUri);
                                if (cookie != null)
                                {
                                    int    num;
                                    Cookie cookie2 = CookieJar.Find(cookie, out num);
                                    if (!string.IsNullOrEmpty(cookie.Value) && cookie.WillExpireInTheFuture())
                                    {
                                        if (cookie2 == null)
                                        {
                                            CookieJar.Cookies.Add(cookie);
                                            list.Add(cookie);
                                        }
                                        else
                                        {
                                            cookie.Date            = cookie2.Date;
                                            CookieJar.Cookies[num] = cookie;
                                            list.Add(cookie);
                                        }
                                    }
                                    else if (num != -1)
                                    {
                                        CookieJar.Cookies.RemoveAt(num);
                                    }
                                }
                            }
                            catch
                            {
                            }
                        }
                        response.Cookies = list;
                    }
                }
                catch
                {
                }
            }
        }
示例#2
0
        internal static void Set(HTTPResponse response)
        {
            if (response == null)
            {
                return;
            }

            lock (Locker)
            {
                try
                {
                    Maintain();

                    List <Cookie> newCookies       = new List <Cookie>();
                    var           setCookieHeaders = response.GetHeaderValues("set-cookie");

                    // No cookies. :'(
                    if (setCookieHeaders == null)
                    {
                        return;
                    }

                    foreach (var cookieHeader in setCookieHeaders)
                    {
                        try
                        {
                            Cookie cookie = Cookie.Parse(cookieHeader, response.baseRequest.CurrentUri);

                            if (cookie != null)
                            {
                                int idx;
                                var old = Find(cookie, out idx);

                                // if no value for the cookie or already expired then the server asked us to delete the cookie
                                bool expired = string.IsNullOrEmpty(cookie.Value) || !cookie.WillExpireInTheFuture();

                                if (!expired)
                                {
                                    // no old cookie, add it straith to the list
                                    if (old == null)
                                    {
                                        Cookies.Add(cookie);

                                        newCookies.Add(cookie);
                                    }
                                    else
                                    {
                                        // Update the creation-time of the newly created cookie to match the creation-time of the old-cookie.
                                        cookie.Date  = old.Date;
                                        Cookies[idx] = cookie;

                                        newCookies.Add(cookie);
                                    }
                                }
                                else if (idx != -1) // delete the cookie
                                {
                                    Cookies.RemoveAt(idx);
                                }
                            }
                        }
                        catch
                        {
                            // Ignore cookie on error
                        }
                    }

                    response.Cookies = newCookies;
                }
                catch
                {}
            }
        }
示例#3
0
        /// <summary>
        /// Will set or update all cookies from the response object.
        /// </summary>
        internal static bool Set(HTTPResponse response)
        {
            if (response == null)
            {
                return(false);
            }

            List <Cookie> newCookies       = new List <Cookie>();
            var           setCookieHeaders = response.GetHeaderValues("set-cookie");

            // No cookies. :'(
            if (setCookieHeaders == null)
            {
                return(false);
            }

            foreach (var cookieHeader in setCookieHeaders)
            {
                Cookie cookie = Cookie.Parse(cookieHeader, response.baseRequest.CurrentUri, response.baseRequest.Context);
                if (cookie != null)
                {
                    rwLock.EnterWriteLock();
                    try
                    {
                        int idx;
                        var old = Find(cookie, out idx);

                        // if no value for the cookie or already expired then the server asked us to delete the cookie
                        bool expired = string.IsNullOrEmpty(cookie.Value) || !cookie.WillExpireInTheFuture();

                        if (!expired)
                        {
                            // no old cookie, add it straight to the list
                            if (old == null)
                            {
                                Cookies.Add(cookie);

                                newCookies.Add(cookie);
                            }
                            else
                            {
                                // Update the creation-time of the newly created cookie to match the creation-time of the old-cookie.
                                cookie.Date  = old.Date;
                                Cookies[idx] = cookie;

                                newCookies.Add(cookie);
                            }
                        }
                        else if (idx != -1) // delete the cookie
                        {
                            Cookies.RemoveAt(idx);
                        }
                    }
                    catch
                    {
                        // Ignore cookie on error
                    }
                    finally
                    {
                        rwLock.ExitWriteLock();
                    }
                }
            }

            response.Cookies = newCookies;

            PluginEventHelper.EnqueuePluginEvent(new PluginEventInfo(PluginEvents.SaveCookieLibrary));

            return(true);
        }