in httpcore5/src/main/java/org/apache/hc/core5/http/protocol/RequestContent.java [96:138]
public void process(final HttpRequest request, final EntityDetails entity, final HttpContext context)
throws HttpException, IOException {
Args.notNull(request, "HTTP request");
final String method = request.getMethod();
if (Method.TRACE.isSame(method) && entity != null) {
throw new ProtocolException("TRACE request may not enclose an entity");
}
if (this.overwrite) {
request.removeHeaders(HttpHeaders.TRANSFER_ENCODING);
request.removeHeaders(HttpHeaders.CONTENT_LENGTH);
} else {
if (request.containsHeader(HttpHeaders.TRANSFER_ENCODING)) {
throw new ProtocolException("Transfer-encoding header already present");
}
if (request.containsHeader(HttpHeaders.CONTENT_LENGTH)) {
throw new ProtocolException("Content-Length header already present");
}
}
if (entity == null && isContentEnclosingMethod(method)) {
request.addHeader(HttpHeaders.CONTENT_LENGTH, "0");
return;
}
if (entity != null) {
// Check for OPTIONS request with content but no Content-Type header
validateOptionsContentType(request);
final ProtocolVersion ver = context.getProtocolVersion();
// Must specify a transfer encoding or a content length
if (entity.isChunked() || entity.getContentLength() < 0) {
if (ver.lessEquals(HttpVersion.HTTP_1_0)) {
throw new ProtocolException(
"Chunked transfer encoding not allowed for " + ver);
}
request.addHeader(HttpHeaders.TRANSFER_ENCODING, HeaderElements.CHUNKED_ENCODING);
MessageSupport.addTrailerHeader(request, entity);
} else {
request.addHeader(HttpHeaders.CONTENT_LENGTH, Long.toString(entity.getContentLength()));
}
MessageSupport.addContentTypeHeader(request, entity);
MessageSupport.addContentEncodingHeader(request, entity);
}
}