Only add port to HOST header value if needed

Motivation:

We only need to add the port to the HOST header value if its not a standard port.

Modifications:

- Only add port if needed.
- Fix parsing of ipv6 address which is enclosed by [].

Result:

Fixes [#6426].
This commit is contained in:
Norman Maurer 2017-02-27 17:56:42 +01:00
parent 57fd316a82
commit 8b2badf44f
7 changed files with 107 additions and 5 deletions

View File

@ -31,9 +31,11 @@ import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestEncoder;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpResponseDecoder;
import io.netty.util.NetUtil;
import io.netty.util.ReferenceCountUtil;
import io.netty.util.internal.ThrowableUtil;
import java.net.InetSocketAddress;
import java.net.URI;
import java.nio.channels.ClosedChannelException;
@ -451,13 +453,35 @@ public abstract class WebSocketClientHandshaker {
return wsPort;
}
static CharSequence websocketHostValue(URI wsURL) {
int port = wsURL.getPort();
if (port == -1) {
return wsURL.getHost();
}
String host = wsURL.getHost();
if (port == 80) {
return "http".equals(wsURL.getScheme())
|| "ws".equals(wsURL.getScheme()) ?
host : NetUtil.toSocketAddressString(host, 80);
}
if (port == 443) {
return "https".equals(wsURL.getScheme())
|| "wss".equals(wsURL.getScheme()) ?
host : NetUtil.toSocketAddressString(host, 443);
}
// if the port is not standard (80/443) its needed to add the port to the header.
// See http://tools.ietf.org/html/rfc6454#section-6.2
return NetUtil.toSocketAddressString(InetSocketAddress.createUnresolved(host, port));
}
static CharSequence websocketOriginValue(String host, int wsPort) {
String originValue = (wsPort == 443 ?
"https" : "http") + "://" + host;
if (wsPort != 80 && wsPort != 443) {
// if the port is not standard (80/443) its needed to add the port to the header.
// See http://tools.ietf.org/html/rfc6454#section-6.2
return originValue + ':' + wsPort;
return NetUtil.toSocketAddressString(originValue, wsPort);
}
return originValue;
}

View File

@ -132,7 +132,7 @@ public class WebSocketClientHandshaker00 extends WebSocketClientHandshaker {
HttpHeaders headers = request.headers();
headers.add(HttpHeaders.Names.UPGRADE, HttpHeaders.Values.WEBSOCKET)
.add(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.UPGRADE)
.add(HttpHeaders.Names.HOST, host)
.add(HttpHeaders.Names.HOST, websocketHostValue(wsURL))
.add(HttpHeaders.Names.ORIGIN, websocketOriginValue(host, wsPort))
.add(HttpHeaders.Names.SEC_WEBSOCKET_KEY1, key1)
.add(HttpHeaders.Names.SEC_WEBSOCKET_KEY2, key2);

View File

@ -117,7 +117,7 @@ public class WebSocketClientHandshaker07 extends WebSocketClientHandshaker {
headers.add(HttpHeaders.Names.UPGRADE, HttpHeaders.Values.WEBSOCKET)
.add(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.UPGRADE)
.add(HttpHeaders.Names.SEC_WEBSOCKET_KEY, key)
.add(HttpHeaders.Names.HOST, host)
.add(HttpHeaders.Names.HOST, websocketHostValue(wsURL))
.add(HttpHeaders.Names.SEC_WEBSOCKET_ORIGIN, websocketOriginValue(host, wsPort));
String expectedSubprotocol = expectedSubprotocol();

View File

@ -117,7 +117,7 @@ public class WebSocketClientHandshaker08 extends WebSocketClientHandshaker {
headers.add(HttpHeaders.Names.UPGRADE, HttpHeaders.Values.WEBSOCKET)
.add(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.UPGRADE)
.add(HttpHeaders.Names.SEC_WEBSOCKET_KEY, key)
.add(HttpHeaders.Names.HOST, host)
.add(HttpHeaders.Names.HOST, websocketHostValue(wsURL))
.add(HttpHeaders.Names.SEC_WEBSOCKET_ORIGIN, websocketOriginValue(host, wsPort));
String expectedSubprotocol = expectedSubprotocol();

View File

@ -116,7 +116,7 @@ public class WebSocketClientHandshaker13 extends WebSocketClientHandshaker {
headers.add(HttpHeaders.Names.UPGRADE, HttpHeaders.Values.WEBSOCKET)
.add(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.UPGRADE)
.add(HttpHeaders.Names.SEC_WEBSOCKET_KEY, key)
.add(HttpHeaders.Names.HOST, host + ':' + wsPort)
.add(HttpHeaders.Names.HOST, websocketHostValue(wsURL))
.add(HttpHeaders.Names.SEC_WEBSOCKET_ORIGIN, websocketOriginValue(host, wsPort));
String expectedSubprotocol = expectedSubprotocol();

View File

@ -40,6 +40,37 @@ import static org.junit.Assert.assertTrue;
public abstract class WebSocketClientHandshakerTest {
protected abstract WebSocketClientHandshaker newHandshaker(URI uri);
@Test
public void testHostHeader() {
testHostHeaderDefaultHttp(URI.create("ws://localhost:80/"), "localhost");
testHostHeaderDefaultHttp(URI.create("http://localhost:80/"), "localhost");
testHostHeaderDefaultHttp(URI.create("ws://[::1]:80/"), "[::1]");
testHostHeaderDefaultHttp(URI.create("http://[::1]:80/"), "[::1]");
testHostHeaderDefaultHttp(URI.create("ws://localhost:9999/"), "localhost:9999");
testHostHeaderDefaultHttp(URI.create("http://localhost:9999/"), "localhost:9999");
testHostHeaderDefaultHttp(URI.create("ws://[::1]:9999/"), "[::1]:9999");
testHostHeaderDefaultHttp(URI.create("http://[::1]:9999/"), "[::1]:9999");
testHostHeaderDefaultHttp(URI.create("wss://localhost:443/"), "localhost");
testHostHeaderDefaultHttp(URI.create("https://localhost:443/"), "localhost");
testHostHeaderDefaultHttp(URI.create("wss://[::1]:443/"), "[::1]");
testHostHeaderDefaultHttp(URI.create("https://[::1]:443/"), "[::1]");
testHostHeaderDefaultHttp(URI.create("wss://localhost:9999/"), "localhost:9999");
testHostHeaderDefaultHttp(URI.create("https://localhost:9999/"), "localhost:9999");
testHostHeaderDefaultHttp(URI.create("wss://[::1]:9999/"), "[::1]:9999");
testHostHeaderDefaultHttp(URI.create("https://[::1]:9999/"), "[::1]:9999");
}
private void testHostHeaderDefaultHttp(URI uri, String expected) {
WebSocketClientHandshaker handshaker = newHandshaker(uri);
FullHttpRequest request = handshaker.newHandshakeRequest();
try {
assertEquals(expected, request.headers().get(HttpHeaders.Names.HOST));
} finally {
request.release();
}
}
@Test
public void testRawPath() {
URI uri = URI.create("ws://localhost:9999/path%20with%20ws");

View File

@ -26,6 +26,7 @@ import java.io.FileReader;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
@ -870,6 +871,52 @@ public final class NetUtil {
}
/**
* Returns the {@link String} representation of an {@link InetSocketAddress}.
* <p>
* The output does not include Scope ID.
* @param addr {@link InetSocketAddress} to be converted to an address string
* @return {@code String} containing the text-formatted IP address
*/
public static String toSocketAddressString(InetSocketAddress addr) {
String port = String.valueOf(addr.getPort());
final StringBuilder sb;
if (addr.isUnresolved()) {
String hostString = PlatformDependent.javaVersion() >= 7 ? addr.getHostString() : addr.getHostName();
sb = newSocketAddressStringBuilder(hostString, port, !isValidIpV6Address(hostString));
} else {
InetAddress address = addr.getAddress();
String hostString = toAddressString(address);
sb = newSocketAddressStringBuilder(hostString, port, address instanceof Inet4Address);
}
return sb.append(':').append(port).toString();
}
/**
* Returns the {@link String} representation of a host port combo.
*/
public static String toSocketAddressString(String host, int port) {
String portStr = String.valueOf(port);
return newSocketAddressStringBuilder(
host, portStr, isValidIpV4Address(host)).append(portStr).toString();
}
private static StringBuilder newSocketAddressStringBuilder(String host, String port, boolean ipv4) {
int hostLen = host.length();
if (ipv4) {
// Need to include enough space for hostString:port.
return new StringBuilder(hostLen + 1 + port.length()).append(host);
}
// Need to include enough space for [hostString]:port.
StringBuilder stringBuilder = new StringBuilder(hostLen + 3 + port.length());
if (hostLen > 1 && host.charAt(0) == '[' && host.charAt(hostLen - 1) == ']') {
return stringBuilder.append(host);
}
return stringBuilder.append('[').append(host).append(']');
}
/**
>>>>>>> 0514b0c... Only add port to HOST header value if needed
* Returns the {@link String} representation of an {@link InetAddress}.
* <ul>
* <li>Inet4Address results are identical to {@link InetAddress#getHostAddress()}</li>