Search in sources :

Example 6 with ProtocolVersion

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

the class DefaultConnectionReuseStrategy method keepAlive.

// see interface ConnectionReuseStrategy
@Override
public boolean keepAlive(final HttpRequest request, final HttpResponse response, final HttpContext context) {
    Args.notNull(response, "HTTP response");
    if (request != null) {
        final Iterator<String> ti = new BasicTokenIterator(request.headerIterator(HttpHeaders.CONNECTION));
        while (ti.hasNext()) {
            final String token = ti.next();
            if (HeaderElements.CLOSE.equalsIgnoreCase(token)) {
                return false;
            }
        }
    }
    // returns content as part of a HTTP 204 response.
    if (response.getCode() == HttpStatus.SC_NO_CONTENT) {
        final Header clh = response.getFirstHeader(HttpHeaders.CONTENT_LENGTH);
        if (clh != null) {
            try {
                final long contentLen = Long.parseLong(clh.getValue());
                if (contentLen > 0) {
                    return false;
                }
            } catch (final NumberFormatException ex) {
            // fall through
            }
        }
        if (response.containsHeader(HttpHeaders.TRANSFER_ENCODING)) {
            return false;
        }
    }
    // Check for a self-terminating entity. If the end of the entity will
    // be indicated by closing the connection, there is no keep-alive.
    final Header teh = response.getFirstHeader(HttpHeaders.TRANSFER_ENCODING);
    if (teh != null) {
        if (!HeaderElements.CHUNKED_ENCODING.equalsIgnoreCase(teh.getValue())) {
            return false;
        }
    } else {
        final String method = request != null ? request.getMethod() : null;
        if (MessageSupport.canResponseHaveBody(method, response) && response.countHeaders(HttpHeaders.CONTENT_LENGTH) != 1) {
            return false;
        }
    }
    // Check for the "Connection" header. If that is absent, check for
    // the "Proxy-Connection" header. The latter is an unspecified and
    // broken but unfortunately common extension of HTTP.
    Iterator<Header> headerIterator = response.headerIterator(HttpHeaders.CONNECTION);
    if (!headerIterator.hasNext()) {
        headerIterator = response.headerIterator("Proxy-Connection");
    }
    final ProtocolVersion ver = response.getVersion() != null ? response.getVersion() : context.getProtocolVersion();
    if (headerIterator.hasNext()) {
        if (ver.greaterEquals(HttpVersion.HTTP_1_1)) {
            final Iterator<String> it = new BasicTokenIterator(headerIterator);
            while (it.hasNext()) {
                final String token = it.next();
                if (HeaderElements.CLOSE.equalsIgnoreCase(token)) {
                    return false;
                }
            }
            return true;
        }
        final Iterator<String> it = new BasicTokenIterator(headerIterator);
        while (it.hasNext()) {
            final String token = it.next();
            if (HeaderElements.KEEP_ALIVE.equalsIgnoreCase(token)) {
                return true;
            }
        }
        return false;
    }
    return ver.greaterEquals(HttpVersion.HTTP_1_1);
}
Also used : Header(org.apache.hc.core5.http.Header) ProtocolVersion(org.apache.hc.core5.http.ProtocolVersion) BasicTokenIterator(org.apache.hc.core5.http.message.BasicTokenIterator)

Example 7 with ProtocolVersion

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

the class DefaultHttpRequestWriter method writeHeadLine.

@Override
protected void writeHeadLine(final ClassicHttpRequest message, final CharArrayBuffer lineBuf) throws IOException {
    final ProtocolVersion transportVersion = message.getVersion();
    getLineFormatter().formatRequestLine(lineBuf, new RequestLine(message.getMethod(), message.getRequestUri(), transportVersion != null ? transportVersion : HttpVersion.HTTP_1_1));
}
Also used : RequestLine(org.apache.hc.core5.http.message.RequestLine) ProtocolVersion(org.apache.hc.core5.http.ProtocolVersion)

