Skip to content

Commit

Permalink
Fix apache#1288: automatically set content-length or chunked on platf…
Browse files Browse the repository at this point in the history
…orm-http replies
  • Loading branch information
nicolaferraro authored and jamesnetherton committed Jun 10, 2020
1 parent 263ca25 commit 20f5b80
Showing 1 changed file with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,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 @@ -228,6 +236,18 @@ static Object toHttpResponse(HttpServerResponse response, Message message, Heade
return body;
}

/*
* Copied from org.apache.camel.component.platform.http.vertx.VertxPlatformHttpSupport.determineContentLength(Exchange, Object)
*/
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 20f5b80

Please sign in to comment.