Search in sources :

Example 61 with HttpRequest

use of org.apache.http.HttpRequest in project SmartAndroidSource by jaychou2012.

the class MyRedirectHandler method getLocationURI.

@Override
public URI getLocationURI(final HttpResponse response, final HttpContext context) throws ProtocolException {
    if (response == null) {
        throw new IllegalArgumentException("HTTP response may not be null");
    }
    // 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");
    }
    // HERE IS THE MODIFIED LINE OF CODE
    String location = locationHeader.getValue().replaceAll(" ", "%20");
    URI uri;
    try {
        uri = new URI(location);
    } catch (URISyntaxException ex) {
        throw new ProtocolException("Invalid redirect URI: " + location, ex);
    }
    HttpParams params = response.getParams();
    // Location       = "Location" ":" absoluteURI
    if (!uri.isAbsolute()) {
        if (params.isParameterTrue(ClientPNames.REJECT_RELATIVE_REDIRECT)) {
            throw new ProtocolException("Relative redirect location '" + uri + "' not allowed");
        }
        // Adjust location URI
        HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        if (target == null) {
            throw new IllegalStateException("Target host not available " + "in the HTTP context");
        }
        HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
        try {
            URI requestURI = new URI(request.getRequestLine().getUri());
            URI absoluteRequestURI = URIUtils.rewriteURI(requestURI, target, true);
            uri = URIUtils.resolve(absoluteRequestURI, uri);
        } catch (URISyntaxException ex) {
            throw new ProtocolException(ex.getMessage(), ex);
        }
    }
    if (params.isParameterFalse(ClientPNames.ALLOW_CIRCULAR_REDIRECTS)) {
        RedirectLocations redirectLocations = (RedirectLocations) context.getAttribute(REDIRECT_LOCATIONS);
        if (redirectLocations == null) {
            redirectLocations = new RedirectLocations();
            context.setAttribute(REDIRECT_LOCATIONS, redirectLocations);
        }
        URI redirectURI;
        if (uri.getFragment() != null) {
            try {
                HttpHost target = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
                redirectURI = URIUtils.rewriteURI(uri, target, true);
            } catch (URISyntaxException ex) {
                throw new ProtocolException(ex.getMessage(), ex);
            }
        } else {
            redirectURI = uri;
        }
        if (redirectLocations.contains(redirectURI)) {
            throw new CircularRedirectException("Circular redirect to '" + redirectURI + "'");
        } else {
            redirectLocations.add(redirectURI);
        }
    }
    return uri;
}
Also used : HttpRequest(org.apache.http.HttpRequest) ProtocolException(org.apache.http.ProtocolException) CircularRedirectException(org.apache.http.client.CircularRedirectException) HttpParams(org.apache.http.params.HttpParams) RedirectLocations(org.apache.http.impl.client.RedirectLocations) Header(org.apache.http.Header) HttpHost(org.apache.http.HttpHost) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 62 with HttpRequest

use of org.apache.http.HttpRequest in project jmeter by apache.

the class HTTPHC4Impl method sample.

@Override
protected HTTPSampleResult sample(URL url, String method, boolean areFollowingRedirect, int frameDepth) {
    if (log.isDebugEnabled()) {
        log.debug("Start : sample {} method {} followingRedirect {} depth {}", url, method, areFollowingRedirect, frameDepth);
    }
    JMeterVariables jMeterVariables = JMeterContextService.getContext().getVariables();
    HTTPSampleResult res = createSampleResult(url, method);
    CloseableHttpClient httpClient = null;
    HttpRequestBase httpRequest = null;
    HttpContext localContext = new BasicHttpContext();
    HttpClientContext clientContext = HttpClientContext.adapt(localContext);
    clientContext.setAttribute(CONTEXT_ATTRIBUTE_AUTH_MANAGER, getAuthManager());
    HttpClientKey key = createHttpClientKey(url);
    MutableTriple<CloseableHttpClient, AuthState, PoolingHttpClientConnectionManager> triple;
    try {
        triple = setupClient(key, jMeterVariables, clientContext);
        httpClient = triple.getLeft();
        URI uri = url.toURI();
        httpRequest = createHttpRequest(uri, method, areFollowingRedirect);
        // can throw IOException
        setupRequest(url, httpRequest, res);
    } catch (Exception e) {
        res.sampleStart();
        res.sampleEnd();
        errorResult(e, res);
        return res;
    }
    setupClientContextBeforeSample(jMeterVariables, localContext);
    res.sampleStart();
    final CacheManager cacheManager = getCacheManager();
    if (cacheManager != null && HTTPConstants.GET.equalsIgnoreCase(method) && cacheManager.inCache(url, httpRequest.getAllHeaders())) {
        return updateSampleResultForResourceInCache(res);
    }
    CloseableHttpResponse httpResponse = null;
    try {
        currentRequest = httpRequest;
        handleMethod(method, res, httpRequest, localContext);
        // store the SampleResult in LocalContext to compute connect time
        localContext.setAttribute(CONTEXT_ATTRIBUTE_SAMPLER_RESULT, res);
        // perform the sample
        httpResponse = executeRequest(httpClient, httpRequest, localContext, url);
        saveProxyAuth(triple, localContext);
        if (log.isDebugEnabled()) {
            log.debug("Headers in request before:{}", Arrays.asList(httpRequest.getAllHeaders()));
        }
        // Needs to be done after execute to pick up all the headers
        final HttpRequest request = (HttpRequest) localContext.getAttribute(HttpCoreContext.HTTP_REQUEST);
        if (log.isDebugEnabled()) {
            log.debug("Headers in request after:{}, in localContext#request:{}", Arrays.asList(httpRequest.getAllHeaders()), Arrays.asList(request.getAllHeaders()));
        }
        extractClientContextAfterSample(jMeterVariables, localContext);
        // We've finished with the request, so we can add the LocalAddress to it for display
        if (localAddress != null) {
            request.addHeader(HEADER_LOCAL_ADDRESS, localAddress.toString());
        }
        res.setRequestHeaders(getAllHeadersExceptCookie(request));
        Header contentType = httpResponse.getLastHeader(HTTPConstants.HEADER_CONTENT_TYPE);
        if (contentType != null) {
            String ct = contentType.getValue();
            res.setContentType(ct);
            res.setEncodingAndType(ct);
        }
        HttpEntity entity = httpResponse.getEntity();
        if (entity != null) {
            res.setResponseData(readResponse(res, entity.getContent(), entity.getContentLength()));
        }
        // Done with the sampling proper.
        res.sampleEnd();
        currentRequest = null;
        // Now collect the results into the HTTPSampleResult:
        StatusLine statusLine = httpResponse.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        res.setResponseCode(Integer.toString(statusCode));
        res.setResponseMessage(statusLine.getReasonPhrase());
        res.setSuccessful(isSuccessCode(statusCode));
        res.setResponseHeaders(getResponseHeaders(httpResponse));
        if (res.isRedirect()) {
            final Header headerLocation = httpResponse.getLastHeader(HTTPConstants.HEADER_LOCATION);
            if (headerLocation == null) {
                // HTTP protocol violation, but avoids NPE
                throw new IllegalArgumentException("Missing location header in redirect for " + httpRequest.getRequestLine());
            }
            String redirectLocation = headerLocation.getValue();
            res.setRedirectLocation(redirectLocation);
        }
        // record some sizes to allow HTTPSampleResult.getBytes() with different options
        long headerBytes = // condensed length (without \r)
        (long) res.getResponseHeaders().length() + // Add \r for each header
        (long) httpResponse.getAllHeaders().length + // Add \r for initial header
        1L + // final \r\n before data
        2L;
        HttpConnectionMetrics metrics = (HttpConnectionMetrics) localContext.getAttribute(CONTEXT_ATTRIBUTE_METRICS);
        long totalBytes = metrics.getReceivedBytesCount();
        res.setHeadersSize((int) headerBytes);
        res.setBodySize(totalBytes - headerBytes);
        res.setSentBytes((Long) localContext.getAttribute(CONTEXT_ATTRIBUTE_SENT_BYTES));
        if (log.isDebugEnabled()) {
            long total = res.getHeadersSize() + res.getBodySizeAsLong();
            log.debug("ResponseHeadersSize={} Content-Length={} Total={}", res.getHeadersSize(), res.getBodySizeAsLong(), total);
        }
        // If we redirected automatically, the URL may have changed
        if (getAutoRedirects()) {
            HttpUriRequest req = (HttpUriRequest) localContext.getAttribute(HttpCoreContext.HTTP_REQUEST);
            HttpHost target = (HttpHost) localContext.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
            URI redirectURI = req.getURI();
            if (redirectURI.isAbsolute()) {
                res.setURL(redirectURI.toURL());
            } else {
                res.setURL(new URL(new URL(target.toURI()), redirectURI.toString()));
            }
        }
        // Store any cookies received in the cookie manager:
        saveConnectionCookies(httpResponse, res.getURL(), getCookieManager());
        // Save cache information
        if (cacheManager != null) {
            cacheManager.saveDetails(httpResponse, res);
        }
        // Follow redirects and download page resources if appropriate:
        res = resultProcessing(areFollowingRedirect, frameDepth, res);
        if (!isSuccessCode(statusCode)) {
            EntityUtils.consumeQuietly(httpResponse.getEntity());
        }
    } catch (IOException e) {
        log.debug("IOException", e);
        if (res.getEndTime() == 0) {
            res.sampleEnd();
        }
        // pick up headers if failed to execute the request
        if (res.getRequestHeaders() != null) {
            log.debug("Overwriting request old headers: {}", res.getRequestHeaders());
        }
        res.setRequestHeaders(getAllHeadersExceptCookie((HttpRequest) localContext.getAttribute(HttpCoreContext.HTTP_REQUEST)));
        errorResult(e, res);
        return res;
    } catch (RuntimeException e) {
        log.debug("RuntimeException", e);
        if (res.getEndTime() == 0) {
            res.sampleEnd();
        }
        errorResult(e, res);
        return res;
    } finally {
        JOrphanUtils.closeQuietly(httpResponse);
        currentRequest = null;
        JMeterContextService.getContext().getSamplerContext().remove(CONTEXT_ATTRIBUTE_PARENT_SAMPLE_CLIENT_STATE);
    }
    return res;
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpEntity(org.apache.http.HttpEntity) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) URI(java.net.URI) URL(java.net.URL) HttpConnectionMetrics(org.apache.http.HttpConnectionMetrics) JMeterVariables(org.apache.jmeter.threads.JMeterVariables) HttpHost(org.apache.http.HttpHost) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) CacheManager(org.apache.jmeter.protocol.http.control.CacheManager) HttpRequest(org.apache.http.HttpRequest) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpContext(org.apache.http.protocol.HttpContext) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) IOException(java.io.IOException) GeneralSecurityException(java.security.GeneralSecurityException) HttpException(org.apache.http.HttpException) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MalformedURLException(java.net.MalformedURLException) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) StatusLine(org.apache.http.StatusLine) Header(org.apache.http.Header) BufferedHeader(org.apache.http.message.BufferedHeader) AuthState(org.apache.http.auth.AuthState)

