Skip to content

Multipart Requests do not allow unknown content length #427

Closed
@skopf

Description

@skopf

The HTTP specification encourages the usage of the Content-Length field, but does not enforce it (RFC2616: "Applications SHOULD use this field to indicate the transfer-length of the message-body....").
A PartSource can return -1 as the length of this part to indicate a unknown size of this part. The static method getLengthOfParts in com.ning.http.multipart.Part is already handling this case by stopping size calculation if one of the parts returns a value < 0 and returns -1 itself.
But the method construct() in com.ning.http.client.providers.netty.NettyAsyncHttpProvider always sets the Content-Length field and thus sets the header to the invalid value Content-Length: -1:

} else if (request.getParts() != null) {
    MultipartRequestEntity mre = AsyncHttpProviderUtils.createMultipartRequestEntity(request.getParts(), request.getHeaders());
    nettyRequest.setHeader(HttpHeaders.Names.CONTENT_TYPE, mre.getContentType());
    nettyRequest.setHeader(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(mre.getContentLength()));
} else if (request.getEntityWriter() != null) {

Instead, this code should check the content-length for a value > 0 and only set the header in this case:

} else if (request.getParts() != null) {
    MultipartRequestEntity mre = AsyncHttpProviderUtils.createMultipartRequestEntity(request.getParts(), request.getHeaders());
    nettyRequest.setHeader(HttpHeaders.Names.CONTENT_TYPE, mre.getContentType());
    if(mre.getContentLength() >= 0) {
        nettyRequest.setHeader(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(mre.getContentLength()));
    }
} else if (request.getEntityWriter() != null) {

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions