Search in sources :

Example 26 with HttpCoreContext

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

the class Worker method run.

@Override
public void run() {
    try {
        final BasicHttpContext localContext = new BasicHttpContext();
        final HttpCoreContext context = HttpCoreContext.adapt(localContext);
        while (!Thread.interrupted() && this.conn.isOpen()) {
            this.httpservice.handleRequest(this.conn, context);
            localContext.clear();
        }
        this.conn.close();
    } catch (final Exception ex) {
        this.exceptionListener.onError(this.conn, ex);
    } finally {
        this.conn.close(CloseMode.IMMEDIATE);
    }
}
Also used : BasicHttpContext(org.apache.hc.core5.http.protocol.BasicHttpContext) HttpCoreContext(org.apache.hc.core5.http.protocol.HttpCoreContext)

Example 27 with HttpCoreContext

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

the class ClassicServerAndRequesterTest method testSequentialRequestsNonPersistentConnection.

@Test
public void testSequentialRequestsNonPersistentConnection() throws Exception {
    server.start();
    final HttpHost target = new HttpHost(scheme.id, "localhost", server.getLocalPort());
    final HttpCoreContext context = HttpCoreContext.create();
    final ClassicHttpRequest request1 = new BasicClassicHttpRequest(Method.POST, "/no-keep-alive/stuff");
    request1.setEntity(new StringEntity("some stuff", ContentType.TEXT_PLAIN));
    try (final ClassicHttpResponse response1 = requester.execute(target, request1, TIMEOUT, context)) {
        assertThat(response1.getCode(), CoreMatchers.equalTo(HttpStatus.SC_OK));
        final String body1 = EntityUtils.toString(response1.getEntity());
        assertThat(body1, CoreMatchers.equalTo("some stuff"));
    }
    final ClassicHttpRequest request2 = new BasicClassicHttpRequest(Method.POST, "/no-keep-alive/other-stuff");
    request2.setEntity(new StringEntity("some other stuff", ContentType.TEXT_PLAIN));
    try (final ClassicHttpResponse response2 = requester.execute(target, request2, TIMEOUT, context)) {
        assertThat(response2.getCode(), CoreMatchers.equalTo(HttpStatus.SC_OK));
        final String body2 = EntityUtils.toString(response2.getEntity());
        assertThat(body2, CoreMatchers.equalTo("some other stuff"));
    }
    final ClassicHttpRequest request3 = new BasicClassicHttpRequest(Method.POST, "/no-keep-alive/more-stuff");
    request3.setEntity(new StringEntity("some more stuff", ContentType.TEXT_PLAIN));
    try (final ClassicHttpResponse response3 = requester.execute(target, request3, TIMEOUT, context)) {
        assertThat(response3.getCode(), CoreMatchers.equalTo(HttpStatus.SC_OK));
        final String body3 = EntityUtils.toString(response3.getEntity());
        assertThat(body3, CoreMatchers.equalTo("some more stuff"));
    }
}
Also used : ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) StringEntity(org.apache.hc.core5.http.io.entity.StringEntity) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) HttpHost(org.apache.hc.core5.http.HttpHost) HttpCoreContext(org.apache.hc.core5.http.protocol.HttpCoreContext) Test(org.junit.Test)

Example 28 with HttpCoreContext

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

the class MonitoringResponseOutOfOrderStrategyIntegrationTest method testResponseOutOfOrderWithDefaultStrategy.

