Search in sources :

Example 6 with ProtocolException

use of org.apache.hc.core5.http.ProtocolException in project httpcomponents-core by apache.

the class DefaultH2RequestConverter method convert.

@Override
public List<Header> convert(final HttpRequest message) throws HttpException {
    if (TextUtils.isBlank(message.getMethod())) {
        throw new ProtocolException("Request method is empty");
    }
    final boolean optionMethod = Method.CONNECT.name().equalsIgnoreCase(message.getMethod());
    if (optionMethod) {
        if (message.getAuthority() == null) {
            throw new ProtocolException("CONNECT request authority is not set");
        }
        if (message.getPath() != null) {
            throw new ProtocolException("CONNECT request path must be null");
        }
    } else {
        if (TextUtils.isBlank(message.getScheme())) {
            throw new ProtocolException("Request scheme is not set");
        }
        if (TextUtils.isBlank(message.getPath())) {
            throw new ProtocolException("Request path is not set");
        }
    }
    final List<Header> headers = new ArrayList<>();
    headers.add(new BasicHeader(H2PseudoRequestHeaders.METHOD, message.getMethod(), false));
    if (optionMethod) {
        headers.add(new BasicHeader(H2PseudoRequestHeaders.AUTHORITY, message.getAuthority(), false));
    } else {
        headers.add(new BasicHeader(H2PseudoRequestHeaders.SCHEME, message.getScheme(), false));
        if (message.getAuthority() != null) {
            headers.add(new BasicHeader(H2PseudoRequestHeaders.AUTHORITY, message.getAuthority(), false));
        }
        headers.add(new BasicHeader(H2PseudoRequestHeaders.PATH, message.getPath(), false));
    }
    for (final Iterator<Header> it = message.headerIterator(); it.hasNext(); ) {
        final Header header = it.next();
        final String name = header.getName();
        final String value = header.getValue();
        if (name.startsWith(":")) {
            throw new ProtocolException("Header name '%s' is invalid", name);
        }
        if (name.equalsIgnoreCase(HttpHeaders.CONNECTION) || name.equalsIgnoreCase(HttpHeaders.KEEP_ALIVE) || name.equalsIgnoreCase(HttpHeaders.PROXY_CONNECTION) || name.equalsIgnoreCase(HttpHeaders.TRANSFER_ENCODING) || name.equalsIgnoreCase(HttpHeaders.HOST) || name.equalsIgnoreCase(HttpHeaders.UPGRADE)) {
            throw new ProtocolException("Header '%s: %s' is illegal for HTTP/2 messages", header.getName(), header.getValue());
        }
        if (name.equalsIgnoreCase(HttpHeaders.TE) && !value.equalsIgnoreCase("trailers")) {
            throw new ProtocolException("Header '%s: %s' is illegal for HTTP/2 messages", header.getName(), header.getValue());
        }
        headers.add(new BasicHeader(TextUtils.toLowerCase(name), value));
    }
    return headers;
}
Also used : ProtocolException(org.apache.hc.core5.http.ProtocolException) Header(org.apache.hc.core5.http.Header) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) ArrayList(java.util.ArrayList) BasicHeader(org.apache.hc.core5.http.message.BasicHeader)

Example 7 with ProtocolException

use of org.apache.hc.core5.http.ProtocolException in project httpcomponents-core by apache.

the class ClientHttp1StreamHandler method consumeHeader.

void consumeHeader(final HttpResponse response, final EntityDetails entityDetails) throws HttpException, IOException {
    if (done.get() || responseState != MessageState.HEADERS) {
        throw new ProtocolException("Unexpected message head");
    }
    final ProtocolVersion transportVersion = response.getVersion();
    if (transportVersion != null && transportVersion.greaterEquals(HttpVersion.HTTP_2)) {
        throw new UnsupportedHttpVersionException(transportVersion);
    }
    final int status = response.getCode();
    if (status < HttpStatus.SC_INFORMATIONAL) {
        throw new ProtocolException("Invalid response: " + new StatusLine(response));
    }
    if (status > HttpStatus.SC_CONTINUE && status < HttpStatus.SC_SUCCESS) {
        exchangeHandler.consumeInformation(response, context);
    } else {
        if (!connectionReuseStrategy.keepAlive(committedRequest, response, context)) {
            keepAlive = false;
        }
    }
    if (requestState == MessageState.ACK) {
        if (status == HttpStatus.SC_CONTINUE || status >= HttpStatus.SC_SUCCESS) {
            outputChannel.setSocketTimeout(timeout);
            requestState = MessageState.BODY;
            if (status < HttpStatus.SC_CLIENT_ERROR) {
                exchangeHandler.produce(internalDataChannel);
            }
        }
    }
    if (status < HttpStatus.SC_SUCCESS) {
        return;
    }
    if (requestState == MessageState.BODY) {
        if (status >= HttpStatus.SC_CLIENT_ERROR) {
            requestState = MessageState.COMPLETE;
            if (!outputChannel.abortGracefully()) {
                keepAlive = false;
            }
        }
    }
    context.setProtocolVersion(transportVersion != null ? transportVersion : HttpVersion.HTTP_1_1);
    context.setAttribute(HttpCoreContext.HTTP_RESPONSE, response);
    httpProcessor.process(response, entityDetails, context);
    if (entityDetails == null && !keepAlive) {
        outputChannel.close();
    }
    exchangeHandler.consumeResponse(response, entityDetails, context);
    if (entityDetails == null) {
        responseState = MessageState.COMPLETE;
    } else {
        responseState = MessageState.BODY;
    }
}
Also used : StatusLine(org.apache.hc.core5.http.message.StatusLine) ProtocolException(org.apache.hc.core5.http.ProtocolException) UnsupportedHttpVersionException(org.apache.hc.core5.http.UnsupportedHttpVersionException) ProtocolVersion(org.apache.hc.core5.http.ProtocolVersion)

