use of org.apache.hc.core5.http.HttpMessage in project httpcomponents-core by apache.
the class DefaultContentLengthStrategy method determineLength.
@Override
public long determineLength(final HttpMessage message) throws HttpException {
Args.notNull(message, "HTTP message");
// Although Transfer-Encoding is specified as a list, in practice
// it is either missing or has the single value "chunked". So we
// treat it as a single-valued header here.
final Header transferEncodingHeader = message.getFirstHeader(HttpHeaders.TRANSFER_ENCODING);
if (transferEncodingHeader != null) {
final String headerValue = transferEncodingHeader.getValue();
if (HeaderElements.CHUNKED_ENCODING.equalsIgnoreCase(headerValue)) {
return CHUNKED;
}
throw new NotImplementedException("Unsupported transfer encoding: " + headerValue);
}
if (message.countHeaders(HttpHeaders.CONTENT_LENGTH) > 1) {
throw new ProtocolException("Multiple Content-Length headers");
}
final Header contentLengthHeader = message.getFirstHeader(HttpHeaders.CONTENT_LENGTH);
if (contentLengthHeader != null) {
final String s = contentLengthHeader.getValue();
try {
final long len = Long.parseLong(s);
if (len < 0) {
throw new ProtocolException("Negative content length: " + s);
}
return len;
} catch (final NumberFormatException e) {
throw new ProtocolException("Invalid content length: " + s);
}
}
return UNDEFINED;
}
use of org.apache.hc.core5.http.HttpMessage in project httpcomponents-core by apache.
the class TestDefaultContentLengthStrategy method testEntityWithChunkTransferEncoding.
@Test
public void testEntityWithChunkTransferEncoding() throws Exception {
final ContentLengthStrategy lenStrategy = new DefaultContentLengthStrategy();
final HttpMessage message = new TestHttpMessage();
message.addHeader("Transfer-Encoding", "Chunked");
Assertions.assertEquals(ContentLengthStrategy.CHUNKED, lenStrategy.determineLength(message));
}
use of org.apache.hc.core5.http.HttpMessage in project httpcomponents-core by apache.
the class TestDefaultContentLengthStrategy method testEntityWithNegativeContentLength.
@Test
public void testEntityWithNegativeContentLength() throws Exception {
final ContentLengthStrategy lenStrategy = new DefaultContentLengthStrategy();
final HttpMessage message = new TestHttpMessage();
message.addHeader("Content-Length", "-10");
Assertions.assertThrows(ProtocolException.class, () -> lenStrategy.determineLength(message));
}
use of org.apache.hc.core5.http.HttpMessage in project httpcomponents-core by apache.
the class TestDefaultContentLengthStrategy method testEntityNoContentDelimiter.
@Test
public void testEntityNoContentDelimiter() throws Exception {
final ContentLengthStrategy lenStrategy = new DefaultContentLengthStrategy();
final HttpMessage message = new TestHttpMessage();
Assertions.assertEquals(ContentLengthStrategy.UNDEFINED, lenStrategy.determineLength(message));
}
use of org.apache.hc.core5.http.HttpMessage in project httpcomponents-core by apache.
the class TestDefaultContentLengthStrategy method testEntityWithInvalidTransferEncoding.
@Test
public void testEntityWithInvalidTransferEncoding() throws Exception {
final ContentLengthStrategy lenStrategy = new DefaultContentLengthStrategy();
final HttpMessage message = new TestHttpMessage();
message.addHeader("Transfer-Encoding", "whatever");
Assertions.assertThrows(ProtocolException.class, () -> lenStrategy.determineLength(message));
}
Aggregations