Example 63 with HttpRequest

use of org.apache.http.HttpRequest in project ribbon by Netflix.

the class NFHttpMethodRetryHandler method retryRequest.

@Override
@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "ICAST_INTEGER_MULTIPLY_CAST_TO_LONG")
public boolean retryRequest(final IOException exception, int executionCount, HttpContext context) {
    if (super.retryRequest(exception, executionCount, context)) {
        HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
        String methodName = request.getRequestLine().getMethod();
        String path = "UNKNOWN_PATH";
        if (request instanceof HttpUriRequest) {
            HttpUriRequest uriReq = (HttpUriRequest) request;
            path = uriReq.getURI().toString();
        }
        try {
            Thread.sleep(executionCount * this.sleepTimeFactorMs);
        } catch (InterruptedException e) {
            logger.warn("Interrupted while sleep before retrying http method " + methodName + " " + path, e);
        }
        DynamicCounter.increment(RETRY_COUNTER + methodName + ":" + path);
        return true;
    }
    return false;
}
Also used : HttpRequest(org.apache.http.HttpRequest) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest)

Example 64 with HttpRequest

use of org.apache.http.HttpRequest in project platform_external_apache-http by android.

the class DefaultRequestDirector method createTunnelToTarget.

// establishConnection
/**
 * Creates a tunnel to the target server.
 * The connection must be established to the (last) proxy.
 * A CONNECT request for tunnelling through the proxy will
 * be created and sent, the response received and checked.
 * This method does <i>not</i> update the connection with
 * information about the tunnel, that is left to the caller.
 *
 * @param route     the route to establish
 * @param context   the context for request execution
 *
 * @return  <code>true</code> if the tunnelled route is secure,
 *          <code>false</code> otherwise.
 *          The implementation here always returns <code>false</code>,
 *          but derived classes may override.
 *
 * @throws HttpException    in case of a problem
 * @throws IOException      in case of an IO problem
 */
