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);
}
}
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"));
}
}
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());
}
}
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));
}
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)));
}
}
Aggregations