final Connection connection = ctx.getConnection();
if (HttpPacket.isHttp(message)) {
// Otherwise cast message to a HttpContent
final HttpContent httpContent = (HttpContent) message;
final HttpContext context = HttpContext.get(ctx);
Request handlerRequest = httpRequestInProgress.get(context);
if (handlerRequest == null) {
// It's a new HTTP request
final HttpRequestPacket request = (HttpRequestPacket) httpContent.getHttpHeader();
final HttpResponsePacket response = request.getResponse();
activeRequestsCounter.incrementAndGet();
handlerRequest = Request.create();
handlerRequest.parameters.setLimit(config.getMaxRequestParameters());
httpRequestInProgress.set(context, handlerRequest);
final Response handlerResponse = handlerRequest.getResponse();
handlerRequest.initialize(request, ctx, this);
handlerResponse.initialize(handlerRequest, response,
ctx, suspendedResponseQueue, this);
handlerRequest.addAfterServiceListener(flushResponseHandler);
HttpServerProbeNotifier.notifyRequestReceive(this, connection,
handlerRequest);
boolean wasSuspended = false;
try {
ctx.setMessage(handlerResponse);
if (!isShuttingDown) {
if (!config.isPassTraceRequest()
&& request.getMethod() == Method.TRACE) {
onTraceRequest(handlerRequest, handlerResponse);
} else {
final HttpHandler httpHandlerLocal = httpHandler;
if (httpHandlerLocal != null) {
wasSuspended = !httpHandlerLocal.doHandle(
handlerRequest, handlerResponse);
}
}
} else { // if we're in the shutting down phase - serve shutdown page and exit
handlerResponse.getResponse().getProcessingState().setError(true);
HtmlHelper.setErrorAndSendErrorPage(
handlerRequest, handlerResponse,
config.getDefaultErrorPageGenerator(),
503, HttpStatus.SERVICE_UNAVAILABLE_503.getReasonPhrase(),
"The server is being shutting down...", null);
}
} catch (Exception t) {
LOGGER.log(Level.WARNING, "Exception during HttpHandler invocation", t);
request.getProcessingState().setError(true);
if (!response.isCommitted()) {
HtmlHelper.setErrorAndSendErrorPage(
handlerRequest, handlerResponse,
config.getDefaultErrorPageGenerator(),
500, HttpStatus.INTERNAL_SERVER_ERROR_500.getReasonPhrase(),
HttpStatus.INTERNAL_SERVER_ERROR_500.getReasonPhrase(),
t);
}
} catch (Throwable t) {
LOGGER.log(Level.WARNING, "Unexpected error", t);
throw new IllegalStateException(t);
}
if (!wasSuspended) {
return afterService(ctx, connection,
handlerRequest, handlerResponse);
} else {
return ctx.getSuspendAction();
}
} else {
// We're working with suspended HTTP request
try {
ctx.suspend();
final NextAction action = ctx.getSuspendAction();
if (!handlerRequest.getInputBuffer().append(httpContent)) {
// we don't want this thread/context to reset
// OP_READ on Connection
// we have enough data? - terminate filter chain execution
ctx.completeAndRecycle();
} else {
ctx.resume(ctx.getStopAction());
}
return action;
} finally {
httpContent.recycle();
}
}
} else { // this code will be run, when we resume the context
// We're finishing the request processing
final Response response = (Response) message;