Search in sources :

Example 96 with DefaultFullHttpRequest

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

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

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

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

Example 100 with DefaultFullHttpRequest

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpRequest in project carbon-apimgt by wso2.

the class WebsocketInboundHandlerTestCase method testWSHandshakeResponse.

@Test
public void testWSHandshakeResponse() throws Exception {
    InboundMessageContext inboundMessageContext = createWebSocketApiMessageContext();
    InboundMessageContextDataHolder.getInstance().addInboundMessageContextForConnection(channelIdString, inboundMessageContext);
    String headerName = "test-header";
    String headerValue = "test-header-value";
    String strWebSocket = "websocket";
    InboundProcessorResponseDTO responseDTO = new InboundProcessorResponseDTO();
    FullHttpRequest fullHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "ws://localhost:8080/graphql");
    fullHttpRequest.headers().set(headerName, headerValue);
    fullHttpRequest.headers().set(HttpHeaders.UPGRADE, strWebSocket);
    Mockito.when(inboundWebSocketProcessor.handleHandshake(fullHttpRequest, channelHandlerContext, inboundMessageContext)).thenReturn(responseDTO);
    websocketInboundHandler.channelRead(channelHandlerContext, fullHttpRequest);
    Assert.assertTrue((InboundMessageContextDataHolder.getInstance().getInboundMessageContextMap().containsKey(// No error has occurred context exists in data-holder map.
    channelIdString)));
    Assert.assertEquals(inboundMessageContext.getRequestHeaders().get(headerName), headerValue);
    Assert.assertEquals(inboundMessageContext.getToken(), fullHttpRequest.headers().get(APIMgtGatewayConstants.WS_JWT_TOKEN_HEADER));
    Assert.assertEquals(inboundMessageContext.getUserIP(), remoteIP);
    // error response
    responseDTO.setError(true);
    responseDTO.setErrorMessage("error");
    fullHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "ws://localhost:8080/graphql");
    Mockito.when(inboundWebSocketProcessor.handleHandshake(fullHttpRequest, channelHandlerContext, inboundMessageContext)).thenReturn(responseDTO);
    fullHttpRequest.headers().set(headerName, headerValue);
    fullHttpRequest.headers().set(HttpHeaders.UPGRADE, strWebSocket);
    websocketInboundHandler.channelRead(channelHandlerContext, fullHttpRequest);
    Assert.assertFalse(InboundMessageContextDataHolder.getInstance().getInboundMessageContextMap().containsKey(// Closing connection error has occurred
    channelIdString));
    Assert.assertFalse(fullHttpRequest.headers().contains(APIMgtGatewayConstants.WS_JWT_TOKEN_HEADER));
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) InboundProcessorResponseDTO(org.wso2.carbon.apimgt.gateway.inbound.websocket.InboundProcessorResponseDTO) InboundMessageContext(org.wso2.carbon.apimgt.gateway.inbound.InboundMessageContext) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)215 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)117 Test (org.junit.jupiter.api.Test)72 Test (org.junit.Test)61 ByteBuf (io.netty.buffer.ByteBuf)56 HttpRequest (io.netty.handler.codec.http.HttpRequest)47 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)43 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)40 AsciiString (io.netty.util.AsciiString)30 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)23 Channel (io.netty.channel.Channel)20 ChannelPromise (io.netty.channel.ChannelPromise)19 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)18 IOException (java.io.IOException)17 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)15 HttpResponse (io.netty.handler.codec.http.HttpResponse)15 URI (java.net.URI)15 HttpTrade (org.jocean.http.server.HttpServerBuilder.HttpTrade)14 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)13 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)12