Search in sources :

Example 46 with BasicHttpRequest

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

the class Http1ServerAndRequesterTest method testNonPersistentHeads.

@Test
public void testNonPersistentHeads() throws Exception {
    server.start();
    final Future<ListenerEndpoint> future = server.listen(new InetSocketAddress(0), scheme);
    final ListenerEndpoint listener = future.get();
    final InetSocketAddress address = (InetSocketAddress) listener.getAddress();
    requester.start();
    final HttpHost target = new HttpHost(scheme.id, "localhost", address.getPort());
    final Queue<Future<Message<HttpResponse, String>>> queue = new LinkedList<>();
    for (int i = 0; i < 20; i++) {
        final HttpRequest head = new BasicHttpRequest(Method.HEAD, target, "/no-keep-alive/stuff?p=" + i);
        queue.add(requester.execute(new BasicRequestProducer(head, null), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), TIMEOUT, null));
    }
    while (!queue.isEmpty()) {
        final Future<Message<HttpResponse, String>> resultFuture = queue.remove();
        final Message<HttpResponse, String> message = resultFuture.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
        assertThat(message, CoreMatchers.notNullValue());
        final HttpResponse response = message.getHead();
        assertThat(response.getCode(), CoreMatchers.equalTo(HttpStatus.SC_OK));
        assertThat(message.getBody(), CoreMatchers.nullValue());
    }
}
Also used : BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) HttpRequest(org.apache.hc.core5.http.HttpRequest) StringAsyncEntityConsumer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer) Message(org.apache.hc.core5.http.Message) InetSocketAddress(java.net.InetSocketAddress) BasicRequestProducer(org.apache.hc.core5.http.nio.support.BasicRequestProducer) HttpResponse(org.apache.hc.core5.http.HttpResponse) LinkedList(java.util.LinkedList) ListenerEndpoint(org.apache.hc.core5.reactor.ListenerEndpoint) AsyncClientEndpoint(org.apache.hc.core5.http.nio.AsyncClientEndpoint) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) ListenerEndpoint(org.apache.hc.core5.reactor.ListenerEndpoint) HttpHost(org.apache.hc.core5.http.HttpHost) Future(java.util.concurrent.Future) BasicResponseConsumer(org.apache.hc.core5.http.nio.support.BasicResponseConsumer) Test(org.junit.Test)

Example 47 with BasicHttpRequest

use of org.apache.hc.core5.http.message.BasicHttpRequest 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 48 with BasicHttpRequest

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

the class Http1AuthenticationTest method testPostRequestAuthentication.

@Test
public void testPostRequestAuthentication() throws Exception {
    server.start();
    final Future<ListenerEndpoint> future = server.listen(new InetSocketAddress(0), URIScheme.HTTP);
    final ListenerEndpoint listener = future.get();
    final InetSocketAddress address = (InetSocketAddress) listener.getAddress();
    requester.start();
    final HttpHost target = new HttpHost("localhost", address.getPort());
    final Random rnd = new Random();
    final byte[] stuff = new byte[10240];
    for (int i = 0; i < stuff.length; i++) {
        stuff[i] = (byte) ('a' + rnd.nextInt(10));
    }
    final HttpRequest request1 = new BasicHttpRequest(Method.POST, target, "/stuff");
    final Future<Message<HttpResponse, String>> resultFuture1 = requester.execute(new BasicRequestProducer(request1, AsyncEntityProducers.create(stuff, ContentType.TEXT_PLAIN)), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), TIMEOUT, null);
    final Message<HttpResponse, String> message1 = resultFuture1.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
    assertThat(message1, CoreMatchers.notNullValue());
    final HttpResponse response1 = message1.getHead();
    assertThat(response1.getCode(), CoreMatchers.equalTo(HttpStatus.SC_UNAUTHORIZED));
    final String body1 = message1.getBody();
    assertThat(body1, CoreMatchers.equalTo("You shall not pass!!!"));
    final HttpRequest request2 = new BasicHttpRequest(Method.POST, target, "/stuff");
    request2.setHeader(HttpHeaders.AUTHORIZATION, "let me pass");
    final Future<Message<HttpResponse, String>> resultFuture2 = requester.execute(new BasicRequestProducer(request2, AsyncEntityProducers.create(stuff, ContentType.TEXT_PLAIN)), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), TIMEOUT, null);
    final Message<HttpResponse, String> message2 = resultFuture2.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
    assertThat(message2, CoreMatchers.notNullValue());
    final HttpResponse response2 = message2.getHead();
    assertThat(response2.getCode(), CoreMatchers.equalTo(HttpStatus.SC_OK));
    final String body2 = message2.getBody();
    assertThat(body2, CoreMatchers.equalTo(new String(stuff, StandardCharsets.US_ASCII)));
}
Also used : BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) HttpRequest(org.apache.hc.core5.http.HttpRequest) StringAsyncEntityConsumer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer) Message(org.apache.hc.core5.http.Message) InetSocketAddress(java.net.InetSocketAddress) BasicRequestProducer(org.apache.hc.core5.http.nio.support.BasicRequestProducer) HttpResponse(org.apache.hc.core5.http.HttpResponse) ListenerEndpoint(org.apache.hc.core5.reactor.ListenerEndpoint) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) Random(java.util.Random) ListenerEndpoint(org.apache.hc.core5.reactor.ListenerEndpoint) HttpHost(org.apache.hc.core5.http.HttpHost) Test(org.junit.Test)

