Skip to content

Commit 119fe4f

Browse files
authored
Merge pull request #4055 from shawzyy457/jetty-chunked-max-request-size
jetty: apply maxRequestSize to chunked request bodies
2 parents f3e7223 + 0d596e9 commit 119fe4f

2 files changed

Lines changed: 67 additions & 1 deletion

File tree

‎modules/jooby-jetty/src/main/java/io/jooby/internal/jetty/JettyContext.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ public Body body() {
150150
}
151151
InputStream in = Content.Source.asInputStream(request);
152152
long len = request.getLength();
153-
if (len >= maxRequestSize) {
153+
// A chunked body reports -1, so it gets the limit too.
154+
if (len < 0 || len >= maxRequestSize) {
154155
in = new LimitedInputStream(in, maxRequestSize);
155156
}
156157
return Body.of(this, in, len);
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package io.jooby.i4053;
7+
8+
import static io.jooby.test.TestUtil._16kb;
9+
import static io.jooby.test.TestUtil._19kb;
10+
import static org.junit.jupiter.api.Assertions.assertEquals;
11+
12+
import java.io.IOException;
13+
14+
import io.jooby.ServerOptions;
15+
import io.jooby.junit.ServerTest;
16+
import io.jooby.junit.ServerTestRunner;
17+
import okhttp3.MediaType;
18+
import okhttp3.RequestBody;
19+
import okio.BufferedSink;
20+
21+
public class Issue4053 {
22+
23+
@ServerTest
24+
public void maxRequestSizeAppliesToChunkedBody(ServerTestRunner runner) {
25+
runner
26+
.options(new ServerOptions().setMaxRequestSize(ServerOptions._16KB))
27+
.define(app -> app.post("/4053", ctx -> ctx.body().value("")))
28+
.ready(
29+
client -> {
30+
client.post(
31+
"/4053",
32+
chunked(_19kb),
33+
rsp -> {
34+
assertEquals(413, rsp.code());
35+
});
36+
client.post(
37+
"/4053",
38+
chunked(_16kb),
39+
rsp -> {
40+
assertEquals(200, rsp.code());
41+
assertEquals(_16kb, rsp.body().string());
42+
});
43+
});
44+
}
45+
46+
// OkHttp sends a body of unknown length with Transfer-Encoding: chunked.
47+
private static RequestBody chunked(String value) {
48+
return new RequestBody() {
49+
@Override
50+
public MediaType contentType() {
51+
return MediaType.get("text/plain");
52+
}
53+
54+
@Override
55+
public long contentLength() {
56+
return -1;
57+
}
58+
59+
@Override
60+
public void writeTo(BufferedSink sink) throws IOException {
61+
sink.writeUtf8(value);
62+
}
63+
};
64+
}
65+
}

0 commit comments

Comments
 (0)