Search in sources :

Example 1 with NotImplementedException

use of org.apache.hc.core5.http.NotImplementedException 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)

Aggregations

Header (org.apache.hc.core5.http.Header)1 NotImplementedException (org.apache.hc.core5.http.NotImplementedException)1 ProtocolException (org.apache.hc.core5.http.ProtocolException)1