Skip to content

Commit 3595d80

Browse files
authored
Merge pull request #1023 from terrestris/fix/proxy-scheme
fix: remove baseURL from request params and scheme from proxy host
2 parents 88584cb + f4d55ba commit 3595d80

File tree

4 files changed

+56
-5
lines changed

4 files changed

+56
-5
lines changed

shogun-lib/src/main/java/de/terrestris/shogun/lib/util/HttpUtil.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2229,7 +2229,7 @@ private static HttpHost getSystemProxy(URI uri) throws UnknownHostException {
22292229

22302230
if (proxyHost != null && proxyPort > 0) {
22312231
log.debug("Using proxy from system properties: Host=" + proxyHost + ", Port=" + proxyPort);
2232-
return new HttpHost(scheme, InetAddress.getByName(proxyHost), proxyPort);
2232+
return new HttpHost(InetAddress.getByName(proxyHost), proxyPort);
22332233
}
22342234

22352235
log.debug("Attempting to detect system proxy via ProxySelector.");
@@ -2245,7 +2245,6 @@ private static HttpHost getSystemProxy(URI uri) throws UnknownHostException {
22452245
);
22462246

22472247
return new HttpHost(
2248-
scheme,
22492248
InetAddress.getByName(address.getHostName()),
22502249
address.getPort()
22512250
);

shogun-proxy/src/main/java/de/terrestris/shogun/service/HttpProxyService.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,15 @@ private URL buildUriWithParameters(URL url, Map<String, String> params) throws U
239239
return url;
240240
}
241241
URIBuilder uriBuilder = new URIBuilder(url.toURI());
242-
for (Map.Entry<String, String> entry : params.entrySet()) {
243-
uriBuilder.addParameter(entry.getKey(), entry.getValue());
244-
}
242+
243+
params.forEach((key, value) -> {
244+
if (!StringUtils.equalsIgnoreCase(key, "baseUrl")) {
245+
uriBuilder.addParameter(key, value);
246+
} else {
247+
log.warn("Skipping baseUrl empty parameter: baseUrl ={}", value);
248+
}
249+
});
250+
245251
return uriBuilder.build().toURL();
246252
}
247253

shogun-proxy/src/test/java/de/terrestris/shogun/service/HttpProxyServiceTest.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,4 +291,49 @@ public void proxy_returns_400_for_erroneous_FormMultipartPost_requests() throws
291291
assertEquals("Returned Status matched", HttpProxyService.ERR_MSG_400_COMMON, responseEntity.getBody());
292292
}
293293
}
294+
295+
@ParameterizedTest
296+
@ValueSource(strings = {"http", "https"})
297+
@DisplayName("Should use system proxy settings when forwarding request")
298+
public void proxy_uses_system_proxy_settings(String scheme) throws Exception {
299+
// Set system proxy properties
300+
final String proxyHost = "1.2.2.4";
301+
System.setProperty("%s.proxyHost".formatted(scheme), proxyHost);
302+
System.setProperty("%s.proxyPort".formatted(scheme), "8888");
303+
304+
HttpServletRequest mockedRequest = mock(HttpServletRequest.class);
305+
final String baseUrl = "%s://my-test-example.org/resource".formatted(scheme);
306+
final URI baseUri = new URI(baseUrl);
307+
HttpResponse mockedResponse = mock(HttpResponse.class);
308+
309+
final HttpHeaders headers = new HttpHeaders();
310+
headers.add(HttpHeaders.CONTENT_TYPE, "text/plain");
311+
HttpStatus status = HttpStatus.OK;
312+
313+
when(mockedResponse.getHeaders()).thenReturn(headers);
314+
when(mockedResponse.getBody()).thenReturn("proxied".getBytes());
315+
when(mockedResponse.getStatusCode()).thenReturn(status);
316+
317+
try (
318+
MockedStatic<HttpUtil> httpUtilMock = mockStatic(HttpUtil.class)
319+
) {
320+
httpUtilMock.when(() -> HttpUtil.isHttpGetRequest(mockedRequest)).thenReturn(true);
321+
httpUtilMock.when(() -> HttpUtil.forwardGet(baseUri, mockedRequest, false)).thenAnswer(invocation -> {
322+
// Assert system proxy properties are set
323+
assertEquals("Proxy host does not match", proxyHost, System.getProperty("%s.proxyHost".formatted(scheme)));
324+
assertEquals("Proxy port does not match", "8888", System.getProperty("%s.proxyPort".formatted(scheme)));
325+
return mockedResponse;
326+
});
327+
328+
final ResponseEntity<?> responseEntity = httpProxyService.doProxy(mockedRequest, baseUrl, null);
329+
assertEquals("HttpStatus does not match", HttpStatus.OK, responseEntity.getStatusCode());
330+
assertNotNull("Response body is null.", responseEntity.getBody());
331+
assertEquals("Proxies request body does not match", "proxied", new String((byte[]) responseEntity.getBody()));
332+
} finally {
333+
// Cleanup system properties
334+
System.clearProperty("%s.proxyHost".formatted(scheme));
335+
System.clearProperty("%s.proxyPort".formatted(scheme));
336+
}
337+
}
338+
294339
}

shogun-proxy/src/test/resources/application-proxy-test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,4 @@ shogun-proxy:
3939
- localhost
4040
- localhost:8080
4141
- terrestris.de
42+
- my-test-example.org

0 commit comments

Comments
 (0)