Example 49 with BasicHttpRequest

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

the class Http1AuthenticationTest method testPostRequestAuthenticationNoExpectContinue.

@Test
public void testPostRequestAuthenticationNoExpectContinue() throws Exception {
    server.start();
    final Future<ListenerEndpoint> future = server.listen(new InetSocketAddress(0), URIScheme.HTTP);
    final ListenerEndpoint listener = future.get();
    final InetSocketAddress address = (InetSocketAddress) listener.getAddress();
    requester.start();
    final HttpHost target = new HttpHost("localhost", address.getPort());
    final Random rnd = new Random();
    final byte[] stuff = new byte[10240];
    for (int i = 0; i < stuff.length; i++) {
        stuff[i] = (byte) ('a' + rnd.nextInt(10));
    }
    final HttpRequest request1 = new BasicHttpRequest(Method.POST, target, "/stuff");
    request1.setVersion(HttpVersion.HTTP_1_0);
    final Future<Message<HttpResponse, String>> resultFuture1 = requester.execute(new BasicRequestProducer(request1, AsyncEntityProducers.create(stuff, ContentType.TEXT_PLAIN)), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), TIMEOUT, null);
    final Message<HttpResponse, String> message1 = resultFuture1.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
    assertThat(message1, CoreMatchers.notNullValue());
    final HttpResponse response1 = message1.getHead();
    assertThat(response1.getCode(), CoreMatchers.equalTo(HttpStatus.SC_UNAUTHORIZED));
    final String body1 = message1.getBody();
    assertThat(body1, CoreMatchers.equalTo("You shall not pass!!!"));
    final HttpRequest request2 = new BasicHttpRequest(Method.POST, target, "/stuff");
    request2.setVersion(HttpVersion.HTTP_1_0);
    request2.setHeader(HttpHeaders.AUTHORIZATION, "let me pass");
    final Future<Message<HttpResponse, String>> resultFuture2 = requester.execute(new BasicRequestProducer(request2, AsyncEntityProducers.create(stuff, ContentType.TEXT_PLAIN)), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), TIMEOUT, null);
    final Message<HttpResponse, String> message2 = resultFuture2.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
    assertThat(message2, CoreMatchers.notNullValue());
    final HttpResponse response2 = message2.getHead();
    assertThat(response2.getCode(), CoreMatchers.equalTo(HttpStatus.SC_OK));
    final String body2 = message2.getBody();
    assertThat(body2, CoreMatchers.equalTo(new String(stuff, StandardCharsets.US_ASCII)));
}
Also used : BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) HttpRequest(org.apache.hc.core5.http.HttpRequest) StringAsyncEntityConsumer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer) Message(org.apache.hc.core5.http.Message) InetSocketAddress(java.net.InetSocketAddress) BasicRequestProducer(org.apache.hc.core5.http.nio.support.BasicRequestProducer) HttpResponse(org.apache.hc.core5.http.HttpResponse) ListenerEndpoint(org.apache.hc.core5.reactor.ListenerEndpoint) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) Random(java.util.Random) ListenerEndpoint(org.apache.hc.core5.reactor.ListenerEndpoint) HttpHost(org.apache.hc.core5.http.HttpHost) Test(org.junit.Test)

