Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
43 changes: 43 additions & 0 deletions src/main/java/io/vertx/httpproxy/ProxyOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
import io.vertx.codegen.json.annotations.JsonGen;
import io.vertx.core.json.JsonObject;
import io.vertx.httpproxy.cache.CacheOptions;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static io.vertx.core.http.HttpHeaders.*;
import static io.vertx.core.http.HttpHeaders.UPGRADE;

/**
* Proxy options.
Expand All @@ -17,15 +23,28 @@ public class ProxyOptions {
*/
public static final boolean DEFAULT_SUPPORT_WEBSOCKET = true;

public static final List<String> DEFAULT_HOP_BY_HOP_HEADERS = new ArrayList<>(Arrays.asList(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a Set would be better here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, updated this. Used Set instead of List

CONNECTION.toString(),
KEEP_ALIVE.toString(),
PROXY_AUTHENTICATE.toString(),
PROXY_AUTHORIZATION.toString(),
TRANSFER_ENCODING.toString(),
UPGRADE.toString(),
"te",
"trailer"
));

private CacheOptions cacheOptions;
private boolean supportWebSocket;
private List<String> customHopHeaders;

public ProxyOptions(JsonObject json) {
ProxyOptionsConverter.fromJson(json, this);
}

public ProxyOptions() {
supportWebSocket = DEFAULT_SUPPORT_WEBSOCKET;
customHopHeaders = DEFAULT_HOP_BY_HOP_HEADERS;
}

/**
Expand Down Expand Up @@ -66,6 +85,30 @@ public ProxyOptions setSupportWebSocket(boolean supportWebSocket) {
return this;
}

/**
* @return custom hop-by-hop headers
*/
public List<String> getCustomHopHeaders() {
return customHopHeaders;
}

/**
* Sets custom hop-by-hop headers, overriding the default {@code DEFAULT_HOP_BY_HOP_HEADERS} headers.
* <p>
* <b>Warning:</b> Please read the following specification before removing or modifying
* any hop-by-hop header:
* <a href="https://datatracker.ietf.org/doc/html/rfc2616#section-13.5.1">
* RFC 2616, Section 13.5.1
* </a>
* </p>
*
* @param customHopHeaders the list of hop-by-hop headers to set
*/
public ProxyOptions setCustomHopHeaders(List<String> customHopHeaders){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add an "adder" method : addCustomHopHeader

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, added a new function addCustomHopHeader to allow adding hop header to existing set

this.customHopHeaders = customHopHeaders;
return this;
}

