Search in sources :

Example 16 with HttpMessage

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

the class Http2ServerInitializer method configureClearText.

/**
 * Configure the pipeline for a cleartext upgrade from HTTP to HTTP/2.0
 */
private void configureClearText(SocketChannel ch) {
    final ChannelPipeline p = ch.pipeline();
    final HttpServerCodec sourceCodec = new HttpServerCodec();
    final HttpServerUpgradeHandler upgradeHandler = new HttpServerUpgradeHandler(sourceCodec, upgradeCodecFactory);
    final CleartextHttp2ServerUpgradeHandler cleartextHttp2ServerUpgradeHandler = new CleartextHttp2ServerUpgradeHandler(sourceCodec, upgradeHandler, new HelloWorldHttp2HandlerBuilder().build());
    p.addLast(cleartextHttp2ServerUpgradeHandler);
    p.addLast(new SimpleChannelInboundHandler<HttpMessage>() {

        @Override
        protected void channelRead0(ChannelHandlerContext ctx, HttpMessage msg) throws Exception {
            // If this handler is hit then no upgrade has been attempted and the client is just talking HTTP.
            System.err.println("Directly talking: " + msg.protocolVersion() + " (no upgrade was attempted)");
            ChannelPipeline pipeline = ctx.pipeline();
            pipeline.addAfter(ctx.name(), null, new HelloWorldHttp1Handler("Direct. No Upgrade Attempted."));
            pipeline.replace(this, null, new HttpObjectAggregator(maxHttpContentLength));
            ctx.fireChannelRead(ReferenceCountUtil.retain(msg));
        }
    });
    p.addLast(new UserEventLogger());
}
Also used : CleartextHttp2ServerUpgradeHandler(io.netty.handler.codec.http2.CleartextHttp2ServerUpgradeHandler) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HttpServerUpgradeHandler(io.netty.handler.codec.http.HttpServerUpgradeHandler) ChannelPipeline(io.netty.channel.ChannelPipeline) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) HttpMessage(io.netty.handler.codec.http.HttpMessage)

Example 17 with HttpMessage

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

the class Http2ServerInitializer method configureClearText.

/**
 * Configure the pipeline for a cleartext upgrade from HTTP to HTTP/2.0
 */
private void configureClearText(SocketChannel ch) {
    final ChannelPipeline p = ch.pipeline();
    final HttpServerCodec sourceCodec = new HttpServerCodec();
    p.addLast(sourceCodec);
    p.addLast(new HttpServerUpgradeHandler(sourceCodec, upgradeCodecFactory));
    p.addLast(new SimpleChannelInboundHandler<HttpMessage>() {

        @Override
        protected void channelRead0(ChannelHandlerContext ctx, HttpMessage msg) throws Exception {
            // If this handler is hit then no upgrade has been attempted and the client is just talking HTTP.
            System.err.println("Directly talking: " + msg.protocolVersion() + " (no upgrade was attempted)");
            ChannelPipeline pipeline = ctx.pipeline();
            pipeline.addAfter(ctx.name(), null, new HelloWorldHttp1Handler("Direct. No Upgrade Attempted."));
            pipeline.replace(this, null, new HttpObjectAggregator(maxHttpContentLength));
            ctx.fireChannelRead(ReferenceCountUtil.retain(msg));
        }
    });
    p.addLast(new UserEventLogger());
}
Also used : HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) HelloWorldHttp1Handler(io.netty.example.http2.helloworld.server.HelloWorldHttp1Handler) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HttpServerUpgradeHandler(io.netty.handler.codec.http.HttpServerUpgradeHandler) HttpMessage(io.netty.handler.codec.http.HttpMessage) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 18 with HttpMessage

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

the class HttpToHttp2ConnectionHandler method write.

/**
 * Handles conversion of {@link HttpMessage} and {@link HttpContent} to HTTP/2 frames.
 */
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
    if (!(msg instanceof HttpMessage || msg instanceof HttpContent)) {
        ctx.write(msg, promise);
        return;
    }
    boolean release = true;
    SimpleChannelPromiseAggregator promiseAggregator = new SimpleChannelPromiseAggregator(promise, ctx.channel(), ctx.executor());
    try {
        Http2ConnectionEncoder encoder = encoder();
        boolean endStream = false;
        if (msg instanceof HttpMessage) {
            final HttpMessage httpMsg = (HttpMessage) msg;
            // Provide the user the opportunity to specify the streamId
            currentStreamId = getStreamId(httpMsg.headers());
            // Add HttpScheme if it's defined in constructor and header does not contain it.
            if (httpScheme != null && !httpMsg.headers().contains(HttpConversionUtil.ExtensionHeaderNames.SCHEME.text())) {
                httpMsg.headers().set(HttpConversionUtil.ExtensionHeaderNames.SCHEME.text(), httpScheme.name());
            }
            // Convert and write the headers.
            Http2Headers http2Headers = HttpConversionUtil.toHttp2Headers(httpMsg, validateHeaders);
            endStream = msg instanceof FullHttpMessage && !((FullHttpMessage) msg).content().isReadable();
            writeHeaders(ctx, encoder, currentStreamId, httpMsg.headers(), http2Headers, endStream, promiseAggregator);
        }
        if (!endStream && msg instanceof HttpContent) {
            boolean isLastContent = false;
            HttpHeaders trailers = EmptyHttpHeaders.INSTANCE;
            Http2Headers http2Trailers = EmptyHttp2Headers.INSTANCE;
            if (msg instanceof LastHttpContent) {
                isLastContent = true;
                // Convert any trailing headers.
                final LastHttpContent lastContent = (LastHttpContent) msg;
                trailers = lastContent.trailingHeaders();
                http2Trailers = HttpConversionUtil.toHttp2Headers(trailers, validateHeaders);
            }
            // Write the data
            final ByteBuf content = ((HttpContent) msg).content();
            endStream = isLastContent && trailers.isEmpty();
            encoder.writeData(ctx, currentStreamId, content, 0, endStream, promiseAggregator.newPromise());
            release = false;
            if (!trailers.isEmpty()) {
                // Write trailing headers.
                writeHeaders(ctx, encoder, currentStreamId, trailers, http2Trailers, true, promiseAggregator);
            }
        }
    } catch (Throwable t) {
        onError(ctx, true, t);
        promiseAggregator.setFailure(t);
    } finally {
        if (release) {
            ReferenceCountUtil.release(msg);
        }
        promiseAggregator.doneAllocatingPromises();
    }
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) EmptyHttpHeaders(io.netty.handler.codec.http.EmptyHttpHeaders) FullHttpMessage(io.netty.handler.codec.http.FullHttpMessage) SimpleChannelPromiseAggregator(io.netty.handler.codec.http2.Http2CodecUtil.SimpleChannelPromiseAggregator) HttpMessage(io.netty.handler.codec.http.HttpMessage) FullHttpMessage(io.netty.handler.codec.http.FullHttpMessage) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) ByteBuf(io.netty.buffer.ByteBuf) HttpContent(io.netty.handler.codec.http.HttpContent) LastHttpContent(io.netty.handler.codec.http.LastHttpContent)

