use of org.apache.hc.core5.http.message.BasicClassicHttpResponse in project httpcomponents-core by apache.
the class TestStandardInterceptors method testResponseConnControlEntityChunked.
@Test
public void testResponseConnControlEntityChunked() throws Exception {
final HttpContext context = new BasicHttpContext(null);
final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
response.setEntity(new BasicHttpEntity(EmptyInputStream.INSTANCE, null, true));
final ResponseConnControl interceptor = new ResponseConnControl();
interceptor.process(response, response.getEntity(), context);
final Header header = response.getFirstHeader(HttpHeaders.CONNECTION);
Assertions.assertNull(header);
}
use of org.apache.hc.core5.http.message.BasicClassicHttpResponse in project httpcomponents-core by apache.
the class TestStandardInterceptors method testResponseContentStatusNoContent.
@Test
public void testResponseContentStatusNoContent() throws Exception {
final HttpContext context = new BasicHttpContext(null);
final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
response.setCode(HttpStatus.SC_NO_CONTENT);
final ResponseContent interceptor = new ResponseContent();
interceptor.process(response, response.getEntity(), context);
final Header header = response.getFirstHeader(HttpHeaders.CONTENT_LENGTH);
Assertions.assertNull(header);
}
use of org.apache.hc.core5.http.message.BasicClassicHttpResponse in project httpcomponents-core by apache.
the class HttpService method handleRequest.
/**
* Handles receives one HTTP request over the given connection within the
* given execution context and sends a response back to the client.
*
* @param conn the active connection to the client
* @param context the actual execution context.
* @throws IOException in case of an I/O error.
* @throws HttpException in case of HTTP protocol violation or a processing
* problem.
*/
public void handleRequest(final HttpServerConnection conn, final HttpContext context) throws IOException, HttpException {
final AtomicBoolean responseSubmitted = new AtomicBoolean(false);
try {
final ClassicHttpRequest request = conn.receiveRequestHeader();
if (request == null) {
conn.close();
return;
}
if (streamListener != null) {
streamListener.onRequestHead(conn, request);
}
conn.receiveRequestEntity(request);
final ProtocolVersion transportVersion = request.getVersion();
context.setProtocolVersion(transportVersion != null ? transportVersion : HttpVersion.HTTP_1_1);
context.setAttribute(HttpCoreContext.SSL_SESSION, conn.getSSLSession());
context.setAttribute(HttpCoreContext.CONNECTION_ENDPOINT, conn.getEndpointDetails());
context.setAttribute(HttpCoreContext.HTTP_REQUEST, request);
this.processor.process(request, request.getEntity(), context);
this.requestHandler.handle(request, new HttpServerRequestHandler.ResponseTrigger() {
@Override
public void sendInformation(final ClassicHttpResponse response) throws HttpException, IOException {
if (responseSubmitted.get()) {
throw new HttpException("Response already submitted");
}
if (response.getCode() >= HttpStatus.SC_SUCCESS) {
throw new HttpException("Invalid intermediate response");
}
if (streamListener != null) {
streamListener.onResponseHead(conn, response);
}
conn.sendResponseHeader(response);
conn.flush();
}
@Override
public void submitResponse(final ClassicHttpResponse response) throws HttpException, IOException {
try {
final ProtocolVersion transportVersion = response.getVersion();
if (transportVersion != null && transportVersion.greaterEquals(HttpVersion.HTTP_2)) {
throw new UnsupportedHttpVersionException(transportVersion);
}
ServerSupport.validateResponse(response, response.getEntity());
context.setProtocolVersion(transportVersion != null ? transportVersion : HttpVersion.HTTP_1_1);
context.setAttribute(HttpCoreContext.HTTP_RESPONSE, response);
processor.process(response, response.getEntity(), context);
responseSubmitted.set(true);
conn.sendResponseHeader(response);
if (streamListener != null) {
streamListener.onResponseHead(conn, response);
}
if (MessageSupport.canResponseHaveBody(request.getMethod(), response)) {
conn.sendResponseEntity(response);
}
// Make sure the request content is fully consumed
EntityUtils.consume(request.getEntity());
final boolean keepAlive = connReuseStrategy.keepAlive(request, response, context);
if (streamListener != null) {
streamListener.onExchangeComplete(conn, keepAlive);
}
if (!keepAlive) {
conn.close();
}
conn.flush();
} finally {
response.close();
}
}
}, context);
} catch (final HttpException ex) {
if (responseSubmitted.get()) {
throw ex;
}
try (final ClassicHttpResponse errorResponse = new BasicClassicHttpResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR)) {
handleException(ex, errorResponse);
errorResponse.setHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE);
context.setAttribute(HttpCoreContext.HTTP_RESPONSE, errorResponse);
this.processor.process(errorResponse, errorResponse.getEntity(), context);
conn.sendResponseHeader(errorResponse);
if (streamListener != null) {
streamListener.onResponseHead(conn, errorResponse);
}
conn.sendResponseEntity(errorResponse);
conn.close();
}
}
}
use of org.apache.hc.core5.http.message.BasicClassicHttpResponse in project dialogue by palantir.
the class InactivityValidationAwareConnectionKeepAliveStrategyTest method testKeepAliveHeaderWithoutTimeout.
@Test
void testKeepAliveHeaderWithoutTimeout() {
InactivityValidationAwareConnectionKeepAliveStrategy strategy = new InactivityValidationAwareConnectionKeepAliveStrategy(manager, "name");
BasicClassicHttpResponse response = new BasicClassicHttpResponse(200);
response.addHeader("Keep-Alive", "max=60");
TimeValue value = strategy.getKeepAliveDuration(response, CONTEXT);
assertThat(value).isEqualTo(CONTEXT.getRequestConfig().getConnectionKeepAlive());
verify(manager).setValidateAfterInactivity(eq(INITIAL_TIMEOUT));
}
Aggregations