Search in sources :

Example 11 with ProtocolVersion

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

the class RequestContent method process.

@Override
public void process(final HttpRequest request, final EntityDetails entity, final HttpContext context) throws HttpException, IOException {
    Args.notNull(request, "HTTP request");
    final String method = request.getMethod();
    if (Method.TRACE.isSame(method) && entity != null) {
        throw new ProtocolException("TRACE request may not enclose an entity");
    }
    if (this.overwrite) {
        request.removeHeaders(HttpHeaders.TRANSFER_ENCODING);
        request.removeHeaders(HttpHeaders.CONTENT_LENGTH);
    } else {
        if (request.containsHeader(HttpHeaders.TRANSFER_ENCODING)) {
            throw new ProtocolException("Transfer-encoding header already present");
        }
        if (request.containsHeader(HttpHeaders.CONTENT_LENGTH)) {
            throw new ProtocolException("Content-Length header already present");
        }
    }
    if (entity != null) {
        final ProtocolVersion ver = context.getProtocolVersion();
        // Must specify a transfer encoding or a content length
        if (entity.isChunked() || entity.getContentLength() < 0) {
            if (ver.lessEquals(HttpVersion.HTTP_1_0)) {
                throw new ProtocolException("Chunked transfer encoding not allowed for " + ver);
            }
            request.addHeader(HttpHeaders.TRANSFER_ENCODING, HeaderElements.CHUNKED_ENCODING);
            MessageSupport.addTrailerHeader(request, entity);
        } else {
            request.addHeader(HttpHeaders.CONTENT_LENGTH, Long.toString(entity.getContentLength()));
        }
        MessageSupport.addContentTypeHeader(request, entity);
        MessageSupport.addContentEncodingHeader(request, entity);
    }
}
Also used : ProtocolException(org.apache.hc.core5.http.ProtocolException) ProtocolVersion(org.apache.hc.core5.http.ProtocolVersion)

Example 12 with ProtocolVersion

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

the class TlsVersionParser method parse.

ProtocolVersion parse(final CharSequence buffer, final Tokenizer.Cursor cursor, final BitSet delimiters) throws ParseException {
    final int lowerBound = cursor.getLowerBound();
    final int upperBound = cursor.getUpperBound();
    int pos = cursor.getPos();
    if (pos + 4 > cursor.getUpperBound()) {
        throw new ParseException("Invalid TLS protocol version", buffer, lowerBound, upperBound, pos);
    }
    if (buffer.charAt(pos) != 'T' || buffer.charAt(pos + 1) != 'L' || buffer.charAt(pos + 2) != 'S' || buffer.charAt(pos + 3) != 'v') {
        throw new ParseException("Invalid TLS protocol version", buffer, lowerBound, upperBound, pos);
    }
    pos = pos + 4;
    cursor.updatePos(pos);
    if (cursor.atEnd()) {
        throw new ParseException("Invalid TLS version", buffer, lowerBound, upperBound, pos);
    }
    final String s = this.tokenizer.parseToken(buffer, cursor, delimiters);
    final int idx = s.indexOf('.');
    if (idx == -1) {
        final int major;
        try {
            major = Integer.parseInt(s);
        } catch (final NumberFormatException e) {
            throw new ParseException("Invalid TLS major version", buffer, lowerBound, upperBound, pos);
        }
        return new ProtocolVersion("TLS", major, 0);
    } else {
        final String s1 = s.substring(0, idx);
        final int major;
        try {
            major = Integer.parseInt(s1);
        } catch (final NumberFormatException e) {
            throw new ParseException("Invalid TLS major version", buffer, lowerBound, upperBound, pos);
        }
        final String s2 = s.substring(idx + 1);
        final int minor;
        try {
            minor = Integer.parseInt(s2);
        } catch (final NumberFormatException e) {
            throw new ParseException("Invalid TLS minor version", buffer, lowerBound, upperBound, pos);
        }
        return new ProtocolVersion("TLS", major, minor);
    }
}
Also used : ParseException(org.apache.hc.core5.http.ParseException) ProtocolVersion(org.apache.hc.core5.http.ProtocolVersion)

Example 13 with ProtocolVersion

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

the class BasicLineParser method parseStatusLine.

