Skip to content

Commit

Permalink
Handle invalid forms
Browse files Browse the repository at this point in the history
  • Loading branch information
vietj committed Mar 13, 2016
1 parent 0d7a042 commit c3ce612
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 13 deletions.
Expand Up @@ -149,7 +149,7 @@ void handleReset(long code) {
} }


@Override @Override
void handleError(Throwable cause) { void handleException(Throwable cause) {
if (exceptionHandler != null) { if (exceptionHandler != null) {
exceptionHandler.handle(cause); exceptionHandler.handle(cause);
response.handleError(cause); response.handleError(cause);
Expand All @@ -171,8 +171,8 @@ private void callHandler(Buffer data) {
if (decoder != null) { if (decoder != null) {
try { try {
decoder.offer(new DefaultHttpContent(data.getByteBuf())); decoder.offer(new DefaultHttpContent(data.getByteBuf()));
} catch (HttpPostRequestDecoder.ErrorDataDecoderException e) { } catch (Exception e) {
e.printStackTrace(); handleException(e);
} }
} }
if (dataHandler != null) { if (dataHandler != null) {
Expand All @@ -197,10 +197,10 @@ private void callEnd() {
} }
} }
} }
} catch (HttpPostRequestDecoder.ErrorDataDecoderException e) {
handleException(e);
} catch (HttpPostRequestDecoder.EndOfDataDecoderException e) { } catch (HttpPostRequestDecoder.EndOfDataDecoderException e) {
// ignore this as it is expected // ignore this as it is expected
} catch (Exception e) {
handleException(e);
} finally { } finally {
decoder.destroy(); decoder.destroy();
} }
Expand All @@ -210,10 +210,6 @@ private void callEnd() {
} }
} }


private void handleException(Throwable t) {
t.printStackTrace();
}

private void consume(int numBytes) { private void consume(int numBytes) {
try { try {
boolean windowUpdateSent = conn.local().flowController().consumeBytes(stream, numBytes); boolean windowUpdateSent = conn.local().flowController().consumeBytes(stream, numBytes);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/io/vertx/core/http/impl/VertxHttp2Handler.java
Expand Up @@ -286,7 +286,7 @@ public Push(Http2ServerResponseImpl response, Handler<AsyncResult<HttpServerResp
} }


@Override @Override
void handleError(Throwable cause) { void handleException(Throwable cause) {
response.handleError(cause); response.handleError(cause);
} }


Expand Down Expand Up @@ -458,7 +458,7 @@ protected void onStreamError(ChannelHandlerContext ctx, Throwable cause, Http2Ex
VertxHttp2Stream stream = streams.get(http2Ex.streamId()); VertxHttp2Stream stream = streams.get(http2Ex.streamId());
if (stream != null) { if (stream != null) {
handlerContext.executeFromIO(() -> { handlerContext.executeFromIO(() -> {
stream.handleError(http2Ex); stream.handleException(http2Ex);
}); });
} }
// Default behavior reset stream // Default behavior reset stream
Expand All @@ -475,7 +475,7 @@ protected void onConnectionError(ChannelHandlerContext ctx, Throwable cause, Htt
} }
for (VertxHttp2Stream stream : streams.values()) { for (VertxHttp2Stream stream : streams.values()) {
handlerContext.executeFromIO(() -> { handlerContext.executeFromIO(() -> {
stream.handleError(cause); stream.handleException(cause);
}); });
} }
// Default behavior send go away // Default behavior send go away
Expand Down
Expand Up @@ -25,7 +25,7 @@ abstract class VertxHttp2Stream {


abstract void handleReset(long code); abstract void handleReset(long code);


abstract void handleError(Throwable cause); abstract void handleException(Throwable cause);


abstract void handleClose(); abstract void handleClose();


Expand Down
37 changes: 37 additions & 0 deletions src/test/java/io/vertx/test/core/Http2Test.java
Expand Up @@ -597,6 +597,43 @@ public void testPostFileUpload() throws Exception {
await(); await();
} }


@Test
public void testInvalidPostFileUpload() throws Exception {
server.requestHandler(req -> {
req.setExpectMultipart(true);
AtomicInteger errCount = new AtomicInteger();
req.exceptionHandler(err -> {
errCount.incrementAndGet();
});
req.endHandler(v -> {
assertTrue(errCount.get() > 0);
testComplete();
});
});
startServer();

String contentType = "multipart/form-data; boundary=a4e41223-a527-49b6-ac1c-315d76be757e";
String contentLength = "225";
String body = "--a4e41223-a527-49b6-ac1c-315d76be757e\r\n" +
"Content-Disposition: form-data; name=\"file\"; filename=\"tmp-0.txt\"\r\n" +
"Content-Type: image/gif; charset=ABCD\r\n" +
"Content-Length: 12\r\n" +
"\r\n" +
"some-content\r\n" +
"--a4e41223-a527-49b6-ac1c-315d76be757e--\r\n";

TestClient client = new TestClient();
ChannelFuture fut = client.connect(4043, "localhost", request -> {
int id = request.nextStreamId();
request.encoder.writeHeaders(request.context, id, POST("/form").
set("content-type", contentType).set("content-length", contentLength), 0, false, request.context.newPromise());
request.encoder.writeData(request.context, id, Buffer.buffer(body).getByteBuf(), 0, true, request.context.newPromise());
request.context.flush();
});
fut.sync();
await();
}

@Test @Test
public void testConnect() throws Exception { public void testConnect() throws Exception {
server.requestHandler(req -> { server.requestHandler(req -> {
Expand Down

0 comments on commit c3ce612

Please sign in to comment.