Skip to content
Open
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 @@ -19,6 +19,7 @@
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
Expand All @@ -38,14 +39,14 @@
import jakarta.servlet.http.HttpServletRequestWrapper;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpServletResponseWrapper;
import jakarta.servlet.http.Part;
import org.eclipse.jetty.ee10.servlet.util.ServletOutputStreamWrapper;
import org.eclipse.jetty.http.HttpURI;
import org.eclipse.jetty.http.pathmap.MatchedResource;
import org.eclipse.jetty.io.WriterOutputStream;
import org.eclipse.jetty.util.Fields;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.UrlEncoded;

public class Dispatcher implements RequestDispatcher
Expand All @@ -59,14 +60,19 @@ public class Dispatcher implements RequestDispatcher
* Dispatch include attribute names
*/
public static final String __FORWARD_PREFIX = "jakarta.servlet.forward.";

/**
* Name of original request attribute
*/
public static final String __ORIGINAL_REQUEST = "org.eclipse.jetty.originalRequest";

public static final String JETTY_INCLUDE_HEADER_PREFIX = "org.eclipse.jetty.server.include.";

/**
* This attribute is used to store the wrapped request for internal use during a dispatch if needed.
*/
public static final String WRAPPED_REQUEST_ATTRIBUTE = "org.eclipse.jetty.server.wrappedRequest";

private final ServletContextHandler _contextHandler;
private final HttpURI _uri;
private final String _decodedPathInContext;
Expand Down Expand Up @@ -398,6 +404,34 @@ public Enumeration<String> getAttributeNames()
names.add(RequestDispatcher.FORWARD_QUERY_STRING);
return Collections.enumeration(names);
}

@Override
public Collection<Part> getParts() throws IOException, ServletException
{
try
{
setAttribute(WRAPPED_REQUEST_ATTRIBUTE, this);
return super.getParts();
}
finally
{
setAttribute(WRAPPED_REQUEST_ATTRIBUTE, null);
}
}

@Override
public Part getPart(String name) throws IOException, ServletException
{
try
{
setAttribute(WRAPPED_REQUEST_ATTRIBUTE, this);
return super.getPart(name);
}
finally
{
setAttribute(WRAPPED_REQUEST_ATTRIBUTE, null);
}
}
}

private class IncludeRequest extends ParameterRequestWrapper
Expand Down Expand Up @@ -434,6 +468,14 @@ public Object getAttribute(String name)
case RequestDispatcher.INCLUDE_REQUEST_URI -> (_uri == null) ? null : _uri.getPath();
case RequestDispatcher.INCLUDE_CONTEXT_PATH -> _httpServletRequest.getContextPath();
case RequestDispatcher.INCLUDE_QUERY_STRING -> (_uri == null) ? null : _uri.getQuery();
case ServletContextRequest.MULTIPART_CONFIG_ELEMENT ->
{
// If we already have future parts, return the configuration of the wrapped request.
if (super.getAttribute(ServletMultiPartFormData.class.getName()) != null)
yield super.getAttribute(name);
// otherwise, return the configuration of this mapping
yield _mappedServlet.getServletHolder().getMultipartConfigElement();
}
default -> super.getAttribute(name);
};
}
Expand All @@ -443,6 +485,11 @@ public Enumeration<String> getAttributeNames()
{
//Servlet Spec 9.3.1 no include attributes if a named dispatcher
ArrayList<String> names = new ArrayList<>(Collections.list(super.getAttributeNames()));

//only return the multipart attribute name if this servlet mapping has multipart config
if (names.contains(ServletContextRequest.MULTIPART_CONFIG_ELEMENT) && _mappedServlet.getServletHolder().getMultipartConfigElement() == null)
names.remove(ServletContextRequest.MULTIPART_CONFIG_ELEMENT);

if (_named != null)
return Collections.enumeration(names);

Expand All @@ -460,6 +507,34 @@ public String getQueryString()
{
return _httpServletRequest.getQueryString();
}

@Override
public Collection<Part> getParts() throws IOException, ServletException
{
try
{
setAttribute(WRAPPED_REQUEST_ATTRIBUTE, this);
return super.getParts();
}
finally
{
setAttribute(WRAPPED_REQUEST_ATTRIBUTE, null);
}
}

@Override
public Part getPart(String name) throws IOException, ServletException
{
try
{
setAttribute(WRAPPED_REQUEST_ATTRIBUTE, this);
return super.getPart(name);
}
finally
{
setAttribute(WRAPPED_REQUEST_ATTRIBUTE, null);
}
}
}

