Java Code Examples for io.netty.handler.codec.http.FullHttpResponse#release()

The following examples show how to use io.netty.handler.codec.http.FullHttpResponse#release() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WebSocketClientProtocolHandshakeHandler.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (!(msg instanceof FullHttpResponse)) {
        ctx.fireChannelRead(msg);
        return;
    }

    FullHttpResponse response = (FullHttpResponse) msg;
    try {
        if (!handshaker.isHandshakeComplete()) {
            handshaker.finishHandshake(ctx.channel(), response);
            ctx.fireUserEventTriggered(
                    WebSocketClientProtocolHandler.ClientHandshakeStateEvent.HANDSHAKE_COMPLETE);
            ctx.pipeline().remove(this);
            return;
        }
        throw new IllegalStateException("WebSocketClientHandshaker should have been non finished yet");
    } finally {
        response.release();
    }
}
 
Example 2
Source File: WebSocketServerProtocolHandlerTest.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Test
public void testHttpUpgradeRequestInvalidUpgradeHeader() {
    EmbeddedChannel ch = createChannel();
    FullHttpRequest httpRequestWithEntity = new WebSocketRequestBuilder().httpVersion(HTTP_1_1)
            .method(HttpMethod.GET)
            .uri("/test")
            .connection("Upgrade")
            .version00()
            .upgrade("BogusSocket")
            .build();

    ch.writeInbound(httpRequestWithEntity);

    FullHttpResponse response = responses.remove();
    assertEquals(BAD_REQUEST, response.status());
    assertEquals("not a WebSocket handshake request: missing upgrade", getResponseMessage(response));
    response.release();
}
 
Example 3
Source File: WebSocketServerProtocolHandlerTest.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Test
public void testHttpUpgradeRequestMissingWSKeyHeader() {
    EmbeddedChannel ch = createChannel();
    HttpRequest httpRequest = new WebSocketRequestBuilder().httpVersion(HTTP_1_1)
            .method(HttpMethod.GET)
            .uri("/test")
            .key(null)
            .connection("Upgrade")
            .upgrade(HttpHeaderValues.WEBSOCKET)
            .version13()
            .build();

    ch.writeInbound(httpRequest);

    FullHttpResponse response = responses.remove();
    assertEquals(BAD_REQUEST, response.status());
    assertEquals("not a WebSocket request: missing key", getResponseMessage(response));
    response.release();
}
 
Example 4
Source File: HttpConversionUtil.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
/**
 * Create a new object to contain the response data
 *
 * @param streamId The stream associated with the response
 * @param http2Headers The initial set of HTTP/2 headers to create the response with
 * @param alloc The {@link ByteBufAllocator} to use to generate the content of the message
 * @param validateHttpHeaders <ul>
 *        <li>{@code true} to validate HTTP headers in the http-codec</li>
 *        <li>{@code false} not to validate HTTP headers in the http-codec</li>
 *        </ul>
 * @return A new response object which represents headers/data
 * @throws Http2Exception see {@link #addHttp2ToHttpHeaders(int, Http2Headers, FullHttpMessage, boolean)}
 */
public static FullHttpResponse toFullHttpResponse(int streamId, Http2Headers http2Headers, ByteBufAllocator alloc,
                                                  boolean validateHttpHeaders)
                throws Http2Exception {
    HttpResponseStatus status = parseStatus(http2Headers.status());
    // HTTP/2 does not define a way to carry the version or reason phrase that is included in an
    // HTTP/1.1 status line.
    FullHttpResponse msg = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, alloc.buffer(),
                                                       validateHttpHeaders);
    try {
        addHttp2ToHttpHeaders(streamId, http2Headers, msg, false);
    } catch (Http2Exception e) {
        msg.release();
        throw e;
    } catch (Throwable t) {
        msg.release();
        throw streamError(streamId, PROTOCOL_ERROR, t, "HTTP/2 to HTTP/1.x headers conversion error");
    }
    return msg;
}
 
Example 5
Source File: Http2StreamFrameToHttpObjectCodecTest.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example 6
Source File: Http2StreamFrameToHttpObjectCodecTest.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Test
public void testDecodeFullResponseHeaders() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(new Http2StreamFrameToHttpObjectCodec(false));
    Http2Headers headers = new DefaultHttp2Headers();
    headers.scheme(HttpScheme.HTTP.name());
    headers.status(HttpResponseStatus.OK.codeAsText());

    assertTrue(ch.writeInbound(new DefaultHttp2HeadersFrame(headers, true)));

    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));
    } finally {
        response.release();
    }

    assertThat(ch.readInbound(), is(nullValue()));
    assertFalse(ch.finish());
}
 
Example 7
Source File: WebSocketClientProtocolHandshakeHandler.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (!(msg instanceof FullHttpResponse)) {
        ctx.fireChannelRead(msg);
        return;
    }

    FullHttpResponse response = (FullHttpResponse) msg;
    try {
        if (!handshaker.isHandshakeComplete()) {
            handshaker.finishHandshake(ctx.channel(), response);
            ctx.fireUserEventTriggered(
                    WebSocketClientProtocolHandler.ClientHandshakeStateEvent.HANDSHAKE_COMPLETE);
            ctx.pipeline().remove(this);
            return;
        }
        throw new IllegalStateException("WebSocketClientHandshaker should have been non finished yet");
    } finally {
        response.release();
    }
}
 
Example 8
Source File: WebSocketServerProtocolHandlerTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test
public void testHttpUpgradeRequest() throws Exception {
    EmbeddedChannel ch = createChannel(new MockOutboundHandler());
    ChannelHandlerContext handshakerCtx = ch.pipeline().context(WebSocketServerProtocolHandshakeHandler.class);
    writeUpgradeRequest(ch);

    FullHttpResponse response = responses.remove();
    assertEquals(SWITCHING_PROTOCOLS, response.status());
    response.release();
    assertNotNull(WebSocketServerProtocolHandler.getHandshaker(handshakerCtx.channel()));
}
 
Example 9
Source File: WebSocketServerProtocolHandlerTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test
public void testSubsequentHttpRequestsAfterUpgradeShouldReturn403() throws Exception {
    EmbeddedChannel ch = createChannel();

    writeUpgradeRequest(ch);

    FullHttpResponse response = responses.remove();
    assertEquals(SWITCHING_PROTOCOLS, response.status());
    response.release();

    ch.writeInbound(new DefaultFullHttpRequest(HTTP_1_1, HttpMethod.GET, "/test"));
    response = responses.remove();
    assertEquals(FORBIDDEN, response.status());
    response.release();
}