Search in sources :

Example 76 with FullHttpResponse

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

the class Http2StreamFrameToHttpObjectCodecTest method testDecodeResponseHeadersWithContentLength.

@Test
public void testDecodeResponseHeadersWithContentLength() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(new Http2StreamFrameToHttpObjectCodec(false));
    Http2Headers headers = new DefaultHttp2Headers();
    headers.scheme(HttpScheme.HTTP.name());
    headers.status(HttpResponseStatus.OK.codeAsText());
    headers.setInt("content-length", 0);
    assertTrue(ch.writeInbound(new DefaultHttp2HeadersFrame(headers)));
    HttpResponse response = ch.readInbound();
    assertThat(response.status(), is(HttpResponseStatus.OK));
    assertThat(response.protocolVersion(), is(HttpVersion.HTTP_1_1));
    assertFalse(response instanceof FullHttpResponse);
    assertFalse(HttpUtil.isTransferEncodingChunked(response));
    assertThat(ch.readInbound(), is(nullValue()));
    assertFalse(ch.finish());
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) Test(org.junit.jupiter.api.Test)

Example 77 with FullHttpResponse

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

the class Http2StreamFrameToHttpObjectCodecTest method testDecodeFullResponseHeaders.

private void testDecodeFullResponseHeaders(boolean withStreamId) throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(new Http2StreamFrameToHttpObjectCodec(false));
    Http2Headers headers = new DefaultHttp2Headers();
    headers.scheme(HttpScheme.HTTP.name());
    headers.status(HttpResponseStatus.OK.codeAsText());
    Http2HeadersFrame frame = new DefaultHttp2HeadersFrame(headers, true);
    if (withStreamId) {
        frame.stream(new Http2FrameStream() {

            @Override
            public int id() {
                return 1;
            }

            @Override
            public Http2Stream.State state() {
                return Http2Stream.State.OPEN;
            }
        });
    }
    assertTrue(ch.writeInbound(frame));
    FullHttpResponse response = ch.readInbound();
    try {
        assertThat(response.status(), is(HttpResponseStatus.OK));
        assertThat(response.protocolVersion(), is(HttpVersion.HTTP_1_1));
        assertThat(response.content().readableBytes(), is(0));
        assertTrue(response.trailingHeaders().isEmpty());
        assertFalse(HttpUtil.isTransferEncodingChunked(response));
        if (withStreamId) {
            assertEquals(1, (int) response.headers().getInt(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text()));
        }
    } finally {
        response.release();
    }
    assertThat(ch.readInbound(), is(nullValue()));
    assertFalse(ch.finish());
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse)

Example 78 with FullHttpResponse

use of io.netty.handler.codec.http.FullHttpResponse 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 79 with FullHttpResponse

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

the class HttpStaticFileServerHandler method sendRedirect.

private void sendRedirect(ChannelHandlerContext ctx, String newUri) {
    FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, FOUND, Unpooled.EMPTY_BUFFER);
    response.headers().set(HttpHeaderNames.LOCATION, newUri);
    sendAndCleanupConnection(ctx, response);
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse)

Example 80 with FullHttpResponse

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

the class HttpHelloWorldServerHandler method channelRead0.

@Override
public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) {
    if (msg instanceof HttpRequest) {
        HttpRequest req = (HttpRequest) msg;
        boolean keepAlive = HttpUtil.isKeepAlive(req);
        FullHttpResponse response = new DefaultFullHttpResponse(req.protocolVersion(), OK, Unpooled.wrappedBuffer(CONTENT));
        response.headers().set(CONTENT_TYPE, TEXT_PLAIN).setInt(CONTENT_LENGTH, response.content().readableBytes());
        if (keepAlive) {
            if (!req.protocolVersion().isKeepAliveDefault()) {
                response.headers().set(CONNECTION, KEEP_ALIVE);
            }
        } else {
            // Tell the client we're going to close the connection.
            response.headers().set(CONNECTION, CLOSE);
        }
        ChannelFuture f = ctx.write(response);
        if (!keepAlive) {
            f.addListener(ChannelFutureListener.CLOSE);
        }
    }
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) ChannelFuture(io.netty.channel.ChannelFuture) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse)

Aggregations

FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)256 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)172 ByteBuf (io.netty.buffer.ByteBuf)53 Test (org.junit.Test)50 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)38 HttpRequest (io.netty.handler.codec.http.HttpRequest)34 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)24 HttpObject (io.netty.handler.codec.http.HttpObject)23 IOException (java.io.IOException)23 HttpTrade (org.jocean.http.server.HttpServerBuilder.HttpTrade)23 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)21 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)21 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)21 Test (org.junit.jupiter.api.Test)21 ChannelFuture (io.netty.channel.ChannelFuture)20 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)20 HttpInitiator (org.jocean.http.client.HttpClient.HttpInitiator)20 Subscription (rx.Subscription)20 LocalAddress (io.netty.channel.local.LocalAddress)19 HttpResponse (io.netty.handler.codec.http.HttpResponse)19