Search in sources :

Example 61 with HttpParams

use of org.apache.http.params.HttpParams in project jmeter by apache.

the class HTTPHC4Impl method setupClient.

private CloseableHttpClient setupClient(URL url) {
    Map<HttpClientKey, CloseableHttpClient> mapHttpClientPerHttpClientKey = HTTPCLIENTS_CACHE_PER_THREAD_AND_HTTPCLIENTKEY.get();
    final String host = url.getHost();
    String proxyHost = getProxyHost();
    int proxyPort = getProxyPortInt();
    String proxyPass = getProxyPass();
    String proxyUser = getProxyUser();
    // static proxy is the globally define proxy eg command line or properties
    boolean useStaticProxy = isStaticProxy(host);
    // dynamic proxy is the proxy defined for this sampler
    boolean useDynamicProxy = isDynamicProxy(proxyHost, proxyPort);
    boolean useProxy = useStaticProxy || useDynamicProxy;
    // if both dynamic and static are used, the dynamic proxy has priority over static
    if (!useDynamicProxy) {
        proxyHost = PROXY_HOST;
        proxyPort = PROXY_PORT;
        proxyUser = PROXY_USER;
        proxyPass = PROXY_PASS;
    }
    // Lookup key - must agree with all the values used to create the HttpClient.
    HttpClientKey key = new HttpClientKey(url, useProxy, proxyHost, proxyPort, proxyUser, proxyPass);
    CloseableHttpClient httpClient = null;
    boolean concurrentDwn = this.testElement.isConcurrentDwn();
    if (concurrentDwn) {
        httpClient = (CloseableHttpClient) JMeterContextService.getContext().getSamplerContext().get(HTTPCLIENT_TOKEN);
    }
    if (httpClient == null) {
        httpClient = mapHttpClientPerHttpClientKey.get(key);
    }
    if (httpClient != null && resetSSLContext && HTTPConstants.PROTOCOL_HTTPS.equalsIgnoreCase(url.getProtocol())) {
        ((AbstractHttpClient) httpClient).clearRequestInterceptors();
        ((AbstractHttpClient) httpClient).clearResponseInterceptors();
        httpClient.getConnectionManager().closeIdleConnections(1L, TimeUnit.MICROSECONDS);
        httpClient = null;
        JsseSSLManager sslMgr = (JsseSSLManager) SSLManager.getInstance();
        sslMgr.resetContext();
        resetSSLContext = false;
    }
    if (httpClient == null) {
        // One-time init for this client
        HttpParams clientParams = new DefaultedHttpParams(new BasicHttpParams(), DEFAULT_HTTP_PARAMS);
        DnsResolver resolver = this.testElement.getDNSResolver();
        if (resolver == null) {
            resolver = SystemDefaultDnsResolver.INSTANCE;
        }
        MeasuringConnectionManager connManager = new MeasuringConnectionManager(createSchemeRegistry(), resolver, TIME_TO_LIVE, VALIDITY_AFTER_INACTIVITY_TIMEOUT);
        // to be realistic JMeter must set an higher value to DefaultMaxPerRoute
        if (concurrentDwn) {
            try {
                int maxConcurrentDownloads = Integer.parseInt(this.testElement.getConcurrentPool());
                connManager.setDefaultMaxPerRoute(Math.max(maxConcurrentDownloads, connManager.getDefaultMaxPerRoute()));
            } catch (NumberFormatException nfe) {
            // no need to log -> will be done by the sampler
            }
        }
        httpClient = new DefaultHttpClient(connManager, clientParams) {

            @Override
            protected HttpRequestRetryHandler createHttpRequestRetryHandler() {
                return new StandardHttpRequestRetryHandler(RETRY_COUNT, REQUEST_SENT_RETRY_ENABLED);
            }
        };
        if (IDLE_TIMEOUT > 0) {
            ((AbstractHttpClient) httpClient).setKeepAliveStrategy(IDLE_STRATEGY);
        }
        // see https://issues.apache.org/jira/browse/HTTPCORE-397
        ((AbstractHttpClient) httpClient).setReuseStrategy(DefaultClientConnectionReuseStrategy.INSTANCE);
        ((AbstractHttpClient) httpClient).addResponseInterceptor(RESPONSE_CONTENT_ENCODING);
        // HACK
        ((AbstractHttpClient) httpClient).addResponseInterceptor(METRICS_SAVER);
        ((AbstractHttpClient) httpClient).addRequestInterceptor(METRICS_RESETTER);
        // Override the default schemes as necessary
        SchemeRegistry schemeRegistry = httpClient.getConnectionManager().getSchemeRegistry();
        if (SLOW_HTTP != null) {
            schemeRegistry.register(SLOW_HTTP);
        }
        // Set up proxy details
        if (useProxy) {
            HttpHost proxy = new HttpHost(proxyHost, proxyPort);
            clientParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
            if (proxyUser.length() > 0) {
                ((AbstractHttpClient) httpClient).getCredentialsProvider().setCredentials(new AuthScope(proxyHost, proxyPort), new NTCredentials(proxyUser, proxyPass, LOCALHOST, PROXY_DOMAIN));
            }
        }
        // Bug 52126 - we do our own cookie handling
        clientParams.setParameter(ClientPNames.COOKIE_POLICY, CookieSpecs.IGNORE_COOKIES);
        if (log.isDebugEnabled()) {
            log.debug("Created new HttpClient: @" + System.identityHashCode(httpClient) + " " + key.toString());
        }
        // save the agent for next time round
        mapHttpClientPerHttpClientKey.put(key, httpClient);
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Reusing the HttpClient: @" + System.identityHashCode(httpClient) + " " + key.toString());
        }
    }
    if (concurrentDwn) {
        JMeterContextService.getContext().getSamplerContext().put(HTTPCLIENT_TOKEN, httpClient);
    }
    // TODO - should this be done when the client is created?
    // If so, then the details need to be added as part of HttpClientKey
    setConnectionAuthorization(httpClient, url, getAuthManager(), key);
    return httpClient;
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) SystemDefaultDnsResolver(org.apache.http.impl.conn.SystemDefaultDnsResolver) DnsResolver(org.apache.http.conn.DnsResolver) JsseSSLManager(org.apache.jmeter.util.JsseSSLManager) DefaultedHttpParams(org.apache.http.params.DefaultedHttpParams) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) StandardHttpRequestRetryHandler(org.apache.http.impl.client.StandardHttpRequestRetryHandler) NTCredentials(org.apache.http.auth.NTCredentials) AbstractHttpClient(org.apache.http.impl.client.AbstractHttpClient) DefaultedHttpParams(org.apache.http.params.DefaultedHttpParams) SyncBasicHttpParams(org.apache.http.params.SyncBasicHttpParams) HttpParams(org.apache.http.params.HttpParams) BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpHost(org.apache.http.HttpHost) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) AuthScope(org.apache.http.auth.AuthScope) SyncBasicHttpParams(org.apache.http.params.SyncBasicHttpParams) BasicHttpParams(org.apache.http.params.BasicHttpParams) StandardHttpRequestRetryHandler(org.apache.http.impl.client.StandardHttpRequestRetryHandler) HttpRequestRetryHandler(org.apache.http.client.HttpRequestRetryHandler)

