Skip to content

Commit

Permalink
Fixes #299
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Kraaz authored and asolntsev committed Sep 25, 2023
1 parent b39758f commit bec10e4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.atomic.AtomicBoolean;
Expand Down Expand Up @@ -1243,12 +1242,7 @@ private void stripConnectionTokens(HttpHeaders headers) {
* The headers to modify
*/
private void stripHopByHopHeaders(HttpHeaders headers) {
Set<String> headerNames = headers.names();
for (String headerName : headerNames) {
if (ProxyUtils.shouldRemoveHopByHopHeader(headerName)) {
headers.remove(headerName);
}
}
ProxyUtils.stripHopByHopHeaders(headers);
}

/* *************************************************************************
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/littleshoot/proxy/impl/ProxyUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,18 @@ public static boolean shouldRemoveHopByHopHeader(String headerName) {
return SHOULD_NOT_PROXY_HOP_BY_HOP_HEADERS.contains(headerName);
}

/**
* Removes all headers that should not be forwarded. See RFC 2616 13.5.1
* End-to-end and Hop-by-hop Headers.
*
* @param headers
* The headers to modify
*/
public static void stripHopByHopHeaders(HttpHeaders headers) {
// Not explicitly documented, but remove is case insensitve as a HTTP header handling function should be
SHOULD_NOT_PROXY_HOP_BY_HOP_HEADERS.forEach(headers::remove);
}

/**
* Splits comma-separated header values into tokens. For example, if the value of the Connection header is "Transfer-Encoding, close",
* this method will return "Transfer-Encoding" and "close". This method strips trims any optional whitespace from
Expand Down
26 changes: 26 additions & 0 deletions src/test/java/org/littleshoot/proxy/ProxyHeadersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
import org.mockserver.integration.ClientAndServer;
import org.mockserver.matchers.Times;
import org.mockserver.model.ConnectionOptions;
import org.mockserver.model.HttpRequest;
import org.mockserver.model.NottableString;

import static org.hamcrest.Matchers.emptyArray;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockserver.model.HttpRequest.request;
import static org.mockserver.model.HttpResponse.response;
import static org.junit.Assert.assertEquals;

/**
* Tests the proxy's handling and manipulation of headers.
Expand Down Expand Up @@ -74,4 +77,27 @@ public void testProxyRemovesConnectionHeadersFromServer() throws Exception {
assertThat("Expected proxy to remove the Dummy-Header specified in the Connection header", dummyHeaders, emptyArray());
}
}

@Test
public void testProxyRemovesHopByHopHeadersFromClient() throws Exception {
HttpRequest expectedServerRequest = request() //
.withMethod("GET") //
.withPath("/connectionheaders") //
.withHeader(NottableString.not("proxy-authenticate")) //
.withHeader(NottableString.not("proxy-authorization"));
mockServer.when(expectedServerRequest, Times.exactly(1)) //
.respond(response().withStatusCode(200).withBody("success"));

this.proxyServer = DefaultHttpProxyServer.bootstrap().withPort(0).start();

try (CloseableHttpClient httpClient = TestUtils
.createProxiedHttpClient(proxyServer.getListenAddress().getPort())) {
HttpGet clientRequest = new HttpGet("http://localhost:" + mockServerPort + "/connectionheaders");
clientRequest.addHeader("Proxy-Authenticate", "");
clientRequest.addHeader("Proxy-Authorization", "");
HttpResponse response = httpClient.execute(clientRequest);
EntityUtils.consume(response.getEntity());
assertEquals(200, response.getStatusLine().getStatusCode());
}
}
}

0 comments on commit bec10e4

Please sign in to comment.