Example 50 with BasicHttpRequest

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

the class Http1IntegrationTest method testHeaderTooLargePost.

@Test
public void testHeaderTooLargePost() throws Exception {
    server.register("/hello", () -> new SingleLineResponseHandler("Hi there"));
    final InetSocketAddress serverEndpoint = server.start(null, Http1Config.custom().setMaxLineLength(100).build());
    client.start(new DefaultHttpProcessor(RequestContent.INSTANCE, RequestTargetHost.INSTANCE, RequestConnControl.INSTANCE), null);
    final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
    final ClientSessionEndpoint streamEndpoint = connectFuture.get();
    final HttpRequest request1 = new BasicHttpRequest(Method.POST, createRequestURI(serverEndpoint, "/hello"));
    request1.setHeader("big-f-header", "1234567890123456789012345678901234567890123456789012345678901234567890" + "1234567890123456789012345678901234567890");
    final byte[] b = new byte[2048];
    for (int i = 0; i < b.length; i++) {
        b[i] = (byte) ('a' + i % 10);
    }
    final Future<Message<HttpResponse, String>> future1 = streamEndpoint.execute(new BasicRequestProducer(request1, AsyncEntityProducers.create(b, ContentType.TEXT_PLAIN)), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
    final Message<HttpResponse, String> result1 = future1.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
    Assertions.assertNotNull(result1);
    final HttpResponse response1 = result1.getHead();
    Assertions.assertNotNull(response1);
    Assertions.assertEquals(431, response1.getCode());
    Assertions.assertEquals("Maximum line length limit exceeded", result1.getBody());
}
Also used : BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) HttpRequest(org.apache.hc.core5.http.HttpRequest) StringAsyncEntityConsumer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer) Message(org.apache.hc.core5.http.Message) InetSocketAddress(java.net.InetSocketAddress) BasicRequestProducer(org.apache.hc.core5.http.nio.support.BasicRequestProducer) HttpResponse(org.apache.hc.core5.http.HttpResponse) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) DefaultHttpProcessor(org.apache.hc.core5.http.protocol.DefaultHttpProcessor) Test(org.junit.Test)

Aggregations

HttpRequest (org.apache.hc.core5.http.HttpRequest)75 BasicHttpRequest (org.apache.hc.core5.http.message.BasicHttpRequest)68 Test (org.junit.jupiter.api.Test)45 HttpResponse (org.apache.hc.core5.http.HttpResponse)41 BasicRequestProducer (org.apache.hc.core5.http.nio.support.BasicRequestProducer)37 Message (org.apache.hc.core5.http.Message)34 StringAsyncEntityConsumer (org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer)33 InetSocketAddress (java.net.InetSocketAddress)30 HttpHost (org.apache.hc.core5.http.HttpHost)30 Test (org.junit.Test)30 URI (java.net.URI)23 HttpException (org.apache.hc.core5.http.HttpException)21 IOException (java.io.IOException)20 BasicHttpResponse (org.apache.hc.core5.http.message.BasicHttpResponse)19 URIAuthority (org.apache.hc.core5.net.URIAuthority)17 InterruptedIOException (java.io.InterruptedIOException)16 HttpContext (org.apache.hc.core5.http.protocol.HttpContext)15 Header (org.apache.hc.core5.http.Header)14 URISyntaxException (java.net.URISyntaxException)8 ContentType (org.apache.hc.core5.http.ContentType)8