use of org.asynchttpclient.proxy.ProxyServer in project async-http-client by AsyncHttpClient.
the class ProxyTunnellingTest method runTest.
private void runTest(boolean secure) throws Exception {
setUpServers(secure);
String targetUrl = String.format("%s://localhost:%d/", secure ? "wss" : "ws", port2);
// CONNECT happens over HTTP, not HTTPS
ProxyServer ps = proxyServer("localhost", port1).build();
try (AsyncHttpClient asyncHttpClient = asyncHttpClient(config().setProxyServer(ps).setUseInsecureTrustManager(true))) {
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<String> text = new AtomicReference<>("");
WebSocket websocket = asyncHttpClient.prepareGet(targetUrl).execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketTextListener() {
@Override
public void onMessage(String message) {
text.set(message);
latch.countDown();
}
@Override
public void onOpen(WebSocket websocket) {
}
@Override
public void onClose(WebSocket websocket) {
latch.countDown();
}
@Override
public void onError(Throwable t) {
t.printStackTrace();
latch.countDown();
}
}).build()).get();
websocket.sendMessage("ECHO");
latch.await();
assertEquals(text.get(), "ECHO");
}
}
use of org.asynchttpclient.proxy.ProxyServer in project async-http-client by AsyncHttpClient.
the class Interceptors method exitAfterIntercept.
public //
boolean exitAfterIntercept(//
Channel channel, //
NettyResponseFuture<?> future, //
AsyncHandler<?> handler, //
HttpResponse response, //
HttpResponseStatus status, HttpResponseHeaders responseHeaders) throws Exception {
HttpRequest httpRequest = future.getNettyRequest().getHttpRequest();
ProxyServer proxyServer = future.getProxyServer();
int statusCode = response.status().code();
Request request = future.getCurrentRequest();
Realm realm = request.getRealm() != null ? request.getRealm() : config.getRealm();
if (hasResponseFilters && responseFiltersInterceptor.exitAfterProcessingFilters(channel, future, handler, status, responseHeaders)) {
return true;
}
if (statusCode == UNAUTHORIZED_401) {
return unauthorized401Interceptor.exitAfterHandling401(channel, future, response, request, statusCode, realm, proxyServer, httpRequest);
} else if (statusCode == PROXY_AUTHENTICATION_REQUIRED_407) {
return proxyUnauthorized407Interceptor.exitAfterHandling407(channel, future, response, request, statusCode, proxyServer, httpRequest);
} else if (statusCode == CONTINUE_100) {
return continue100Interceptor.exitAfterHandling100(channel, future, statusCode);
} else if (Redirect30xInterceptor.REDIRECT_STATUSES.contains(statusCode)) {
return redirect30xInterceptor.exitAfterHandlingRedirect(channel, future, response, request, statusCode, realm);
} else if (httpRequest.method() == HttpMethod.CONNECT && statusCode == OK_200) {
return connectSuccessInterceptor.exitAfterHandlingConnect(channel, future, request, proxyServer, statusCode, httpRequest);
}
return false;
}
use of org.asynchttpclient.proxy.ProxyServer in project async-http-client by AsyncHttpClient.
the class ProxyUtils method createProxyServerSelector.
/**
* Creates a proxy server instance from the given properties.
* Currently the default http.* proxy properties are supported as well as properties specific for AHC.
*
* @param properties the properties to evaluate. Must not be null.
* @return a ProxyServer instance or null, if no valid properties were set.
* @see <a href="https://docs.oracle.com/javase/8/docs/api/java/net/doc-files/net-properties.html">Networking Properties</a>
* @see #PROXY_HOST
* @see #PROXY_PORT
* @see #PROXY_NONPROXYHOSTS
*/
public static ProxyServerSelector createProxyServerSelector(Properties properties) {
String host = properties.getProperty(PROXY_HOST);
if (host != null) {
int port = Integer.valueOf(properties.getProperty(PROXY_PORT, "80"));
String principal = properties.getProperty(PROXY_USER);
String password = properties.getProperty(PROXY_PASSWORD);
Realm realm = null;
if (principal != null) {
realm = basicAuthRealm(principal, password).build();
}
ProxyServer.Builder proxyServer = proxyServer(host, port).setRealm(realm);
String nonProxyHosts = properties.getProperty(PROXY_NONPROXYHOSTS);
if (nonProxyHosts != null) {
proxyServer.setNonProxyHosts(new ArrayList<>(Arrays.asList(nonProxyHosts.split("\\|"))));
}
ProxyServer proxy = proxyServer.build();
return uri -> proxy;
}
return ProxyServerSelector.NO_PROXY_SELECTOR;
}
use of org.asynchttpclient.proxy.ProxyServer in project async-http-client by AsyncHttpClient.
the class NettyRequestSender method sendRequest.
public <//
T> //
ListenableFuture<T> sendRequest(//
final Request request, //
final AsyncHandler<T> asyncHandler, //
NettyResponseFuture<T> future, boolean performingNextRequest) {
if (isClosed())
throw new IllegalStateException("Closed");
validateWebSocketRequest(request, asyncHandler);
ProxyServer proxyServer = getProxyServer(config, request);
// websockets use connect tunnelling to work with proxies
if (proxyServer != null && (request.getUri().isSecured() || request.getUri().isWebSocket()) && !isConnectDone(request, future))
if (future != null && future.isConnectAllowed())
// SSL proxy or websocket: CONNECT for sure
return sendRequestWithCertainForceConnect(request, asyncHandler, future, performingNextRequest, proxyServer, true);
else
// CONNECT will depend if we can pool or connection or if we have to open a new one
return sendRequestThroughSslProxy(request, asyncHandler, future, performingNextRequest, proxyServer);
else
// no CONNECT for sure
return sendRequestWithCertainForceConnect(request, asyncHandler, future, performingNextRequest, proxyServer, false);
}
Aggregations