From 8409da886851ac1ae925fd4b2450a33a1db2e038 Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Thu, 19 Dec 2024 15:31:09 +1100 Subject: [PATCH 1/3] Issue #325 - fixes and testing for HttpServletRequest getServerName Signed-off-by: Lachlan Roberts --- .../runtime/jetty9/JettyRequestAPIData.java | 13 +--- .../runtime/jetty9/RemoteAddressTest.java | 76 ++++++++++++++++++- .../remoteaddrapp/EE10RemoteAddrServlet.java | 7 +- .../remoteaddrapp/EE8RemoteAddrServlet.java | 6 +- 4 files changed, 85 insertions(+), 17 deletions(-) diff --git a/runtime/runtime_impl_jetty9/src/main/java/com/google/apphosting/runtime/jetty9/JettyRequestAPIData.java b/runtime/runtime_impl_jetty9/src/main/java/com/google/apphosting/runtime/jetty9/JettyRequestAPIData.java index c5f80ead..27793348 100644 --- a/runtime/runtime_impl_jetty9/src/main/java/com/google/apphosting/runtime/jetty9/JettyRequestAPIData.java +++ b/runtime/runtime_impl_jetty9/src/main/java/com/google/apphosting/runtime/jetty9/JettyRequestAPIData.java @@ -67,12 +67,12 @@ import java.util.stream.Stream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; - import org.eclipse.jetty.http.HttpField; import org.eclipse.jetty.http.HttpFields; import org.eclipse.jetty.http.HttpScheme; import org.eclipse.jetty.http.HttpURI; import org.eclipse.jetty.server.Request; +import org.eclipse.jetty.util.HostPort; /** * Implementation for the {@link RequestAPIData} to allow for the Jetty {@link Request} to be used @@ -286,7 +286,7 @@ public JettyRequestAPIData( traceContext = TraceContextProto.getDefaultInstance(); } - String finalUserIp = userIp; + String normalizeUserIp = HostPort.normalizeHost(userIp); this.httpServletRequest = new HttpServletRequestWrapper(httpServletRequest) { @@ -332,17 +332,12 @@ public boolean isSecure() { @Override public String getRemoteAddr() { - return finalUserIp; - } - - @Override - public String getServerName() { - return UNSPECIFIED_IP; + return normalizeUserIp; } @Override public String getRemoteHost() { - return finalUserIp; + return normalizeUserIp; } @Override diff --git a/runtime/test/src/test/java/com/google/apphosting/runtime/jetty9/RemoteAddressTest.java b/runtime/test/src/test/java/com/google/apphosting/runtime/jetty9/RemoteAddressTest.java index f2ade9ba..461e0c9b 100644 --- a/runtime/test/src/test/java/com/google/apphosting/runtime/jetty9/RemoteAddressTest.java +++ b/runtime/test/src/test/java/com/google/apphosting/runtime/jetty9/RemoteAddressTest.java @@ -25,6 +25,7 @@ import org.eclipse.jetty.client.HttpClient; import org.eclipse.jetty.client.api.ContentResponse; import org.eclipse.jetty.http.HttpStatus; +import org.eclipse.jetty.http.HttpVersion; import org.junit.After; import org.junit.Before; import org.junit.Rule; @@ -54,6 +55,7 @@ public static Collection data() { private final boolean httpMode; private final String environment; private RuntimeContext runtime; + private String url; public RemoteAddressTest(String environment, boolean httpMode) { this.environment = environment; @@ -67,6 +69,7 @@ public void before() throws Exception { copyAppToDir(app, temp.getRoot().toPath()); httpClient.start(); runtime = runtimeContext(); + url = runtime.jettyUrl("/"); System.err.println("==== Using Environment: " + environment + " " + httpMode + " ===="); } @@ -77,20 +80,85 @@ public void after() throws Exception { } @Test - public void test() throws Exception { - String url = runtime.jettyUrl("/"); + public void testWithHostHeader() throws Exception { ContentResponse response = httpClient.newRequest(url) + .header("Host", "foobar:1234") .header("X-AppEngine-User-IP", "203.0.113.1") .send(); assertThat(response.getStatus(), equalTo(HttpStatus.OK_200)); String contentReceived = response.getContentAsString(); assertThat(contentReceived, containsString("getRemoteAddr: 203.0.113.1")); + assertThat(contentReceived, containsString("getRemoteHost: 203.0.113.1")); + assertThat(contentReceived, containsString("getRemotePort: 0")); assertThat(contentReceived, containsString("getLocalAddr: 0.0.0.0")); - assertThat(contentReceived, containsString("getServerPort: " + runtime.getPort())); + assertThat(contentReceived, containsString("getLocalName: 0.0.0.0")); + assertThat(contentReceived, containsString("getLocalPort: 0")); + assertThat(contentReceived, containsString("getServerName: foobar")); + assertThat(contentReceived, containsString("getServerPort: 1234")); + } + + @Test + public void testWithIPv6() throws Exception { + // Test the host header to be IPv6 with a port. + ContentResponse response = httpClient.newRequest(url) + .header("Host", "[2001:db8:85a3:8d3:1319:8a2e:370:7348]:1234") + .header("X-AppEngine-User-IP", "203.0.113.1") + .send(); + assertThat(response.getStatus(), equalTo(HttpStatus.OK_200)); + String contentReceived = response.getContentAsString(); + assertThat(contentReceived, containsString("getRemoteAddr: 203.0.113.1")); + assertThat(contentReceived, containsString("getRemoteHost: 203.0.113.1")); + assertThat(contentReceived, containsString("getRemotePort: 0")); + assertThat(contentReceived, containsString("getLocalAddr: 0.0.0.0")); + assertThat(contentReceived, containsString("getLocalName: 0.0.0.0")); + assertThat(contentReceived, containsString("getLocalPort: 0")); + assertThat(contentReceived, containsString("getServerName: [2001:db8:85a3:8d3:1319:8a2e:370:7348]")); + assertThat(contentReceived, containsString("getServerPort: 1234")); + + // Test the user IP to be IPv6 with a port. + response = httpClient.newRequest(url) + .header("Host", "203.0.113.1:1234") + .header("X-AppEngine-User-IP", "2001:db8:85a3:8d3:1319:8a2e:370:7348") + .send(); + assertThat(response.getStatus(), equalTo(HttpStatus.OK_200)); + contentReceived = response.getContentAsString(); + if ("jetty94".equals(environment)) { + assertThat(contentReceived, containsString("getRemoteAddr: [2001:db8:85a3:8d3:1319:8a2e:370:7348]")); + assertThat(contentReceived, containsString("getRemoteHost: [2001:db8:85a3:8d3:1319:8a2e:370:7348]")); + } + else { + assertThat(contentReceived, containsString("getRemoteAddr: 2001:db8:85a3:8d3:1319:8a2e:370:7348")); + assertThat(contentReceived, containsString("getRemoteHost: 2001:db8:85a3:8d3:1319:8a2e:370:7348")); + } + assertThat(contentReceived, containsString("getRemotePort: 0")); + assertThat(contentReceived, containsString("getLocalAddr: 0.0.0.0")); + assertThat(contentReceived, containsString("getLocalName: 0.0.0.0")); + assertThat(contentReceived, containsString("getLocalPort: 0")); + assertThat(contentReceived, containsString("getServerName: 203.0.113.1")); + assertThat(contentReceived, containsString("getServerPort: 1234")); + } + + @Test + public void testWithoutHostHeader() throws Exception { + String url = runtime.jettyUrl("/"); + + ContentResponse response = httpClient.newRequest(url) + .version(HttpVersion.HTTP_1_0) + .header("X-AppEngine-User-IP", "203.0.113.1") + .onRequestHeaders(request -> request.getHeaders().remove("Host")) + .send(); + + assertThat(response.getStatus(), equalTo(HttpStatus.OK_200)); + String contentReceived = response.getContentAsString(); + assertThat(contentReceived, containsString("getRemoteAddr: 203.0.113.1")); + assertThat(contentReceived, containsString("getRemoteHost: 203.0.113.1")); assertThat(contentReceived, containsString("getRemotePort: 0")); + assertThat(contentReceived, containsString("getLocalAddr: 0.0.0.0")); + assertThat(contentReceived, containsString("getLocalName: 0.0.0.0")); assertThat(contentReceived, containsString("getLocalPort: 0")); - assertThat(contentReceived, containsString("getServerName: 0.0.0.0")); + assertThat(contentReceived, containsString("getServerName: 127.0.0.1")); + assertThat(contentReceived, containsString("getServerPort: " + runtime.getPort())); } private RuntimeContext runtimeContext() throws Exception { diff --git a/runtime/testapps/src/main/java/com/google/apphosting/runtime/jetty9/remoteaddrapp/EE10RemoteAddrServlet.java b/runtime/testapps/src/main/java/com/google/apphosting/runtime/jetty9/remoteaddrapp/EE10RemoteAddrServlet.java index 50f772aa..cf86915f 100644 --- a/runtime/testapps/src/main/java/com/google/apphosting/runtime/jetty9/remoteaddrapp/EE10RemoteAddrServlet.java +++ b/runtime/testapps/src/main/java/com/google/apphosting/runtime/jetty9/remoteaddrapp/EE10RemoteAddrServlet.java @@ -28,10 +28,13 @@ protected void service(HttpServletRequest req, HttpServletResponse resp) throws resp.setContentType("text/plain"); PrintWriter writer = resp.getWriter(); writer.println("getRemoteAddr: " + req.getRemoteAddr()); - writer.println("getLocalAddr: " + req.getLocalAddr()); - writer.println("getServerPort: " + req.getServerPort()); + writer.println("getRemoteHost: " + req.getRemoteHost()); writer.println("getRemotePort: " + req.getRemotePort()); + writer.println("getLocalAddr: " + req.getLocalAddr()); + writer.println("getLocalName: " + req.getLocalName()); writer.println("getLocalPort: " + req.getLocalPort()); writer.println("getServerName: " + req.getServerName()); + writer.println("getServerPort: " + req.getServerPort()); + } } diff --git a/runtime/testapps/src/main/java/com/google/apphosting/runtime/jetty9/remoteaddrapp/EE8RemoteAddrServlet.java b/runtime/testapps/src/main/java/com/google/apphosting/runtime/jetty9/remoteaddrapp/EE8RemoteAddrServlet.java index cb2338b5..a4b792f4 100644 --- a/runtime/testapps/src/main/java/com/google/apphosting/runtime/jetty9/remoteaddrapp/EE8RemoteAddrServlet.java +++ b/runtime/testapps/src/main/java/com/google/apphosting/runtime/jetty9/remoteaddrapp/EE8RemoteAddrServlet.java @@ -28,10 +28,12 @@ protected void service(HttpServletRequest req, HttpServletResponse resp) throws resp.setContentType("text/plain"); PrintWriter writer = resp.getWriter(); writer.println("getRemoteAddr: " + req.getRemoteAddr()); - writer.println("getLocalAddr: " + req.getLocalAddr()); - writer.println("getServerPort: " + req.getServerPort()); + writer.println("getRemoteHost: " + req.getRemoteHost()); writer.println("getRemotePort: " + req.getRemotePort()); + writer.println("getLocalAddr: " + req.getLocalAddr()); + writer.println("getLocalName: " + req.getLocalName()); writer.println("getLocalPort: " + req.getLocalPort()); writer.println("getServerName: " + req.getServerName()); + writer.println("getServerPort: " + req.getServerPort()); } } From b3de558b2c83eee7586c549c69ef515e954b2de8 Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Fri, 20 Dec 2024 17:12:57 +1100 Subject: [PATCH 2/3] Issue #325 - fixes for HttpServletRequest getServerName Signed-off-by: Lachlan Roberts --- .../runtime/jetty9/JettyRequestAPIData.java | 7 +- .../runtime/jetty9/RemoteAddressTest.java | 67 ++++++++++++++----- 2 files changed, 53 insertions(+), 21 deletions(-) diff --git a/runtime/runtime_impl_jetty9/src/main/java/com/google/apphosting/runtime/jetty9/JettyRequestAPIData.java b/runtime/runtime_impl_jetty9/src/main/java/com/google/apphosting/runtime/jetty9/JettyRequestAPIData.java index 27793348..1287cb73 100644 --- a/runtime/runtime_impl_jetty9/src/main/java/com/google/apphosting/runtime/jetty9/JettyRequestAPIData.java +++ b/runtime/runtime_impl_jetty9/src/main/java/com/google/apphosting/runtime/jetty9/JettyRequestAPIData.java @@ -51,6 +51,7 @@ import static com.google.apphosting.runtime.AppEngineConstants.X_FORWARDED_PROTO; import static com.google.apphosting.runtime.AppEngineConstants.X_GOOGLE_INTERNAL_PROFILER; import static com.google.apphosting.runtime.AppEngineConstants.X_GOOGLE_INTERNAL_SKIPADMINCHECK; +import static com.google.apphosting.runtime.jetty9.RpcConnection.NORMALIZE_INET_ADDR; import com.google.apphosting.base.protos.HttpPb; import com.google.apphosting.base.protos.RuntimePb; @@ -286,7 +287,7 @@ public JettyRequestAPIData( traceContext = TraceContextProto.getDefaultInstance(); } - String normalizeUserIp = HostPort.normalizeHost(userIp); + String finalUserIp = NORMALIZE_INET_ADDR ? HostPort.normalizeHost(userIp) : userIp; this.httpServletRequest = new HttpServletRequestWrapper(httpServletRequest) { @@ -332,12 +333,12 @@ public boolean isSecure() { @Override public String getRemoteAddr() { - return normalizeUserIp; + return finalUserIp; } @Override public String getRemoteHost() { - return normalizeUserIp; + return finalUserIp; } @Override diff --git a/runtime/test/src/test/java/com/google/apphosting/runtime/jetty9/RemoteAddressTest.java b/runtime/test/src/test/java/com/google/apphosting/runtime/jetty9/RemoteAddressTest.java index 461e0c9b..80cb255f 100644 --- a/runtime/test/src/test/java/com/google/apphosting/runtime/jetty9/RemoteAddressTest.java +++ b/runtime/test/src/test/java/com/google/apphosting/runtime/jetty9/RemoteAddressTest.java @@ -24,6 +24,7 @@ import java.util.Collection; import org.eclipse.jetty.client.HttpClient; import org.eclipse.jetty.client.api.ContentResponse; +import org.eclipse.jetty.http.HttpHeader; import org.eclipse.jetty.http.HttpStatus; import org.eclipse.jetty.http.HttpVersion; import org.junit.After; @@ -100,24 +101,26 @@ public void testWithHostHeader() throws Exception { @Test public void testWithIPv6() throws Exception { - // Test the host header to be IPv6 with a port. - ContentResponse response = httpClient.newRequest(url) - .header("Host", "[2001:db8:85a3:8d3:1319:8a2e:370:7348]:1234") - .header("X-AppEngine-User-IP", "203.0.113.1") - .send(); - assertThat(response.getStatus(), equalTo(HttpStatus.OK_200)); - String contentReceived = response.getContentAsString(); - assertThat(contentReceived, containsString("getRemoteAddr: 203.0.113.1")); - assertThat(contentReceived, containsString("getRemoteHost: 203.0.113.1")); - assertThat(contentReceived, containsString("getRemotePort: 0")); - assertThat(contentReceived, containsString("getLocalAddr: 0.0.0.0")); - assertThat(contentReceived, containsString("getLocalName: 0.0.0.0")); - assertThat(contentReceived, containsString("getLocalPort: 0")); - assertThat(contentReceived, containsString("getServerName: [2001:db8:85a3:8d3:1319:8a2e:370:7348]")); - assertThat(contentReceived, containsString("getServerPort: 1234")); + // Test the host header to be IPv6 with a port. + ContentResponse response = httpClient.newRequest(url) + .header("Host", "[2001:db8:85a3:8d3:1319:8a2e:370:7348]:1234") + .header("X-AppEngine-User-IP", "203.0.113.1") + .send(); + assertThat(response.getStatus(), equalTo(HttpStatus.OK_200)); + String contentReceived = response.getContentAsString(); + assertThat(contentReceived, containsString("getRemoteAddr: 203.0.113.1")); + assertThat(contentReceived, containsString("getRemoteHost: 203.0.113.1")); + assertThat(contentReceived, containsString("getRemotePort: 0")); + assertThat(contentReceived, containsString("getLocalAddr: 0.0.0.0")); + assertThat(contentReceived, containsString("getLocalName: 0.0.0.0")); + assertThat(contentReceived, containsString("getLocalPort: 0")); + assertThat(contentReceived, containsString("getServerName: [2001:db8:85a3:8d3:1319:8a2e:370:7348]")); + assertThat(contentReceived, containsString("getServerPort: 1234")); // Test the user IP to be IPv6 with a port. - response = httpClient.newRequest(url) + response = + httpClient + .newRequest(url) .header("Host", "203.0.113.1:1234") .header("X-AppEngine-User-IP", "2001:db8:85a3:8d3:1319:8a2e:370:7348") .send(); @@ -126,8 +129,9 @@ public void testWithIPv6() throws Exception { if ("jetty94".equals(environment)) { assertThat(contentReceived, containsString("getRemoteAddr: [2001:db8:85a3:8d3:1319:8a2e:370:7348]")); assertThat(contentReceived, containsString("getRemoteHost: [2001:db8:85a3:8d3:1319:8a2e:370:7348]")); - } - else { + } else { + // The correct behaviour for getRemoteAddr and getRemoteHost is to not include [] + // because they return raw IP/hostname and not URI-formatted addresses. assertThat(contentReceived, containsString("getRemoteAddr: 2001:db8:85a3:8d3:1319:8a2e:370:7348")); assertThat(contentReceived, containsString("getRemoteHost: 2001:db8:85a3:8d3:1319:8a2e:370:7348")); } @@ -161,6 +165,33 @@ public void testWithoutHostHeader() throws Exception { assertThat(contentReceived, containsString("getServerPort: " + runtime.getPort())); } + @Test + public void testForwardedHeadersIgnored() throws Exception { + ContentResponse response = + httpClient + .newRequest(url) + .header("Host", "foobar:1234") + .header("X-AppEngine-User-IP", "203.0.113.1") + .header(HttpHeader.X_FORWARDED_FOR, "test1:2221") + .header(HttpHeader.X_FORWARDED_PROTO, "test2:2222") + .header(HttpHeader.X_FORWARDED_HOST, "test3:2223") + .header(HttpHeader.X_FORWARDED_PORT, "test4:2224") + .header(HttpHeader.FORWARDED, "test5:2225") + .send(); + + assertThat(response.getStatus(), equalTo(HttpStatus.OK_200)); + String contentReceived = response.getContentAsString(); + assertThat(contentReceived, containsString("getRemoteAddr: 203.0.113.1")); + assertThat(contentReceived, containsString("getRemoteHost: 203.0.113.1")); + assertThat(contentReceived, containsString("getRemotePort: 0")); + assertThat(contentReceived, containsString("getLocalAddr: 0.0.0.0")); + assertThat(contentReceived, containsString("getLocalName: 0.0.0.0")); + assertThat(contentReceived, containsString("getLocalPort: 0")); + assertThat(contentReceived, containsString("getServerName: foobar")); + assertThat(contentReceived, containsString("getServerPort: 1234")); + } + + private RuntimeContext runtimeContext() throws Exception { RuntimeContext.Config config = RuntimeContext.Config.builder().setApplicationPath(temp.getRoot().toString()).build(); From 39dc7240d27da3da054e8e85204c63b3c111c2bc Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Thu, 2 Jan 2025 16:31:53 +1100 Subject: [PATCH 3/3] PR #327 - formatting fixes from review Signed-off-by: Lachlan Roberts --- .../jetty/http/JettyRequestAPIData.java | 3 +- .../runtime/jetty9/JettyRequestAPIData.java | 3 +- .../runtime/jetty9/RemoteAddressTest.java | 56 +++++++++++-------- .../remoteaddrapp/EE10RemoteAddrServlet.java | 1 - 4 files changed, 36 insertions(+), 27 deletions(-) diff --git a/runtime/runtime_impl_jetty12/src/main/java/com/google/apphosting/runtime/jetty/http/JettyRequestAPIData.java b/runtime/runtime_impl_jetty12/src/main/java/com/google/apphosting/runtime/jetty/http/JettyRequestAPIData.java index 627b35eb..75d2e3a7 100644 --- a/runtime/runtime_impl_jetty12/src/main/java/com/google/apphosting/runtime/jetty/http/JettyRequestAPIData.java +++ b/runtime/runtime_impl_jetty12/src/main/java/com/google/apphosting/runtime/jetty/http/JettyRequestAPIData.java @@ -124,7 +124,8 @@ public JettyRequestAPIData( HttpFields.Mutable fields = HttpFields.build(); for (HttpField field : request.getHeaders()) { - // If it has a HttpHeader it is one of the standard headers so won't match any appengine specific header. + // If it has a HttpHeader it is one of the standard headers so won't match any appengine + // specific header. if (field.getHeader() != null) { fields.add(field); continue; diff --git a/runtime/runtime_impl_jetty9/src/main/java/com/google/apphosting/runtime/jetty9/JettyRequestAPIData.java b/runtime/runtime_impl_jetty9/src/main/java/com/google/apphosting/runtime/jetty9/JettyRequestAPIData.java index 1287cb73..e4168f8c 100644 --- a/runtime/runtime_impl_jetty9/src/main/java/com/google/apphosting/runtime/jetty9/JettyRequestAPIData.java +++ b/runtime/runtime_impl_jetty9/src/main/java/com/google/apphosting/runtime/jetty9/JettyRequestAPIData.java @@ -129,7 +129,8 @@ public JettyRequestAPIData( HttpFields fields = new HttpFields(); for (HttpField field : request.getHttpFields()) { - // If it has a HttpHeader it is one of the standard headers so won't match any appengine specific header. + // If it has a HttpHeader it is one of the standard headers so won't match any appengine + // specific header. if (field.getHeader() != null) { fields.add(field); continue; diff --git a/runtime/test/src/test/java/com/google/apphosting/runtime/jetty9/RemoteAddressTest.java b/runtime/test/src/test/java/com/google/apphosting/runtime/jetty9/RemoteAddressTest.java index 80cb255f..4aabd7d6 100644 --- a/runtime/test/src/test/java/com/google/apphosting/runtime/jetty9/RemoteAddressTest.java +++ b/runtime/test/src/test/java/com/google/apphosting/runtime/jetty9/RemoteAddressTest.java @@ -82,7 +82,9 @@ public void after() throws Exception { @Test public void testWithHostHeader() throws Exception { - ContentResponse response = httpClient.newRequest(url) + ContentResponse response = + httpClient + .newRequest(url) .header("Host", "foobar:1234") .header("X-AppEngine-User-IP", "203.0.113.1") .send(); @@ -101,21 +103,24 @@ public void testWithHostHeader() throws Exception { @Test public void testWithIPv6() throws Exception { - // Test the host header to be IPv6 with a port. - ContentResponse response = httpClient.newRequest(url) - .header("Host", "[2001:db8:85a3:8d3:1319:8a2e:370:7348]:1234") - .header("X-AppEngine-User-IP", "203.0.113.1") - .send(); - assertThat(response.getStatus(), equalTo(HttpStatus.OK_200)); - String contentReceived = response.getContentAsString(); - assertThat(contentReceived, containsString("getRemoteAddr: 203.0.113.1")); - assertThat(contentReceived, containsString("getRemoteHost: 203.0.113.1")); - assertThat(contentReceived, containsString("getRemotePort: 0")); - assertThat(contentReceived, containsString("getLocalAddr: 0.0.0.0")); - assertThat(contentReceived, containsString("getLocalName: 0.0.0.0")); - assertThat(contentReceived, containsString("getLocalPort: 0")); - assertThat(contentReceived, containsString("getServerName: [2001:db8:85a3:8d3:1319:8a2e:370:7348]")); - assertThat(contentReceived, containsString("getServerPort: 1234")); + // Test the host header to be IPv6 with a port. + ContentResponse response = + httpClient + .newRequest(url) + .header("Host", "[2001:db8:85a3:8d3:1319:8a2e:370:7348]:1234") + .header("X-AppEngine-User-IP", "203.0.113.1") + .send(); + assertThat(response.getStatus(), equalTo(HttpStatus.OK_200)); + String contentReceived = response.getContentAsString(); + assertThat(contentReceived, containsString("getRemoteAddr: 203.0.113.1")); + assertThat(contentReceived, containsString("getRemoteHost: 203.0.113.1")); + assertThat(contentReceived, containsString("getRemotePort: 0")); + assertThat(contentReceived, containsString("getLocalAddr: 0.0.0.0")); + assertThat(contentReceived, containsString("getLocalName: 0.0.0.0")); + assertThat(contentReceived, containsString("getLocalPort: 0")); + assertThat( + contentReceived, containsString("getServerName: [2001:db8:85a3:8d3:1319:8a2e:370:7348]")); + assertThat(contentReceived, containsString("getServerPort: 1234")); // Test the user IP to be IPv6 with a port. response = @@ -127,13 +132,17 @@ public void testWithIPv6() throws Exception { assertThat(response.getStatus(), equalTo(HttpStatus.OK_200)); contentReceived = response.getContentAsString(); if ("jetty94".equals(environment)) { - assertThat(contentReceived, containsString("getRemoteAddr: [2001:db8:85a3:8d3:1319:8a2e:370:7348]")); - assertThat(contentReceived, containsString("getRemoteHost: [2001:db8:85a3:8d3:1319:8a2e:370:7348]")); + assertThat( + contentReceived, containsString("getRemoteAddr: [2001:db8:85a3:8d3:1319:8a2e:370:7348]")); + assertThat( + contentReceived, containsString("getRemoteHost: [2001:db8:85a3:8d3:1319:8a2e:370:7348]")); } else { // The correct behaviour for getRemoteAddr and getRemoteHost is to not include [] // because they return raw IP/hostname and not URI-formatted addresses. - assertThat(contentReceived, containsString("getRemoteAddr: 2001:db8:85a3:8d3:1319:8a2e:370:7348")); - assertThat(contentReceived, containsString("getRemoteHost: 2001:db8:85a3:8d3:1319:8a2e:370:7348")); + assertThat( + contentReceived, containsString("getRemoteAddr: 2001:db8:85a3:8d3:1319:8a2e:370:7348")); + assertThat( + contentReceived, containsString("getRemoteHost: 2001:db8:85a3:8d3:1319:8a2e:370:7348")); } assertThat(contentReceived, containsString("getRemotePort: 0")); assertThat(contentReceived, containsString("getLocalAddr: 0.0.0.0")); @@ -145,9 +154,9 @@ public void testWithIPv6() throws Exception { @Test public void testWithoutHostHeader() throws Exception { - String url = runtime.jettyUrl("/"); - - ContentResponse response = httpClient.newRequest(url) + ContentResponse response = + httpClient + .newRequest(url) .version(HttpVersion.HTTP_1_0) .header("X-AppEngine-User-IP", "203.0.113.1") .onRequestHeaders(request -> request.getHeaders().remove("Host")) @@ -191,7 +200,6 @@ public void testForwardedHeadersIgnored() throws Exception { assertThat(contentReceived, containsString("getServerPort: 1234")); } - private RuntimeContext runtimeContext() throws Exception { RuntimeContext.Config config = RuntimeContext.Config.builder().setApplicationPath(temp.getRoot().toString()).build(); diff --git a/runtime/testapps/src/main/java/com/google/apphosting/runtime/jetty9/remoteaddrapp/EE10RemoteAddrServlet.java b/runtime/testapps/src/main/java/com/google/apphosting/runtime/jetty9/remoteaddrapp/EE10RemoteAddrServlet.java index cf86915f..baebc6d3 100644 --- a/runtime/testapps/src/main/java/com/google/apphosting/runtime/jetty9/remoteaddrapp/EE10RemoteAddrServlet.java +++ b/runtime/testapps/src/main/java/com/google/apphosting/runtime/jetty9/remoteaddrapp/EE10RemoteAddrServlet.java @@ -35,6 +35,5 @@ protected void service(HttpServletRequest req, HttpServletResponse resp) throws writer.println("getLocalPort: " + req.getLocalPort()); writer.println("getServerName: " + req.getServerName()); writer.println("getServerPort: " + req.getServerPort()); - } }