Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #5057 - Included root context path #5058

Merged
merged 3 commits into from
Jul 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void include(ServletRequest request, ServletResponse response) throws Ser
IncludeAttributes attr = new IncludeAttributes(old_attr);

attr._requestURI = _uri.getPath();
attr._contextPath = _contextHandler.getContextPath();
attr._contextPath = _contextHandler.getRequestContextPath();
attr._servletPath = null; // set by ServletHandler
attr._pathInfo = _pathInContext;
attr._query = _uri.getQuery();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ public void handleAsync(HttpChannel channel) throws IOException, ServletExceptio
// this is a dispatch with a path
ServletContext context = event.getServletContext();
String query = baseRequest.getQueryString();
baseRequest.setURIPathQuery(URIUtil.addEncodedPaths(context == null ? null : URIUtil.encodePath(context.getContextPath()), path));
baseRequest.setURIPathQuery(URIUtil.addEncodedPaths(context == null ? null : context.getContextPath(), path));
HttpURI uri = baseRequest.getHttpURI();
baseRequest.setPathInfo(uri.getDecodedPath());
if (uri.getQuery() != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,17 @@ public String getContextPathEncoded()
return _contextPathEncoded;
}

/**
* Get the context path in a form suitable to be returned from {@link HttpServletRequest#getContextPath()}
* or {@link ServletContext#getContextPath()}.
* @return Returns the encoded contextPath, or empty string for root context
*/
public String getRequestContextPath()
joakime marked this conversation as resolved.
Show resolved Hide resolved
{
String contextPathEncoded = getContextPathEncoded();
return "/".equals(contextPathEncoded) ? "" : contextPathEncoded;
}

/*
* @see javax.servlet.ServletContext#getInitParameter(java.lang.String)
*/
Expand Down Expand Up @@ -1267,10 +1278,7 @@ else if (_contextPath.length() == 1)
__context.set(_scontext);
if (!DispatcherType.INCLUDE.equals(dispatch) && target.startsWith("/"))
{
if (_contextPath.length() == 1)
baseRequest.setContextPath("");
else
baseRequest.setContextPath(getContextPathEncoded());
baseRequest.setContextPath(getRequestContextPath());
baseRequest.setServletPath(null);
baseRequest.setPathInfo(pathInfo);
}
Expand Down Expand Up @@ -2418,10 +2426,7 @@ public String getServletContextName()
@Override
public String getContextPath()
{
if ((_contextPath != null) && _contextPath.equals(URIUtil.SLASH))
return "";

return _contextPath;
return getRequestContextPath();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,28 @@

package org.eclipse.jetty.servlet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.HttpURLConnection;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.DispatcherType;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.toolchain.test.IO;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -71,6 +77,45 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
}
}

public static class IncludedAttrServlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
if (req.getDispatcherType() == DispatcherType.INCLUDE)
{
if (req.getAttribute("included") == null)
{
req.setAttribute("included", Boolean.TRUE);
dumpAttrs("BEFORE1", req, resp.getOutputStream());
req.getRequestDispatcher("two").include(req, resp);
dumpAttrs("AFTER1", req, resp.getOutputStream());
}
else
{
dumpAttrs("DURING", req, resp.getOutputStream());
}
}
else
{
resp.setContentType("text/plain");
dumpAttrs("BEFORE0", req, resp.getOutputStream());
req.getRequestDispatcher("one").include(req, resp);
dumpAttrs("AFTER0", req, resp.getOutputStream());
}
}

private void dumpAttrs(String tag, HttpServletRequest req, ServletOutputStream out) throws IOException
{
out.println(String.format("%s: %s='%s'", tag, RequestDispatcher.INCLUDE_CONTEXT_PATH,
req.getAttribute(RequestDispatcher.INCLUDE_CONTEXT_PATH)));
out.println(String.format("%s: %s='%s'", tag, RequestDispatcher.INCLUDE_SERVLET_PATH,
req.getAttribute(RequestDispatcher.INCLUDE_SERVLET_PATH)));
out.println(String.format("%s: %s='%s'", tag, RequestDispatcher.INCLUDE_PATH_INFO,
req.getAttribute(RequestDispatcher.INCLUDE_PATH_INFO)));
}
}

private Server server;
private URI baseUri;

Expand All @@ -87,6 +132,7 @@ public void startServer() throws Exception
context.setContextPath("/");
context.addServlet(TopServlet.class, "/top");
context.addServlet(IncludedServlet.class, "/included");
context.addServlet(IncludedAttrServlet.class, "/attr/*");

server.setHandler(context);

Expand Down Expand Up @@ -173,4 +219,52 @@ private String getPotentialBody(HttpURLConnection connection)
IO.close(in);
}
}

@Test
public void testIncludeAttributes() throws IOException
{
URI uri = baseUri.resolve("/attr/one");
InputStream in = null;
BufferedReader reader = null;
HttpURLConnection connection = null;

try
{
connection = (HttpURLConnection)uri.toURL().openConnection();
connection.connect();
assertThat(connection.getResponseCode(), is(HttpURLConnection.HTTP_OK));
in = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(in));
List<String> result = new ArrayList<>();
String line = reader.readLine();
while (line != null)
{
result.add(line);
line = reader.readLine();
}

assertThat(result, Matchers.contains(
"BEFORE0: javax.servlet.include.context_path='null'",
"BEFORE0: javax.servlet.include.servlet_path='null'",
"BEFORE0: javax.servlet.include.path_info='null'",
"BEFORE1: javax.servlet.include.context_path=''",
"BEFORE1: javax.servlet.include.servlet_path='/attr'",
"BEFORE1: javax.servlet.include.path_info='/one'",
"DURING: javax.servlet.include.context_path=''",
"DURING: javax.servlet.include.servlet_path='/attr'",
"DURING: javax.servlet.include.path_info='/two'",
"AFTER1: javax.servlet.include.context_path=''",
"AFTER1: javax.servlet.include.servlet_path='/attr'",
"AFTER1: javax.servlet.include.path_info='/one'",
"AFTER0: javax.servlet.include.context_path='null'",
"AFTER0: javax.servlet.include.servlet_path='null'",
"AFTER0: javax.servlet.include.path_info='null'"
));
}
finally
{
IO.close(reader);
IO.close(in);
}
}
}