io.micronaut.http.server.netty.RoutingInBoundHandler Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.http.server.netty;
import io.micronaut.context.event.ApplicationEventPublisher;
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.convert.ConversionService;
import io.micronaut.core.execution.ExecutionFlow;
import io.micronaut.core.propagation.PropagatedContext;
import io.micronaut.http.ByteBodyHttpResponse;
import io.micronaut.http.ByteBodyHttpResponseWrapper;
import io.micronaut.http.HttpMethod;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.body.CloseableByteBody;
import io.micronaut.http.body.MessageBodyHandlerRegistry;
import io.micronaut.http.context.ServerHttpRequestContext;
import io.micronaut.http.context.event.HttpRequestReceivedEvent;
import io.micronaut.http.context.event.HttpRequestReceivedEvent;
import io.micronaut.http.context.event.HttpRequestTerminatedEvent;
import io.micronaut.http.netty.NettyMutableHttpResponse;
import io.micronaut.http.netty.body.AvailableNettyByteBody;
import io.micronaut.http.netty.channel.ChannelPipelineCustomizer;
import io.micronaut.http.server.RouteExecutor;
import io.micronaut.http.server.binding.RequestArgumentSatisfier;
import io.micronaut.http.server.netty.configuration.NettyHttpServerConfiguration;
import io.micronaut.http.server.netty.handler.OutboundAccess;
import io.micronaut.http.server.netty.handler.RequestHandler;
import io.micronaut.web.router.resource.StaticResourceResolver;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.PrematureChannelClosureException;
import io.netty.handler.codec.compression.DecompressionException;
import io.netty.handler.codec.http.DefaultHttpRequest;
import io.netty.handler.codec.http.DefaultHttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.util.AttributeKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.net.ssl.SSLException;
import java.io.IOException;
import java.nio.channels.ClosedChannelException;
import java.util.Optional;
import java.util.concurrent.ExecutorService;
import java.util.function.Supplier;
import java.util.regex.Pattern;
/**
* Internal implementation of the {@link io.netty.channel.ChannelInboundHandler} for Micronaut.
*
* @author Graeme Rocher
* @since 1.0
*/
@Internal
@Sharable
@SuppressWarnings("FileLength")
public final class RoutingInBoundHandler implements RequestHandler {
private static final Logger LOG = LoggerFactory.getLogger(RoutingInBoundHandler.class);
/*
* Also present in {@link RouteExecutor}.
*/
private static final Pattern IGNORABLE_ERROR_MESSAGE = Pattern.compile(
"^.*(?:connection (?:reset|closed|abort|broken)|broken pipe).*$", Pattern.CASE_INSENSITIVE);
final StaticResourceResolver staticResourceResolver;
final NettyHttpServerConfiguration serverConfiguration;
final RequestArgumentSatisfier requestArgumentSatisfier;
final Supplier ioExecutorSupplier;
final boolean multipartEnabled;
final MessageBodyHandlerRegistry messageBodyHandlerRegistry;
ExecutorService ioExecutor;
final ApplicationEventPublisher terminateEventPublisher;
final ApplicationEventPublisher receivedPublisher;
final RouteExecutor routeExecutor;
final ConversionService conversionService;
/**
* This is set to {@code true} if any {@link HttpPipelineBuilder} has a logging handler.
* When this is not set, we can do a shortcut for performance.
*/
boolean supportLoggingHandler = false;
/**
* @param serverConfiguration The Netty HTTP server configuration
* @param embeddedServerContext The embedded server context
* @param ioExecutor The IO executor
* @param terminateEventPublisher The terminate event publisher
* @param receivedPublisher The received publisher
* @param conversionService The conversion service
*/
RoutingInBoundHandler(
NettyHttpServerConfiguration serverConfiguration,
NettyEmbeddedServices embeddedServerContext,
Supplier ioExecutor,
ApplicationEventPublisher terminateEventPublisher,
ApplicationEventPublisher receivedPublisher, ConversionService conversionService) {
this.staticResourceResolver = embeddedServerContext.getStaticResourceResolver();
this.messageBodyHandlerRegistry = embeddedServerContext.getMessageBodyHandlerRegistry();
this.ioExecutorSupplier = ioExecutor;
this.requestArgumentSatisfier = embeddedServerContext.getRequestArgumentSatisfier();
this.serverConfiguration = serverConfiguration;
this.terminateEventPublisher = terminateEventPublisher;
this.receivedPublisher = receivedPublisher;
Optional isMultiPartEnabled = serverConfiguration.getMultipart().getEnabled();
this.multipartEnabled = isMultiPartEnabled.isEmpty() || isMultiPartEnabled.get();
this.routeExecutor = embeddedServerContext.getRouteExecutor();
this.conversionService = conversionService;
}
private void cleanupRequest(NettyHttpRequest> request) {
try {
request.release();
} finally {
if (!terminateEventPublisher.isEmpty()) {
try {
terminateEventPublisher.publishEvent(new HttpRequestTerminatedEvent(request));
} catch (Exception e) {
if (LOG.isErrorEnabled()) {
LOG.error("Error publishing request terminated event: {}", e.getMessage(), e);
}
}
}
}
}
@Override
public void responseWritten(Object attachment) {
if (attachment != null) {
cleanupRequest((NettyHttpRequest>) attachment);
}
}
@Override
public void handleUnboundError(Throwable cause) {
// short-circuit ignorable exceptions: This is also handled by RouteExecutor, but handling this early avoids
// running any filters
if (isIgnorable(cause)) {
if (LOG.isDebugEnabled()) {
LOG.debug("Swallowed an IOException caused by client connectivity: {}", cause.getMessage(), cause);
}
return;
}
if (cause instanceof SSLException || cause.getCause() instanceof SSLException || cause instanceof DecompressionException) {
if (LOG.isDebugEnabled()) {
LOG.debug("Micronaut Server Error - No request state present. Cause: {}", cause.getMessage(), cause);
}
} else {
if (LOG.isErrorEnabled()) {
LOG.error("Micronaut Server Error - No request state present. Cause: {}", cause.getMessage(), cause);
}
}
}
@Override
public void accept(ChannelHandlerContext ctx, io.netty.handler.codec.http.HttpRequest request, CloseableByteBody body, OutboundAccess outboundAccess) {
NettyHttpRequest © 2015 - 2025 Weber Informatics LLC | Privacy Policy