Search in sources :

Example 6 with BasicHttpRequest

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

the class TestDefaultH2RequestConverter method testConvertFromFieldsKeepAliveHeader.

@Test
public void testConvertFromFieldsKeepAliveHeader() throws Exception {
    final HttpRequest request = new BasicHttpRequest("GET", new HttpHost("host"), "/");
    request.addHeader("Keep-Alive", "timeout=5, max=1000");
    final DefaultH2RequestConverter converter = new DefaultH2RequestConverter();
    Assertions.assertThrows(HttpException.class, () -> converter.convert(request), "Header 'Keep-Alive: timeout=5, max=1000' is illegal for HTTP/2 messages");
}
Also used : BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) HttpRequest(org.apache.hc.core5.http.HttpRequest) HttpHost(org.apache.hc.core5.http.HttpHost) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) Test(org.junit.jupiter.api.Test)

Example 7 with BasicHttpRequest

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

the class TestDefaultH2RequestConverter method testConvertFromMessageConnect.

@Test
public void testConvertFromMessageConnect() throws Exception {
    final HttpRequest request = new BasicHttpRequest("CONNECT", new HttpHost("host:80"), null);
    request.addHeader("custom123", "Value");
    final DefaultH2RequestConverter converter = new DefaultH2RequestConverter();
    final List<Header> headers = converter.convert(request);
    Assertions.assertNotNull(headers);
    Assertions.assertEquals(3, headers.size());
    final Header header1 = headers.get(0);
    Assertions.assertEquals(":method", header1.getName());
    Assertions.assertEquals("CONNECT", header1.getValue());
    final Header header2 = headers.get(1);
    Assertions.assertEquals(":authority", header2.getName());
    Assertions.assertEquals("host:80", header2.getValue());
    final Header header3 = headers.get(2);
    Assertions.assertEquals("custom123", header3.getName());
    Assertions.assertEquals("Value", header3.getValue());
}
Also used : BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) HttpRequest(org.apache.hc.core5.http.HttpRequest) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) Header(org.apache.hc.core5.http.Header) HttpHost(org.apache.hc.core5.http.HttpHost) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) Test(org.junit.jupiter.api.Test)

Example 8 with BasicHttpRequest

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

the class H2IntegrationTest method testPrematureResponse.

@Test
public void testPrematureResponse() throws Exception {
    server.register("*", new Supplier<AsyncServerExchangeHandler>() {

        @Override
        public AsyncServerExchangeHandler get() {
            return new AsyncServerExchangeHandler() {

                private final AtomicReference<AsyncResponseProducer> responseProducer = new AtomicReference<>();

                @Override
                public void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
                    capacityChannel.update(Integer.MAX_VALUE);
                }

                @Override
                public void consume(final ByteBuffer src) throws IOException {
                }

                @Override
                public void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
                }

                @Override
                public void handleRequest(final HttpRequest request, final EntityDetails entityDetails, final ResponseChannel responseChannel, final HttpContext context) throws HttpException, IOException {
                    final AsyncResponseProducer producer;
                    final Header h = request.getFirstHeader("password");
                    if (h != null && "secret".equals(h.getValue())) {
                        producer = new BasicResponseProducer(HttpStatus.SC_OK, "All is well");
                    } else {
                        producer = new BasicResponseProducer(HttpStatus.SC_UNAUTHORIZED, "You shall not pass");
                    }
                    responseProducer.set(producer);
                    producer.sendResponse(responseChannel, context);
                }

                @Override
                public int available() {
                    final AsyncResponseProducer producer = this.responseProducer.get();
                    return producer.available();
                }

                @Override
                public void produce(final DataStreamChannel channel) throws IOException {
                    final AsyncResponseProducer producer = this.responseProducer.get();
                    producer.produce(channel);
                }

                @Override
                public void failed(final Exception cause) {
                }

                @Override
                public void releaseResources() {
                }
            };
        }
    });
    final InetSocketAddress serverEndpoint = server.start();
    client.start();
    final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
    final ClientSessionEndpoint streamEndpoint = connectFuture.get();
    final HttpRequest request1 = new BasicHttpRequest(Method.POST, createRequestURI(serverEndpoint, "/echo"));
    final Future<Message<HttpResponse, String>> future1 = streamEndpoint.execute(new BasicRequestProducer(request1, new MultiLineEntityProducer("0123456789abcdef", 5000)), 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(HttpStatus.SC_UNAUTHORIZED, response1.getCode());
    Assertions.assertNotNull("You shall not pass", result1.getBody());
}
Also used : Message(org.apache.hc.core5.http.Message) InetSocketAddress(java.net.InetSocketAddress) DataStreamChannel(org.apache.hc.core5.http.nio.DataStreamChannel) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) ResponseChannel(org.apache.hc.core5.http.nio.ResponseChannel) AsyncResponseProducer(org.apache.hc.core5.http.nio.AsyncResponseProducer) CapacityChannel(org.apache.hc.core5.http.nio.CapacityChannel) EntityDetails(org.apache.hc.core5.http.EntityDetails) HttpException(org.apache.hc.core5.http.HttpException) AsyncServerExchangeHandler(org.apache.hc.core5.http.nio.AsyncServerExchangeHandler) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) HttpRequest(org.apache.hc.core5.http.HttpRequest) StringAsyncEntityConsumer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer) BasicRequestProducer(org.apache.hc.core5.http.nio.support.BasicRequestProducer) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) HttpResponse(org.apache.hc.core5.http.HttpResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) URISyntaxException(java.net.URISyntaxException) H2StreamResetException(org.apache.hc.core5.http2.H2StreamResetException) HttpException(org.apache.hc.core5.http.HttpException) ProtocolException(org.apache.hc.core5.http.ProtocolException) Header(org.apache.hc.core5.http.Header) BasicResponseProducer(org.apache.hc.core5.http.nio.support.BasicResponseProducer) Test(org.junit.Test)

