/// <exception cref="Apache.Http.HttpException"></exception>
        /// <exception cref="System.IO.IOException"></exception>
        public virtual void Process(HttpResponse response, HttpContext context)
        {
            Args.NotNull(response, "HTTP request");
            Args.NotNull(context, "HTTP context");
            HttpClientContext clientContext = ((HttpClientContext)HttpClientContext.Adapt(context
                                                                                          ));
            // Obtain actual CookieSpec instance
            CookieSpec cookieSpec = clientContext.GetCookieSpec();

            if (cookieSpec == null)
            {
                this.log.Debug("Cookie spec not specified in HTTP context");
                return;
            }
            // Obtain cookie store
            CookieStore cookieStore = clientContext.GetCookieStore();

            if (cookieStore == null)
            {
                this.log.Debug("Cookie store not specified in HTTP context");
                return;
            }
            // Obtain actual CookieOrigin instance
            CookieOrigin cookieOrigin = clientContext.GetCookieOrigin();

            if (cookieOrigin == null)
            {
                this.log.Debug("Cookie origin not specified in HTTP context");
                return;
            }
            HeaderIterator it = response.HeaderIterator(SM.SetCookie);

            ProcessCookies(it, cookieSpec, cookieOrigin, cookieStore);
            // see if the cookie spec supports cookie versioning.
            if (cookieSpec.GetVersion() > 0)
            {
                // process set-cookie2 headers.
                // Cookie2 will replace equivalent Cookie instances
                it = response.HeaderIterator(SM.SetCookie2);
                ProcessCookies(it, cookieSpec, cookieOrigin, cookieStore);
            }
        }
 private void ProcessCookies(HeaderIterator iterator, CookieSpec cookieSpec, CookieOrigin
                             cookieOrigin, CookieStore cookieStore)
 {
     while (iterator.HasNext())
     {
         Header header = iterator.NextHeader();
         try
         {
             IList <Apache.Http.Cookie.Cookie> cookies = cookieSpec.Parse(header, cookieOrigin);
             foreach (Apache.Http.Cookie.Cookie cookie in cookies)
             {
                 try
                 {
                     cookieSpec.Validate(cookie, cookieOrigin);
                     cookieStore.AddCookie(cookie);
                     if (this.log.IsDebugEnabled())
                     {
                         this.log.Debug("Cookie accepted [" + FormatCooke(cookie) + "]");
                     }
                 }
                 catch (MalformedCookieException ex)
                 {
                     if (this.log.IsWarnEnabled())
                     {
                         this.log.Warn("Cookie rejected [" + FormatCooke(cookie) + "] " + ex.Message);
                     }
                 }
             }
         }
         catch (MalformedCookieException ex)
         {
             if (this.log.IsWarnEnabled())
             {
                 this.log.Warn("Invalid cookie header: \"" + header + "\". " + ex.Message);
             }
         }
     }
 }