// Failures may hang
@Test(timeout = 5000)
public void testResponseOutOfOrderWithDefaultStrategy() throws Exception {
    this.server.registerHandler("*", (request, response, context) -> {
        response.setCode(400);
        response.setEntity(new AllOnesHttpEntity(200000));
    });
    this.server.start(null, null, null);
    final HttpCoreContext context = HttpCoreContext.create();
    final HttpHost host = new HttpHost(scheme.id, "localhost", this.server.getPort());
    final ClassicHttpRequest post = new BasicClassicHttpRequest(Method.POST, "/");
    post.setEntity(new AllOnesHttpEntity(200000));
    try (final ClassicHttpResponse response = requester.execute(host, post, TIMEOUT, context)) {
        Assertions.assertEquals(400, response.getCode());
        EntityUtils.consumeQuietly(response.getEntity());
    }
}
Also used : ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) HttpHost(org.apache.hc.core5.http.HttpHost) HttpCoreContext(org.apache.hc.core5.http.protocol.HttpCoreContext) Test(org.junit.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 29 with HttpCoreContext

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

the class H2IntegrationTest method testRequestWithInvalidConnectionHeader.

@Test
public void testRequestWithInvalidConnectionHeader() throws Exception {
    server.register("/hello", () -> new SingleLineResponseHandler("Hi there"));
    final InetSocketAddress serverEndpoint = server.start();
    client.start();
    final Future<IOSession> sessionFuture = client.requestSession(new HttpHost("localhost", serverEndpoint.getPort()), TIMEOUT, null);
    final IOSession session = sessionFuture.get();
    final ClientSessionEndpoint streamEndpoint = new ClientSessionEndpoint(session);
    final HttpRequest request = new BasicHttpRequest(Method.GET, createRequestURI(serverEndpoint, "/hello"));
    request.addHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE);
    final HttpCoreContext coreContext = HttpCoreContext.create();
    final Future<Message<HttpResponse, String>> future = streamEndpoint.execute(new BasicRequestProducer(request, null), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), coreContext, null);
    final ExecutionException exception = Assertions.assertThrows(ExecutionException.class, () -> future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit()));
    assertThat(exception.getCause(), CoreMatchers.instanceOf(ProtocolException.class));
    final EndpointDetails endpointDetails = coreContext.getEndpointDetails();
    assertThat(endpointDetails.getRequestCount(), CoreMatchers.equalTo(0L));
}
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) ProtocolException(org.apache.hc.core5.http.ProtocolException) Message(org.apache.hc.core5.http.Message) InetSocketAddress(java.net.InetSocketAddress) BasicRequestProducer(org.apache.hc.core5.http.nio.support.BasicRequestProducer) EndpointDetails(org.apache.hc.core5.http.EndpointDetails) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) HttpHost(org.apache.hc.core5.http.HttpHost) HttpCoreContext(org.apache.hc.core5.http.protocol.HttpCoreContext) IOSession(org.apache.hc.core5.reactor.IOSession) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 30 with HttpCoreContext

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

the class ClassicAuthenticationTest method testPostRequestAuthentication.

@Test
public void testPostRequestAuthentication() throws Exception {
    server.start();
    final HttpHost target = new HttpHost("localhost", server.getLocalPort());
    final HttpCoreContext context = HttpCoreContext.create();
    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 ClassicHttpRequest request1 = new BasicClassicHttpRequest(Method.POST, "/stuff");
    request1.setEntity(new ByteArrayEntity(stuff, ContentType.TEXT_PLAIN));
    try (final ClassicHttpResponse response1 = requester.execute(target, request1, TIMEOUT, context)) {
        assertThat(response1.getCode(), CoreMatchers.equalTo(HttpStatus.SC_UNAUTHORIZED));
        final String body1 = EntityUtils.toString(response1.getEntity());
        assertThat(body1, CoreMatchers.equalTo("You shall not pass!!!"));
    }
    final ClassicHttpRequest request2 = new BasicClassicHttpRequest(Method.POST, "/stuff");
    request2.setHeader(HttpHeaders.AUTHORIZATION, "let me pass");
    request2.setEntity(new ByteArrayEntity(stuff, ContentType.TEXT_PLAIN));
    try (final ClassicHttpResponse response2 = requester.execute(target, request2, TIMEOUT, context)) {
        assertThat(response2.getCode(), CoreMatchers.equalTo(HttpStatus.SC_OK));
        final String body1 = EntityUtils.toString(response2.getEntity());
        assertThat(body1, CoreMatchers.equalTo(new String(stuff, StandardCharsets.US_ASCII)));
    }
}
Also used : ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) Random(java.util.Random) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) ByteArrayEntity(org.apache.hc.core5.http.io.entity.ByteArrayEntity) HttpHost(org.apache.hc.core5.http.HttpHost) HttpCoreContext(org.apache.hc.core5.http.protocol.HttpCoreContext) Test(org.junit.Test)

Aggregations

HttpCoreContext (org.apache.hc.core5.http.protocol.HttpCoreContext)56 BasicClassicHttpRequest (org.apache.hc.core5.http.message.BasicClassicHttpRequest)46 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)39 ClassicHttpRequest (org.apache.hc.core5.http.ClassicHttpRequest)33 BasicClassicHttpResponse (org.apache.hc.core5.http.message.BasicClassicHttpResponse)29 HttpHost (org.apache.hc.core5.http.HttpHost)26 Test (org.junit.jupiter.api.Test)25 Test (org.junit.Test)21 HttpEntity (org.apache.hc.core5.http.HttpEntity)16 StringEntity (org.apache.hc.core5.http.io.entity.StringEntity)13 HttpClientConnection (org.apache.hc.core5.http.io.HttpClientConnection)12 ByteArrayEntity (org.apache.hc.core5.http.io.entity.ByteArrayEntity)12 HttpProcessor (org.apache.hc.core5.http.protocol.HttpProcessor)12 AbstractHttpEntity (org.apache.hc.core5.http.io.entity.AbstractHttpEntity)9 Random (java.util.Random)8 Header (org.apache.hc.core5.http.Header)8 ArrayList (java.util.ArrayList)6 HttpRequest (org.apache.hc.core5.http.HttpRequest)6 ProtocolException (org.apache.hc.core5.http.ProtocolException)6 ContentType (org.apache.hc.core5.http.ContentType)5