Class HttpServerUpgradeHandler
java.lang.Object
io.netty.channel.ChannelHandlerAdapter
io.netty.channel.ChannelInboundHandlerAdapter
io.netty.handler.codec.MessageToMessageDecoder<HttpObject>
io.netty.handler.codec.MessageAggregator<HttpObject, HttpMessage, HttpContent, FullHttpMessage>
io.netty.handler.codec.http.HttpObjectAggregator
io.netty.handler.codec.http.HttpServerUpgradeHandler
- All Implemented Interfaces:
ChannelHandler, ChannelInboundHandler
A server-side handler that receives HTTP requests and optionally performs a protocol switch if
the requested protocol is supported. Once an upgrade is performed, this handler removes itself
from the pipeline.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceThe source codec that is used in the pipeline initially.static interfaceA codec that the source can be upgraded to.static interfaceCreates a newHttpServerUpgradeHandler.UpgradeCodecfor the requested protocol name.static final classUser event that is fired to notify about the completion of an HTTP upgrade to another protocol.Nested classes/interfaces inherited from interface ChannelHandler
ChannelHandler.Sharable -
Constructor Summary
ConstructorsConstructorDescriptionHttpServerUpgradeHandler(HttpServerUpgradeHandler.SourceCodec sourceCodec, HttpServerUpgradeHandler.UpgradeCodecFactory upgradeCodecFactory) Constructs the upgrader with the supported codecs.HttpServerUpgradeHandler(HttpServerUpgradeHandler.SourceCodec sourceCodec, HttpServerUpgradeHandler.UpgradeCodecFactory upgradeCodecFactory, int maxContentLength) Constructs the upgrader with the supported codecs.HttpServerUpgradeHandler(HttpServerUpgradeHandler.SourceCodec sourceCodec, HttpServerUpgradeHandler.UpgradeCodecFactory upgradeCodecFactory, int maxContentLength, boolean validateHeaders) Constructs the upgrader with the supported codecs.HttpServerUpgradeHandler(HttpServerUpgradeHandler.SourceCodec sourceCodec, HttpServerUpgradeHandler.UpgradeCodecFactory upgradeCodecFactory, int maxContentLength, HttpHeadersFactory headersFactory, HttpHeadersFactory trailersFactory) Constructs the upgrader with the supported codecs.HttpServerUpgradeHandler(HttpServerUpgradeHandler.SourceCodec sourceCodec, HttpServerUpgradeHandler.UpgradeCodecFactory upgradeCodecFactory, int maxContentLength, HttpHeadersFactory headersFactory, HttpHeadersFactory trailersFactory, boolean removeAfterFirstRequest) Constructs the upgrader with the supported codecs. -
Method Summary
Modifier and TypeMethodDescriptionprotected FullHttpMessagebeginAggregation(HttpMessage start, ByteBuf content) Creates a new aggregated message from the specified start message and the specified content.protected voiddecode(ChannelHandlerContext ctx, HttpObject msg, List<Object> out) Decode from one message to an other.protected booleanDetermines whether the specified upgradeHttpRequestshould be handled by this handler or not.Methods inherited from class HttpObjectAggregator
aggregate, closeAfterContinueResponse, finishAggregation, handleOversizedMessage, ignoreContentAfterContinueResponse, isAggregated, isContentLengthInvalid, isContentMessage, isLastContentMessage, isStartMessage, newContinueResponseMethods inherited from class MessageAggregator
acceptInboundMessage, channelInactive, channelReadComplete, ctx, handlerAdded, handlerRemoved, isHandlingOversizedMessage, maxContentLength, maxCumulationBufferComponents, releaseCurrentMessage, setMaxCumulationBufferComponentsMethods inherited from class MessageToMessageDecoder
channelReadMethods inherited from class ChannelInboundHandlerAdapter
channelActive, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggeredMethods inherited from class ChannelHandlerAdapter
ensureNotSharable, isSharable
-
Constructor Details
-
HttpServerUpgradeHandler
public HttpServerUpgradeHandler(HttpServerUpgradeHandler.SourceCodec sourceCodec, HttpServerUpgradeHandler.UpgradeCodecFactory upgradeCodecFactory) Constructs the upgrader with the supported codecs.The handler instantiated by this constructor will reject an upgrade request with non-empty content. It should not be a concern because an upgrade request is most likely a GET request. If you have a client that sends a non-GET upgrade request, please consider using
HttpServerUpgradeHandler(SourceCodec, UpgradeCodecFactory, int)to specify the maximum length of the content of an upgrade request.- Parameters:
sourceCodec- the codec that is being used initiallyupgradeCodecFactory- the factory that creates a new upgrade codec for one of the requested upgrade protocols
-
HttpServerUpgradeHandler
public HttpServerUpgradeHandler(HttpServerUpgradeHandler.SourceCodec sourceCodec, HttpServerUpgradeHandler.UpgradeCodecFactory upgradeCodecFactory, int maxContentLength) Constructs the upgrader with the supported codecs.- Parameters:
sourceCodec- the codec that is being used initiallyupgradeCodecFactory- the factory that creates a new upgrade codec for one of the requested upgrade protocolsmaxContentLength- the maximum length of the content of an upgrade request
-
HttpServerUpgradeHandler
public HttpServerUpgradeHandler(HttpServerUpgradeHandler.SourceCodec sourceCodec, HttpServerUpgradeHandler.UpgradeCodecFactory upgradeCodecFactory, int maxContentLength, boolean validateHeaders) Constructs the upgrader with the supported codecs.- Parameters:
sourceCodec- the codec that is being used initiallyupgradeCodecFactory- the factory that creates a new upgrade codec for one of the requested upgrade protocolsmaxContentLength- the maximum length of the content of an upgrade requestvalidateHeaders- validate the header names and values of the upgrade response.
-
HttpServerUpgradeHandler
public HttpServerUpgradeHandler(HttpServerUpgradeHandler.SourceCodec sourceCodec, HttpServerUpgradeHandler.UpgradeCodecFactory upgradeCodecFactory, int maxContentLength, HttpHeadersFactory headersFactory, HttpHeadersFactory trailersFactory) Constructs the upgrader with the supported codecs.- Parameters:
sourceCodec- the codec that is being used initiallyupgradeCodecFactory- the factory that creates a new upgrade codec for one of the requested upgrade protocolsmaxContentLength- the maximum length of the content of an upgrade requestheadersFactory- TheHttpHeadersFactoryto use for headers. The recommended default factory isDefaultHttpHeadersFactory.headersFactory().trailersFactory- TheHttpHeadersFactoryto use for trailers. The recommended default factory isDefaultHttpHeadersFactory.trailersFactory().
-
HttpServerUpgradeHandler
public HttpServerUpgradeHandler(HttpServerUpgradeHandler.SourceCodec sourceCodec, HttpServerUpgradeHandler.UpgradeCodecFactory upgradeCodecFactory, int maxContentLength, HttpHeadersFactory headersFactory, HttpHeadersFactory trailersFactory, boolean removeAfterFirstRequest) Constructs the upgrader with the supported codecs.- Parameters:
sourceCodec- the codec that is being used initiallyupgradeCodecFactory- the factory that creates a new upgrade codec for one of the requested upgrade protocolsmaxContentLength- the maximum length of the content of an upgrade requestheadersFactory- TheHttpHeadersFactoryto use for headers. The recommended default factory isDefaultHttpHeadersFactory.headersFactory().trailersFactory- TheHttpHeadersFactoryto use for trailers. The recommended default factory isDefaultHttpHeadersFactory.trailersFactory().removeAfterFirstRequest-trueif the handler should remove itself after the first request was processed (even if it was not an upgrade request),falseotherwise.
-
-
Method Details
-
decode
Description copied from class:MessageToMessageDecoderDecode from one message to an other. This method will be called for each written message that can be handled by this decoder.- Overrides:
decodein classMessageAggregator<HttpObject, HttpMessage, HttpContent, FullHttpMessage>- Parameters:
ctx- theChannelHandlerContextwhich thisMessageToMessageDecoderbelongs tomsg- the message to decode to an other oneout- theListto which decoded messages should be added- Throws:
Exception- is thrown if an error occurs
-
beginAggregation
Description copied from class:MessageAggregatorCreates a new aggregated message from the specified start message and the specified content. If the start message implementsByteBufHolder, its content is appended to the specifiedcontent. This aggregator will continue to append the received content to the specifiedcontent.- Overrides:
beginAggregationin classHttpObjectAggregator- Throws:
Exception
-
shouldHandleUpgradeRequest
Determines whether the specified upgradeHttpRequestshould be handled by this handler or not. This method will be invoked only when the request contains anUpgradeheader. It always returnstrueby default, which means any request with anUpgradeheader will be handled. You can override this method to ignore certainUpgradeheaders, for example:@Override protected boolean isUpgradeRequest(HttpRequest req) { // Do not handle WebSocket upgrades. return !req.headers().contains(HttpHeaderNames.UPGRADE, "websocket", false); }
-