Package io.netty.handler.codec.http
Class HttpServerUpgradeHandler
- java.lang.Object
-
- io.netty.channel.ChannelHandlerAdapter
-
- io.netty.channel.ChannelInboundHandlerAdapter
-
- io.netty.handler.codec.MessageToMessageDecoder<I>
-
- 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
public class HttpServerUpgradeHandler extends HttpObjectAggregator
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 Classes Modifier and Type Class Description static interfaceHttpServerUpgradeHandler.SourceCodecThe source codec that is used in the pipeline initially.static interfaceHttpServerUpgradeHandler.UpgradeCodecA codec that the source can be upgraded to.static interfaceHttpServerUpgradeHandler.UpgradeCodecFactoryCreates a newHttpServerUpgradeHandler.UpgradeCodecfor the requested protocol name.static classHttpServerUpgradeHandler.UpgradeEventUser event that is fired to notify about the completion of an HTTP upgrade to another protocol.-
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
Field Summary
Fields Modifier and Type Field Description private booleanhandlingUpgradeprivate HttpServerUpgradeHandler.SourceCodecsourceCodecprivate HttpServerUpgradeHandler.UpgradeCodecFactoryupgradeCodecFactory
-
Constructor Summary
Constructors Constructor Description HttpServerUpgradeHandler(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.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static FullHttpResponsecreateUpgradeResponse(java.lang.CharSequence upgradeProtocol)Creates the 101 Switching Protocols response message.protected voiddecode(ChannelHandlerContext ctx, HttpObject msg, java.util.List<java.lang.Object> out)Decode from one message to an other.private static booleanisUpgradeRequest(HttpObject msg)Determines whether or not the message is an HTTP upgrade request.private static java.util.List<java.lang.CharSequence>splitHeader(java.lang.CharSequence header)Splits a comma-separated header value.private booleanupgrade(ChannelHandlerContext ctx, FullHttpRequest request)Attempts to upgrade to the protocol(s) identified by theHttpHeaderNames.UPGRADEheader (if provided in the request).-
Methods inherited from class io.netty.handler.codec.http.HttpObjectAggregator
aggregate, beginAggregation, closeAfterContinueResponse, finishAggregation, handleOversizedMessage, ignoreContentAfterContinueResponse, isAggregated, isContentLengthInvalid, isContentMessage, isLastContentMessage, isStartMessage, newContinueResponse
-
Methods inherited from class io.netty.handler.codec.MessageAggregator
acceptInboundMessage, channelInactive, channelReadComplete, ctx, handlerAdded, handlerRemoved, isHandlingOversizedMessage, maxContentLength, maxCumulationBufferComponents, setMaxCumulationBufferComponents
-
Methods inherited from class io.netty.handler.codec.MessageToMessageDecoder
channelRead
-
Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelActive, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggered
-
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, isSharable
-
-
-
-
Field Detail
-
sourceCodec
private final HttpServerUpgradeHandler.SourceCodec sourceCodec
-
upgradeCodecFactory
private final HttpServerUpgradeHandler.UpgradeCodecFactory upgradeCodecFactory
-
handlingUpgrade
private boolean handlingUpgrade
-
-
Constructor Detail
-
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
-
-
Method Detail
-
decode
protected void decode(ChannelHandlerContext ctx, HttpObject msg, java.util.List<java.lang.Object> out) throws java.lang.Exception
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:
java.lang.Exception- is thrown if an error occurs
-
isUpgradeRequest
private static boolean isUpgradeRequest(HttpObject msg)
Determines whether or not the message is an HTTP upgrade request.
-
upgrade
private boolean upgrade(ChannelHandlerContext ctx, FullHttpRequest request)
Attempts to upgrade to the protocol(s) identified by theHttpHeaderNames.UPGRADEheader (if provided in the request).- Parameters:
ctx- the context for this handler.request- the HTTP request.- Returns:
trueif the upgrade occurred, otherwisefalse.
-
createUpgradeResponse
private static FullHttpResponse createUpgradeResponse(java.lang.CharSequence upgradeProtocol)
Creates the 101 Switching Protocols response message.
-
splitHeader
private static java.util.List<java.lang.CharSequence> splitHeader(java.lang.CharSequence header)
Splits a comma-separated header value. The returned set is case-insensitive and contains each part with whitespace removed.
-
-