protected boolean createTunnelToTarget(HttpRoute route, HttpContext context) throws HttpException, IOException {
    HttpHost proxy = route.getProxyHost();
    HttpHost target = route.getTargetHost();
    HttpResponse response = null;
    boolean done = false;
    while (!done) {
        done = true;
        if (!this.managedConn.isOpen()) {
            this.managedConn.open(route, context, this.params);
        }
        HttpRequest connect = createConnectRequest(route, context);
        String agent = HttpProtocolParams.getUserAgent(params);
        if (agent != null) {
            connect.addHeader(HTTP.USER_AGENT, agent);
        }
        connect.addHeader(HTTP.TARGET_HOST, target.toHostString());
        AuthScheme authScheme = this.proxyAuthState.getAuthScheme();
        AuthScope authScope = this.proxyAuthState.getAuthScope();
        Credentials creds = this.proxyAuthState.getCredentials();
        if (creds != null) {
            if (authScope != null || !authScheme.isConnectionBased()) {
                try {
                    connect.addHeader(authScheme.authenticate(creds, connect));
                } catch (AuthenticationException ex) {
                    if (this.log.isErrorEnabled()) {
                        this.log.error("Proxy authentication error: " + ex.getMessage());
                    }
                }
            }
        }
        response = requestExec.execute(connect, this.managedConn, context);
        int status = response.getStatusLine().getStatusCode();
        if (status < 200) {
            throw new HttpException("Unexpected response to CONNECT request: " + response.getStatusLine());
        }
        CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
        if (credsProvider != null && HttpClientParams.isAuthenticating(params)) {
            if (this.proxyAuthHandler.isAuthenticationRequested(response, context)) {
                this.log.debug("Proxy requested authentication");
                Map<String, Header> challenges = this.proxyAuthHandler.getChallenges(response, context);
                try {
                    processChallenges(challenges, this.proxyAuthState, this.proxyAuthHandler, response, context);
                } catch (AuthenticationException ex) {
                    if (this.log.isWarnEnabled()) {
                        this.log.warn("Authentication error: " + ex.getMessage());
                        break;
                    }
                }
                updateAuthState(this.proxyAuthState, proxy, credsProvider);
                if (this.proxyAuthState.getCredentials() != null) {
                    done = false;
                    // Retry request
                    if (this.reuseStrategy.keepAlive(response, context)) {
                        this.log.debug("Connection kept alive");
                        // Consume response content
                        HttpEntity entity = response.getEntity();
                        if (entity != null) {
                            entity.consumeContent();
                        }
                    } else {
                        this.managedConn.close();
                    }
                }
            } else {
                // Reset proxy auth scope
                this.proxyAuthState.setAuthScope(null);
            }
        }
    }
    int status = response.getStatusLine().getStatusCode();
    if (status > 299) {
        // Buffer response content
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            response.setEntity(new BufferedHttpEntity(entity));
        }
        this.managedConn.close();
        throw new TunnelRefusedException("CONNECT refused by proxy: " + response.getStatusLine(), response);
    }
    this.managedConn.markReusable();
    // Leave it to derived classes, consider insecure by default here.
    return false;
}
Also used : HttpRequest(org.apache.http.HttpRequest) BasicHttpRequest(org.apache.http.message.BasicHttpRequest) AbortableHttpRequest(org.apache.http.client.methods.AbortableHttpRequest) HttpEntity(org.apache.http.HttpEntity) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) AuthenticationException(org.apache.http.auth.AuthenticationException) HttpResponse(org.apache.http.HttpResponse) CredentialsProvider(org.apache.http.client.CredentialsProvider) AuthScheme(org.apache.http.auth.AuthScheme) Header(org.apache.http.Header) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) HttpException(org.apache.http.HttpException) Credentials(org.apache.http.auth.Credentials)

Example 65 with HttpRequest

use of org.apache.http.HttpRequest in project platform_external_apache-http by android.

the class DefaultRequestDirector method handleResponse.

/**
 * Analyzes a response to check need for a followup.
 *
 * @param roureq    the request and route.
 * @param response  the response to analayze
 * @param context   the context used for the current request execution
 *
 * @return  the followup request and route if there is a followup, or
 *          <code>null</code> if the response should be returned as is
 *
 * @throws HttpException    in case of a problem
 * @throws IOException      in case of an IO problem
 */
