Search in sources :

Example 96 with DefaultFullHttpRequest

use of io.netty.handler.codec.http.DefaultFullHttpRequest in project graylog2-server by Graylog2.

the class HttpHandlerTest method withCustomContentType.

@Test
public void withCustomContentType() {
    final FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/gelf");
    httpRequest.headers().add(HttpHeaderNames.HOST, "localhost");
    httpRequest.headers().add(HttpHeaderNames.CONTENT_TYPE, "foo/bar");
    httpRequest.headers().add(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
    httpRequest.content().writeBytes(GELF_MESSAGE);
    channel.writeInbound(httpRequest);
    channel.finish();
    final HttpResponse httpResponse = channel.readOutbound();
    assertThat(httpResponse.status()).isEqualTo(HttpResponseStatus.ACCEPTED);
    final HttpHeaders headers = httpResponse.headers();
    assertThat(headers.get(HttpHeaderNames.CONTENT_LENGTH)).isEqualTo("0");
    assertThat(headers.get(HttpHeaderNames.CONNECTION)).isEqualTo(HttpHeaderValues.CLOSE.toString());
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpResponse(io.netty.handler.codec.http.HttpResponse) Test(org.junit.Test)

Example 97 with DefaultFullHttpRequest

use of io.netty.handler.codec.http.DefaultFullHttpRequest in project graylog2-server by Graylog2.

the class HttpHandlerTest method withJSONContentType.

@Test
public void withJSONContentType() {
    final FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/gelf");
    httpRequest.headers().add(HttpHeaderNames.HOST, "localhost");
    httpRequest.headers().add(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON);
    httpRequest.headers().add(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
    httpRequest.content().writeBytes(GELF_MESSAGE);
    channel.writeInbound(httpRequest);
    channel.finish();
    final HttpResponse httpResponse = channel.readOutbound();
    assertThat(httpResponse.status()).isEqualTo(HttpResponseStatus.ACCEPTED);
    final HttpHeaders headers = httpResponse.headers();
    assertThat(headers.get(HttpHeaderNames.CONTENT_LENGTH)).isEqualTo("0");
    assertThat(headers.get(HttpHeaderNames.CONNECTION)).isEqualTo(HttpHeaderValues.CLOSE.toString());
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpResponse(io.netty.handler.codec.http.HttpResponse) Test(org.junit.Test)

Example 98 with DefaultFullHttpRequest

use of io.netty.handler.codec.http.DefaultFullHttpRequest in project graylog2-server by Graylog2.

the class HttpHandlerTest method withKeepalive.

@Test
public void withKeepalive() {
    final FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/gelf");
    httpRequest.headers().add(HttpHeaderNames.HOST, "localhost");
    httpRequest.headers().add(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
    httpRequest.content().writeBytes(GELF_MESSAGE);
    channel.writeInbound(httpRequest);
    channel.finish();
    final HttpResponse httpResponse = channel.readOutbound();
    assertThat(httpResponse.status()).isEqualTo(HttpResponseStatus.ACCEPTED);
    final HttpHeaders headers = httpResponse.headers();
    assertThat(headers.get(HttpHeaderNames.CONTENT_LENGTH)).isEqualTo("0");
    assertThat(headers.get(HttpHeaderNames.CONNECTION)).isEqualTo(HttpHeaderValues.KEEP_ALIVE.toString());
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpResponse(io.netty.handler.codec.http.HttpResponse) Test(org.junit.Test)

Example 99 with DefaultFullHttpRequest

use of io.netty.handler.codec.http.DefaultFullHttpRequest in project rest.li by linkedin.

the class TestHttp2ProtocolUpgradeHandler method testInitialization.

@Test
public void testInitialization() throws Exception {
    Http2UpgradeHandler handler = new Http2UpgradeHandler();
    EmbeddedChannel channel = new EmbeddedChannel(handler);
    Assert.assertTrue(channel.finish());
    Assert.assertEquals(channel.outboundMessages().size(), 1);
    DefaultFullHttpRequest message = channel.readOutbound();
    Assert.assertNotNull(message);
    Assert.assertEquals(message.method(), HttpMethod.OPTIONS);
    Assert.assertEquals(message.uri(), PATH);
    // 1) any value is ok in the host for the upgrade request
    // 2) since we are using the EmbeddedChannel, which uses an EmbeddedSocketAddress and not a InetSocketAddress
    // we cannot extract host and port from the channel context and "localhost" is used as default
    Assert.assertEquals(message.headers().get(HttpHeaderNames.HOST), "localhost");
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Test(org.testng.annotations.Test)

Example 100 with DefaultFullHttpRequest

use of io.netty.handler.codec.http.DefaultFullHttpRequest in project rest.li by linkedin.

the class Http2ProtocolUpgradeHandler method processChannelActive.

public static void processChannelActive(ChannelHandlerContext ctx, Logger log, ChannelPromise upgradePromise) {
    // For an upgrade request, clients should use an OPTIONS request for path “*” or a HEAD request for “/”.
    // RFC: https://tools.ietf.org/html/rfc7540#section-3.2
    // Implementation detail: https://http2.github.io/faq/#can-i-implement-http2-without-implementing-http11
    final DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.OPTIONS, "*");
    final String hostname;
    if (ctx.channel().remoteAddress() instanceof InetSocketAddress) {
        // 1) The documentation of remoteAddress says that it should be down-casted to InetSocketAddress.
        // 2) The getHostString doesn't attempt a reverse lookup
        InetSocketAddress inetAddress = ((InetSocketAddress) ctx.channel().remoteAddress());
        hostname = inetAddress.getHostString() + ":" + inetAddress.getPort();
    } else {
        // if it is not a InetSocketAddress, it is a DomainSocketAddress, a LocalAddress or a EmbeddedSocketAddress.
        // In the R2 stack it should never happen
        hostname = "localhost";
        log.warn("The remoteAddress is not an InetSocketAddress, therefore it has been used '" + hostname + "'" + " for the HOST of the upgrade request", ctx.channel().remoteAddress());
    }
    // The host is required given rfc2616 14.23 also for the upgrade request.
    // Without it, the host the upgrade request fails
    // https://tools.ietf.org/html/rfc2616#section-14.23
    request.headers().add(HttpHeaderNames.HOST, hostname);
    ctx.writeAndFlush(request);
    // Fail the upgrade promise when channel is closed
    ctx.channel().closeFuture().addListener(future -> {
        if (!upgradePromise.isDone()) {
            upgradePromise.setFailure(new ChannelException("HTTP/2 upgrade did not complete before channel closed"));
        }
    });
    ctx.fireChannelActive();
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) InetSocketAddress(java.net.InetSocketAddress) ClosedChannelException(java.nio.channels.ClosedChannelException) ChannelException(io.netty.channel.ChannelException)

Aggregations

DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)226 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)124 Test (org.junit.jupiter.api.Test)74 Test (org.junit.Test)68 ByteBuf (io.netty.buffer.ByteBuf)64 HttpRequest (io.netty.handler.codec.http.HttpRequest)50 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)44 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)41 AsciiString (io.netty.util.AsciiString)30 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)25 Channel (io.netty.channel.Channel)21 ChannelPromise (io.netty.channel.ChannelPromise)19 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)19 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)18 IOException (java.io.IOException)18 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)15 HttpResponse (io.netty.handler.codec.http.HttpResponse)15 HttpTrade (org.jocean.http.server.HttpServerBuilder.HttpTrade)14 CountDownLatch (java.util.concurrent.CountDownLatch)12 Bootstrap (io.netty.bootstrap.Bootstrap)11