Search in sources :

Example 6 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();
            ChannelHandlerContext thisCtx = pipeline.context(this);
            pipeline.addAfter(thisCtx.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 7 with HttpMessage

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

the class Http2StreamFrameToHttpObjectCodec method encode.

/**
 * Encode from an {@link HttpObject} to an {@link Http2StreamFrame}. This method will
 * be called for each written message that can be handled by this encoder.
 *
 * NOTE: 100-Continue responses that are NOT {@link FullHttpResponse} will be rejected.
 *
 * @param ctx           the {@link ChannelHandlerContext} which this handler belongs to
 * @param obj           the {@link HttpObject} message to encode
 * @param out           the {@link List} into which the encoded msg should be added
 *                      needs to do some kind of aggregation
 * @throws Exception    is thrown if an error occurs
 */
@Override
protected void encode(ChannelHandlerContext ctx, HttpObject obj, List<Object> out) throws Exception {
    // Http2HeadersFrame should not be marked as endStream=true
    if (obj instanceof HttpResponse) {
        final HttpResponse res = (HttpResponse) obj;
        if (res.status().equals(HttpResponseStatus.CONTINUE)) {
            if (res instanceof FullHttpResponse) {
                final Http2Headers headers = toHttp2Headers(ctx, res);
                out.add(new DefaultHttp2HeadersFrame(headers, false));
                return;
            } else {
                throw new EncoderException(HttpResponseStatus.CONTINUE + " must be a FullHttpResponse");
            }
        }
    }
    if (obj instanceof HttpMessage) {
        Http2Headers headers = toHttp2Headers(ctx, (HttpMessage) obj);
        boolean noMoreFrames = false;
        if (obj instanceof FullHttpMessage) {
            FullHttpMessage full = (FullHttpMessage) obj;
            noMoreFrames = !full.content().isReadable() && full.trailingHeaders().isEmpty();
        }
        out.add(new DefaultHttp2HeadersFrame(headers, noMoreFrames));
    }
    if (obj instanceof LastHttpContent) {
        LastHttpContent last = (LastHttpContent) obj;
        encodeLastContent(last, out);
    } else if (obj instanceof HttpContent) {
        HttpContent cont = (HttpContent) obj;
        out.add(new DefaultHttp2DataFrame(cont.content().retain(), false));
    }
}
Also used : EncoderException(io.netty.handler.codec.EncoderException) FullHttpMessage(io.netty.handler.codec.http.FullHttpMessage) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) HttpMessage(io.netty.handler.codec.http.HttpMessage) FullHttpMessage(io.netty.handler.codec.http.FullHttpMessage) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent)

Example 8 with HttpMessage

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

the class JerseyServerInitializer method configureClearText.

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

        @Override
        public HttpServerUpgradeHandler.UpgradeCodec newUpgradeCodec(CharSequence protocol) {
            if (AsciiString.contentEquals(Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME, protocol)) {
                return new Http2ServerUpgradeCodec(new Http2Codec(true, new JerseyHttp2ServerHandler(baseUri, container)));
            } else {
                return null;
            }
        }
    }));
    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.
            // "Directly talking: " + msg.protocolVersion() + " (no upgrade was attempted)");
            ChannelPipeline pipeline = ctx.pipeline();
            ChannelHandlerContext thisCtx = pipeline.context(this);
            pipeline.addAfter(thisCtx.name(), null, new JerseyServerHandler(baseUri, container));
            pipeline.replace(this, null, new ChunkedWriteHandler());
            ctx.fireChannelRead(msg);
        }
    });
}
Also used : Http2ServerUpgradeCodec(io.netty.handler.codec.http2.Http2ServerUpgradeCodec) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HttpServerUpgradeHandler(io.netty.handler.codec.http.HttpServerUpgradeHandler) ChannelPipeline(io.netty.channel.ChannelPipeline) Http2Codec(io.netty.handler.codec.http2.Http2Codec) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) HttpMessage(io.netty.handler.codec.http.HttpMessage)

Example 9 with HttpMessage

use of io.netty.handler.codec.http.HttpMessage in project riposte by Nike-Inc.

the class RequestStateCleanerHandlerTest method channelRead_does_nothing_if_msg_is_not_HttpRequest_or_LastHttpContent.

@Test
public void channelRead_does_nothing_if_msg_is_not_HttpRequest_or_LastHttpContent() throws Exception {
    // given
    HttpMessage ignoredMsgMock = mock(HttpMessage.class);
    // when
    handler.channelRead(ctxMock, ignoredMsgMock);
    // then
    // the normal continuation behavior from the super class.
    verify(ctxMock).fireChannelRead(ignoredMsgMock);
    // nothing else should have happened related to the ctx.
    verifyNoMoreInteractions(ctxMock);
    verifyZeroInteractions(stateMock);
    verifyZeroInteractions(metricsListenerMock);
}
Also used : HttpMessage(io.netty.handler.codec.http.HttpMessage) Test(org.junit.Test)

Example 10 with HttpMessage

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

the class SaslAndHMACAuthenticationHandlerTest method testHttpChannelReadWhenAuthenticatorHasNotBeenAdded.

@Test
public void testHttpChannelReadWhenAuthenticatorHasNotBeenAdded() throws Exception {
    final HMACAuthenticator hmacAuth = createMock(HMACAuthenticator.class);
    final SaslAndHMACAuthenticator authenticator = createMock(SaslAndHMACAuthenticator.class);
    final ChannelHandlerContext ctx = createMock(ChannelHandlerContext.class);
    final ChannelPipeline pipeline = createMock(ChannelPipeline.class);
    final HttpMessage msg = createMock(HttpMessage.class);
    final HttpHeaders headers = createMock(HttpHeaders.class);
    expect(authenticator.getHMACAuthenticator()).andReturn(hmacAuth);
    expect(authenticator.getSimpleAuthenticator()).andReturn(createMock(JanusGraphSimpleAuthenticator.class));
    expect(ctx.pipeline()).andReturn(pipeline).times(2);
    expect(pipeline.get("hmac_authenticator")).andReturn(null);
    expect(pipeline.addAfter(eq(PIPELINE_AUTHENTICATOR), eq("hmac_authenticator"), isA(ChannelHandler.class))).andReturn(null);
    expect(msg.headers()).andReturn(headers).times(2);
    expect(headers.get(isA(String.class))).andReturn(null).times(2);
    expect(ctx.fireChannelRead(eq(msg))).andReturn(ctx);
    replayAll();
    final SaslAndHMACAuthenticationHandler handler = new SaslAndHMACAuthenticationHandler(authenticator, null);
    handler.channelRead(ctx, msg);
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) JanusGraphSimpleAuthenticator(org.janusgraph.graphdb.tinkerpop.gremlin.server.auth.JanusGraphSimpleAuthenticator) SaslAndHMACAuthenticator(org.janusgraph.graphdb.tinkerpop.gremlin.server.auth.SaslAndHMACAuthenticator) HMACAuthenticator(org.janusgraph.graphdb.tinkerpop.gremlin.server.auth.HMACAuthenticator) SaslAndHMACAuthenticator(org.janusgraph.graphdb.tinkerpop.gremlin.server.auth.SaslAndHMACAuthenticator) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelHandler(io.netty.channel.ChannelHandler) HttpMessage(io.netty.handler.codec.http.HttpMessage) ChannelPipeline(io.netty.channel.ChannelPipeline) Test(org.junit.Test)

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