Skip to content

Commit df775c9

Browse files
committed
Ensured relative URIs are used to connect to proxied WebSockets
This needed to be done both for the Grizzly and the Netty provider. Since this is essentially the same configuration parameter as the useRelativeURIsWithSSLProxies, I reused that parameter, but I renamed it to useRelativeURIsWithConnectProxies to reflect its true meaning since it's no longer used just for SSL, and I deprecated the old parameter.
1 parent 2946123 commit df775c9

File tree

5 files changed

+68
-16
lines changed

5 files changed

+68
-16
lines changed

src/main/java/com/ning/http/client/AsyncHttpClientConfig.java

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public class AsyncHttpClientConfig {
8383
protected HostnameVerifier hostnameVerifier;
8484
protected int ioThreadMultiplier;
8585
protected boolean strict302Handling;
86-
protected boolean useRelativeURIsWithSSLProxies;
86+
protected boolean useRelativeURIsWithConnectProxies;
8787
protected int maxConnectionLifeTimeInMs;
8888
protected TimeConverter timeConverter;
8989

@@ -120,7 +120,7 @@ private AsyncHttpClientConfig(int maxTotalConnections,
120120
HostnameVerifier hostnameVerifier,
121121
int ioThreadMultiplier,
122122
boolean strict302Handling,
123-
boolean useRelativeURIsWithSSLProxies,
123+
boolean useRelativeURIsWithConnectProxies,
124124
TimeConverter timeConverter) {
125125

126126
this.maxTotalConnections = maxTotalConnections;
@@ -151,7 +151,7 @@ private AsyncHttpClientConfig(int maxTotalConnections,
151151
this.hostnameVerifier = hostnameVerifier;
152152
this.ioThreadMultiplier = ioThreadMultiplier;
153153
this.strict302Handling = strict302Handling;
154-
this.useRelativeURIsWithSSLProxies = useRelativeURIsWithSSLProxies;
154+
this.useRelativeURIsWithConnectProxies = useRelativeURIsWithConnectProxies;
155155

156156
if (applicationThreadPool == null) {
157157
this.applicationThreadPool = Executors.newCachedThreadPool();
@@ -494,15 +494,27 @@ public boolean isStrict302Handling() {
494494
}
495495

496496
/**
497-
* @return<code>true</code> if AHC should use relative URIs instead of absolute ones when talking with a SSL proxy,
498-
* otherwise <code>false</code>.
497+
* @return<code>true</code> if AHC should use relative URIs instead of absolute ones when talking with a SSL proxy
498+
* or WebSocket proxy, otherwise <code>false</code>.
499499
*
500500
* @since 1.7.12
501+
* @deprecated Use isUseRelativeURIsWithConnectProxies instead.
501502
*/
503+
@Deprecated
502504
public boolean isUseRelativeURIsWithSSLProxies() {
503-
return useRelativeURIsWithSSLProxies;
505+
return useRelativeURIsWithConnectProxies;
506+
}
507+
508+
/**
509+
* @return<code>true</code> if AHC should use relative URIs instead of absolute ones when talking with a proxy
510+
* using the CONNECT method, for example when using SSL or WebSockets.
511+
*
512+
* @since 1.8.13
513+
*/
514+
public boolean isUseRelativeURIsWithConnectProxies() {
515+
return useRelativeURIsWithConnectProxies;
504516
}
505-
517+
506518
/**
507519
* Return the maximum time in millisecond an {@link com.ning.http.client.AsyncHttpClient} will keep connection in the pool, or -1 to keep connection while possible.
508520
*
@@ -538,7 +550,7 @@ public static class Builder {
538550
private boolean useProxyProperties = defaultUseProxyProperties();
539551
private boolean useProxySelector = defaultUseProxySelector();
540552
private boolean allowPoolingConnection = defaultAllowPoolingConnection();
541-
private boolean useRelativeURIsWithSSLProxies = defaultUseRelativeURIsWithSSLProxies();
553+
private boolean useRelativeURIsWithConnectProxies = defaultUseRelativeURIsWithConnectProxies();
542554
private int requestCompressionLevel = defaultRequestCompressionLevel();
543555
private int maxRequestRetry = defaultMaxRequestRetry();
544556
private int ioThreadMultiplier = defaultIoThreadMultiplier();
@@ -1005,15 +1017,31 @@ public Builder setStrict302Handling(final boolean strict302Handling) {
10051017
}
10061018

10071019
/**
1008-
* Configures this AHC instance to use relative URIs instead of absolute ones when talking with a SSL proxy.
1020+
* Configures this AHC instance to use relative URIs instead of absolute ones when talking with a SSL proxy or
1021+
* WebSocket proxy.
10091022
*
10101023
* @param useRelativeURIsWithSSLProxies
10111024
* @return this
10121025
*
10131026
* @since 1.7.2
1027+
* @deprecated Use setUseRelativeURIsWithConnectProxies instead.
10141028
*/
10151029
public Builder setUseRelativeURIsWithSSLProxies(boolean useRelativeURIsWithSSLProxies) {
1016-
this.useRelativeURIsWithSSLProxies = useRelativeURIsWithSSLProxies;
1030+
this.useRelativeURIsWithConnectProxies = useRelativeURIsWithSSLProxies;
1031+
return this;
1032+
}
1033+
1034+
/**
1035+
* Configures this AHC instance to use relative URIs instead of absolute ones when making requests through
1036+
* proxies using the CONNECT method, such as when making SSL requests or WebSocket requests.
1037+
*
1038+
* @param useRelativeURIsWithConnectProxies
1039+
* @return this
1040+
*
1041+
* @since 1.8.13
1042+
*/
1043+
public Builder setUseRelativeURIsWithConnectProxies(boolean useRelativeURIsWithConnectProxies) {
1044+
this.useRelativeURIsWithConnectProxies = useRelativeURIsWithConnectProxies;
10171045
return this;
10181046
}
10191047

@@ -1139,7 +1167,7 @@ public Thread newThread(Runnable r) {
11391167
hostnameVerifier,
11401168
ioThreadMultiplier,
11411169
strict302Handling,
1142-
useRelativeURIsWithSSLProxies,
1170+
useRelativeURIsWithConnectProxies,
11431171
timeConverter);
11441172
}
11451173
}

src/main/java/com/ning/http/client/AsyncHttpClientConfigBean.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void configureDefaults() {
5858
compressionEnabled = defaultCompressionEnabled();
5959
userAgent = defaultUserAgent();
6060
allowPoolingConnection = defaultAllowPoolingConnection();
61-
useRelativeURIsWithSSLProxies = defaultUseRelativeURIsWithSSLProxies();
61+
useRelativeURIsWithConnectProxies = defaultUseRelativeURIsWithConnectProxies();
6262
requestCompressionLevel = defaultRequestCompressionLevel();
6363
maxRequestRetry = defaultMaxRequestRetry();
6464
ioThreadMultiplier = defaultIoThreadMultiplier();

src/main/java/com/ning/http/client/AsyncHttpClientConfigDefaults.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,17 @@
1313
package com.ning.http.client;
1414

1515
import com.ning.http.util.AllowAllHostnameVerifier;
16+
import org.slf4j.Logger;
17+
import org.slf4j.LoggerFactory;
18+
1619
import static com.ning.http.util.MiscUtil.getBoolean;
1720

1821
import javax.net.ssl.HostnameVerifier;
1922

2023
public final class AsyncHttpClientConfigDefaults {
2124

25+
private final static Logger log = LoggerFactory.getLogger(AsyncHttpClientConfigDefaults.class);
26+
2227
private AsyncHttpClientConfigDefaults() {
2328
}
2429

@@ -92,8 +97,23 @@ public static boolean defaultAllowPoolingConnection() {
9297
return getBoolean(ASYNC_CLIENT + "allowPoolingConnection", true);
9398
}
9499

100+
/**
101+
* @deprecated Use defaultUseRelativeURIsWithConnectProxies instead.
102+
*/
103+
@Deprecated
95104
public static boolean defaultUseRelativeURIsWithSSLProxies() {
96-
return getBoolean(ASYNC_CLIENT + "useRelativeURIsWithSSLProxies", true);
105+
String systemPropValue = System.getProperty(ASYNC_CLIENT + "useRelativeURIsWithSSLProxies");
106+
if (systemPropValue != null) {
107+
log.warn(ASYNC_CLIENT + "useRelativeURIsWithSSLProxies is deprecated, use " + ASYNC_CLIENT +
108+
"defaultUseRelativeURIsWithConnectProxies instead");
109+
return systemPropValue.equalsIgnoreCase("true");
110+
} else {
111+
return true;
112+
}
113+
}
114+
115+
public static boolean defaultUseRelativeURIsWithConnectProxies() {
116+
return getBoolean(ASYNC_CLIENT + "useRelativeURIsWithConnectProxies", defaultUseRelativeURIsWithSSLProxies());
97117
}
98118

99119
// unused/broken, left there for compatibility, fixed in Netty 4

src/main/java/com/ning/http/client/providers/grizzly/GrizzlyAsyncHttpProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,7 @@ private boolean sendAsGrizzlyRequest(final Request request,
891891
httpCtx.establishingTunnel = true;
892892
builder.method(Method.CONNECT);
893893
builder.uri(AsyncHttpProviderUtils.getAuthority(uri));
894-
} else if (secure && config.isUseRelativeURIsWithSSLProxies()){
894+
} else if ((secure || httpCtx.isWSRequest) && config.isUseRelativeURIsWithConnectProxies()){
895895
builder.uri(uri.getPath());
896896
} else {
897897
builder.uri(uri.toString());

src/main/java/com/ning/http/client/providers/netty/NettyAsyncHttpProvider.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,12 +649,16 @@ protected final static HttpRequest buildRequest(AsyncHttpClientConfig config, Re
649649
throws IOException {
650650

651651
String method = request.getMethod();
652-
if (allowConnect && proxyServer != null && (isSecure(uri) || isWebSocket(uri.getScheme()))) {
652+
if (allowConnect && proxyServer != null && useProxyConnect(uri)) {
653653
method = HttpMethod.CONNECT.toString();
654654
}
655655
return construct(config, request, new HttpMethod(method), uri, buffer, proxyServer);
656656
}
657657

658+
protected final static boolean useProxyConnect(URI uri) {
659+
return isSecure(uri) || isWebSocket(uri.getScheme());
660+
}
661+
658662
private static SpnegoEngine getSpnegoEngine() {
659663
if (spnegoEngine == null)
660664
spnegoEngine = new SpnegoEngine();
@@ -676,7 +680,7 @@ private static HttpRequest construct(AsyncHttpClientConfig config, Request reque
676680
nettyRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_0, m, AsyncHttpProviderUtils.getAuthority(uri));
677681
} else {
678682
String path = null;
679-
if (proxyServer != null && !(isSecure(uri) && config.isUseRelativeURIsWithSSLProxies()))
683+
if (proxyServer != null && !(useProxyConnect(uri) && config.isUseRelativeURIsWithConnectProxies()))
680684
path = uri.toString();
681685
else if (uri.getRawQuery() != null)
682686
path = uri.getRawPath() + "?" + uri.getRawQuery();

0 commit comments

Comments
 (0)