Skip to content

Commit

Permalink
Merge pull request #302 from nicolaferraro/fix-transfer-encoding
Browse files Browse the repository at this point in the history
Fix #301: Auto-set content-length or chunked transfer encoding
  • Loading branch information
nicolaferraro authored Apr 15, 2020
2 parents a0db7e1 + 80665b0 commit ae0001f
Showing 1 changed file with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down

0 comments on commit ae0001f

Please sign in to comment.