From 2c90dbf748ec3180842cfb6ec420598c56a3b58d Mon Sep 17 00:00:00 2001 From: Nicola Ferraro Date: Wed, 15 Apr 2020 18:37:49 +0200 Subject: [PATCH] Backport fix for https://github.com/apache/camel-k-runtime/issues/301 into camel-platform-http-vertx --- .../http/vertx/VertxPlatformHttpSupport.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpSupport.java b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpSupport.java index ddf3760da7b39..61277a7853982 100644 --- a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpSupport.java +++ b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpSupport.java @@ -110,6 +110,14 @@ static Object toHttpResponse(HttpServerResponse response, Message message, Heade ExchangeHelper.setFailureHandled(exchange); } + // set the content-length if it can be determined, or chunked encoding + final Integer length = determineContentLength(exchange, body); + if (length != null) { + response.putHeader("Content-Length", String.valueOf(length)); + } else { + response.setChunked(true); + } + // set the content type in the response. final String contentType = MessageHelper.getContentType(message); if (contentType != null) { @@ -119,6 +127,15 @@ static Object toHttpResponse(HttpServerResponse response, Message message, Heade return body; } + static Integer determineContentLength(Exchange camelExchange, Object body) { + if (body instanceof byte[]) { + return ((byte[]) body).length; + } else if (body instanceof ByteBuffer) { + return ((ByteBuffer) body).remaining(); + } + return null; + } + /* * Copied from org.apache.camel.http.common.DefaultHttpBinding.determineResponseCode(Exchange, Object) * If DefaultHttpBinding.determineResponseCode(Exchange, Object) is moved to a module without the servlet-api