@Override
public String toString() {
return toJson().toString();
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/io/vertx/httpproxy/ProxyRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.vertx.core.http.HttpVersion;
import io.vertx.core.net.HostAndPort;
import io.vertx.httpproxy.impl.ProxiedRequest;
import java.util.List;

/**
*
Expand All @@ -42,6 +43,18 @@ static ProxyRequest reverseProxy(HttpServerRequest proxiedRequest) {
return new ProxiedRequest(proxiedRequest);
}

/**
* Create a new {@code ProxyRequest} instance, the proxied request will be paused.
*
* @param proxiedRequest the {@code HttpServerRequest} that is proxied
* @param customHopHeaders list of Custom Hop-By-Hop Headers
* @return a reference to this, so the API can be used fluently
*/
static ProxyRequest reverseProxy(HttpServerRequest proxiedRequest, List<String> customHopHeaders) {
proxiedRequest.pause();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather not add a new constructor and instead use a modifier on the request

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted & made the changes for the same.

return new ProxiedRequest(proxiedRequest, customHopHeaders);
}

/**
* @return the HTTP version of the proxied request
*/
Expand Down
53 changes: 35 additions & 18 deletions src/main/java/io/vertx/httpproxy/impl/ProxiedRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,23 @@
import io.vertx.core.net.HostAndPort;
import io.vertx.core.streams.Pipe;
import io.vertx.httpproxy.Body;
import io.vertx.httpproxy.MediaType;
import io.vertx.httpproxy.ProxyRequest;
import io.vertx.httpproxy.ProxyResponse;

import io.vertx.httpproxy.ProxyOptions;
import java.util.Map;
import java.util.Objects;
import java.util.HashSet;
import java.util.List;

import static io.vertx.core.http.HttpHeaders.CONNECTION;
import static io.vertx.core.http.HttpHeaders.CONTENT_LENGTH;
import static io.vertx.core.http.HttpHeaders.KEEP_ALIVE;
import static io.vertx.core.http.HttpHeaders.PROXY_AUTHENTICATE;
import static io.vertx.core.http.HttpHeaders.PROXY_AUTHORIZATION;
import static io.vertx.core.http.HttpHeaders.TRANSFER_ENCODING;
import static io.vertx.core.http.HttpHeaders.UPGRADE;

public class ProxiedRequest implements ProxyRequest {

private static final CharSequence X_FORWARDED_HOST = HttpHeaders.createOptimized("x-forwarded-host");

private static final MultiMap HOP_BY_HOP_HEADERS = MultiMap.caseInsensitiveMultiMap()
.add(CONNECTION, "whatever")
.add(KEEP_ALIVE, "whatever")
.add(PROXY_AUTHENTICATE, "whatever")
.add(PROXY_AUTHORIZATION, "whatever")
.add("te", "whatever")
.add("trailer", "whatever")
.add(TRANSFER_ENCODING, "whatever")
.add(UPGRADE, "whatever");
private static final HashSet<String> DEFAULT_HOP_BY_HOP_HEADERS = new HashSet<>(ProxyOptions.DEFAULT_HOP_BY_HOP_HEADERS);

private final HashSet<String> HOP_BY_HOP_HEADERS;
final ContextInternal context;
private HttpMethod method;
private final HttpVersion version;
Expand Down Expand Up @@ -88,6 +76,35 @@ public ProxiedRequest(HttpServerRequest proxiedRequest) {
this.proxiedRequest = proxiedRequest;
this.context = ((HttpServerRequestInternal) proxiedRequest).context();
this.authority = null; // null is used as a signal to indicate an unchanged authority
this.HOP_BY_HOP_HEADERS = DEFAULT_HOP_BY_HOP_HEADERS;
}

public ProxiedRequest(HttpServerRequest proxiedRequest, List<String> customHopHeaders) {

// Determine content length
long contentLength = -1L;
String contentLengthHeader = proxiedRequest.getHeader(CONTENT_LENGTH);
if (contentLengthHeader != null) {
try {
contentLength = Long.parseLong(contentLengthHeader);
} catch (NumberFormatException e) {
// Ignore ???
}
}

// Content type
String contentType = proxiedRequest.getHeader(HttpHeaders.CONTENT_TYPE);

this.method = proxiedRequest.method();
this.version = proxiedRequest.version();
this.body = Body.body(proxiedRequest, contentLength, contentType);
this.uri = proxiedRequest.uri();
this.headers = MultiMap.caseInsensitiveMultiMap().addAll(proxiedRequest.headers());
this.absoluteURI = proxiedRequest.absoluteURI();
this.proxiedRequest = proxiedRequest;
this.context = ((HttpServerRequestInternal) proxiedRequest).context();
this.authority = null; // null is used as a signal to indicate an unchanged authority
this.HOP_BY_HOP_HEADERS = new HashSet<>(customHopHeaders);
}

@Override
Expand Down Expand Up @@ -173,7 +190,7 @@ Future<ProxyResponse> sendRequest() {
for (Map.Entry<String, String> header : headers) {
String name = header.getKey();
String value = header.getValue();
if (!HOP_BY_HOP_HEADERS.contains(name) && !name.equalsIgnoreCase(HttpHeaders.HOST.toString())) {
if (!HOP_BY_HOP_HEADERS.contains(name)) {
request.headers().add(name, value);
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/io/vertx/httpproxy/impl/ReverseProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class ReverseProxy implements HttpProxy {
private final boolean supportWebSocket;
private OriginRequestProvider originRequestProvider = (pc) -> Future.failedFuture("No origin available");
private final List<ProxyInterceptorEntry> interceptors = new ArrayList<>();
private final List<String> hopByHopHeaders;

public ReverseProxy(ProxyOptions options, HttpClient client) {
CacheOptions cacheOptions = options.getCacheOptions();
Expand All @@ -47,6 +48,7 @@ public ReverseProxy(ProxyOptions options, HttpClient client) {
}
this.client = client;
this.supportWebSocket = options.getSupportWebSocket();
this.hopByHopHeaders = options.getCustomHopHeaders();
}

public Cache newCache(CacheOptions options, Vertx vertx) {
Expand All @@ -73,7 +75,7 @@ public HttpProxy addInterceptor(ProxyInterceptor interceptor, boolean supportsWe

@Override
public void handle(HttpServerRequest request) {
ProxyRequest proxyRequest = ProxyRequest.reverseProxy(request);
ProxyRequest proxyRequest = ProxyRequest.reverseProxy(request, hopByHopHeaders);

// Encoding sanity check
Boolean chunked = HttpUtils.isChunked(request.headers());
Expand Down