if (response == null) {
throw new IllegalArgumentException("HTTP request may not be null");
}
if (response.containsHeader(HTTP.TRANSFER_ENCODING)) {
throw new ProtocolException("Transfer-encoding header already present");
}
if (response.containsHeader(HTTP.CONTENT_LEN)) {
throw new ProtocolException("Content-Length header already present");
}
ProtocolVersion ver = response.getStatusLine().getProtocolVersion();
HttpEntity entity = response.getEntity();
if (entity != null) {
long len = entity.getContentLength();
if (entity.isChunked() && !ver.lessEquals(HttpVersion.HTTP_1_0)) {
response.addHeader(HTTP.TRANSFER_ENCODING, HTTP.CHUNK_CODING);
} else if (len >= 0) {
response.addHeader(HTTP.CONTENT_LEN, Long.toString(entity.getContentLength()));
}
// Specify a content type if known
if (entity.getContentType() != null && !response.containsHeader(
HTTP.CONTENT_TYPE )) {
response.addHeader(entity.getContentType());
}
// Specify a content encoding if known
if (entity.getContentEncoding() != null && !response.containsHeader(
HTTP.CONTENT_ENCODING)) {
response.addHeader(entity.getContentEncoding());
}
} else {
int status = response.getStatusLine().getStatusCode();
if (status != HttpStatus.SC_NO_CONTENT
&& status != HttpStatus.SC_NOT_MODIFIED
&& status != HttpStatus.SC_RESET_CONTENT) {
response.addHeader(HTTP.CONTENT_LEN, "0");
}
}