Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Use the CONNECT method URI as host fallback making the proxy HTTP/1.0 compatible #371

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.google.common.net.HostAndPort;
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.HttpRequest;

import java.net.URI;
Expand Down Expand Up @@ -42,6 +43,11 @@ public static String getHostFromRequest(HttpRequest httpRequest) {
host = parseHostHeader(httpRequest, false);
}

// if there was not a Host header, and the method is CONNECT, use that as the host
if (host == null || host.isEmpty()) {
host = hostFromConnect(httpRequest, false);
}

return host;
}

Expand All @@ -53,15 +59,26 @@ public static String getHostFromRequest(HttpRequest httpRequest) {
* @return host and port of the request
*/
public static String getHostAndPortFromRequest(HttpRequest httpRequest) {
String host = null;
if (startsWithHttpOrHttps(httpRequest.uri())) {
try {
return getHostAndPortFromUri(httpRequest.uri());
host = getHostAndPortFromUri(httpRequest.uri());
} catch (URISyntaxException e) {
// the URI could not be parsed, so return the host and port in the Host header
}
}

return parseHostHeader(httpRequest, true);
// if there was no host in the URI, attempt to grab the host from the Host header
if (host == null || host.isEmpty()) {
host = parseHostHeader(httpRequest, true);
}

// if there was not Host header, and the method is CONNECT, use that as the host
if (host == null || host.isEmpty()) {
host = hostFromConnect(httpRequest, true);
}

return host;
}

/**
Expand Down Expand Up @@ -125,4 +142,24 @@ private static String parseHostHeader(HttpRequest httpRequest, boolean includePo
return null;
}
}

/**
* Retrieves the host and, optionally, the port from the specified request's URI if the method is CONNECT.
*
* @param httpRequest HTTP request
* @param includePort when true, include the port
* @return the host and, optionally, the port specified in the request's URI
*/
private static String hostFromConnect(HttpRequest httpRequest, boolean includePort) {
if (HttpMethod.CONNECT.equals(httpRequest.method())) {
if (includePort) {
return httpRequest.uri();
} else {
HostAndPort parsedHostAndPort = HostAndPort.fromString(httpRequest.uri());
return parsedHostAndPort.getHost();
}
} else {
return null;
}
}
}