Can redirection to a context root with a trailing slash be disabled?
cslater May 13, 2016 10:50 AMI recently set up a WildFly instance to host automated test application endpoints.
Originally I had an application with a context root of "/" and a servlet at "/demo" and everything worked as expected.
jboss-web.xml
<jboss-web> <context-root>/</context-root> </jboss-web>
DemoServlet
@WebServlet("/demo") public class DemoServlet extends HttpServlet { private static final long serialVersionUID = 7102934004098061933L; protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("application/json;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { out.print("{\"result\":\"success\"}"); } } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } }
GET Request
curl -v http://localhost:8080/demo * Trying ::1... * connect to ::1 port 8080 failed: Connection refused * Trying 127.0.0.1... * Connected to localhost (127.0.0.1) port 8080 (#0) > GET /demo HTTP/1.1 > Host: localhost:8080 > User-Agent: curl/7.43.0 > Accept: */* > < HTTP/1.1 200 OK < Connection: keep-alive < X-Powered-By: Undertow/1 < Server: WildFly/10 < Content-Type: application/json;charset=UTF-8 < Content-Length: 20 < Date: Fri, 13 May 2016 14:22:38 GMT < * Connection #0 to host localhost left intact {"result":"success"}
POST Request
curl -v -d "" http://localhost:8080/demo * Trying ::1... * connect to ::1 port 8080 failed: Connection refused * Trying 127.0.0.1... * Connected to localhost (127.0.0.1) port 8080 (#0) > POST /demo HTTP/1.1 > Host: localhost:8080 > User-Agent: curl/7.43.0 > Accept: */* > Content-Length: 0 > Content-Type: application/x-www-form-urlencoded > < HTTP/1.1 200 OK < Connection: keep-alive < X-Powered-By: Undertow/1 < Server: WildFly/10 < Content-Type: application/json;charset=UTF-8 < Content-Length: 20 < Date: Fri, 13 May 2016 14:30:23 GMT < * Connection #0 to host localhost left intact {"result":"success"}
However, I then had to move my application out of "/", but needed my tests to work. I set the context root to "/demo" and my servlet to "/" and now WildFly is returning redirects to the same URL with a slash at the end.
jboss-web.xml
<jboss-web> <context-root>/demo</context-root> </jboss-web>
DemoServlet
@WebServlet("/") public class DemoServlet extends HttpServlet { private static final long serialVersionUID = 7102934004098061933L; protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("application/json;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { out.print("{\"result\":\"success\"}"); } } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } }
GET Request
curl -v http://localhost:8080/demo * Trying ::1... * connect to ::1 port 8080 failed: Connection refused * Trying 127.0.0.1... * Connected to localhost (127.0.0.1) port 8080 (#0) > GET /demo HTTP/1.1 > Host: localhost:8080 > User-Agent: curl/7.43.0 > Accept: */* > < HTTP/1.1 302 Found < Connection: keep-alive < X-Powered-By: Undertow/1 < Server: WildFly/10 < Location: http://localhost:8080/demo/ < Content-Length: 0 < Date: Fri, 13 May 2016 14:37:26 GMT < * Connection #0 to host localhost left intact
POST Request
curl -v -d "" http://localhost:8080/demo * Trying ::1... * connect to ::1 port 8080 failed: Connection refused * Trying 127.0.0.1... * Connected to localhost (127.0.0.1) port 8080 (#0) > POST /demo HTTP/1.1 > Host: localhost:8080 > User-Agent: curl/7.43.0 > Accept: */* > Content-Length: 0 > Content-Type: application/x-www-form-urlencoded > < HTTP/1.1 307 Temporary Redirect < Connection: keep-alive < X-Powered-By: Undertow/1 < Server: WildFly/10 < Location: http://localhost:8080/demo/ < Content-Length: 0 < Date: Fri, 13 May 2016 14:38:06 GMT * HTTP error before end of send, stop sending < * Closing connection 0
In researching this, it looks as this behavior is as designed.
https://issues.jboss.org/browse/UNDERTOW-404
https://issues.jboss.org/browse/UNDERTOW-89
However, this seems incorrect to me. The same URL in my above example has inconsistent behavior depending on location of the context root, of which clients should not be aware of. Is there any way to disable this feature?