Search in sources :

Example 91 with FullHttpResponse

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

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

Example 93 with FullHttpResponse

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

the class WebSocketClientHandshakerTest method testWebSocketClientHandshakeException.

@Test
public void testWebSocketClientHandshakeException() {
    URI uri = URI.create("ws://localhost:9999/exception");
    WebSocketClientHandshaker handshaker = newHandshaker(uri, null, null, false);
    FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.UNAUTHORIZED);
    response.headers().set(HttpHeaderNames.WWW_AUTHENTICATE, "realm = access token required");
    try {
        handshaker.finishHandshake(null, response);
    } catch (WebSocketClientHandshakeException exception) {
        assertEquals("Invalid handshake response getStatus: 401 Unauthorized", exception.getMessage());
        assertEquals(HttpResponseStatus.UNAUTHORIZED, exception.response().status());
        assertTrue(exception.response().headers().contains(HttpHeaderNames.WWW_AUTHENTICATE, "realm = access token required", false));
    } finally {
        response.release();
    }
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 94 with FullHttpResponse

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

the class WebSocketClientHandshakerTest method testHttpResponseAndFrameInSameBuffer.

private void testHttpResponseAndFrameInSameBuffer(boolean codec) {
    String url = "ws://localhost:9999/ws";
    final WebSocketClientHandshaker shaker = newHandshaker(URI.create(url));
    final WebSocketClientHandshaker handshaker = new WebSocketClientHandshaker(shaker.uri(), shaker.version(), null, EmptyHttpHeaders.INSTANCE, Integer.MAX_VALUE, -1) {

        @Override
        protected FullHttpRequest newHandshakeRequest() {
            return shaker.newHandshakeRequest();
        }

        @Override
        protected void verify(FullHttpResponse response) {
        // Not do any verification, so we not need to care sending the correct headers etc in the test,
        // which would just make things more complicated.
        }

        @Override
        protected WebSocketFrameDecoder newWebsocketDecoder() {
            return shaker.newWebsocketDecoder();
        }

        @Override
        protected WebSocketFrameEncoder newWebSocketEncoder() {
            return shaker.newWebSocketEncoder();
        }
    };
    // use randomBytes helper from utils to check that it functions properly
    byte[] data = WebSocketUtil.randomBytes(24);
    // Create a EmbeddedChannel which we will use to encode a BinaryWebsocketFrame to bytes and so use these
    // to test the actual handshaker.
    WebSocketServerHandshakerFactory factory = new WebSocketServerHandshakerFactory(url, null, false);
    FullHttpRequest request = shaker.newHandshakeRequest();
    WebSocketServerHandshaker socketServerHandshaker = factory.newHandshaker(request);
    request.release();
    EmbeddedChannel websocketChannel = new EmbeddedChannel(socketServerHandshaker.newWebSocketEncoder(), socketServerHandshaker.newWebsocketDecoder());
    assertTrue(websocketChannel.writeOutbound(new BinaryWebSocketFrame(Unpooled.wrappedBuffer(data))));
    byte[] bytes = "HTTP/1.1 101 Switching Protocols\r\nContent-Length: 0\r\n\r\n".getBytes(CharsetUtil.US_ASCII);
    CompositeByteBuf compositeByteBuf = Unpooled.compositeBuffer();
    compositeByteBuf.addComponent(true, Unpooled.wrappedBuffer(bytes));
    for (; ; ) {
        ByteBuf frameBytes = websocketChannel.readOutbound();
        if (frameBytes == null) {
            break;
        }
        compositeByteBuf.addComponent(true, frameBytes);
    }
    EmbeddedChannel ch = new EmbeddedChannel(new HttpObjectAggregator(Integer.MAX_VALUE), new SimpleChannelInboundHandler<FullHttpResponse>() {

        @Override
        protected void channelRead0(ChannelHandlerContext ctx, FullHttpResponse msg) throws Exception {
            handshaker.finishHandshake(ctx.channel(), msg);
            ctx.pipeline().remove(this);
        }
    });
    if (codec) {
        ch.pipeline().addFirst(new HttpClientCodec());
    } else {
        ch.pipeline().addFirst(new HttpRequestEncoder(), new HttpResponseDecoder());
    }
    // We need to first write the request as HttpClientCodec will fail if we receive a response before a request
    // was written.
    shaker.handshake(ch).syncUninterruptibly();
    for (; ; ) {
        // Just consume the bytes, we are not interested in these.
        ByteBuf buf = ch.readOutbound();
        if (buf == null) {
            break;
        }
        buf.release();
    }
    assertTrue(ch.writeInbound(compositeByteBuf));
    assertTrue(ch.finish());
    BinaryWebSocketFrame frame = ch.readInbound();
    ByteBuf expect = Unpooled.wrappedBuffer(data);
    try {
        assertEquals(expect, frame.content());
        assertTrue(frame.isFinalFragment());
        assertEquals(0, frame.rsv());
    } finally {
        expect.release();
        frame.release();
    }
}
Also used : FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) HttpRequestEncoder(io.netty.handler.codec.http.HttpRequestEncoder) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponseDecoder(io.netty.handler.codec.http.HttpResponseDecoder)

Example 95 with FullHttpResponse

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

the class WebSocketServerProtocolHandlerTest method testHttpUpgradeRequestInvalidUpgradeHeader.

@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();
    assertFalse(ch.finish());
}
Also used : FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Test(org.junit.jupiter.api.Test)

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