Ejemplo n.º 1
0
 private void SetupContext(HttpClientContext context)
 {
     if (context.GetAttribute(HttpClientContext.TargetAuthState) == null)
     {
         context.SetAttribute(HttpClientContext.TargetAuthState, new AuthState());
     }
     if (context.GetAttribute(HttpClientContext.ProxyAuthState) == null)
     {
         context.SetAttribute(HttpClientContext.ProxyAuthState, new AuthState());
     }
     if (context.GetAttribute(HttpClientContext.AuthschemeRegistry) == null)
     {
         context.SetAttribute(HttpClientContext.AuthschemeRegistry, this.authSchemeRegistry
                              );
     }
     if (context.GetAttribute(HttpClientContext.CookiespecRegistry) == null)
     {
         context.SetAttribute(HttpClientContext.CookiespecRegistry, this.cookieSpecRegistry
                              );
     }
     if (context.GetAttribute(HttpClientContext.CookieStore) == null)
     {
         context.SetAttribute(HttpClientContext.CookieStore, this.cookieStore);
     }
     if (context.GetAttribute(HttpClientContext.CredsProvider) == null)
     {
         context.SetAttribute(HttpClientContext.CredsProvider, this.credentialsProvider);
     }
     if (context.GetAttribute(HttpClientContext.RequestConfig) == null)
     {
         context.SetAttribute(HttpClientContext.RequestConfig, this.defaultConfig);
     }
 }
        //end of switch
        /// <exception cref="Apache.Http.ProtocolException"></exception>
        public virtual URI GetLocationURI(IHttpRequest request, HttpResponse response, HttpContext
                                          context)
        {
            Args.NotNull(request, "HTTP request");
            Args.NotNull(response, "HTTP response");
            Args.NotNull(context, "HTTP context");
            HttpClientContext clientContext = ((HttpClientContext)HttpClientContext.Adapt(context
                                                                                          ));
            //get the location header to find out where to redirect to
            Header locationHeader = response.GetFirstHeader("location");

            if (locationHeader == null)
            {
                // got a redirect response, but no location header
                throw new ProtocolException("Received redirect response " + response.GetStatusLine
                                                () + " but no location header");
            }
            string location = locationHeader.GetValue();

            if (this.log.IsDebugEnabled())
            {
                this.log.Debug("Redirect requested to location '" + location + "'");
            }
            RequestConfig config = clientContext.GetRequestConfig();
            URI           uri    = CreateLocationURI(location);

            // rfc2616 demands the location value be a complete URI
            // Location       = "Location" ":" absoluteURI
            try
            {
                if (!uri.IsAbsolute())
                {
                    if (!config.IsRelativeRedirectsAllowed())
                    {
                        throw new ProtocolException("Relative redirect location '" + uri + "' not allowed"
                                                    );
                    }
                    // Adjust location URI
                    HttpHost target = clientContext.GetTargetHost();
                    Asserts.NotNull(target, "Target host");
                    URI requestURI         = new URI(request.GetRequestLine().GetUri());
                    URI absoluteRequestURI = URIUtils.RewriteURI(requestURI, target, false);
                    uri = URIUtils.Resolve(absoluteRequestURI, uri);
                }
            }
            catch (URISyntaxException ex)
            {
                throw new ProtocolException(ex.Message, ex);
            }
            RedirectLocations redirectLocations = (RedirectLocations)clientContext.GetAttribute
                                                      (HttpClientContext.RedirectLocations);

            if (redirectLocations == null)
            {
                redirectLocations = new RedirectLocations();
                context.SetAttribute(HttpClientContext.RedirectLocations, redirectLocations);
            }
            if (!config.IsCircularRedirectsAllowed())
            {
                if (redirectLocations.Contains(uri))
                {
                    throw new CircularRedirectException("Circular redirect to '" + uri + "'");
                }
            }
            redirectLocations.Add(uri);
            return(uri);
        }