Search in sources :

Example 11 with HttpContentDecompressor

use of io.netty.handler.codec.http.HttpContentDecompressor in project graylog2-server by Graylog2.

the class HttpTransport method getCustomChildChannelHandlers.

@Override
protected LinkedHashMap<String, Callable<? extends ChannelHandler>> getCustomChildChannelHandlers(MessageInput input) {
    final LinkedHashMap<String, Callable<? extends ChannelHandler>> handlers = new LinkedHashMap<>();
    if (idleWriterTimeout > 0) {
        // Install read timeout handler to close idle connections after a timeout.
        // This avoids dangling HTTP connections when the HTTP client does not close the connection properly.
        // For details see: https://github.com/Graylog2/graylog2-server/issues/3223#issuecomment-270350500
        handlers.put("read-timeout-handler", () -> new ReadTimeoutHandler(idleWriterTimeout, TimeUnit.SECONDS));
    }
    handlers.put("decoder", () -> new HttpRequestDecoder(DEFAULT_MAX_INITIAL_LINE_LENGTH, DEFAULT_MAX_HEADER_SIZE, maxChunkSize));
    handlers.put("decompressor", HttpContentDecompressor::new);
    handlers.put("encoder", HttpResponseEncoder::new);
    handlers.put("aggregator", () -> new HttpObjectAggregator(maxChunkSize));
    handlers.put("http-handler", () -> new HttpHandler(enableCors));
    handlers.putAll(super.getCustomChildChannelHandlers(input));
    return handlers;
}
Also used : HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) HttpHandler(org.graylog2.inputs.transports.netty.HttpHandler) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) ReadTimeoutHandler(io.netty.handler.timeout.ReadTimeoutHandler) ChannelHandler(io.netty.channel.ChannelHandler) Callable(java.util.concurrent.Callable) LinkedHashMap(java.util.LinkedHashMap) HttpContentDecompressor(io.netty.handler.codec.http.HttpContentDecompressor)

Example 12 with HttpContentDecompressor

use of io.netty.handler.codec.http.HttpContentDecompressor in project netty by netty.

the class WebSocketClientHandshaker method finishHandshake.

/**
 * Validates and finishes the opening handshake initiated by {@link #handshake}}.
 *
 * @param channel
 *            Channel
 * @param response
 *            HTTP response containing the closing handshake details
 */
