diff --git a/reactor-netty5-http/src/main/java/reactor/netty5/http/server/HttpServerOperations.java b/reactor-netty5-http/src/main/java/reactor/netty5/http/server/HttpServerOperations.java index 8c6907cb8f..7c030039aa 100644 --- a/reactor-netty5-http/src/main/java/reactor/netty5/http/server/HttpServerOperations.java +++ b/reactor-netty5-http/src/main/java/reactor/netty5/http/server/HttpServerOperations.java @@ -36,7 +36,6 @@ import io.netty5.channel.ChannelFutureListeners; import io.netty5.channel.ChannelHandlerContext; import io.netty5.channel.ChannelOption; -import io.netty5.handler.codec.TooLongFrameException; import io.netty5.handler.codec.http.DefaultFullHttpResponse; import io.netty5.handler.codec.http.DefaultHttpResponse; import io.netty5.handler.codec.http.DefaultLastHttpContent; @@ -58,6 +57,8 @@ import io.netty5.handler.codec.http.HttpUtil; import io.netty5.handler.codec.http.HttpVersion; import io.netty5.handler.codec.http.LastHttpContent; +import io.netty5.handler.codec.http.TooLongHttpHeaderException; +import io.netty5.handler.codec.http.TooLongHttpLineException; import io.netty5.handler.codec.http.headers.HttpSetCookie; import io.netty5.handler.codec.http.websocketx.CloseWebSocketFrame; import io.netty5.handler.codec.http.websocketx.WebSocketCloseStatus; @@ -723,9 +724,17 @@ static void sendDecodingFailures( Resource.dispose(msg); - HttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, - cause instanceof TooLongFrameException ? HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE : - HttpResponseStatus.BAD_REQUEST, + final HttpResponseStatus status; + if (cause instanceof TooLongHttpLineException) { + status = HttpResponseStatus.REQUEST_URI_TOO_LONG; + } + else if (cause instanceof TooLongHttpHeaderException) { + status = HttpResponseStatus.REQUEST_HEADER_FIELDS_TOO_LARGE; + } + else { + status = HttpResponseStatus.BAD_REQUEST; + } + HttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, ctx.bufferAllocator().allocate(0)); response.headers() .set(HttpHeaderNames.CONTENT_LENGTH, HttpHeaderValues.ZERO) diff --git a/reactor-netty5-http/src/test/java/reactor/netty5/http/HttpMetricsHandlerTests.java b/reactor-netty5-http/src/test/java/reactor/netty5/http/HttpMetricsHandlerTests.java index fe967fd461..bad41a2db4 100644 --- a/reactor-netty5-http/src/test/java/reactor/netty5/http/HttpMetricsHandlerTests.java +++ b/reactor-netty5-http/src/test/java/reactor/netty5/http/HttpMetricsHandlerTests.java @@ -764,7 +764,7 @@ void testBadRequest(HttpProtocol[] serverProtocols, HttpProtocol[] clientProtoco .uri("/max_header_size") .responseSingle((res, bufferMono) -> Mono.just(res.status().code())) .as(StepVerifier::create) - .expectNext(413) + .expectNext(431) .expectComplete() .verify(Duration.ofSeconds(30)); @@ -879,7 +879,7 @@ private void checkExpectationsNonExisting(String serverAddress, int connIndex, i private void checkExpectationsBadRequest(String serverAddress, boolean checkTls) { String uri = "/max_header_size"; - String[] timerTags1 = new String[] {URI, uri, METHOD, "GET", STATUS, "413"}; + String[] timerTags1 = new String[] {URI, uri, METHOD, "GET", STATUS, "431"}; String[] summaryTags1 = new String[] {URI, uri}; checkTimer(SERVER_RESPONSE_TIME, timerTags1, 1); @@ -887,7 +887,7 @@ private void checkExpectationsBadRequest(String serverAddress, boolean checkTls) checkDistributionSummary(SERVER_DATA_SENT, summaryTags1, 1, 0); checkCounter(SERVER_ERRORS, summaryTags1, false, 0); - timerTags1 = new String[] {REMOTE_ADDRESS, serverAddress, URI, uri, METHOD, "GET", STATUS, "413"}; + timerTags1 = new String[] {REMOTE_ADDRESS, serverAddress, URI, uri, METHOD, "GET", STATUS, "431"}; String[] timerTags2 = new String[] {REMOTE_ADDRESS, serverAddress, URI, uri, METHOD, "GET"}; String[] timerTags3 = new String[] {REMOTE_ADDRESS, serverAddress, STATUS, "SUCCESS"}; summaryTags1 = new String[] {REMOTE_ADDRESS, serverAddress, URI, uri}; diff --git a/reactor-netty5-http/src/test/java/reactor/netty5/http/server/HttpServerTests.java b/reactor-netty5-http/src/test/java/reactor/netty5/http/server/HttpServerTests.java index 4d65c14de0..ffa1bf8e2c 100644 --- a/reactor-netty5-http/src/test/java/reactor/netty5/http/server/HttpServerTests.java +++ b/reactor-netty5-http/src/test/java/reactor/netty5/http/server/HttpServerTests.java @@ -909,7 +909,29 @@ private void doTestIssue309(String path, HttpServer httpServer) { .responseSingle((res, bufferMono) -> Mono.just(res.status())); StepVerifier.create(status) - .expectNextMatches(HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE::equals) + .expectNextMatches(HttpResponseStatus.REQUEST_URI_TOO_LONG::equals) + .expectComplete() + .verify(); + } + + @Test + void httpServerRequestHeadersTooLong() { + HttpServer httpServer = HttpServer.create() + .port(0) + .httpRequestDecoder(c -> c.maxHeaderSize(20)); + disposableServer = + httpServer.handle((req, res) -> res.sendString(Mono.just("Should not be reached"))) + .bindNow(); + + Mono status = + createClient(disposableServer.port()) + .headers(h -> h.set("content-type", "somethingtooolooong")) + .get() + .uri("/path") + .responseSingle((res, byteBufMono) -> Mono.just(res.status())); + + StepVerifier.create(status) + .expectNextMatches(HttpResponseStatus.REQUEST_HEADER_FIELDS_TOO_LARGE::equals) .expectComplete() .verify(); }