Beispiel #1
0
 /// <exception cref="Apache.Http.ProtocolException"></exception>
 internal virtual void RewriteRequestURI(HttpRequestWrapper request, HttpRoute route
                                         )
 {
     try
     {
         URI uri = request.GetURI();
         if (uri != null)
         {
             if (route.GetProxyHost() != null && !route.IsTunnelled())
             {
                 // Make sure the request URI is absolute
                 if (!uri.IsAbsolute())
                 {
                     HttpHost target = route.GetTargetHost();
                     uri = URIUtils.RewriteURI(uri, target, true);
                 }
                 else
                 {
                     uri = URIUtils.RewriteURI(uri);
                 }
             }
             else
             {
                 // Make sure the request URI is relative
                 if (uri.IsAbsolute())
                 {
                     uri = URIUtils.RewriteURI(uri, null, true);
                 }
                 else
                 {
                     uri = URIUtils.RewriteURI(uri);
                 }
             }
             request.SetURI(uri);
         }
     }
     catch (URISyntaxException ex)
     {
         throw new ProtocolException("Invalid URI: " + request.GetRequestLine().GetUri(),
                                     ex);
     }
 }
        //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);
        }