Example 9 with BasicHttpRequest

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

the class H2IntegrationTest method testHeaderTooLargePost.

@Test
public void testHeaderTooLargePost() throws Exception {
    server.register("/hello", () -> new SingleLineResponseHandler("Hi there"));
    final InetSocketAddress serverEndpoint = server.start(H2Config.custom().setMaxHeaderListSize(100).build());
    client.start(new DefaultHttpProcessor(new H2RequestContent(), new H2RequestTargetHost(), new H2RequestConnControl()), H2Config.DEFAULT);
    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 header list size 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) H2RequestContent(org.apache.hc.core5.http2.protocol.H2RequestContent) HttpResponse(org.apache.hc.core5.http.HttpResponse) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) DefaultHttpProcessor(org.apache.hc.core5.http.protocol.DefaultHttpProcessor) H2RequestConnControl(org.apache.hc.core5.http2.protocol.H2RequestConnControl) H2RequestTargetHost(org.apache.hc.core5.http2.protocol.H2RequestTargetHost) Test(org.junit.Test)

Example 10 with BasicHttpRequest

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

the class H2IntegrationTest method testExcessOfConcurrentStreams.

@Test
public void testExcessOfConcurrentStreams() throws Exception {
    server.register("/", () -> new MultiLineResponseHandler("0123456789abcdef", 2000));
    final InetSocketAddress serverEndpoint = server.start(H2Config.custom().setMaxConcurrentStreams(20).build());
    client.start(H2Config.custom().setMaxConcurrentStreams(20).build());
    final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
    final ClientSessionEndpoint streamEndpoint = connectFuture.get();
    final Queue<Future<Message<HttpResponse, Void>>> queue = new LinkedList<>();
    for (int i = 0; i < 2000; i++) {
        final HttpRequest request1 = new BasicHttpRequest(Method.GET, createRequestURI(serverEndpoint, "/"));
        final Future<Message<HttpResponse, Void>> future = streamEndpoint.execute(new BasicRequestProducer(request1, null), new BasicResponseConsumer<>(new DiscardingEntityConsumer<>()), null);
        queue.add(future);
    }
    while (!queue.isEmpty()) {
        final Future<Message<HttpResponse, Void>> future = queue.remove();
        final Message<HttpResponse, Void> result = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
        Assertions.assertNotNull(result);
        final HttpResponse response = result.getHead();
        Assertions.assertNotNull(response);
        Assertions.assertEquals(200, response.getCode());
    }
}
Also used : BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) HttpRequest(org.apache.hc.core5.http.HttpRequest) 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) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) DiscardingEntityConsumer(org.apache.hc.core5.http.nio.entity.DiscardingEntityConsumer) Future(java.util.concurrent.Future) 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)47 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