Esempio n. 3
0
        /// <exception cref="Apache.Http.HttpException"></exception>
        /// <exception cref="System.IO.IOException"></exception>
        public virtual void Process(IHttpRequest request, HttpContext context)
        {
            Args.NotNull(request, "HTTP request");
            Args.NotNull(context, "HTTP context");
            string method = request.GetRequestLine().GetMethod();

            if (Sharpen.Runtime.EqualsIgnoreCase(method, "CONNECT"))
            {
                return;
            }
            HttpClientContext clientContext = ((HttpClientContext)HttpClientContext.Adapt(context
                                                                                          ));
            // Obtain cookie store
            CookieStore cookieStore = clientContext.GetCookieStore();

            if (cookieStore == null)
            {
                this.log.Debug("Cookie store not specified in HTTP context");
                return;
            }
            // Obtain the registry of cookie specs
            Lookup <CookieSpecProvider> registry = clientContext.GetCookieSpecRegistry();

            if (registry == null)
            {
                this.log.Debug("CookieSpec registry not specified in HTTP context");
                return;
            }
            // Obtain the target host, possibly virtual (required)
            HttpHost targetHost = clientContext.GetTargetHost();

            if (targetHost == null)
            {
                this.log.Debug("Target host not set in the context");
                return;
            }
            // Obtain the route (required)
            RouteInfo route = clientContext.GetHttpRoute();

            if (route == null)
            {
                this.log.Debug("Connection route not set in the context");
                return;
            }
            RequestConfig config = clientContext.GetRequestConfig();
            string        policy = config.GetCookieSpec();

            if (policy == null)
            {
                policy = CookieSpecs.BestMatch;
            }
            if (this.log.IsDebugEnabled())
            {
                this.log.Debug("CookieSpec selected: " + policy);
            }
            URI requestURI = null;

            if (request is IHttpUriRequest)
            {
                requestURI = ((IHttpUriRequest)request).GetURI();
            }
            else
            {
                try
                {
                    requestURI = new URI(request.GetRequestLine().GetUri());
                }
                catch (URISyntaxException)
                {
                }
            }
            string path = requestURI != null?requestURI.GetPath() : null;

            string hostName = targetHost.GetHostName();
            int    port     = targetHost.GetPort();

            if (port < 0)
            {
                port = route.GetTargetHost().GetPort();
            }
            CookieOrigin cookieOrigin = new CookieOrigin(hostName, port >= 0 ? port : 0, !TextUtils
                                                         .IsEmpty(path) ? path : "/", route.IsSecure());
            // Get an instance of the selected cookie policy
            CookieSpecProvider provider = registry.Lookup(policy);

            if (provider == null)
            {
                throw new HttpException("Unsupported cookie policy: " + policy);
            }
            CookieSpec cookieSpec = provider.Create(clientContext);
            // Get all cookies available in the HTTP state
            IList <Apache.Http.Cookie.Cookie> cookies = new AList <Apache.Http.Cookie.Cookie>(cookieStore
                                                                                              .GetCookies());
            // Find cookies matching the given origin
            IList <Apache.Http.Cookie.Cookie> matchedCookies = new AList <Apache.Http.Cookie.Cookie
                                                                          >();
            DateTime now = new DateTime();

            foreach (Apache.Http.Cookie.Cookie cookie in cookies)
            {
                if (!cookie.IsExpired(now))
                {
                    if (cookieSpec.Match(cookie, cookieOrigin))
                    {
                        if (this.log.IsDebugEnabled())
                        {
                            this.log.Debug("Cookie " + cookie + " match " + cookieOrigin);
                        }
                        matchedCookies.AddItem(cookie);
                    }
                }
                else
                {
                    if (this.log.IsDebugEnabled())
                    {
                        this.log.Debug("Cookie " + cookie + " expired");
                    }
                }
            }
            // Generate Cookie request headers
            if (!matchedCookies.IsEmpty())
            {
                IList <Header> headers = cookieSpec.FormatCookies(matchedCookies);
                foreach (Header header in headers)
                {
                    request.AddHeader(header);
                }
            }
            int ver = cookieSpec.GetVersion();

            if (ver > 0)
            {
                bool needVersionHeader = false;
                foreach (Apache.Http.Cookie.Cookie cookie_1 in matchedCookies)
                {
                    if (ver != cookie_1.GetVersion() || !(cookie_1 is SetCookie2))
                    {
                        needVersionHeader = true;
                    }
                }
                if (needVersionHeader)
                {
                    Header header = cookieSpec.GetVersionHeader();
                    if (header != null)
                    {
                        // Advertise cookie version support
                        request.AddHeader(header);
                    }
                }
            }
            // Stick the CookieSpec and CookieOrigin instances to the HTTP context
            // so they could be obtained by the response interceptor
            context.SetAttribute(HttpClientContext.CookieSpec, cookieSpec);
            context.SetAttribute(HttpClientContext.CookieOrigin, cookieOrigin);
        }