protected RoutedRequest handleResponse(RoutedRequest roureq, HttpResponse response, HttpContext context) throws HttpException, IOException {
    HttpRoute route = roureq.getRoute();
    HttpHost proxy = route.getProxyHost();
    RequestWrapper request = roureq.getRequest();
    HttpParams params = request.getParams();
    if (HttpClientParams.isRedirecting(params) && this.redirectHandler.isRedirectRequested(response, context)) {
        if (redirectCount >= maxRedirects) {
            throw new RedirectException("Maximum redirects (" + maxRedirects + ") exceeded");
        }
        redirectCount++;
        URI uri = this.redirectHandler.getLocationURI(response, context);
        HttpHost newTarget = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
        HttpGet redirect = new HttpGet(uri);
        HttpRequest orig = request.getOriginal();
        redirect.setHeaders(orig.getAllHeaders());
        RequestWrapper wrapper = new RequestWrapper(redirect);
        wrapper.setParams(params);
        HttpRoute newRoute = determineRoute(newTarget, wrapper, context);
        RoutedRequest newRequest = new RoutedRequest(wrapper, newRoute);
        if (this.log.isDebugEnabled()) {
            this.log.debug("Redirecting to '" + uri + "' via " + newRoute);
        }
        return newRequest;
    }
    CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
    if (credsProvider != null && HttpClientParams.isAuthenticating(params)) {
        if (this.targetAuthHandler.isAuthenticationRequested(response, context)) {
            HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
            if (target == null) {
                target = route.getTargetHost();
            }
            this.log.debug("Target requested authentication");
            Map<String, Header> challenges = this.targetAuthHandler.getChallenges(response, context);
            try {
                processChallenges(challenges, this.targetAuthState, this.targetAuthHandler, response, context);
            } catch (AuthenticationException ex) {
                if (this.log.isWarnEnabled()) {
                    this.log.warn("Authentication error: " + ex.getMessage());
                    return null;
                }
            }
            updateAuthState(this.targetAuthState, target, credsProvider);
            if (this.targetAuthState.getCredentials() != null) {
                // Re-try the same request via the same route
                return roureq;
            } else {
                return null;
            }
        } else {
            // Reset target auth scope
            this.targetAuthState.setAuthScope(null);
        }
        if (this.proxyAuthHandler.isAuthenticationRequested(response, context)) {
            this.log.debug("Proxy requested authentication");
            Map<String, Header> challenges = this.proxyAuthHandler.getChallenges(response, context);
            try {
                processChallenges(challenges, this.proxyAuthState, this.proxyAuthHandler, response, context);
            } catch (AuthenticationException ex) {
                if (this.log.isWarnEnabled()) {
                    this.log.warn("Authentication error: " + ex.getMessage());
                    return null;
                }
            }
            updateAuthState(this.proxyAuthState, proxy, credsProvider);
            if (this.proxyAuthState.getCredentials() != null) {
                // Re-try the same request via the same route
                return roureq;
            } else {
                return null;
            }
        } else {
            // Reset proxy auth scope
            this.proxyAuthState.setAuthScope(null);
        }
    }
    return null;
}
Also used : HttpRequest(org.apache.http.HttpRequest) BasicHttpRequest(org.apache.http.message.BasicHttpRequest) AbortableHttpRequest(org.apache.http.client.methods.AbortableHttpRequest) AuthenticationException(org.apache.http.auth.AuthenticationException) HttpGet(org.apache.http.client.methods.HttpGet) CredentialsProvider(org.apache.http.client.CredentialsProvider) URI(java.net.URI) HttpRoute(org.apache.http.conn.routing.HttpRoute) HttpParams(org.apache.http.params.HttpParams) Header(org.apache.http.Header) HttpHost(org.apache.http.HttpHost) RedirectException(org.apache.http.client.RedirectException)

Aggregations

HttpRequest (org.apache.http.HttpRequest)155 HttpResponse (org.apache.http.HttpResponse)57 HttpContext (org.apache.http.protocol.HttpContext)56 HttpHost (org.apache.http.HttpHost)52 Test (org.junit.Test)40 IOException (java.io.IOException)36 Header (org.apache.http.Header)33 HttpException (org.apache.http.HttpException)33 HttpEntity (org.apache.http.HttpEntity)27 HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)25 BasicHttpRequest (org.apache.http.message.BasicHttpRequest)22 HttpGet (org.apache.http.client.methods.HttpGet)21 HttpPost (org.apache.http.client.methods.HttpPost)21 URI (java.net.URI)19 ProtocolException (org.apache.http.ProtocolException)18 AbortableHttpRequest (org.apache.http.client.methods.AbortableHttpRequest)16 StringEntity (org.apache.http.entity.StringEntity)16 ArrayList (java.util.ArrayList)14 NameValuePair (org.apache.http.NameValuePair)14 CredentialsProvider (org.apache.http.client.CredentialsProvider)14