Class 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.
  • 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 initially
      upgradeCodecFactory - 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 initially
      upgradeCodecFactory - the factory that creates a new upgrade codec for one of the requested upgrade protocols
      maxContentLength - 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 initially
      upgradeCodecFactory - the factory that creates a new upgrade codec for one of the requested upgrade protocols
      maxContentLength - the maximum length of the content of an upgrade request
      validateHeaders - 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 initially
      upgradeCodecFactory - the factory that creates a new upgrade codec for one of the requested upgrade protocols
      maxContentLength - the maximum length of the content of an upgrade request
      headersFactory - The HttpHeadersFactory to use for headers. The recommended default factory is DefaultHttpHeadersFactory.headersFactory().
      trailersFactory - The HttpHeadersFactory to use for trailers. The recommended default factory is DefaultHttpHeadersFactory.trailersFactory().
  • Method Details

    • decode

      protected void decode(ChannelHandlerContext ctx, HttpObject msg, List<Object> out) throws Exception
      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 class MessageAggregator<HttpObject, HttpMessage, HttpContent, FullHttpMessage>
      Parameters:
      ctx - the ChannelHandlerContext which this MessageToMessageDecoder belongs to
      msg - the message to decode to an other one
      out - the List to which decoded messages should be added
      Throws:
      Exception - is thrown if an error occurs
    • beginAggregation

      protected FullHttpMessage beginAggregation(HttpMessage start, ByteBuf content) throws Exception
      Description copied from class: MessageAggregator
      Creates a new aggregated message from the specified start message and the specified content. If the start message implements ByteBufHolder, its content is appended to the specified content. This aggregator will continue to append the received content to the specified content.
      Overrides:
      beginAggregation in class HttpObjectAggregator
      Throws:
      Exception
    • shouldHandleUpgradeRequest

      protected boolean shouldHandleUpgradeRequest(HttpRequest req)
      Determines whether the specified upgrade HttpRequest should be handled by this handler or not. This method will be invoked only when the request contains an Upgrade header. It always returns true by default, which means any request with an Upgrade header will be handled. You can override this method to ignore certain Upgrade headers, for example:
      @Override
      protected boolean isUpgradeRequest(HttpRequest req) {
        // Do not handle WebSocket upgrades.
        return !req.headers().contains(HttpHeaderNames.UPGRADE, "websocket", false);
      }