Search in sources :

Example 1 with DefaultHttp2HeadersFrame

use of io.netty.handler.codec.http2.DefaultHttp2HeadersFrame in project netty by netty.

the class HelloWorldHttp2Handler method sendResponse.

/**
     * Sends a "Hello World" DATA frame to the client.
     */
private static void sendResponse(ChannelHandlerContext ctx, ByteBuf payload) {
    // Send a frame for the response status
    Http2Headers headers = new DefaultHttp2Headers().status(OK.codeAsText());
    ctx.write(new DefaultHttp2HeadersFrame(headers));
    ctx.writeAndFlush(new DefaultHttp2DataFrame(payload, true));
}
Also used : DefaultHttp2HeadersFrame(io.netty.handler.codec.http2.DefaultHttp2HeadersFrame) DefaultHttp2DataFrame(io.netty.handler.codec.http2.DefaultHttp2DataFrame) Http2Headers(io.netty.handler.codec.http2.Http2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers)

Example 2 with DefaultHttp2HeadersFrame

use of io.netty.handler.codec.http2.DefaultHttp2HeadersFrame in project netty by netty.

the class Http2MultiplexCodecTest method outboundStreamShouldWriteResetFrameOnClose_headersSent.

@Test
public void outboundStreamShouldWriteResetFrameOnClose_headersSent() {
    childChannelInitializer.handler = new ChannelInboundHandlerAdapter() {

        @Override
        public void channelActive(ChannelHandlerContext ctx) throws Exception {
            ctx.writeAndFlush(new DefaultHttp2HeadersFrame(new DefaultHttp2Headers()));
            ctx.fireChannelActive();
        }
    };
    Http2StreamChannelBootstrap b = new Http2StreamChannelBootstrap();
    b.parentChannel(parentChannel).handler(childChannelInitializer);
    Channel childChannel = b.connect().channel();
    assertTrue(childChannel.isActive());
    Http2HeadersFrame headersFrame = parentChannel.readOutbound();
    assertNotNull(headersFrame);
    assertFalse(Http2CodecUtil.isStreamIdValid(headersFrame.streamId()));
    parentChannel.pipeline().fireUserEventTriggered(new Http2StreamActiveEvent(2, headersFrame));
    childChannel.close();
    parentChannel.runPendingTasks();
    Http2ResetFrame reset = parentChannel.readOutbound();
    assertEquals(2, reset.streamId());
    assertEquals(Http2Error.CANCEL.code(), reset.errorCode());
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) StreamException(io.netty.handler.codec.http2.Http2Exception.StreamException) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.Test)

Example 3 with DefaultHttp2HeadersFrame

use of io.netty.handler.codec.http2.DefaultHttp2HeadersFrame in project jersey by jersey.

the class NettyHttp2ResponseWriter method writeResponseStatusAndHeaders.

@Override
public OutputStream writeResponseStatusAndHeaders(long contentLength, ContainerResponse responseContext) throws ContainerException {
    String reasonPhrase = responseContext.getStatusInfo().getReasonPhrase();
    int statusCode = responseContext.getStatus();
    HttpResponseStatus status = reasonPhrase == null ? HttpResponseStatus.valueOf(statusCode) : new HttpResponseStatus(statusCode, reasonPhrase);
    DefaultHttp2Headers response = new DefaultHttp2Headers();
    response.status(Integer.toString(responseContext.getStatus()));
    for (final Map.Entry<String, List<String>> e : responseContext.getStringHeaders().entrySet()) {
        response.add(e.getKey().toLowerCase(), e.getValue());
    }
    response.set(HttpHeaderNames.CONTENT_LENGTH, Long.toString(contentLength));
    ctx.writeAndFlush(new DefaultHttp2HeadersFrame(response));
    if (!headersFrame.headers().method().equals(HttpMethod.HEAD.asciiName()) && (contentLength > 0 || contentLength == -1)) {
        return new OutputStream() {

            @Override
            public void write(int b) throws IOException {
                write(new byte[] { (byte) b });
            }

            @Override
            public void write(byte[] b) throws IOException {
                write(b, 0, b.length);
            }

            @Override
            public void write(byte[] b, int off, int len) throws IOException {
                ByteBuf buffer = ctx.alloc().buffer(len);
                buffer.writeBytes(b, off, len);
                ctx.writeAndFlush(new DefaultHttp2DataFrame(buffer, false));
            }

            @Override
            public void flush() throws IOException {
                ctx.flush();
            }

            @Override
            public void close() throws IOException {
                ctx.write(new DefaultHttp2DataFrame(true)).addListener(NettyResponseWriter.FLUSH_FUTURE);
            }
        };
    } else {
        ctx.writeAndFlush(new DefaultHttp2DataFrame(true));
        return null;
    }
}
Also used : DefaultHttp2HeadersFrame(io.netty.handler.codec.http2.DefaultHttp2HeadersFrame) DefaultHttp2DataFrame(io.netty.handler.codec.http2.DefaultHttp2DataFrame) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) OutputStream(java.io.OutputStream) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) List(java.util.List) ByteBuf(io.netty.buffer.ByteBuf) Map(java.util.Map)

Aggregations

DefaultHttp2DataFrame (io.netty.handler.codec.http2.DefaultHttp2DataFrame)2 DefaultHttp2Headers (io.netty.handler.codec.http2.DefaultHttp2Headers)2 DefaultHttp2HeadersFrame (io.netty.handler.codec.http2.DefaultHttp2HeadersFrame)2 ByteBuf (io.netty.buffer.ByteBuf)1 Channel (io.netty.channel.Channel)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)1 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)1 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)1 StreamException (io.netty.handler.codec.http2.Http2Exception.StreamException)1 Http2Headers (io.netty.handler.codec.http2.Http2Headers)1 OutputStream (java.io.OutputStream)1 List (java.util.List)1 Map (java.util.Map)1 Test (org.junit.Test)1