Skip to content

Commit

Permalink
Backport fix for apache/camel-k-runtime#301 into camel-platform-http-…
Browse files Browse the repository at this point in the history
…vertx
  • Loading branch information
nicolaferraro committed Apr 15, 2020
1 parent 37a537c commit 2c90dbf
Showing 1 changed file with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down

0 comments on commit 2c90dbf

Please sign in to comment.