From 80665b0cdb5249938566ec117145a8c11556b1e5 Mon Sep 17 00:00:00 2001 From: Nicola Ferraro Date: Wed, 15 Apr 2020 15:47:11 +0200 Subject: [PATCH] Fix #301: Auto-set content-length or chunked transfer encoding --- .../engine/RuntimePlatformHttpConsumer.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/camel-k-runtime-http/src/main/java/org/apache/camel/k/http/engine/RuntimePlatformHttpConsumer.java b/camel-k-runtime-http/src/main/java/org/apache/camel/k/http/engine/RuntimePlatformHttpConsumer.java index 300fe915a..bb4486433 100644 --- a/camel-k-runtime-http/src/main/java/org/apache/camel/k/http/engine/RuntimePlatformHttpConsumer.java +++ b/camel-k-runtime-http/src/main/java/org/apache/camel/k/http/engine/RuntimePlatformHttpConsumer.java @@ -200,6 +200,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) { @@ -209,6 +217,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