Search in sources :

Example 86 with FullHttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project netty by netty.

the class WebSocketServerHandshakerTest method testDuplicateHandshakeResponseHeaders.

@Test
public void testDuplicateHandshakeResponseHeaders() {
    WebSocketServerHandshaker serverHandshaker = newHandshaker("ws://example.com/chat", "chat", WebSocketDecoderConfig.DEFAULT);
    FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/chat");
    request.headers().set(HttpHeaderNames.HOST, "example.com").set(HttpHeaderNames.ORIGIN, "example.com").set(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET).set(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE).set(HttpHeaderNames.SEC_WEBSOCKET_KEY, "dGhlIHNhbXBsZSBub25jZQ==").set(HttpHeaderNames.SEC_WEBSOCKET_ORIGIN, "http://example.com").set(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, "chat, superchat").set(HttpHeaderNames.WEBSOCKET_PROTOCOL, "chat, superchat").set(HttpHeaderNames.SEC_WEBSOCKET_VERSION, webSocketVersion().toAsciiString());
    HttpHeaders customResponseHeaders = new DefaultHttpHeaders();
    // set duplicate required headers and one custom
    customResponseHeaders.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE).set(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET).set(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, "superchat").set(HttpHeaderNames.WEBSOCKET_PROTOCOL, "superchat").set("custom", "header");
    if (webSocketVersion() != WebSocketVersion.V00) {
        customResponseHeaders.set(HttpHeaderNames.SEC_WEBSOCKET_ACCEPT, "12345");
    }
    FullHttpResponse response = null;
    try {
        response = serverHandshaker.newHandshakeResponse(request, customResponseHeaders);
        HttpHeaders responseHeaders = response.headers();
        assertEquals(1, responseHeaders.getAll(HttpHeaderNames.CONNECTION).size());
        assertEquals(1, responseHeaders.getAll(HttpHeaderNames.UPGRADE).size());
        assertTrue(responseHeaders.containsValue("custom", "header", true));
        if (webSocketVersion() != WebSocketVersion.V00) {
            assertFalse(responseHeaders.containsValue(HttpHeaderNames.SEC_WEBSOCKET_ACCEPT, "12345", false));
            assertEquals(1, responseHeaders.getAll(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL).size());
            assertEquals("chat", responseHeaders.get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL));
        } else {
            assertEquals(1, responseHeaders.getAll(HttpHeaderNames.WEBSOCKET_PROTOCOL).size());
            assertEquals("chat", responseHeaders.get(HttpHeaderNames.WEBSOCKET_PROTOCOL));
        }
    } finally {
        request.release();
        if (response != null) {
            response.release();
        }
    }
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Test(org.junit.jupiter.api.Test)

Example 87 with FullHttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project netty by netty.

the class Http2StreamFrameToHttpObjectCodecTest method decode100ContinueHttp2HeadersAsFullHttpResponse.

@Test
public void decode100ContinueHttp2HeadersAsFullHttpResponse() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(new Http2StreamFrameToHttpObjectCodec(false));
    Http2Headers headers = new DefaultHttp2Headers();
    headers.scheme(HttpScheme.HTTP.name());
    headers.status(HttpResponseStatus.CONTINUE.codeAsText());
    assertTrue(ch.writeInbound(new DefaultHttp2HeadersFrame(headers, false)));
    final FullHttpResponse response = ch.readInbound();
    try {
        assertThat(response.status(), is(HttpResponseStatus.CONTINUE));
        assertThat(response.protocolVersion(), is(HttpVersion.HTTP_1_1));
    } 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) Test(org.junit.jupiter.api.Test)

Example 88 with FullHttpResponse

use of org.apache.flink.shaded.netty4.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 89 with FullHttpResponse

use of org.apache.flink.shaded.netty4.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 90 with FullHttpResponse

use of org.apache.flink.shaded.netty4.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)

Aggregations

FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)261 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)174 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 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)22 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)21 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)21 Test (org.junit.jupiter.api.Test)21 ChannelFuture (io.netty.channel.ChannelFuture)20 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)20 HttpResponse (io.netty.handler.codec.http.HttpResponse)20 HttpInitiator (org.jocean.http.client.HttpClient.HttpInitiator)20 Subscription (rx.Subscription)20 LocalAddress (io.netty.channel.local.LocalAddress)19