Skip to content

Commit

Permalink
HttpProxyHandler: allow setting headers
Browse files Browse the repository at this point in the history
Motivation:

In some environments, the HTTP CONNECT handshake requires special headers to work.

Modification:

Update HttpProxyHandler to accept a HttpHeaders argument.

Result:

The header is passed along in the HTTP CONNECT request, and the proxy request can be successfully completed.
  • Loading branch information
zpencer authored and normanmaurer committed Jul 6, 2017
1 parent 2a376ee commit ec490b2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpClientCodec;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
Expand All @@ -47,15 +48,26 @@ public final class HttpProxyHandler extends ProxyHandler {
private final String password;
private final CharSequence authorization;
private HttpResponseStatus status;
private HttpHeaders headers;

public HttpProxyHandler(SocketAddress proxyAddress) {
this(proxyAddress, null);
}

public HttpProxyHandler(SocketAddress proxyAddress, HttpHeaders headers) {
super(proxyAddress);
username = null;
password = null;
authorization = null;
this.headers = headers;
}

public HttpProxyHandler(SocketAddress proxyAddress, String username, String password) {
this(proxyAddress, username, password, null);
}

public HttpProxyHandler(SocketAddress proxyAddress, String username, String password,
HttpHeaders headers) {
super(proxyAddress);
if (username == null) {
throw new NullPointerException("username");
Expand All @@ -73,6 +85,8 @@ public HttpProxyHandler(SocketAddress proxyAddress, String username, String pass

authz.release();
authzBase64.release();

this.headers = headers;
}

@Override
Expand Down Expand Up @@ -125,6 +139,10 @@ protected Object newInitialMessage(ChannelHandlerContext ctx) throws Exception {
req.headers().set(HttpHeaderNames.PROXY_AUTHORIZATION, authorization);
}

if (headers != null) {
req.headers().add(headers);
}

return req;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.http.DefaultHttpHeaders;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.util.NetUtil;
import org.junit.Test;
Expand All @@ -34,28 +36,37 @@ public class HttpProxyHandlerTest {
@Test
public void testIpv6() throws Exception {
InetSocketAddress socketAddress = new InetSocketAddress(InetAddress.getByName("::1"), 8080);
testInitialMessage(socketAddress, "[::1]:8080");
testInitialMessage(socketAddress, "[::1]:8080", null);
}

@Test
public void testIpv6Unresolved() throws Exception {
InetSocketAddress socketAddress = InetSocketAddress.createUnresolved("::1", 8080);
testInitialMessage(socketAddress, "[::1]:8080");
testInitialMessage(socketAddress, "[::1]:8080", null);
}

@Test
public void testIpv4() throws Exception {
InetSocketAddress socketAddress = new InetSocketAddress(InetAddress.getByName("10.0.0.1"), 8080);
testInitialMessage(socketAddress, "10.0.0.1:8080");
testInitialMessage(socketAddress, "10.0.0.1:8080", null);
}

@Test
public void testIpv4Unresolved() throws Exception {
InetSocketAddress socketAddress = InetSocketAddress.createUnresolved("10.0.0.1", 8080);
testInitialMessage(socketAddress, "10.0.0.1:8080");
testInitialMessage(socketAddress, "10.0.0.1:8080", null);
}

private static void testInitialMessage(InetSocketAddress socketAddress, String expected) throws Exception {
@Test
public void testCustomHeaders() throws Exception {
InetSocketAddress socketAddress = InetSocketAddress.createUnresolved("10.0.0.1", 8080);
testInitialMessage(socketAddress, "10.0.0.1:8080",
new DefaultHttpHeaders().add("CUSTOM_HEADER", "CUSTOM_VALUE1")
.add("CUSTOM_HEADER", "CUSTOM_VALUE2"));
}

private static void testInitialMessage(InetSocketAddress socketAddress, String hostPort,
HttpHeaders headers) throws Exception {
InetSocketAddress proxyAddress = new InetSocketAddress(NetUtil.LOCALHOST, 8080);

ChannelPromise promise = mock(ChannelPromise.class);
Expand All @@ -64,14 +75,22 @@ private static void testInitialMessage(InetSocketAddress socketAddress, String e
ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
when(ctx.connect(same(proxyAddress), isNull(InetSocketAddress.class), same(promise))).thenReturn(promise);

HttpProxyHandler handler = new HttpProxyHandler(new InetSocketAddress(NetUtil.LOCALHOST, 8080));
HttpProxyHandler handler = new HttpProxyHandler(new InetSocketAddress(NetUtil.LOCALHOST, 8080), headers);
handler.connect(ctx, socketAddress, null, promise);

FullHttpRequest request = (FullHttpRequest) handler.newInitialMessage(ctx);
try {
assertEquals(HttpVersion.HTTP_1_1, request.protocolVersion());
assertEquals(expected, request.uri());
assertEquals(expected, request.headers().get(HttpHeaderNames.HOST));
assertEquals(hostPort, request.uri());
HttpHeaders actualHeaders = request.headers();
assertEquals(hostPort, actualHeaders.get(HttpHeaderNames.HOST));

if (headers != null) {
// The actual request header is a strict superset of the custom header
for (String name : headers.names()) {
assertEquals(headers.getAll(name), actualHeaders.getAll(name));
}
}
} finally {
request.release();
}
Expand Down

0 comments on commit ec490b2

Please sign in to comment.