Search in sources :

Example 1 with MessageHeaders

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

the class DefaultH2RequestConverter method convert.

@Override
public HttpRequest convert(final List<Header> headers) throws HttpException {
    String method = null;
    String scheme = null;
    String authority = null;
    String path = null;
    final List<Header> messageHeaders = new ArrayList<>();
    for (int i = 0; i < headers.size(); i++) {
        final Header header = headers.get(i);
        final String name = header.getName();
        final String value = header.getValue();
        for (int n = 0; n < name.length(); n++) {
            final char ch = name.charAt(n);
            if (Character.isAlphabetic(ch) && !Character.isLowerCase(ch)) {
                throw new ProtocolException("Header name '%s' is invalid (header name contains uppercase characters)", name);
            }
        }
        if (name.startsWith(":")) {
            if (!messageHeaders.isEmpty()) {
                throw new ProtocolException("Invalid sequence of headers (pseudo-headers must precede message headers)");
            }
            switch(name) {
                case H2PseudoRequestHeaders.METHOD:
                    if (method != null) {
                        throw new ProtocolException("Multiple '%s' request headers are illegal", name);
                    }
                    method = value;
                    break;
                case H2PseudoRequestHeaders.SCHEME:
                    if (scheme != null) {
                        throw new ProtocolException("Multiple '%s' request headers are illegal", name);
                    }
                    scheme = value;
                    break;
                case H2PseudoRequestHeaders.PATH:
                    if (path != null) {
                        throw new ProtocolException("Multiple '%s' request headers are illegal", name);
                    }
                    path = value;
                    break;
                case H2PseudoRequestHeaders.AUTHORITY:
                    authority = value;
                    break;
                default:
                    throw new ProtocolException("Unsupported request header '%s'", name);
            }
        } else {
            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());
            }
            messageHeaders.add(header);
        }
    }
    if (method == null) {
        throw new ProtocolException("Mandatory request header '%s' not found", H2PseudoRequestHeaders.METHOD);
    }
    if (Method.CONNECT.isSame(method)) {
        if (authority == null) {
            throw new ProtocolException("Header '%s' is mandatory for CONNECT request", H2PseudoRequestHeaders.AUTHORITY);
        }
        if (scheme != null) {
            throw new ProtocolException("Header '%s' must not be set for CONNECT request", H2PseudoRequestHeaders.SCHEME);
        }
        if (path != null) {
            throw new ProtocolException("Header '%s' must not be set for CONNECT request", H2PseudoRequestHeaders.PATH);
        }
    } else {
        if (scheme == null) {
            throw new ProtocolException("Mandatory request header '%s' not found", H2PseudoRequestHeaders.SCHEME);
        }
        if (path == null) {
            throw new ProtocolException("Mandatory request header '%s' not found", H2PseudoRequestHeaders.PATH);
        }
    }
    final HttpRequest httpRequest = new BasicHttpRequest(method, path);
    httpRequest.setVersion(HttpVersion.HTTP_2);
    httpRequest.setScheme(scheme);
    try {
        httpRequest.setAuthority(URIAuthority.create(authority));
    } catch (final URISyntaxException ex) {
        throw new ProtocolException(ex.getMessage(), ex);
    }
    httpRequest.setPath(path);
    for (int i = 0; i < messageHeaders.size(); i++) {
        httpRequest.addHeader(messageHeaders.get(i));
    }
    return httpRequest;
}
Also used : BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) HttpRequest(org.apache.hc.core5.http.HttpRequest) 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) URISyntaxException(java.net.URISyntaxException) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest)

Example 2 with MessageHeaders

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

the class DefaultH2ResponseConverter method convert.