private static class IncludeResponse extends HttpServletResponseWrapper
Expand Down Expand Up @@ -738,6 +813,14 @@ public Object getAttribute(String name)
case AsyncContextState.ASYNC_PATH_INFO -> _httpServletRequest.getPathInfo();
case AsyncContextState.ASYNC_SERVLET_PATH -> _httpServletRequest.getServletPath();
case AsyncContextState.ASYNC_QUERY_STRING -> _httpServletRequest.getQueryString();
case ServletContextRequest.MULTIPART_CONFIG_ELEMENT ->
{
// If we already have future parts, return the configuration of the wrapped request.
if (super.getAttribute(ServletMultiPartFormData.class.getName()) != null)
yield super.getAttribute(name);
// otherwise, return the configuration of this mapping
yield _mappedServlet.getServletHolder().getMultipartConfigElement();
}
default -> super.getAttribute(name);
};
}
Expand All @@ -746,6 +829,11 @@ public Object getAttribute(String name)
public Enumeration<String> getAttributeNames()
{
ArrayList<String> names = new ArrayList<>(Collections.list(super.getAttributeNames()));

//only return the multipart attribute name if this servlet mapping has multipart config
if (names.contains(ServletContextRequest.MULTIPART_CONFIG_ELEMENT) && _mappedServlet.getServletHolder().getMultipartConfigElement() == null)
names.remove(ServletContextRequest.MULTIPART_CONFIG_ELEMENT);

names.add(AsyncContextState.ASYNC_REQUEST_URI);
names.add(AsyncContextState.ASYNC_SERVLET_PATH);
names.add(AsyncContextState.ASYNC_PATH_INFO);
Expand Down Expand Up @@ -785,6 +873,34 @@ else if (getRequest() instanceof ServletApiRequest servletApiRequest)
}
return super.getParameters();
}

@Override
public Collection<Part> getParts() throws IOException, ServletException
{
try
{
setAttribute(WRAPPED_REQUEST_ATTRIBUTE, this);
return super.getParts();
}
finally
{
setAttribute(WRAPPED_REQUEST_ATTRIBUTE, null);
}
}

@Override
public Part getPart(String name) throws IOException, ServletException
{
try
{
setAttribute(WRAPPED_REQUEST_ATTRIBUTE, this);
return super.getPart(name);
}
finally
{
setAttribute(WRAPPED_REQUEST_ATTRIBUTE, null);
}
}
}

private class ErrorRequest extends ParameterRequestWrapper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.eclipse.jetty.ee10.servlet.Dispatcher.WRAPPED_REQUEST_ATTRIBUTE;

