Package io.netty.handler.codec.http
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 interface
The source codec that is used in the pipeline initially.static interface
A codec that the source can be upgraded to.static interface
Creates a newHttpServerUpgradeHandler.UpgradeCodec
for the requested protocol name.static final class
User 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
FieldsModifier and TypeFieldDescriptionprivate boolean
private final HttpHeadersFactory
private final HttpServerUpgradeHandler.SourceCodec
private final HttpHeadersFactory
private final HttpServerUpgradeHandler.UpgradeCodecFactory
-
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. -
Method Summary
Modifier and TypeMethodDescriptionprivate FullHttpResponse
createUpgradeResponse
(CharSequence upgradeProtocol) Creates the 101 Switching Protocols response message.protected void
decode
(ChannelHandlerContext ctx, HttpObject msg, List<Object> out) Decode from one message to an other.protected boolean
Determines whether the specified upgradeHttpRequest
should be handled by this handler or not.private static List
<CharSequence> splitHeader
(CharSequence header) Splits a comma-separated header value.private boolean
upgrade
(ChannelHandlerContext ctx, FullHttpRequest request) Attempts to upgrade to the protocol(s) identified by theHttpHeaderNames.UPGRADE
header (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, releaseCurrentMessage, 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 Details
-
sourceCodec
-
upgradeCodecFactory
-
headersFactory
-
trailersFactory
-
handlingUpgrade
private boolean handlingUpgrade
-
-
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
- TheHttpHeadersFactory
to use for headers. The recommended default factory isDefaultHttpHeadersFactory.headersFactory()
.trailersFactory
- TheHttpHeadersFactory
to use for trailers. The recommended default factory isDefaultHttpHeadersFactory.trailersFactory()
.
-
-
Method Details
-
decode
Description copied from class:MessageToMessageDecoder
Decode from one message to an other. This method will be called for each written message that can be handled by this decoder.- Overrides:
decode
in classMessageAggregator<HttpObject,
HttpMessage, HttpContent, FullHttpMessage> - Parameters:
ctx
- theChannelHandlerContext
which thisMessageToMessageDecoder
belongs tomsg
- the message to decode to an other oneout
- theList
to which decoded messages should be added- Throws:
Exception
- is thrown if an error occurs
-
shouldHandleUpgradeRequest
Determines whether the specified upgradeHttpRequest
should be handled by this handler or not. This method will be invoked only when the request contains anUpgrade
header. It always returnstrue
by default, which means any request with anUpgrade
header will be handled. You can override this method to ignore certainUpgrade
headers, for example:@Override protected boolean isUpgradeRequest(HttpRequest req) { // Do not handle WebSocket upgrades. return !req.headers().contains(HttpHeaderNames.UPGRADE, "websocket", false); }
-
upgrade
Attempts to upgrade to the protocol(s) identified by theHttpHeaderNames.UPGRADE
header (if provided in the request).- Parameters:
ctx
- the context for this handler.request
- the HTTP request.- Returns:
true
if the upgrade occurred, otherwisefalse
.
-
createUpgradeResponse
Creates the 101 Switching Protocols response message. -
splitHeader
Splits a comma-separated header value. The returned set is case-insensitive and contains each part with whitespace removed.
-