Example 8 with ProtocolVersion

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

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

the class ResponseConnControl method process.

@Override
public void process(final HttpResponse response, final EntityDetails entity, final HttpContext context) throws HttpException, IOException {
    Args.notNull(response, "HTTP response");
    Args.notNull(context, "HTTP context");
    // Always drop connection after certain type of responses
    final int status = response.getCode();
    if (status == HttpStatus.SC_BAD_REQUEST || status == HttpStatus.SC_REQUEST_TIMEOUT || status == HttpStatus.SC_LENGTH_REQUIRED || status == HttpStatus.SC_REQUEST_TOO_LONG || status == HttpStatus.SC_REQUEST_URI_TOO_LONG || status == HttpStatus.SC_SERVICE_UNAVAILABLE || status == HttpStatus.SC_NOT_IMPLEMENTED) {
        response.setHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE);
        return;
    }
    if (!response.containsHeader(HttpHeaders.CONNECTION)) {
        // Always drop connection for HTTP/1.0 responses and below
        // if the content body cannot be correctly delimited
        final ProtocolVersion ver = context.getProtocolVersion();
        if (entity != null && entity.getContentLength() < 0 && ver.lessEquals(HttpVersion.HTTP_1_0)) {
            response.setHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE);
        } else {
            final HttpCoreContext coreContext = HttpCoreContext.adapt(context);
            final HttpRequest request = coreContext.getRequest();
            boolean closeRequested = false;
            boolean keepAliveRequested = false;
            if (request != null) {
                final Iterator<HeaderElement> it = MessageSupport.iterate(request, HttpHeaders.CONNECTION);
                while (it.hasNext()) {
                    final HeaderElement he = it.next();
                    if (he.getName().equalsIgnoreCase(HeaderElements.CLOSE)) {
                        closeRequested = true;
                        break;
                    } else if (he.getName().equalsIgnoreCase(HeaderElements.KEEP_ALIVE)) {
                        keepAliveRequested = true;
                    }
                }
            }
            if (closeRequested) {
                response.addHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE);
            } else {
                if (response.containsHeader(HttpHeaders.UPGRADE)) {
                    response.addHeader(HttpHeaders.CONNECTION, HeaderElements.UPGRADE);
                } else {
                    if (keepAliveRequested) {
                        response.addHeader(HttpHeaders.CONNECTION, HeaderElements.KEEP_ALIVE);
                    } else {
                        if (ver.lessEquals(HttpVersion.HTTP_1_0)) {
                            response.addHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE);
                        }
                    }
                }
            }
        }
    }
}
Also used : HttpRequest(org.apache.hc.core5.http.HttpRequest) HeaderElement(org.apache.hc.core5.http.HeaderElement) ProtocolVersion(org.apache.hc.core5.http.ProtocolVersion)

Example 10 with ProtocolVersion

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

ProtocolVersion (org.apache.hc.core5.http.ProtocolVersion)35 ProtocolException (org.apache.hc.core5.http.ProtocolException)8 UnsupportedHttpVersionException (org.apache.hc.core5.http.UnsupportedHttpVersionException)8 Header (org.apache.hc.core5.http.Header)6 HttpException (org.apache.hc.core5.http.HttpException)6 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)4 ParseException (org.apache.hc.core5.http.ParseException)4 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ClassicHttpRequest (org.apache.hc.core5.http.ClassicHttpRequest)3 HttpHost (org.apache.hc.core5.http.HttpHost)3 HttpResponse (org.apache.hc.core5.http.HttpResponse)3 StringEntity (org.apache.hc.core5.http.io.entity.StringEntity)3 StatusLine (org.apache.hc.core5.http.message.StatusLine)3 Test (org.junit.jupiter.api.Test)3 URI (java.net.URI)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 SSLSession (javax.net.ssl.SSLSession)2