Example 62 with HttpParams

use of org.apache.http.params.HttpParams in project wildfly by wildfly.

the class PicketLinkTestBase method makeCallWithoutRedirect.

/**
     * Requests given URL and returns redirect location URL from response header. If response is not redirected then returns the
     * same URL which was requested
     *
     * @param url               url to which the request should be made
     * @param httpClient httpClient to test multiple access
     * @return URL redirect location
     * @throws ClientProtocolException
     * @throws IOException
     * @throws URISyntaxException
     */
public static URL makeCallWithoutRedirect(URL url, HttpClient httpClient) throws IOException, URISyntaxException {
    HttpParams params = new BasicHttpParams();
    params.setParameter(ClientPNames.HANDLE_REDIRECTS, false);
    String redirectLocation = url.toExternalForm();
    final URI requestURI = Utils.replaceHost(url.toURI(), Utils.getDefaultHost(true));
    HttpGet httpGet = new HttpGet(requestURI);
    httpGet.setParams(params);
    HttpResponse response = httpClient.execute(httpGet);
    int statusCode = response.getStatusLine().getStatusCode();
    LOGGER.trace("Request to: " + requestURI + " responds: " + statusCode);
    Header locationHeader = response.getFirstHeader("location");
    if (locationHeader != null) {
        redirectLocation = locationHeader.getValue();
    }
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        EntityUtils.consume(entity);
    }
    return new URL(redirectLocation);
}
Also used : BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) Header(org.apache.http.Header) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) BasicHttpParams(org.apache.http.params.BasicHttpParams) URI(java.net.URI) URL(java.net.URL)