@Override
public StatusLine parseStatusLine(final CharArrayBuffer buffer) throws ParseException {
    Args.notNull(buffer, "Char array buffer");
    final ParserCursor cursor = new ParserCursor(0, buffer.length());
    this.tokenizer.skipWhiteSpace(buffer, cursor);
    final ProtocolVersion ver = parseProtocolVersion(buffer, cursor);
    this.tokenizer.skipWhiteSpace(buffer, cursor);
    final String s = this.tokenizer.parseToken(buffer, cursor, BLANKS);
    for (int i = 0; i < s.length(); i++) {
        if (!Character.isDigit(s.charAt(i))) {
            throw new ParseException("Status line contains invalid status code", buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos());
        }
    }
    final int statusCode;
    try {
        statusCode = Integer.parseInt(s);
    } catch (final NumberFormatException e) {
        throw new ParseException("Status line contains invalid status code", buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos());
    }
    final String text = buffer.substringTrimmed(cursor.getPos(), cursor.getUpperBound());
    return new StatusLine(ver, statusCode, text);
}
Also used : ParseException(org.apache.hc.core5.http.ParseException) ProtocolVersion(org.apache.hc.core5.http.ProtocolVersion)

Example 14 with ProtocolVersion

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

the class BasicLineParser method parseProtocolVersion.

ProtocolVersion parseProtocolVersion(final CharArrayBuffer buffer, final ParserCursor cursor) throws ParseException {
    final String protoname = this.protocol.getProtocol();
    final int protolength = protoname.length();
    this.tokenizer.skipWhiteSpace(buffer, cursor);
    final int pos = cursor.getPos();
    // long enough for "HTTP/1.1"?
    if (pos + protolength + 4 > cursor.getUpperBound()) {
        throw new ParseException("Invalid protocol version", buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos());
    }
    // check the protocol name and slash
    boolean ok = true;
    for (int i = 0; ok && (i < protolength); i++) {
        ok = buffer.charAt(pos + i) == protoname.charAt(i);
    }
    if (ok) {
        ok = buffer.charAt(pos + protolength) == '/';
    }
    if (!ok) {
        throw new ParseException("Invalid protocol version", buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos());
    }
    cursor.updatePos(pos + protolength + 1);
    final String token1 = this.tokenizer.parseToken(buffer, cursor, FULL_STOP);
    final int major;
    try {
        major = Integer.parseInt(token1);
    } catch (final NumberFormatException e) {
        throw new ParseException("Invalid protocol major version number", buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos());
    }
    if (cursor.atEnd()) {
        throw new ParseException("Invalid protocol version", buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos());
    }
    cursor.updatePos(cursor.getPos() + 1);
    final String token2 = this.tokenizer.parseToken(buffer, cursor, BLANKS);
    final int minor;
    try {
        minor = Integer.parseInt(token2);
    } catch (final NumberFormatException e) {
        throw new ParseException("Invalid protocol minor version number", buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos());
    }
    return HttpVersion.get(major, minor);
}
Also used : ParseException(org.apache.hc.core5.http.ParseException)

Example 15 with ProtocolVersion

use of org.apache.hc.core5.http.ProtocolVersion in project opentelemetry-java-instrumentation by open-telemetry.

the class ApacheHttpClientHttpAttributesGetter method flavor.

@Override
@Nullable
public String flavor(ClassicHttpRequest request, @Nullable HttpResponse response) {
    ProtocolVersion protocolVersion = request.getVersion();
    if (protocolVersion == null) {
        return SemanticAttributes.HttpFlavorValues.HTTP_1_1;
    }
    String protocol = protocolVersion.getProtocol();
    if (!protocol.equals("HTTP")) {
        return null;
    }
    int major = protocolVersion.getMajor();
    int minor = protocolVersion.getMinor();
    if (major == 1 && minor == 0) {
        return SemanticAttributes.HttpFlavorValues.HTTP_1_0;
    }
    if (major == 1 && minor == 1) {
        return SemanticAttributes.HttpFlavorValues.HTTP_1_1;
    }
    if (major == 2 && minor == 0) {
        return SemanticAttributes.HttpFlavorValues.HTTP_2_0;
    }
    logger.debug("unexpected http protocol version: " + protocolVersion);
    return null;
}
Also used : ProtocolVersion(org.apache.hc.core5.http.ProtocolVersion) Nullable(javax.annotation.Nullable)

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