Example 19 with HttpMessage

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

the class Http2StreamFrameToHttpObjectCodec method decode.

@Override
protected void decode(ChannelHandlerContext ctx, Http2StreamFrame frame, List<Object> out) throws Exception {
    if (frame instanceof Http2HeadersFrame) {
        Http2HeadersFrame headersFrame = (Http2HeadersFrame) frame;
        Http2Headers headers = headersFrame.headers();
        Http2FrameStream stream = headersFrame.stream();
        int id = stream == null ? 0 : stream.id();
        final CharSequence status = headers.status();
        // but we need to decode it as a FullHttpResponse to play nice with HttpObjectAggregator.
        if (null != status && HttpResponseStatus.CONTINUE.codeAsText().contentEquals(status)) {
            final FullHttpMessage fullMsg = newFullMessage(id, headers, ctx.alloc());
            out.add(fullMsg);
            return;
        }
        if (headersFrame.isEndStream()) {
            if (headers.method() == null && status == null) {
                LastHttpContent last = new DefaultLastHttpContent(Unpooled.EMPTY_BUFFER, validateHeaders);
                HttpConversionUtil.addHttp2ToHttpHeaders(id, headers, last.trailingHeaders(), HttpVersion.HTTP_1_1, true, true);
                out.add(last);
            } else {
                FullHttpMessage full = newFullMessage(id, headers, ctx.alloc());
                out.add(full);
            }
        } else {
            HttpMessage req = newMessage(id, headers);
            if (!HttpUtil.isContentLengthSet(req)) {
                req.headers().add(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
            }
            out.add(req);
        }
    } else if (frame instanceof Http2DataFrame) {
        Http2DataFrame dataFrame = (Http2DataFrame) frame;
        if (dataFrame.isEndStream()) {
            out.add(new DefaultLastHttpContent(dataFrame.content().retain(), validateHeaders));
        } else {
            out.add(new DefaultHttpContent(dataFrame.content().retain()));
        }
    }
}
Also used : DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) FullHttpMessage(io.netty.handler.codec.http.FullHttpMessage) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) HttpMessage(io.netty.handler.codec.http.HttpMessage) FullHttpMessage(io.netty.handler.codec.http.FullHttpMessage)

Aggregations

HttpMessage (io.netty.handler.codec.http.HttpMessage)19 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)10 ChannelPipeline (io.netty.channel.ChannelPipeline)8 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)5 HttpServerCodec (io.netty.handler.codec.http.HttpServerCodec)5 HttpServerUpgradeHandler (io.netty.handler.codec.http.HttpServerUpgradeHandler)5 FullHttpMessage (io.netty.handler.codec.http.FullHttpMessage)4 HttpContent (io.netty.handler.codec.http.HttpContent)3 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)3 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)3 Test (org.junit.Test)3 ChannelHandler (io.netty.channel.ChannelHandler)2 HelloWorldHttp1Handler (io.netty.example.http2.helloworld.server.HelloWorldHttp1Handler)2 DefaultHttpContent (io.netty.handler.codec.http.DefaultHttpContent)2 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)2 CleartextHttp2ServerUpgradeHandler (io.netty.handler.codec.http2.CleartextHttp2ServerUpgradeHandler)2 HMACAuthenticator (org.janusgraph.graphdb.tinkerpop.gremlin.server.auth.HMACAuthenticator)2 JanusGraphSimpleAuthenticator (org.janusgraph.graphdb.tinkerpop.gremlin.server.auth.JanusGraphSimpleAuthenticator)2 SaslAndHMACAuthenticator (org.janusgraph.graphdb.tinkerpop.gremlin.server.auth.SaslAndHMACAuthenticator)2 AsyncContextAccessor (com.navercorp.pinpoint.bootstrap.async.AsyncContextAccessor)1