public final void finishHandshake(Channel channel, FullHttpResponse response) {
    verify(response);
    // Verify the subprotocol that we received from the server.
    // This must be one of our expected subprotocols - or null/empty if we didn't want to speak a subprotocol
    String receivedProtocol = response.headers().get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL);
    receivedProtocol = receivedProtocol != null ? receivedProtocol.trim() : null;
    String expectedProtocol = expectedSubprotocol != null ? expectedSubprotocol : "";
    boolean protocolValid = false;
    if (expectedProtocol.isEmpty() && receivedProtocol == null) {
        // No subprotocol required and none received
        protocolValid = true;
        // null or "" - we echo what the user requested
        setActualSubprotocol(expectedSubprotocol);
    } else if (!expectedProtocol.isEmpty() && receivedProtocol != null && !receivedProtocol.isEmpty()) {
        // We require a subprotocol and received one -> verify it
        for (String protocol : expectedProtocol.split(",")) {
            if (protocol.trim().equals(receivedProtocol)) {
                protocolValid = true;
                setActualSubprotocol(receivedProtocol);
                break;
            }
        }
    }
    if (!protocolValid) {
        throw new WebSocketClientHandshakeException(String.format("Invalid subprotocol. Actual: %s. Expected one of: %s", receivedProtocol, expectedSubprotocol), response);
    }
    setHandshakeComplete();
    final ChannelPipeline p = channel.pipeline();
    // Remove decompressor from pipeline if its in use
    HttpContentDecompressor decompressor = p.get(HttpContentDecompressor.class);
    if (decompressor != null) {
        p.remove(decompressor);
    }
    // Remove aggregator if present before
    HttpObjectAggregator aggregator = p.get(HttpObjectAggregator.class);
    if (aggregator != null) {
        p.remove(aggregator);
    }
    ChannelHandlerContext ctx = p.context(HttpResponseDecoder.class);
    if (ctx == null) {
        ctx = p.context(HttpClientCodec.class);
        if (ctx == null) {
            throw new IllegalStateException("ChannelPipeline does not contain " + "an HttpRequestEncoder or HttpClientCodec");
        }
        final HttpClientCodec codec = (HttpClientCodec) ctx.handler();
        // Remove the encoder part of the codec as the user may start writing frames after this method returns.
        codec.removeOutboundHandler();
        p.addAfter(ctx.name(), "ws-decoder", newWebsocketDecoder());
        // Delay the removal of the decoder so the user can setup the pipeline if needed to handle
        // WebSocketFrame messages.
        // See https://github.com/netty/netty/issues/4533
        channel.eventLoop().execute(new Runnable() {

            @Override
            public void run() {
                p.remove(codec);
            }
        });
    } else {
        if (p.get(HttpRequestEncoder.class) != null) {
            // Remove the encoder part of the codec as the user may start writing frames after this method returns.
            p.remove(HttpRequestEncoder.class);
        }
        final ChannelHandlerContext context = ctx;
        p.addAfter(context.name(), "ws-decoder", newWebsocketDecoder());
        // Delay the removal of the decoder so the user can setup the pipeline if needed to handle
        // WebSocketFrame messages.
        // See https://github.com/netty/netty/issues/4533
        channel.eventLoop().execute(new Runnable() {

            @Override
            public void run() {
                p.remove(context.handler());
            }
        });
    }
}
Also used : HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) HttpRequestEncoder(io.netty.handler.codec.http.HttpRequestEncoder) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) ChannelPipeline(io.netty.channel.ChannelPipeline) HttpContentDecompressor(io.netty.handler.codec.http.HttpContentDecompressor)

Example 13 with HttpContentDecompressor

use of io.netty.handler.codec.http.HttpContentDecompressor in project netty by netty.

the class HttpUploadClientInitializer method initChannel.

@Override
public void initChannel(SocketChannel ch) {
    ChannelPipeline pipeline = ch.pipeline();
    if (sslCtx != null) {
        pipeline.addLast("ssl", sslCtx.newHandler(ch.alloc()));
    }
    pipeline.addLast("codec", new HttpClientCodec());
    // Remove the following line if you don't want automatic content decompression.
    pipeline.addLast("inflater", new HttpContentDecompressor());
    // to be used since huge file transfer
    pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
    pipeline.addLast("handler", new HttpUploadClientHandler());
}
Also used : ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) ChannelPipeline(io.netty.channel.ChannelPipeline) HttpContentDecompressor(io.netty.handler.codec.http.HttpContentDecompressor)

Aggregations

HttpContentDecompressor (io.netty.handler.codec.http.HttpContentDecompressor)13 ChannelPipeline (io.netty.channel.ChannelPipeline)7 HttpClientCodec (io.netty.handler.codec.http.HttpClientCodec)7 ChunkedWriteHandler (io.netty.handler.stream.ChunkedWriteHandler)4 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)3 Bootstrap (io.netty.bootstrap.Bootstrap)2 ChannelHandler (io.netty.channel.ChannelHandler)2 SocketChannel (io.netty.channel.socket.SocketChannel)2 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)2 HttpRequestDecoder (io.netty.handler.codec.http.HttpRequestDecoder)2 HttpResponseEncoder (io.netty.handler.codec.http.HttpResponseEncoder)2 LoggingHandler (io.netty.handler.logging.LoggingHandler)2 InetSocketAddress (java.net.InetSocketAddress)2 Unpooled (io.netty.buffer.Unpooled)1 Channel (io.netty.channel.Channel)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 ChannelInitializer (io.netty.channel.ChannelInitializer)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)1 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)1