@Override
public HttpResponse convert(final List<Header> headers) throws HttpException {
    String statusText = null;
    final List<Header> messageHeaders = new ArrayList<>();
    for (int i = 0; i < headers.size(); i++) {
        final Header header = headers.get(i);
        final String name = header.getName();
        final String value = header.getValue();
        for (int n = 0; n < name.length(); n++) {
            final char ch = name.charAt(n);
            if (Character.isAlphabetic(ch) && !Character.isLowerCase(ch)) {
                throw new ProtocolException("Header name '%s' is invalid (header name contains uppercase characters)", name);
            }
        }
        if (name.startsWith(":")) {
            if (!messageHeaders.isEmpty()) {
                throw new ProtocolException("Invalid sequence of headers (pseudo-headers must precede message headers)");
            }
            if (name.equals(H2PseudoResponseHeaders.STATUS)) {
                if (statusText != null) {
                    throw new ProtocolException("Multiple '%s' response headers are illegal", name);
                }
                statusText = value;
            } else {
                throw new ProtocolException("Unsupported response header '%s'", name);
            }
        } else {
            if (name.equalsIgnoreCase(HttpHeaders.CONNECTION) || name.equalsIgnoreCase(HttpHeaders.KEEP_ALIVE) || name.equalsIgnoreCase(HttpHeaders.TRANSFER_ENCODING) || name.equalsIgnoreCase(HttpHeaders.UPGRADE)) {
                throw new ProtocolException("Header '%s: %s' is illegal for HTTP/2 messages", header.getName(), header.getValue());
            }
            messageHeaders.add(header);
        }
    }
    if (statusText == null) {
        throw new ProtocolException("Mandatory response header '%s' not found", H2PseudoResponseHeaders.STATUS);
    }
    final int statusCode;
    try {
        statusCode = Integer.parseInt(statusText);
    } catch (final NumberFormatException ex) {
        throw new ProtocolException("Invalid response status: " + statusText);
    }
    final HttpResponse response = new BasicHttpResponse(statusCode, null);
    response.setVersion(HttpVersion.HTTP_2);
    for (int i = 0; i < messageHeaders.size(); i++) {
        response.addHeader(messageHeaders.get(i));
    }
    return response;
}
Also used : ProtocolException(org.apache.hc.core5.http.ProtocolException) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) Header(org.apache.hc.core5.http.Header) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) ArrayList(java.util.ArrayList) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) HttpResponse(org.apache.hc.core5.http.HttpResponse)

Example 3 with MessageHeaders

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

the class IncomingEntityDetailsTest method getContentLength.

@Test
public void getContentLength() {
    final MessageHeaders messageHeaders = new HeaderGroup();
    final HeaderGroup headerGroup = new HeaderGroup();
    final Header header = new BasicHeader("name", "value");
    headerGroup.addHeader(header);
    final IncomingEntityDetails incomingEntityDetails = new IncomingEntityDetails(messageHeaders);
    assertAll(() -> assertEquals(-1, incomingEntityDetails.getContentLength()), () -> assertTrue(incomingEntityDetails.isChunked()));
}
Also used : Header(org.apache.hc.core5.http.Header) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) HeaderGroup(org.apache.hc.core5.http.message.HeaderGroup) MessageHeaders(org.apache.hc.core5.http.MessageHeaders) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) Test(org.junit.jupiter.api.Test)

Example 4 with MessageHeaders

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

the class IncomingEntityDetailsTest method getTrailerNames.

@Test
public void getTrailerNames() {
    final HeaderGroup messageHeaders = new HeaderGroup();
    final Header header = new BasicHeader(HttpHeaders.TRAILER, "a, b, c, c");
    messageHeaders.setHeaders(header);
    final IncomingEntityDetails incomingEntityDetails = new IncomingEntityDetails(messageHeaders);
    final Set<String> incomingSet = incomingEntityDetails.getTrailerNames();
    assertAll(() -> assertFalse(incomingSet.isEmpty()), () -> assertTrue(incomingSet.containsAll(Stream.of("a", "b", "c").collect(Collectors.toCollection(HashSet::new)))));
}
Also used : Header(org.apache.hc.core5.http.Header) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) HeaderGroup(org.apache.hc.core5.http.message.HeaderGroup) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 5 with MessageHeaders

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

the class IncomingEntityDetailsTest method getContentLengthEmpty.

@Test
public void getContentLengthEmpty() {
    final MessageHeaders messageHeaders = new HeaderGroup();
    final IncomingEntityDetails incomingEntityDetails = new IncomingEntityDetails(messageHeaders);
    assertAll(() -> assertEquals(-1, incomingEntityDetails.getContentLength()), () -> assertNull(incomingEntityDetails.getContentType()), () -> assertNull(incomingEntityDetails.getContentEncoding()), () -> assertEquals(incomingEntityDetails.getTrailerNames().size(), 0));
}
Also used : HeaderGroup(org.apache.hc.core5.http.message.HeaderGroup) MessageHeaders(org.apache.hc.core5.http.MessageHeaders) Test(org.junit.jupiter.api.Test)

Aggregations

Header (org.apache.hc.core5.http.Header)4 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)4 HeaderGroup (org.apache.hc.core5.http.message.HeaderGroup)3 Test (org.junit.jupiter.api.Test)3 ArrayList (java.util.ArrayList)2 MessageHeaders (org.apache.hc.core5.http.MessageHeaders)2 ProtocolException (org.apache.hc.core5.http.ProtocolException)2 URISyntaxException (java.net.URISyntaxException)1 HashSet (java.util.HashSet)1 HttpRequest (org.apache.hc.core5.http.HttpRequest)1 HttpResponse (org.apache.hc.core5.http.HttpResponse)1 BasicHttpRequest (org.apache.hc.core5.http.message.BasicHttpRequest)1 BasicHttpResponse (org.apache.hc.core5.http.message.BasicHttpResponse)1