Skip to content

Commit 7df574c

Browse files
committed
[UNDERTOW-1601] - fix max entity handling for multipart
1 parent 2abb6a2 commit 7df574c

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

core/src/main/java/io/undertow/server/HttpServerExchange.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import io.undertow.util.ConduitFactory;
4141
import io.undertow.util.Cookies;
4242
import io.undertow.util.HeaderMap;
43+
import io.undertow.util.HeaderValues;
4344
import io.undertow.util.Headers;
4445
import io.undertow.util.HttpString;
4546
import io.undertow.util.Methods;
@@ -228,12 +229,15 @@ public final class HttpServerExchange extends AbstractAttachable {
228229
* The default value for this is determined by the {@link io.undertow.UndertowOptions#MAX_ENTITY_SIZE} option. A value
229230
* of 0 indicates that this is unbounded.
230231
* <p>
232+
* In case of multipart handling, this will default to {@link io.undertow.UndertowOptions#MULTIPART_MAX_ENTITY_SIZE}
233+
* <p>
231234
* If this entity size is exceeded the request channel will be forcibly closed.
232235
* <p>
233236
* TODO: integrate this with HTTP 100-continue responses, to make it possible to send a 417 rather than just forcibly
234237
* closing the channel.
235238
*
236239
* @see io.undertow.UndertowOptions#MAX_ENTITY_SIZE
240+
* @see io.undertow.UndertowOptions#MULTIPART_MAX_ENTITY_SIZE
237241
*/
238242
private long maxEntitySize;
239243

@@ -1871,6 +1875,16 @@ public XnioIoThread getIoThread() {
18711875
return connection.getIoThread();
18721876
}
18731877

1878+
public boolean isMultiPartExchange() {
1879+
//NOTE: should this include Range response?
1880+
final HeaderValues contentTypeHeaders = getRequestHeaders().get("Content-Type");
1881+
if(contentTypeHeaders != null && contentTypeHeaders.size() >0) {
1882+
return contentTypeHeaders.getFirst().startsWith("multipart");
1883+
} else {
1884+
return false;
1885+
}
1886+
}
1887+
18741888
/**
18751889
* @return The maximum entity size for this exchange
18761890
*/

servlet/src/main/java/io/undertow/servlet/core/ManagedServlet.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class ManagedServlet implements Lifecycle {
6060
private final InstanceStrategy instanceStrategy;
6161
private volatile boolean permanentlyUnavailable = false;
6262

63-
private long maxRequestSize;
63+
private long maxMultipartRequestSize;
6464
private FormParserFactory formParserFactory;
6565
private MultipartConfigElement multipartConfig;
6666

@@ -92,9 +92,9 @@ public void setupMultipart(ServletContextImpl servletContext) {
9292
//todo: fileSizeThreshold
9393
MultipartConfigElement config = multipartConfig;
9494
if (config.getMaxRequestSize() != -1) {
95-
maxRequestSize = config.getMaxRequestSize();
95+
maxMultipartRequestSize = config.getMaxRequestSize();
9696
} else {
97-
maxRequestSize = -1;
97+
maxMultipartRequestSize = -1;
9898
}
9999
final Path tempDir;
100100
if(config.getLocation() == null || config.getLocation().isEmpty()) {
@@ -127,7 +127,7 @@ public void setupMultipart(ServletContextImpl servletContext) {
127127
} else {
128128
//no multipart config we don't allow multipart requests
129129
formParserFactory = FormParserFactory.builder(false).addParser(formDataParser).build();
130-
maxRequestSize = -1;
130+
maxMultipartRequestSize = -1;
131131
}
132132
}
133133

@@ -235,8 +235,16 @@ public ServletInfo getServletInfo() {
235235
return servletInfo;
236236
}
237237

238+
/**
239+
* @deprecated
240+
* @return
241+
*/
238242
public long getMaxRequestSize() {
239-
return maxRequestSize;
243+
return maxMultipartRequestSize;
244+
}
245+
246+
public long getMaxMultipartRequestSize() {
247+
return maxMultipartRequestSize;
240248
}
241249

242250
public FormParserFactory getFormParserFactory() {

servlet/src/main/java/io/undertow/servlet/handlers/ServletInitialHandler.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ public void handleRequest(final HttpServerExchange exchange) throws Exception {
154154
final HttpServletRequestImpl request = new HttpServletRequestImpl(exchange, servletContext);
155155
final ServletRequestContext servletRequestContext = new ServletRequestContext(servletContext.getDeployment(), request, response, info);
156156
//set the max request size if applicable
157-
if (info.getServletChain().getManagedServlet().getMaxRequestSize() > 0) {
158-
exchange.setMaxEntitySize(info.getServletChain().getManagedServlet().getMaxRequestSize());
157+
if (info.getServletChain().getManagedServlet().getMaxMultipartRequestSize() > 0 && exchange.isMultiPartExchange()) {
158+
exchange.setMaxEntitySize(info.getServletChain().getManagedServlet().getMaxMultipartRequestSize());
159159
}
160160
exchange.putAttachment(ServletRequestContext.ATTACHMENT_KEY, servletRequestContext);
161161

@@ -224,8 +224,8 @@ public void dispatchMockRequest(HttpServletRequest request, HttpServletResponse
224224
servletRequestContext.setServletRequest(request);
225225
servletRequestContext.setServletResponse(response);
226226
//set the max request size if applicable
227-
if (info.getServletChain().getManagedServlet().getMaxRequestSize() > 0) {
228-
exchange.setMaxEntitySize(info.getServletChain().getManagedServlet().getMaxRequestSize());
227+
if (info.getServletChain().getManagedServlet().getMaxMultipartRequestSize() > 0 && exchange.isMultiPartExchange()) {
228+
exchange.setMaxEntitySize(info.getServletChain().getManagedServlet().getMaxMultipartRequestSize());
229229
}
230230
exchange.putAttachment(ServletRequestContext.ATTACHMENT_KEY, servletRequestContext);
231231

0 commit comments

Comments
 (0)