Example 63 with HttpParams

use of org.apache.http.params.HttpParams in project undertow by undertow-io.

the class HttpContinueAcceptingHandlerTestCase method testHttpContinueRejected.

@Test
public void testHttpContinueRejected() throws IOException {
    accept = false;
    String message = "My HTTP Request!";
    HttpParams httpParams = new BasicHttpParams();
    httpParams.setParameter("http.protocol.wait-for-continue", Integer.MAX_VALUE);
    TestHttpClient client = new TestHttpClient();
    client.setParams(httpParams);
    try {
        HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL() + "/path");
        post.addHeader("Expect", "100-continue");
        post.setEntity(new StringEntity(message));
        HttpResponse result = client.execute(post);
        Assert.assertEquals(StatusCodes.EXPECTATION_FAILED, result.getStatusLine().getStatusCode());
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) HttpResponse(org.apache.http.HttpResponse) BasicHttpParams(org.apache.http.params.BasicHttpParams) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 64 with HttpParams

use of org.apache.http.params.HttpParams in project undertow by undertow-io.

the class TestHttpClient method createHttpParams.

@Override
protected HttpParams createHttpParams() {
    HttpParams params = super.createHttpParams();
    HttpConnectionParams.setSoTimeout(params, 30000);
    return params;
}
Also used : HttpParams(org.apache.http.params.HttpParams)

Example 65 with HttpParams

use of org.apache.http.params.HttpParams in project voldemort by voldemort.

the class HttpClientBench method createClient.

private static HttpClient createClient() {
    ThreadSafeClientConnManager connectionManager = new ThreadSafeClientConnManager(SchemeRegistryFactory.createDefault(), DEFAULT_CONNECTION_MANAGER_TIMEOUT, TimeUnit.MILLISECONDS);
    DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager);
    HttpParams clientParams = httpClient.getParams();
    HttpConnectionParams.setSocketBufferSize(clientParams, 60000);
    HttpConnectionParams.setTcpNoDelay(clientParams, false);
    HttpProtocolParams.setUserAgent(clientParams, VOLDEMORT_USER_AGENT);
    HttpProtocolParams.setVersion(clientParams, HttpVersion.HTTP_1_1);
    // HostConfiguration hostConfig = new HostConfiguration();
    // hostConfig.setHost("localhost");
    HttpConnectionParams.setConnectionTimeout(clientParams, DEFAULT_CONNECTION_MANAGER_TIMEOUT);
    HttpConnectionParams.setSoTimeout(clientParams, 500);
    httpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false));
    HttpClientParams.setCookiePolicy(clientParams, CookiePolicy.IGNORE_COOKIES);
    connectionManager.setMaxTotal(DEFAULT_MAX_CONNECTIONS);
    connectionManager.setDefaultMaxPerRoute(DEFAULT_MAX_HOST_CONNECTIONS);
    HttpConnectionParams.setStaleCheckingEnabled(clientParams, false);
    return httpClient;
}
Also used : HttpParams(org.apache.http.params.HttpParams) ThreadSafeClientConnManager(org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager) DefaultHttpRequestRetryHandler(org.apache.http.impl.client.DefaultHttpRequestRetryHandler) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Aggregations

HttpParams (org.apache.http.params.HttpParams)121 BasicHttpParams (org.apache.http.params.BasicHttpParams)85 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)43 SchemeRegistry (org.apache.http.conn.scheme.SchemeRegistry)31 Scheme (org.apache.http.conn.scheme.Scheme)30 ClientConnectionManager (org.apache.http.conn.ClientConnectionManager)28 IOException (java.io.IOException)27 ThreadSafeClientConnManager (org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager)27 HttpResponse (org.apache.http.HttpResponse)24 HttpHost (org.apache.http.HttpHost)23 Header (org.apache.http.Header)18 HttpGet (org.apache.http.client.methods.HttpGet)13 URI (java.net.URI)10 HttpRequest (org.apache.http.HttpRequest)10 HttpClient (org.apache.http.client.HttpClient)10 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)10 SSLSocketFactory (org.apache.http.conn.ssl.SSLSocketFactory)10 Socket (java.net.Socket)9 ClientProtocolException (org.apache.http.client.ClientProtocolException)9 GeneralSecurityException (java.security.GeneralSecurityException)8