Search in sources :

Example 1 with HttpMessage

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;
}
Also used : ProtocolException(org.apache.hc.core5.http.ProtocolException) Header(org.apache.hc.core5.http.Header) NotImplementedException(org.apache.hc.core5.http.NotImplementedException)

Example 2 with HttpMessage

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));
}
Also used : ContentLengthStrategy(org.apache.hc.core5.http.ContentLengthStrategy) HttpMessage(org.apache.hc.core5.http.HttpMessage) Test(org.junit.jupiter.api.Test)

Example 3 with HttpMessage

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));
}
Also used : ContentLengthStrategy(org.apache.hc.core5.http.ContentLengthStrategy) HttpMessage(org.apache.hc.core5.http.HttpMessage) Test(org.junit.jupiter.api.Test)

Example 4 with HttpMessage

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));
}
Also used : ContentLengthStrategy(org.apache.hc.core5.http.ContentLengthStrategy) HttpMessage(org.apache.hc.core5.http.HttpMessage) Test(org.junit.jupiter.api.Test)

Example 5 with HttpMessage

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));
}
Also used : ContentLengthStrategy(org.apache.hc.core5.http.ContentLengthStrategy) HttpMessage(org.apache.hc.core5.http.HttpMessage) Test(org.junit.jupiter.api.Test)

Aggregations

HttpMessage (org.apache.hc.core5.http.HttpMessage)9 Test (org.junit.jupiter.api.Test)9 ContentLengthStrategy (org.apache.hc.core5.http.ContentLengthStrategy)7 Header (org.apache.hc.core5.http.Header)3 HttpEntity (org.apache.hc.core5.http.HttpEntity)2 NotImplementedException (org.apache.hc.core5.http.NotImplementedException)1 ProtocolException (org.apache.hc.core5.http.ProtocolException)1