/// <exception cref="Apache.Http.ProtocolException"></exception>
 public RequestWrapper(IHttpRequest request) : base()
 {
     Args.NotNull(request, "HTTP request");
     this.original = request;
     SetParams(request.GetParams());
     SetHeaders(request.GetAllHeaders());
     // Make a copy of the original URI
     if (request is IHttpUriRequest)
     {
         this.uri     = ((IHttpUriRequest)request).GetURI();
         this.method  = ((IHttpUriRequest)request).GetMethod();
         this.version = null;
     }
     else
     {
         RequestLine requestLine = request.GetRequestLine();
         try
         {
             this.uri = new URI(requestLine.GetUri());
         }
         catch (URISyntaxException ex)
         {
             throw new ProtocolException("Invalid request URI: " + requestLine.GetUri(), ex);
         }
         this.method  = requestLine.GetMethod();
         this.version = request.GetProtocolVersion();
     }
     this.execCount = 0;
 }
 protected override void OnRequestSubmitted(IHttpRequest request)
 {
     if (request != null && this.headerlog.IsDebugEnabled())
     {
         this.headerlog.Debug(GetId() + " >> " + request.GetRequestLine().ToString());
         Header[] headers = request.GetAllHeaders();
         foreach (Header header in headers)
         {
             this.headerlog.Debug(GetId() + " >> " + header.ToString());
         }
     }
 }
 private HttpRequestWrapper(IHttpRequest request) : base()
 {
     this.original = request;
     this.version  = this.original.GetRequestLine().GetProtocolVersion();
     this.method   = this.original.GetRequestLine().GetMethod();
     if (request is IHttpUriRequest)
     {
         this.uri = ((IHttpUriRequest)request).GetURI();
     }
     else
     {
         this.uri = null;
     }
     SetHeaders(request.GetAllHeaders());
 }
Example #4
0
 private Apache.Http.Client.Methods.RequestBuilder DoCopy(IHttpRequest request)
 {
     if (request == null)
     {
         return(this);
     }
     method  = request.GetRequestLine().GetMethod();
     version = request.GetRequestLine().GetProtocolVersion();
     if (request is IHttpUriRequest)
     {
         uri = ((IHttpUriRequest)request).GetURI();
     }
     else
     {
         uri = URI.Create(request.GetRequestLine().GetMethod());
     }
     if (headergroup == null)
     {
         headergroup = new HeaderGroup();
     }
     headergroup.Clear();
     headergroup.SetHeaders(request.GetAllHeaders());
     if (request is HttpEntityEnclosingRequest)
     {
         entity = ((HttpEntityEnclosingRequest)request).GetEntity();
     }
     else
     {
         entity = null;
     }
     if (request is Configurable)
     {
         this.config = ((Configurable)request).GetConfig();
     }
     else
     {
         this.config = null;
     }
     this.parameters = null;
     return(this);
 }
Example #5
0
        /// <exception cref="System.IO.IOException"></exception>
        /// <exception cref="Apache.Http.HttpException"></exception>
        public virtual CloseableHttpResponse Execute(HttpRoute route, HttpRequestWrapper
                                                     request, HttpClientContext context, HttpExecutionAware execAware)
        {
            Args.NotNull(route, "HTTP route");
            Args.NotNull(request, "HTTP request");
            Args.NotNull(context, "HTTP context");
            IList <URI> redirectLocations = context.GetRedirectLocations();

            if (redirectLocations != null)
            {
                redirectLocations.Clear();
            }
            RequestConfig      config         = context.GetRequestConfig();
            int                maxRedirects   = config.GetMaxRedirects() > 0 ? config.GetMaxRedirects() : 50;
            HttpRoute          currentRoute   = route;
            HttpRequestWrapper currentRequest = request;

            for (int redirectCount = 0; ;)
            {
                CloseableHttpResponse response = requestExecutor.Execute(currentRoute, currentRequest
                                                                         , context, execAware);
                try
                {
                    if (config.IsRedirectsEnabled() && this.redirectStrategy.IsRedirected(currentRequest
                                                                                          , response, context))
                    {
                        if (redirectCount >= maxRedirects)
                        {
                            throw new RedirectException("Maximum redirects (" + maxRedirects + ") exceeded");
                        }
                        redirectCount++;
                        IHttpRequest redirect = this.redirectStrategy.GetRedirect(currentRequest, response
                                                                                  , context);
                        if (!redirect.HeaderIterator().HasNext())
                        {
                            IHttpRequest original = request.GetOriginal();
                            redirect.SetHeaders(original.GetAllHeaders());
                        }
                        currentRequest = HttpRequestWrapper.Wrap(redirect);
                        if (currentRequest is HttpEntityEnclosingRequest)
                        {
                            Proxies.EnhanceEntity((HttpEntityEnclosingRequest)currentRequest);
                        }
                        URI      uri       = currentRequest.GetURI();
                        HttpHost newTarget = URIUtils.ExtractHost(uri);
                        if (newTarget == null)
                        {
                            throw new ProtocolException("Redirect URI does not specify a valid host name: " +
                                                        uri);
                        }
                        // Reset virtual host and auth states if redirecting to another host
                        if (!currentRoute.GetTargetHost().Equals(newTarget))
                        {
                            AuthState targetAuthState = context.GetTargetAuthState();
                            if (targetAuthState != null)
                            {
                                this.log.Debug("Resetting target auth state");
                                targetAuthState.Reset();
                            }
                            AuthState proxyAuthState = context.GetProxyAuthState();
                            if (proxyAuthState != null)
                            {
                                AuthScheme authScheme = proxyAuthState.GetAuthScheme();
                                if (authScheme != null && authScheme.IsConnectionBased())
                                {
                                    this.log.Debug("Resetting proxy auth state");
                                    proxyAuthState.Reset();
                                }
                            }
                        }
                        currentRoute = this.routePlanner.DetermineRoute(newTarget, currentRequest, context
                                                                        );
                        if (this.log.IsDebugEnabled())
                        {
                            this.log.Debug("Redirecting to '" + uri + "' via " + currentRoute);
                        }
                        EntityUtils.Consume(response.GetEntity());
                        response.Close();
                    }
                    else
                    {
                        return(response);
                    }
                }
                catch (RuntimeException ex)
                {
                    response.Close();
                    throw;
                }
                catch (IOException ex)
                {
                    response.Close();
                    throw;
                }
                catch (HttpException ex)
                {
                    // Protocol exception related to a direct.
                    // The underlying connection may still be salvaged.
                    try
                    {
                        EntityUtils.Consume(response.GetEntity());
                    }
                    catch (IOException ioex)
                    {
                        this.log.Debug("I/O error while releasing connection", ioex);
                    }
                    finally
                    {
                        response.Close();
                    }
                    throw;
                }
            }
        }