@Override protected void configurePipeline(ChannelHandlerContext ctx, String protocol) throws Exception { if (ApplicationProtocolNames.HTTP_2.equals(protocol)) { ctx.channel().attr(PROTOCOL_NAME).set("HTTP/2"); configureHttp2(ctx.pipeline()); return; } if (ApplicationProtocolNames.HTTP_1_1.equals(protocol)) { ctx.channel().attr(PROTOCOL_NAME).set("HTTP/1.1"); configureHttp1(ctx.pipeline()); return; } throw new IllegalStateException("unknown protocol: " + protocol); }
/** * Most operations we want to do even if the channel is not active, because if it's not, then we want to encounter * the error that occurs when that operation happens and so that it can be passed up to the user. However, removing * handlers should only be done if the channel is active, because the error that is encountered when they aren't * makes no sense to the user (NoSuchElementException). */ private void removeHandlerIfActive(ChannelHandlerContext ctx, String name) { if (ctx.channel().isActive()) { ChannelPipeline pipeline = ctx.pipeline(); ChannelHandler handler = pipeline.get(name); if (handler != null) { pipeline.remove(name); } } }
public void addLoggingHandler(ChannelHandlerContext ctx) { if (ctx.pipeline().get(LoggingHandler.class) != null) { ctx.pipeline().remove(LoggingHandler.class); } if (ctx.pipeline().get(SslHandler.class) != null) { ctx.pipeline().addAfter("SslHandler#0", "LoggingHandler#0", this); } else { ctx.pipeline().addFirst(this); } }
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { try { String clientIP = ctx.channel().attr(SourceAddressChannelHandler.ATTR_SOURCE_ADDRESS).get(); if (IPS.get().contains(clientIP)) { ctx.channel().attr(DynamicHttp2FrameLogger.ATTR_ENABLE).set(Boolean.TRUE); ctx.pipeline().remove(this); } } finally { super.channelRead(ctx, msg); } } }
protected static boolean fireCompleteEventIfNotAlready(ChannelHandlerContext ctx, CompleteReason reason) { // Only allow this method to run once per request. Attribute<State> attr = ctx.channel().attr(ATTR_STATE); State state = attr.get(); if (state == null || state != State.STARTED) return false; attr.set(State.COMPLETED); HttpRequest request = ctx.channel().attr(ATTR_HTTP_REQ).get(); HttpResponse response = ctx.channel().attr(ATTR_HTTP_RESP).get(); // Cleanup channel attributes. ctx.channel().attr(ATTR_HTTP_REQ).set(null); ctx.channel().attr(ATTR_HTTP_RESP).set(null); // Fire the event to whole pipeline. ctx.pipeline().fireUserEventTriggered(new CompleteEvent(reason, request, response)); return true; }
connectionMap.put(chctx.channel(), socket); ChannelPipeline pipeline = chctx.pipeline(); ChannelHandler compressor = pipeline.get(HttpChunkContentCompressor.class); if (compressor != null) { pipeline.remove(compressor); pipeline.remove("httpDecoder"); if (pipeline.get("chunkedWriter") != null) { pipeline.remove("chunkedWriter"); chctx.pipeline().replace("handler", "handler", VertxHandler.create(socket)); chctx.pipeline().remove("httpEncoder"); return socket;
@Override protected void doOnError(Throwable t) { s.cancel(); ChannelPipeline pipeline = context.pipeline(); // remove the subscriber HandlerPublisher handlerPublisher = pipeline.get(HandlerPublisher.class); if (handlerPublisher != null) { pipeline.remove(handlerPublisher); } // fire the exception pipeline.fireExceptionCaught(t); }
@SuppressWarnings("unchecked") private boolean initChannel(ChannelHandlerContext ctx) throws Exception { if (initMap.add(ctx)) { // Guard against re-entrance. try { initChannel((C) ctx.channel()); } catch (Throwable cause) { // Explicitly call exceptionCaught(...) as we removed the handler before calling initChannel(...). // We do so to prevent multiple calls to initChannel(...). exceptionCaught(ctx, cause); } finally { ChannelPipeline pipeline = ctx.pipeline(); if (pipeline.context(this) != null) { pipeline.remove(this); } } return true; } return false; }
@Override protected void handshakeFailure(ChannelHandlerContext ctx, Throwable cause) throws Exception { logger.warn("{} TLS handshake failed:", ctx.channel(), cause); ctx.close(); // On handshake failure, ApplicationProtocolNegotiationHandler will remove itself, // leaving no handlers behind it. Add a handler that handles the exceptions raised after this point. ctx.pipeline().addLast(new ChannelInboundHandlerAdapter() { @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { if (cause instanceof DecoderException && cause.getCause() instanceof SSLException) { // Swallow an SSLException raised after handshake failure. return; } Exceptions.logIfUnexpected(logger, ctx.channel(), cause); } }); }
@Override public void channelRead(final ChannelHandlerContext ctx, final Object mesg) throws Exception { if (mesg instanceof FullHttpRequest) { final FullHttpRequest req = (FullHttpRequest) mesg; if ((req.method() == HttpMethod.GET) && (PushProtocol.SSE.getPath().equals(req.uri()))) { ctx.pipeline().fireUserEventTriggered(PushProtocol.SSE.getHandshakeCompleteEvent()); final DefaultHttpResponse resp = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.OK); final HttpHeaders headers = resp.headers(); headers.add("Connection", "keep-alive"); headers.add("Content-Type", "text/event-stream"); headers.add("Transfer-Encoding", "chunked"); final ChannelFuture cf = ctx.channel().writeAndFlush(resp); cf.addListener(future -> { if (future.isSuccess()) { ChannelPipeline pipeline = ctx.pipeline(); if (pipeline.get(HttpObjectAggregator.class) != null) { pipeline.remove(HttpObjectAggregator.class); } if (pipeline.get(HttpContentCompressor.class) != null) { pipeline.remove(HttpContentCompressor.class); } final String reconnetInterval = "retry: " + SSE_RETRY_BASE_INTERVAL.get() + "\r\n\r\n"; ctx.writeAndFlush(reconnetInterval); } }); } } }
@Override public void handlerAdded(ChannelHandlerContext ctx) throws Exception { channel = ctx.channel(); WriteInspector writeInspector = new WriteInspector(this); ChannelHandler parent = ctx.pipeline().get(parentHandlerName); if (null != parent) { ctx.pipeline().addBefore(parentHandlerName, WRITE_INSPECTOR_HANDLER_NAME, writeInspector); } }