use of org.apache.hc.core5.http.protocol.HttpCoreContext in project httpcomponents-core by apache.
the class ResponseConnControl method process.
@Override
public void process(final HttpResponse response, final EntityDetails entity, final HttpContext context) throws HttpException, IOException {
Args.notNull(response, "HTTP response");
Args.notNull(context, "HTTP context");
// Always drop connection after certain type of responses
final int status = response.getCode();
if (status == HttpStatus.SC_BAD_REQUEST || status == HttpStatus.SC_REQUEST_TIMEOUT || status == HttpStatus.SC_LENGTH_REQUIRED || status == HttpStatus.SC_REQUEST_TOO_LONG || status == HttpStatus.SC_REQUEST_URI_TOO_LONG || status == HttpStatus.SC_SERVICE_UNAVAILABLE || status == HttpStatus.SC_NOT_IMPLEMENTED) {
response.setHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE);
return;
}
if (!response.containsHeader(HttpHeaders.CONNECTION)) {
// Always drop connection for HTTP/1.0 responses and below
// if the content body cannot be correctly delimited
final ProtocolVersion ver = context.getProtocolVersion();
if (entity != null && entity.getContentLength() < 0 && ver.lessEquals(HttpVersion.HTTP_1_0)) {
response.setHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE);
} else {
final HttpCoreContext coreContext = HttpCoreContext.adapt(context);
final HttpRequest request = coreContext.getRequest();
boolean closeRequested = false;
boolean keepAliveRequested = false;
if (request != null) {
final Iterator<HeaderElement> it = MessageSupport.iterate(request, HttpHeaders.CONNECTION);
while (it.hasNext()) {
final HeaderElement he = it.next();
if (he.getName().equalsIgnoreCase(HeaderElements.CLOSE)) {
closeRequested = true;
break;
} else if (he.getName().equalsIgnoreCase(HeaderElements.KEEP_ALIVE)) {
keepAliveRequested = true;
}
}
}
if (closeRequested) {
response.addHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE);
} else {
if (response.containsHeader(HttpHeaders.UPGRADE)) {
response.addHeader(HttpHeaders.CONNECTION, HeaderElements.UPGRADE);
} else {
if (keepAliveRequested) {
response.addHeader(HttpHeaders.CONNECTION, HeaderElements.KEEP_ALIVE);
} else {
if (ver.lessEquals(HttpVersion.HTTP_1_0)) {
response.addHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE);
}
}
}
}
}
}
}
use of org.apache.hc.core5.http.protocol.HttpCoreContext in project httpcomponents-core by apache.
the class TestHttpRequestExecutor method testExecutionNoResponseBody.
@Test
public void testExecutionNoResponseBody() throws Exception {
final HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
final HttpClientConnection conn = Mockito.mock(HttpClientConnection.class);
final HttpRequestExecutor executor = new HttpRequestExecutor();
final HttpCoreContext context = HttpCoreContext.create();
final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, "/");
executor.preProcess(request, httprocessor, context);
Mockito.verify(httprocessor).process(request, request.getEntity(), context);
Mockito.when(conn.receiveResponseHeader()).thenReturn(new BasicClassicHttpResponse(204, "OK"));
final ClassicHttpResponse response = executor.execute(request, conn, context);
Mockito.verify(conn).sendRequestHeader(request);
Mockito.verify(conn).flush();
Mockito.verify(conn).receiveResponseHeader();
Mockito.verify(conn, Mockito.never()).receiveResponseEntity(response);
executor.postProcess(response, httprocessor, context);
Mockito.verify(httprocessor).process(response, response.getEntity(), context);
}
use of org.apache.hc.core5.http.protocol.HttpCoreContext in project httpcomponents-core by apache.
the class TestHttpRequestExecutor method testExecutionSkipIntermediateResponses.
@Test
public void testExecutionSkipIntermediateResponses() throws Exception {
final HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
final HttpClientConnection conn = Mockito.mock(HttpClientConnection.class);
final HttpRequestExecutor executor = new HttpRequestExecutor();
final HttpCoreContext context = HttpCoreContext.create();
final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, "/");
executor.preProcess(request, httprocessor, context);
Mockito.verify(httprocessor).process(request, request.getEntity(), context);
Mockito.when(conn.receiveResponseHeader()).thenReturn(new BasicClassicHttpResponse(100, "Continue"), new BasicClassicHttpResponse(110, "Huh?"), new BasicClassicHttpResponse(111, "Huh?"), new BasicClassicHttpResponse(200, "OK"));
final HttpResponseInformationCallback callback = Mockito.mock(HttpResponseInformationCallback.class);
final ClassicHttpResponse response = executor.execute(request, conn, callback, context);
Mockito.verify(conn).sendRequestHeader(request);
Mockito.verify(conn).flush();
Mockito.verify(conn, Mockito.times(4)).receiveResponseHeader();
Mockito.verify(conn, Mockito.times(1)).receiveResponseEntity(response);
final ArgumentCaptor<HttpResponse> responseCaptor = ArgumentCaptor.forClass(HttpResponse.class);
Mockito.verify(callback, Mockito.times(2)).execute(responseCaptor.capture(), ArgumentMatchers.eq(conn), ArgumentMatchers.eq(context));
final List<HttpResponse> infos = responseCaptor.getAllValues();
Assertions.assertNotNull(infos);
Assertions.assertEquals(2, infos.size());
final HttpResponse info1 = infos.get(0);
Assertions.assertNotNull(info1);
Assertions.assertEquals(110, info1.getCode());
final HttpResponse info2 = infos.get(1);
Assertions.assertNotNull(info2);
Assertions.assertEquals(111, info2.getCode());
executor.postProcess(response, httprocessor, context);
Mockito.verify(httprocessor).process(response, response.getEntity(), context);
Assertions.assertSame(request, context.getRequest());
Assertions.assertSame(response, context.getResponse());
}
use of org.apache.hc.core5.http.protocol.HttpCoreContext in project httpcomponents-core by apache.
the class TestHttpService method testMethodNotSupported.
@Test
public void testMethodNotSupported() throws Exception {
final HttpCoreContext context = HttpCoreContext.create();
final ClassicHttpRequest request = new BasicClassicHttpRequest("whatever", "/");
Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
Mockito.when(responseFactory.newHttpResponse(200)).thenReturn(response);
Mockito.when(handlerResolver.resolve(request, context)).thenReturn(requestHandler);
Mockito.doThrow(new MethodNotSupportedException("whatever")).when(requestHandler).handle(request, response, context);
httpservice.handleRequest(conn, context);
final ArgumentCaptor<ClassicHttpResponse> responseCaptor = ArgumentCaptor.forClass(ClassicHttpResponse.class);
Mockito.verify(conn).sendResponseHeader(responseCaptor.capture());
final ClassicHttpResponse error = responseCaptor.getValue();
Assertions.assertNotNull(error);
Assertions.assertSame(request, context.getRequest());
Assertions.assertEquals(HttpStatus.SC_NOT_IMPLEMENTED, error.getCode());
Mockito.verify(httprocessor).process(error, error.getEntity(), context);
Mockito.verify(conn).sendResponseHeader(error);
Mockito.verify(conn).sendResponseEntity(error);
Mockito.verify(conn).close();
}
use of org.apache.hc.core5.http.protocol.HttpCoreContext in project httpcomponents-core by apache.
the class TestHttpService method testConnectionKeepAlive.
@Test
public void testConnectionKeepAlive() throws Exception {
final HttpCoreContext context = HttpCoreContext.create();
final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, "/");
Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
Mockito.when(responseFactory.newHttpResponse(200)).thenReturn(response);
Mockito.when(handlerResolver.resolve(request, context)).thenReturn(requestHandler);
Mockito.when(connReuseStrategy.keepAlive(request, response, context)).thenReturn(Boolean.TRUE);
httpservice.handleRequest(conn, context);
Assertions.assertEquals(HttpStatus.SC_OK, response.getCode());
Assertions.assertSame(request, context.getRequest());
Assertions.assertSame(response, context.getResponse());
Mockito.verify(httprocessor).process(request, request.getEntity(), context);
Mockito.verify(httprocessor).process(response, response.getEntity(), context);
Mockito.verify(conn).sendResponseHeader(response);
Mockito.verify(conn).sendResponseEntity(response);
Mockito.verify(conn).flush();
Mockito.verify(conn, Mockito.never()).close();
Mockito.verify(response).close();
}
Aggregations