Skip to content

Commit

Permalink
Added distribution test cases for EagerContentHandler modules.
Browse files Browse the repository at this point in the history
Signed-off-by: Simone Bordet <[email protected]>
  • Loading branch information
sbordet committed Nov 13, 2024
1 parent da36c86 commit 6bfc885
Show file tree
Hide file tree
Showing 9 changed files with 257 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public FormRequestContent(Fields fields)

public FormRequestContent(Fields fields, Charset charset)
{
super("application/x-www-form-urlencoded", convert(fields, charset), charset);
super("application/x-www-form-urlencoded;charset=" + charset.name(), convert(fields, charset), charset);
}

public static String convert(Fields fields)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -305,10 +304,7 @@ else if (Supplier.class.isAssignableFrom(context.getClass()))
initializeContextPath(contextHandler, path);

if (Files.isDirectory(path))
{
contextHandler.setBaseResource(ResourceFactory.of(this).newResource(path));
System.err.println("SET BASE RESOURCE to " + path);
}

//TODO think of better way of doing this
//pass through properties as attributes directly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
Applies the EagerContentHandler to the entire server
#tag::description[]
The EagerContentHandler can eagerly load content asynchronously before calling the next handler.
Typically this handler is deployed before an application that uses blocking IO to read the request body and if deployed
after this handler, the application will never (or seldom) block for request content.
Typically, this handler is deployed before an application that uses blocking IO to read the request body
and if deployed after this handler, the application will never (or rarely) block for request content.
This gives many of the benefits of asynchronous IO without the need to write an asynchronous application.
#end::description[]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

[description]
Applies MultiPart configuration to the EagerContentHandler

Expand All @@ -17,23 +16,23 @@ etc/jetty-eager-multipart-content.xml
# jetty.eager.multipart.location=/tmp

## The maximum number of parts that can be parsed from the multipart content, or -1 for unlimited.
# jetty.eager.multipart.maxParts=
# jetty.eager.multipart.maxParts=100

## The maximum size in bytes of the whole multipart content, or -1 for unlimited.
# jetty.eager.multipart.maxSize=
# jetty.eager.multipart.maxSize=52428800

## The maximum part size in bytes, or -1 for unlimited.
# jetty.eager.multipart.maxPartSize=
# jetty.eager.multipart.maxPartSize=10485760

## The maximum size of a part in memory, after which it will be written as a file.
# jetty.eager.multipart.maxMemoryPartSize=
# jetty.eager.multipart.maxMemoryPartSize=1024

## The max length of a Part header, in bytes, or -1 for unlimited length.
# jetty.eager.multipart.maxHeadersSize=
# jetty.eager.multipart.maxHeadersSize=8192

## Whether parts without a fileName are stored as files.
# jetty.eager.multipart.useFilesForPartsWithoutFileName=
# jetty.eager.multipart.useFilesForPartsWithoutFileName=true

## The MultiPart compliance mode.
# jetty.eager.multipart.complianceMode=RFC7578
#end::documentation[]
#end::documentation[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

package org.eclipse.jetty.demo.simple;

import java.io.IOException;
import java.util.stream.Collectors;

import jakarta.servlet.MultipartConfigElement;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletInputStream;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.annotation.MultipartConfig;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

@MultipartConfig
public class EchoServlet extends HttpServlet
{
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
response.setContentType(request.getContentType());
ServletOutputStream output = response.getOutputStream();
String pathInfo = request.getPathInfo();
switch (pathInfo)
{
case "/form" ->
{
String content = request.getParameterMap().entrySet().stream()
.map(e -> "%s=%s".formatted(e.getKey(), String.join(", ", e.getValue())))
.collect(Collectors.joining("&"));
output.print(content);
}
case "/multipart" ->
{
MultipartConfigElement config = new MultipartConfigElement("");
request.setAttribute(MultipartConfigElement.class.getName(), config);

String content = request.getParts().stream()
.map(part -> "name=%s&length=%d".formatted(part.getName(), part.getSize()))
.collect(Collectors.joining(","));
output.print(content);
}
default ->
{
ServletInputStream input = request.getInputStream();
response.setContentLengthLong(request.getContentLengthLong());
input.transferTo(output);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,13 @@
<url-pattern>/hello/*</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>echo</servlet-name>
<servlet-class>org.eclipse.jetty.demo.simple.EchoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>echo</servlet-name>
<url-pattern>/echo/*</url-pattern>
</servlet-mapping>

</web-app>
Original file line number Diff line number Diff line change
Expand Up @@ -1283,7 +1283,6 @@ private void extractContentParameters() throws BadMessageException
}
catch (IllegalStateException | IllegalArgumentException | CompletionException e)
{
LOG.warn(e.toString());
throw new BadMessageException("Unable to parse form content", e);
}
}
Expand Down Expand Up @@ -1321,7 +1320,6 @@ else if (MimeTypes.Type.MULTIPART_FORM_DATA.is(baseType) &&
}
catch (IllegalStateException | IllegalArgumentException | CompletionException e)
{
LOG.warn(e.toString());
throw new BadMessageException("Unable to parse form content", e);
}
}
Expand All @@ -1332,7 +1330,6 @@ else if (MimeTypes.Type.MULTIPART_FORM_DATA.is(baseType) &&
}
catch (IllegalStateException | IllegalArgumentException e)
{
LOG.warn(e.toString());
throw new BadMessageException("Unable to parse form content", e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1292,7 +1292,6 @@ private void extractContentParameters() throws BadMessageException
}
catch (IllegalStateException | IllegalArgumentException | CompletionException e)
{
LOG.warn(e.toString());
throw new BadMessageException("Unable to parse form content", e);
}
}
Expand Down Expand Up @@ -1330,7 +1329,6 @@ else if (MimeTypes.Type.MULTIPART_FORM_DATA.is(baseType) &&
}
catch (IllegalStateException | IllegalArgumentException | CompletionException e)
{
LOG.warn(e.toString());
throw new BadMessageException("Unable to parse form content", e);
}
}
Expand All @@ -1341,7 +1339,6 @@ else if (MimeTypes.Type.MULTIPART_FORM_DATA.is(baseType) &&
}
catch (IllegalStateException | IllegalArgumentException e)
{
LOG.warn(e.toString());
throw new BadMessageException("Unable to parse form content", e);
}
}
Expand Down
Loading

0 comments on commit 6bfc885

Please sign in to comment.