response.setContentEncodingsSelected(true);
response.setContentLength(-1);
response.setChunked(false);
}
final MimeHeaders headers = response.getHeaders();
if (!entityBody) {
response.setContentLength(-1);
} else {
String contentLanguage = response.getContentLanguage();
if (contentLanguage != null) {
headers.setValue(Header.ContentLanguage).setString(contentLanguage);
}
// Optimize content-type serialization depending on its state
final ContentType contentType = response.getContentTypeHolder();
if (contentType.isMimeTypeSet()) {
final DataChunk contentTypeValue = headers.setValue(Header.ContentType);
if (contentTypeValue.isNull()) {
contentType.serializeToDataChunk(contentTypeValue);
}
} else if (defaultResponseContentType != null) {
final DataChunk contenTypeValue = headers.setValue(Header.ContentType);
if (contenTypeValue.isNull()) {
final String ce = response.getCharacterEncoding();
if (ce == null) {
contenTypeValue.setBytes(defaultResponseContentTypeBytes);
} else {
final byte[] array = ContentType.compose(
defaultResponseContentTypeBytesNoCharset, ce);
contenTypeValue.setBytes(array);
}
}
}
}
if (!response.containsHeader(Header.Date)) {
response.getHeaders().addValue(Header.Date)
.setBytes(FastHttpDateFormat.getCurrentDateBytes());
}
final ProcessingState state = response.getProcessingState();
if (entityBody && !isHttp11 && response.getContentLength() == -1) {
// HTTP 1.0 response with no content-length having been set.
// Close the connection to signal the response as being complete.
state.keepAlive = false;
} else if (entityBody && !response.isChunked() && response.getContentLength() == -1) {
// HTTP 1.1 response with chunking disabled and no content-length having been set.
// Close the connection to signal the response as being complete.
state.keepAlive = false;
} else if (!checkKeepAliveRequestsCount(state.getHttpContext())) {
// We processed max allowed HTTP requests over the keep alive connection
state.keepAlive = false;
}
// If we know that the request is bad this early, add the
// Connection: close header.
state.keepAlive = (state.keepAlive &&
!statusDropsConnection(response.getStatus()));
if (!state.keepAlive) {
headers.setValue(Header.Connection).setBytes(CLOSE_BYTES);
} else if (!isHttp11 && !state.error) {
headers.setValue(Header.Connection).setBytes(KEEPALIVE_BYTES);
}
return encodedHttpContent;
}