Implement special extension to allow headers to be set in include

This commit is contained in:
Jan Bartel 2022-08-12 08:17:56 +10:00
parent 08f344e7f6
commit 95af780d17
3 changed files with 25 additions and 9 deletions

View File

@ -40,7 +40,6 @@ import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
@Disabled //TODO
public class JspIncludeTest
{
private static Server server;
@ -131,7 +130,6 @@ public class JspIncludeTest
// System.out.printf("Response%n%s",response);
assertThat("Response", response, containsString("<h2> Hello, this is the top page."));
assertThat("Response", response, containsString("<h3> This is the included page"));
assertThat("Response Header[main-page-key]", connection.getHeaderField("main-page-key"), is("main-page-value"));
assertThat("Response Header[included-page-key]", connection.getHeaderField("included-page-key"), is("included-page-value"));
}

View File

@ -370,6 +370,8 @@ public class Dispatcher implements RequestDispatcher
private static class IncludeResponse extends HttpServletResponseWrapper
{
public static final String JETTY_INCLUDE_HEADER_PREFIX = "org.eclipse.jetty.server.include.";
public IncludeResponse(HttpServletResponse response)
{
super(response);
@ -441,13 +443,19 @@ public class Dispatcher implements RequestDispatcher
@Override
public void setHeader(String name, String value)
{
// NOOP for include.
//implement jetty-specific extension to include to allow headers
//to be set
if (!StringUtil.isBlank(name) && name.startsWith(JETTY_INCLUDE_HEADER_PREFIX))
super.setHeader(name.substring(JETTY_INCLUDE_HEADER_PREFIX.length()), value);
}
@Override
public void addHeader(String name, String value)
{
// NOOP for include.
//implement jetty-specific extension to include to allow headers
//to be set
if (!StringUtil.isBlank(name) && name.startsWith(JETTY_INCLUDE_HEADER_PREFIX))
super.addHeader(name.substring(JETTY_INCLUDE_HEADER_PREFIX.length()), value);
}
@Override

View File

@ -312,15 +312,19 @@ public class DispatcherTest
_contextHandler.addServlet(IncludeServlet.class, "/IncludeServlet/*");
_contextHandler.addServlet(AssertIncludeServlet.class, "/AssertIncludeServlet/*");
//test include, along with special extension to include that allows headers to
//be set during an include
String responses = _connector.getResponse("""
GET /context/IncludeServlet?do=assertinclude&do=more&test=1 HTTP/1.1\r
GET /context/IncludeServlet?do=assertinclude&do=more&test=1&headers=true HTTP/1.1\r
Host: local\r
Connection: close\r
\r
""");
String expected = """
HTTP/1.1 200 OK\r
specialSetHeader: specialSetHeader\r
specialAddHeader: specialAddHeader\r
Content-Length: 7\r
Connection: close\r
\r
@ -936,13 +940,14 @@ public class DispatcherTest
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
RequestDispatcher dispatcher = null;
Boolean headers = Boolean.valueOf(request.getParameter("headers"));
if (request.getParameter("do").equals("forward"))
dispatcher = getServletContext().getRequestDispatcher("/ForwardServlet/forwardpath?do=assertincludeforward");
else if (request.getParameter("do").equals("assertforwardinclude"))
dispatcher = getServletContext().getRequestDispatcher("/AssertForwardIncludeServlet/assertpath?do=end");
else if (request.getParameter("do").equals("assertinclude"))
dispatcher = getServletContext().getRequestDispatcher("/AssertIncludeServlet?do=end&do=the");
dispatcher = getServletContext().getRequestDispatcher("/AssertIncludeServlet?do=end&do=the&headers=" + headers);
assert dispatcher != null;
dispatcher.include(request, response);
}
@ -1209,7 +1214,7 @@ public class DispatcherTest
assertEquals("/context", request.getAttribute(Dispatcher.INCLUDE_CONTEXT_PATH));
assertEquals("/AssertIncludeServlet", request.getAttribute(Dispatcher.INCLUDE_SERVLET_PATH));
assertNull(request.getAttribute(Dispatcher.INCLUDE_PATH_INFO));
assertEquals("do=end&do=the", request.getAttribute(Dispatcher.INCLUDE_QUERY_STRING));
assertThat((String)request.getAttribute(Dispatcher.INCLUDE_QUERY_STRING), containsString("do=end&do=the"));
HttpServletMapping incMapping = (HttpServletMapping)request.getAttribute(Dispatcher.INCLUDE_MAPPING);
assertNotNull(incMapping);
assertEquals("AssertIncludeServlet", incMapping.getMatchValue());
@ -1221,12 +1226,17 @@ public class DispatcherTest
assertNull(request.getPathInfo());
assertNull(request.getPathTranslated());
assertEquals("do=assertinclude&do=more&test=1", request.getQueryString());
assertThat(request.getQueryString(), containsString("do=assertinclude&do=more&test=1"));
assertEquals("/context/IncludeServlet", request.getRequestURI());
assertEquals("/context", request.getContextPath());
assertEquals("/IncludeServlet", request.getServletPath());
response.setContentType("text/html");
if (Boolean.valueOf(request.getParameter("headers")))
{
response.setHeader("org.eclipse.jetty.server.include.specialSetHeader", "specialSetHeader");
response.setHeader("org.eclipse.jetty.server.include.specialAddHeader", "specialAddHeader");
}
response.setStatus(HttpServletResponse.SC_OK);
response.getOutputStream().print(request.getDispatcherType().toString());
}