Example 8 with ProtocolException

use of org.apache.hc.core5.http.ProtocolException 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 9 with ProtocolException

use of org.apache.hc.core5.http.ProtocolException in project httpcomponents-core by apache.

the class RequestTargetHost method process.

@Override
public void process(final HttpRequest request, final EntityDetails entity, final HttpContext context) throws HttpException, IOException {
    Args.notNull(request, "HTTP request");
    Args.notNull(context, "HTTP context");
    final ProtocolVersion ver = context.getProtocolVersion();
    final String method = request.getMethod();
    if (Method.CONNECT.isSame(method) && ver.lessEquals(HttpVersion.HTTP_1_0)) {
        return;
    }
    if (!request.containsHeader(HttpHeaders.HOST)) {
        URIAuthority authority = request.getAuthority();
        if (authority == null) {
            if (ver.lessEquals(HttpVersion.HTTP_1_0)) {
                return;
            }
            throw new ProtocolException("Target host is unknown");
        }
        if (authority.getUserInfo() != null) {
            authority = new URIAuthority(authority.getHostName(), authority.getPort());
        }
        request.addHeader(HttpHeaders.HOST, authority);
    }
}
Also used : ProtocolException(org.apache.hc.core5.http.ProtocolException) URIAuthority(org.apache.hc.core5.net.URIAuthority) ProtocolVersion(org.apache.hc.core5.http.ProtocolVersion)

Example 10 with ProtocolException

use of org.apache.hc.core5.http.ProtocolException in project httpcomponents-core by apache.

the class ResponseContent method process.

/**
 * Processes the response (possibly updating or inserting) Content-Length and Transfer-Encoding headers.
 * @param response The HttpResponse to modify.
 * @param context Unused.
 * @throws ProtocolException If either the Content-Length or Transfer-Encoding headers are found.
 * @throws IllegalArgumentException If the response is null.
 */
@Override
public void process(final HttpResponse response, final EntityDetails entity, final HttpContext context) throws HttpException, IOException {
    Args.notNull(response, "HTTP response");
    if (this.overwrite) {
        response.removeHeaders(HttpHeaders.TRANSFER_ENCODING);
        response.removeHeaders(HttpHeaders.CONTENT_LENGTH);
    } else {
        if (response.containsHeader(HttpHeaders.TRANSFER_ENCODING)) {
            throw new ProtocolException("Transfer-encoding header already present");
        }
        if (response.containsHeader(HttpHeaders.CONTENT_LENGTH)) {
            throw new ProtocolException("Content-Length header already present");
        }
    }
    final ProtocolVersion ver = context.getProtocolVersion();
    if (entity != null) {
        final long len = entity.getContentLength();
        if (len >= 0 && !entity.isChunked()) {
            response.addHeader(HttpHeaders.CONTENT_LENGTH, Long.toString(entity.getContentLength()));
        } else if (ver.greaterEquals(HttpVersion.HTTP_1_1)) {
            response.addHeader(HttpHeaders.TRANSFER_ENCODING, HeaderElements.CHUNKED_ENCODING);
            MessageSupport.addTrailerHeader(response, entity);
        }
        MessageSupport.addContentTypeHeader(response, entity);
        MessageSupport.addContentEncodingHeader(response, entity);
    } else {
        final int status = response.getCode();
        if (status != HttpStatus.SC_NO_CONTENT && status != HttpStatus.SC_NOT_MODIFIED) {
            response.addHeader(HttpHeaders.CONTENT_LENGTH, "0");
        }
    }
}
Also used : ProtocolException(org.apache.hc.core5.http.ProtocolException) ProtocolVersion(org.apache.hc.core5.http.ProtocolVersion)

Aggregations

ProtocolException (org.apache.hc.core5.http.ProtocolException)28 HttpResponse (org.apache.hc.core5.http.HttpResponse)10 Header (org.apache.hc.core5.http.Header)9 EntityDetails (org.apache.hc.core5.http.EntityDetails)8 ProtocolVersion (org.apache.hc.core5.http.ProtocolVersion)8 URISyntaxException (java.net.URISyntaxException)6 HttpException (org.apache.hc.core5.http.HttpException)6 HttpRequest (org.apache.hc.core5.http.HttpRequest)6 HttpContext (org.apache.hc.core5.http.protocol.HttpContext)6 IOException (java.io.IOException)5 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)5 BasicHttpResponse (org.apache.hc.core5.http.message.BasicHttpResponse)5 ByteBuffer (java.nio.ByteBuffer)4 ArrayList (java.util.ArrayList)4 URIAuthority (org.apache.hc.core5.net.URIAuthority)4 InetSocketAddress (java.net.InetSocketAddress)3 URI (java.net.URI)3 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)3 ConnectionClosedException (org.apache.hc.core5.http.ConnectionClosedException)3 Message (org.apache.hc.core5.http.Message)3