Skip to content

Commit 4974f2b

Browse files
committed
File upload multipart hits an error of ClassCastException fix #1815
1 parent 512e0e2 commit 4974f2b

3 files changed

Lines changed: 43 additions & 10 deletions

File tree

modules/jooby-netty/src/main/java/io/jooby/internal/netty/NettyContext.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,7 @@ public NettyContext(ChannelHandlerContext ctx, HttpRequest req, Router router, S
223223
}
224224

225225
@Nonnull @Override public Formdata form() {
226-
if (form == null) {
227-
form = Formdata.create(this);
228-
decodeForm(req, form);
229-
}
230-
return form;
226+
return multipart();
231227
}
232228

233229
@Nonnull @Override public Multipart multipart() {

modules/jooby-utow/src/main/java/io/jooby/internal/utow/UtowContext.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,7 @@ public UtowContext(HttpServerExchange exchange, Router router) {
235235
}
236236

237237
@Nonnull @Override public Formdata form() {
238-
if (form == null) {
239-
form = Formdata.create(this);
240-
formData(form, exchange.getAttachment(FORM_DATA));
241-
}
242-
return form;
238+
return multipart();
243239
}
244240

245241
@Nonnull @Override public Multipart multipart() {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package io.jooby;
2+
3+
import io.jooby.junit.ServerTest;
4+
import io.jooby.junit.ServerTestRunner;
5+
import okhttp3.MediaType;
6+
import okhttp3.MultipartBody;
7+
8+
import java.nio.file.Path;
9+
import java.nio.file.Paths;
10+
11+
import static okhttp3.RequestBody.create;
12+
import static org.junit.jupiter.api.Assertions.assertEquals;
13+
14+
public class Issue1815 {
15+
16+
@ServerTest
17+
public void shouldNotThrowClassCastExceptionOnFileUploadWithCsrfHandler(ServerTestRunner runner) {
18+
runner.define(app -> {
19+
app.before(new CsrfHandler());
20+
21+
app.post("/1815", ctx -> ctx.multipart("file"));
22+
}).ready(http -> {
23+
http.post("/1815", new MultipartBody.Builder()
24+
.setType(MultipartBody.FORM)
25+
.addFormDataPart("file", "fileupload.js",
26+
create(userdir("src", "test", "resources", "files", "fileupload.js").toFile(),
27+
MediaType.parse("application/javascript")))
28+
.build(), rsp -> {
29+
assertEquals(StatusCode.FORBIDDEN_CODE, rsp.code());
30+
});
31+
});
32+
}
33+
34+
private static Path userdir(String... segments) {
35+
Path path = Paths.get(System.getProperty("user.dir"));
36+
for (String segment : segments) {
37+
path = path.resolve(segment);
38+
}
39+
return path;
40+
}
41+
}

0 commit comments

Comments
 (0)