Skip to content

Commit 0b97ab2

Browse files
authored
Merge pull request #787 from tmiyargi/UNDERTOW-1569
[UNDERTOW-1569] HttpServletRequest getLocalName() returns IP instead …
2 parents a7477cd + 431015d commit 0b97ab2

3 files changed

Lines changed: 145 additions & 1 deletion

File tree

servlet/src/main/java/io/undertow/servlet/spec/HttpServletRequestImpl.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -981,9 +981,16 @@ public int getRemotePort() {
981981
return exchange.getSourceAddress().getPort();
982982
}
983983

984+
/**
985+
* String java.net.InetAddress.getHostName()
986+
* Gets the host name for this IP address.
987+
* If this InetAddress was created with a host name, this host name will be remembered and returned; otherwise, a reverse name lookup will be performed and the result will be returned based on the system configured name lookup service. If a lookup of the name service is required, call getCanonicalHostName.
988+
* If there is a security manager, its checkConnect method is first called with the hostname and -1 as its arguments to see if the operation is allowed. If the operation is not allowed, it will return the textual representation of the IP address.
989+
* @see InetAddres#getHostName
990+
*/
984991
@Override
985992
public String getLocalName() {
986-
return exchange.getDestinationAddress().getHostString();
993+
return exchange.getDestinationAddress().getHostName();
987994
}
988995

989996
@Override
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* JBoss, Home of Professional Open Source.
3+
* Copyright 2019 Red Hat, Inc., and individual contributors
4+
* as indicated by the @author tags.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package io.undertow.servlet.test.request;
20+
21+
import io.undertow.server.handlers.PathHandler;
22+
import io.undertow.servlet.api.DeploymentInfo;
23+
import io.undertow.servlet.api.DeploymentManager;
24+
import io.undertow.servlet.api.ServletContainer;
25+
import io.undertow.servlet.test.SimpleServletTestCase;
26+
import io.undertow.servlet.test.util.TestClassIntrospector;
27+
import io.undertow.testutils.DefaultServer;
28+
import io.undertow.testutils.HttpClientUtils;
29+
import io.undertow.testutils.TestHttpClient;
30+
import io.undertow.util.StatusCodes;
31+
import org.apache.http.HttpResponse;
32+
import org.apache.http.client.methods.HttpGet;
33+
import org.junit.Assert;
34+
import org.junit.BeforeClass;
35+
import org.junit.Test;
36+
import org.junit.runner.RunWith;
37+
38+
import javax.servlet.ServletException;
39+
40+
import static io.undertow.servlet.Servlets.servlet;
41+
42+
/**
43+
* Tests that ge get ip or host name depending on the method call
44+
* @author tmiyar
45+
*
46+
*/
47+
@RunWith(DefaultServer.class)
48+
public class HttpHostValuesTestCase {
49+
50+
@BeforeClass
51+
public static void setup() throws ServletException {
52+
53+
54+
final PathHandler pathHandler = new PathHandler();
55+
final ServletContainer container = ServletContainer.Factory.newInstance();
56+
DeploymentInfo builder = new DeploymentInfo()
57+
.setClassLoader(SimpleServletTestCase.class.getClassLoader())
58+
.setContextPath("/servletContext")
59+
.setClassIntrospecter(TestClassIntrospector.INSTANCE)
60+
.setDeploymentName("servletContext.war")
61+
.addServlets(
62+
servlet("request", RequestHostValuesServlet.class)
63+
.addMapping("/"));
64+
65+
DeploymentManager manager = container.addDeployment(builder);
66+
manager.deploy();
67+
try {
68+
pathHandler.addPrefixPath(builder.getContextPath(), manager.start());
69+
} catch (ServletException e) {
70+
throw new RuntimeException(e);
71+
}
72+
DefaultServer.setRootHandler(pathHandler);
73+
74+
}
75+
76+
@Test
77+
public void testRequestSpec() throws Exception {
78+
//test request values
79+
TestHttpClient client = new TestHttpClient();
80+
try {
81+
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/");
82+
HttpResponse result = client.execute(get);
83+
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
84+
final String response = HttpClientUtils.readResponse(result);
85+
86+
if(System.getProperty("os.name").toLowerCase().contains("windows") || System.getSecurityManager() != null) {
87+
Assert.assertTrue(String.format("hostName: %s , response: %s", DefaultServer.getDefaultServerAddress().toString(), response), DefaultServer.getDefaultServerAddress().toString().contains(response));
88+
} else {
89+
Assert.assertTrue(String.format("hostName: %s , response: %s", DefaultServer.getHostAddress(), response), DefaultServer.getHostAddress().equals(response));
90+
}
91+
92+
} finally {
93+
client.close();
94+
}
95+
}
96+
97+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* JBoss, Home of Professional Open Source.
3+
* Copyright 2019 Red Hat, Inc., and individual contributors
4+
* as indicated by the @author tags.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package io.undertow.servlet.test.request;
20+
21+
import java.io.IOException;
22+
23+
import javax.servlet.ServletException;
24+
import javax.servlet.http.HttpServlet;
25+
import javax.servlet.http.HttpServletRequest;
26+
import javax.servlet.http.HttpServletResponse;
27+
28+
/**
29+
* Servlet that returns hostname/ip
30+
* @author tmiyar
31+
*
32+
*/
33+
public class RequestHostValuesServlet extends HttpServlet {
34+
35+
@Override
36+
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
37+
38+
resp.getWriter().write(req.getLocalName());
39+
}
40+
}

0 commit comments

Comments
 (0)