/**
* The Jetty implementation of the ee10 {@link HttpServletRequest} object.
* This provides the bridge from Servlet {@link HttpServletRequest} to the Jetty Core {@link Request}
Expand Down Expand Up @@ -634,7 +636,8 @@ public Collection<Part> getParts() throws IOException, ServletException
{
try
{
_parts = ServletMultiPartFormData.getParts(this);
ServletRequest dispatchedRequest = (ServletRequest)getAttribute(WRAPPED_REQUEST_ATTRIBUTE);
_parts = ServletMultiPartFormData.getParts(dispatchedRequest == null ? this : dispatchedRequest);

Collection<Part> parts = _parts.getParts();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Consumer;
import java.util.stream.Stream;
import java.util.zip.GZIPInputStream;

import jakarta.servlet.MultipartConfigElement;
Expand Down Expand Up @@ -56,13 +57,16 @@
import org.eclipse.jetty.logging.StacklessLogging;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.ErrorHandler;
import org.eclipse.jetty.server.handler.gzip.GzipHandler;
import org.eclipse.jetty.util.Blocker;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.component.LifeCycle;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;

import static java.nio.charset.StandardCharsets.UTF_8;
Expand Down Expand Up @@ -96,15 +100,23 @@ public void before() throws Exception

private void start(HttpServlet servlet, MultipartConfigElement config, boolean eager) throws Exception
{
config = config == null ? new MultipartConfigElement(tmpDirString, MAX_FILE_SIZE, -1, 0) : config;
start(servletContextHandler ->
{
ServletHolder servletHolder = new ServletHolder(servlet);
servletHolder.getRegistration().setMultipartConfig(config == null
? new MultipartConfigElement(tmpDirString, MAX_FILE_SIZE, -1, 0) : config);
servletContextHandler.addServlet(servletHolder, "/");
}, eager);
}

private void start(Consumer<ServletContextHandler> consumer, boolean eager) throws Exception
{
server = new Server(null, null, null);
connector = new ServerConnector(server);
server.addConnector(connector);

ServletContextHandler servletContextHandler = new ServletContextHandler("/");
ServletHolder servletHolder = new ServletHolder(servlet);
servletHolder.getRegistration().setMultipartConfig(config);
servletContextHandler.addServlet(servletHolder, "/");
consumer.accept(servletContextHandler);
server.setHandler(servletContextHandler);

GzipHandler gzipHandler = new GzipHandler();
Expand Down Expand Up @@ -570,4 +582,97 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I
assertThat(responseContent, containsString("Parameter: part3=" + contentString));
assertThat(responseContent, not(containsString("Parameter: partFileName=" + contentString)));
}

public static Stream<Arguments> dispatchTestArgs()
{
return Stream.of(
Arguments.of(true, "forward"),
Arguments.of(false, "forward"),
Arguments.of(true, "include"),
Arguments.of(false, "include"),
Arguments.of(true, "async"),
Arguments.of(false, "async")
);
}

@ParameterizedTest
@MethodSource("dispatchTestArgs")
public void testDispatch(boolean eager, String dispatchType) throws Exception
{
start(servletContextHandler ->
{
servletContextHandler.addServlet(new HttpServlet()
{
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
switch (dispatchType)
{
case "forward" -> request.getRequestDispatcher("/multipart").forward(request, response);
case "include" -> request.getRequestDispatcher("/multipart").include(request, response);
case "async" ->
{
request.startAsync();
request.getAsyncContext().dispatch("/multipart");
}
default -> throw new ServletException("Unknown dispatch type: " + dispatchType);
}
}
}, "/");

ServletHolder servletHolder = new ServletHolder(new HttpServlet()
{
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
Collection<Part> parts = request.getParts();
assertNotNull(parts);
assertEquals(1, parts.size());
Part part = parts.iterator().next();
assertEquals("part1", part.getName());
Collection<String> headerNames = part.getHeaderNames();
assertNotNull(headerNames);
assertEquals(2, headerNames.size());
String content1 = IO.toString(part.getInputStream(), UTF_8);
assertEquals("content1", content1);
response.getWriter().print("success!");
}
});
servletHolder.getRegistration().setMultipartConfig(new MultipartConfigElement(tmpDirString, MAX_FILE_SIZE, -1, 0));
servletContextHandler.addServlet(servletHolder, "/multipart");
}, eager);

if (server.getErrorHandler() instanceof ErrorHandler errorHandler)
errorHandler.setShowStacks(true);

try (Socket socket = new Socket("localhost", connector.getLocalPort()))
{
OutputStream output = socket.getOutputStream();

String content = """
--A1B2C3
Content-Disposition: form-data; name="part1"
Content-Type: text/plain; charset="UTF-8"

content1
--A1B2C3--
""";
String header = """
POST / HTTP/1.1
Host: localhost
Content-Type: multipart/form-data; boundary="A1B2C3"
Content-Length: $L

""".replace("$L", String.valueOf(content.length()));

output.write(header.getBytes(UTF_8));
output.write(content.getBytes(UTF_8));
output.flush();

HttpTester.Response response = HttpTester.parseResponse(socket.getInputStream());
assertNotNull(response);
assertEquals(HttpStatus.OK_200, response.getStatus());
assertThat(response.getContent(), equalTo("success!"));
}
}
}