[ 
https://issues.apache.org/jira/browse/HTTPCLIENT-2310?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17790562#comment-17790562
 ] 

Marian commented on HTTPCLIENT-2310:
------------------------------------

[~olegk], there is a code example: 

 
{code:java}
package org.example;

import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
import org.apache.hc.client5.http.async.methods.SimpleRequestProducer;
import org.apache.hc.client5.http.config.ConnectionConfig;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.config.TlsConfig;
import org.apache.hc.client5.http.cookie.StandardCookieSpec;
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
import org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder;
import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
import 
org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder;
import org.apache.hc.client5.http.ssl.ClientTlsStrategyBuilder;
import org.apache.hc.core5.concurrent.FutureCallback;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.Message;
import org.apache.hc.core5.http.nio.AsyncResponseConsumer;
import org.apache.hc.core5.http.nio.entity.BasicAsyncEntityConsumer;
import org.apache.hc.core5.http.nio.support.BasicResponseConsumer;
import org.apache.hc.core5.http.ssl.TLS;
import org.apache.hc.core5.http2.HttpVersionPolicy;
import org.apache.hc.core5.io.CloseMode;
import org.apache.hc.core5.pool.PoolConcurrencyPolicy;
import org.apache.hc.core5.pool.PoolReusePolicy;
import org.apache.hc.core5.reactor.IOReactorConfig;
import org.apache.hc.core5.util.Timeout;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;

public class App 
{
    public static void main(final String[] args) throws Exception {

        final HttpAsyncClientBuilder clientBuilder = HttpAsyncClients.custom()
                .addRequestInterceptorFirst((request, entity, context) -> {
                    String method = request.getMethod();
                    System.out.println("method: " + method);
                    if (method.equals("CONNECT")) {
                        request.addHeader("Custom-Header", "value");
                    }
                })
                
.setConnectionManager(PoolingAsyncClientConnectionManagerBuilder.create()
                        .setTlsStrategy(ClientTlsStrategyBuilder.create()
                                .setTlsVersions(TLS.V_1_3)
                                .build())
                        .setPoolConcurrencyPolicy(PoolConcurrencyPolicy.LAX)
                        .setConnPoolPolicy(PoolReusePolicy.LIFO)
                        .setDefaultConnectionConfig(ConnectionConfig.custom()
                                .setSocketTimeout(Timeout.ofMinutes(1))
                                .setConnectTimeout(Timeout.ofMinutes(1))
                                .setTimeToLive(Timeout.ofMinutes(1))
                                .build())
                        .setDefaultTlsConfig(TlsConfig.custom()
                                
.setVersionPolicy(HttpVersionPolicy.FORCE_HTTP_2)
                                .setHandshakeTimeout(Timeout.ofMinutes(1))
                                .build())
                        .build())
                .setIOReactorConfig(IOReactorConfig.custom()
                        .setSoTimeout(Timeout.ofMinutes(1))
                        .setIoThreadCount(1)
                        .build())
                .setDefaultRequestConfig(RequestConfig.custom()
                        .setCookieSpec(StandardCookieSpec.STRICT)
                        .build())
                .disableCookieManagement()
                .setProxy(new HttpHost("38.15.154.224", 3128)); //Set your own 
proxy

        final CloseableHttpAsyncClient client = clientBuilder.build();
        client.start();

        CountDownLatch latch = new CountDownLatch(1);

        final String requestUri = "https://www.google.com/";;
        final SimpleHttpRequest request = 
SimpleRequestBuilder.get(requestUri).build();
        final SimpleRequestProducer producer = 
SimpleRequestProducer.create(request);
        final AsyncResponseConsumer<Message<HttpResponse, byte[]>> consumer = 
new BasicResponseConsumer<>(new BasicAsyncEntityConsumer());

        System.out.println("Executing request " + request);
        FutureCallback<Message<HttpResponse, byte[]>> callback = new 
FutureCallback<Message<HttpResponse, byte[]>>() {

            @Override
            public void completed(Message<HttpResponse, byte[]> response) {
                latch.countDown();
                System.out.println(response.getHead().getVersion() + " " + 
response.getHead().getCode());
            }

            @Override
            public void failed(Exception ex) {
                latch.countDown();
                System.out.println("Error executing HTTP request: " + 
ex.getMessage());
            }

            @Override
            public void cancelled() {
                latch.countDown();
                System.out.println("HTTP request execution cancelled");
            }

        };

        Future<Message<org.apache.hc.core5.http.HttpResponse, byte[]>> future = 
client.execute(producer, consumer, callback);
        latch.await();
        future.get();

        System.out.println("Shutting down");
        client.close(CloseMode.GRACEFUL);
    }

}
{code}
 

 

The main part of the code is proxy. Due to a proxy, we can catch connect: 

 
{code:java}
Executing request GET https://www.google.com/
method: CONNECT
method: GET
HTTP/2.0 200
Shutting downProcess finished with exit code 0 {code}
This helps us to throw our request to the server with custom headers:

 

!image-2023-11-28-15-12-20-327.png|width=681,height=133!

This won't work without the proxy, but if the client can catch it in the 
described case, could it catch the "connect" request every time?

> HttpRequestInterceptor doesn't catch a CONNECT request
> ------------------------------------------------------
>
>                 Key: HTTPCLIENT-2310
>                 URL: https://issues.apache.org/jira/browse/HTTPCLIENT-2310
>             Project: HttpComponents HttpClient
>          Issue Type: Wish
>          Components: HttpClient (classic)
>    Affects Versions: 5.2.2, 5.2.1
>            Reporter: Marian
>            Priority: Minor
>         Attachments: image-2023-11-28-15-12-04-268.png, 
> image-2023-11-28-15-12-20-327.png
>
>
> HttpRequestInterceptor can be added to both CloseableHttpClient and 
> CloseableHttpAsyncClient, but a CONNECT request can be caught only with 
> CloseableHttpAsyncClient. 
> Even if I use the following examples: [for 
> async|https://github.com/apache/httpcomponents-client/blob/5.2.x/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientInterceptors.java]
>  or [for 
> classic|https://github.com/apache/httpcomponents-client/blob/5.2.x/httpclient5/src/test/java/org/apache/hc/client5/http/examples/ClientInterceptors.java]
>  from GitHub.
> Could it be possible to fix this issue and cover the "connect" request in 
> CloseableHttpClient's request interceptor?



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@hc.apache.org
For additional commands, e-mail: dev-h...@hc.apache.org

Reply via email to