Search in sources :

Example 86 with FullHttpRequest

use of io.netty.handler.codec.http.FullHttpRequest in project spring-framework by spring-projects.

the class Netty4ClientHttpRequest method createFullHttpRequest.

private FullHttpRequest createFullHttpRequest(HttpHeaders headers) {
    io.netty.handler.codec.http.HttpMethod nettyMethod = io.netty.handler.codec.http.HttpMethod.valueOf(this.method.name());
    String authority = this.uri.getRawAuthority();
    String path = this.uri.toString().substring(this.uri.toString().indexOf(authority) + authority.length());
    FullHttpRequest nettyRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, nettyMethod, path, this.body.buffer());
    nettyRequest.headers().set(HttpHeaders.HOST, this.uri.getHost());
    nettyRequest.headers().set(HttpHeaders.CONNECTION, "close");
    for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
        nettyRequest.headers().add(entry.getKey(), entry.getValue());
    }
    if (!nettyRequest.headers().contains(HttpHeaders.CONTENT_LENGTH) && this.body.buffer().readableBytes() > 0) {
        nettyRequest.headers().set(HttpHeaders.CONTENT_LENGTH, this.body.buffer().readableBytes());
    }
    return nettyRequest;
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) List(java.util.List) Map(java.util.Map)

Example 87 with FullHttpRequest

use of io.netty.handler.codec.http.FullHttpRequest in project spring-framework by spring-projects.

the class Netty4ClientHttpRequest method executeInternal.

@Override
protected ListenableFuture<ClientHttpResponse> executeInternal(final HttpHeaders headers) throws IOException {
    final SettableListenableFuture<ClientHttpResponse> responseFuture = new SettableListenableFuture<>();
    ChannelFutureListener connectionListener = new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            if (future.isSuccess()) {
                Channel channel = future.channel();
                channel.pipeline().addLast(new RequestExecuteHandler(responseFuture));
                FullHttpRequest nettyRequest = createFullHttpRequest(headers);
                channel.writeAndFlush(nettyRequest);
            } else {
                responseFuture.setException(future.cause());
            }
        }
    };
    this.bootstrap.connect(this.uri.getHost(), getPort(this.uri)).addListener(connectionListener);
    return responseFuture;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) SettableListenableFuture(org.springframework.util.concurrent.SettableListenableFuture) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) Channel(io.netty.channel.Channel) ChannelFutureListener(io.netty.channel.ChannelFutureListener)

Example 88 with FullHttpRequest

use of io.netty.handler.codec.http.FullHttpRequest in project undertow by undertow-io.

the class HTTP2ViaUpgradeTestCase method testHttp2WithNettyClient.

@Test
public void testHttp2WithNettyClient() throws Exception {
    message = "Hello World";
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    Http2ClientInitializer initializer = new Http2ClientInitializer(Integer.MAX_VALUE);
    try {
        // Configure the client.
        Bootstrap b = new Bootstrap();
        b.group(workerGroup);
        b.channel(NioSocketChannel.class);
        b.option(ChannelOption.SO_KEEPALIVE, true);
        final int port = DefaultServer.getHostPort("default") + 1;
        final String host = DefaultServer.getHostAddress("default");
        b.remoteAddress(host, port);
        b.handler(initializer);
        // Start the client.
        Channel channel = b.connect().syncUninterruptibly().channel();
        Http2SettingsHandler http2SettingsHandler = initializer.settingsHandler();
        http2SettingsHandler.awaitSettings(5, TimeUnit.SECONDS);
        HttpResponseHandler responseHandler = initializer.responseHandler();
        int streamId = 3;
        URI hostName = URI.create("http://" + host + ':' + port);
        System.err.println("Sending request(s)...");
        // Create a simple GET request.
        final ChannelPromise promise = channel.newPromise();
        responseHandler.put(streamId, promise);
        FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, hostName.toString());
        request.headers().add(HttpHeaderNames.HOST, hostName);
        request.headers().add(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP);
        request.headers().add(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.DEFLATE);
        channel.writeAndFlush(request);
        streamId += 2;
        promise.await(10, TimeUnit.SECONDS);
        Assert.assertEquals(message, messages.poll());
        System.out.println("Finished HTTP/2 request(s)");
        // Wait until the connection is closed.
        channel.close().syncUninterruptibly();
    } finally {
        workerGroup.shutdownGracefully();
    }
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) SocketChannel(io.netty.channel.socket.SocketChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) ChannelPromise(io.netty.channel.ChannelPromise) HttpString(io.undertow.util.HttpString) URI(java.net.URI) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) EventLoopGroup(io.netty.channel.EventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.Test)

Example 89 with FullHttpRequest

use of io.netty.handler.codec.http.FullHttpRequest in project riposte by Nike-Inc.

the class RequestInfoSetterHandlerTest method doChannelRead_creates_and_sets_RequestInfo_on_state_and_RequestInfo_is_marked_as_complete_with_all_chunks_if_msg_is_FullHttpRequest.

@Test
public void doChannelRead_creates_and_sets_RequestInfo_on_state_and_RequestInfo_is_marked_as_complete_with_all_chunks_if_msg_is_FullHttpRequest() {
    // given
    FullHttpRequest msgMock = mock(FullHttpRequest.class);
    String uri = "/some/url";
    HttpHeaders headers = new DefaultHttpHeaders();
    doReturn(uri).when(msgMock).getUri();
    doReturn(headers).when(msgMock).headers();
    doReturn(headers).when(msgMock).trailingHeaders();
    doReturn(byteBufMock).when(msgMock).content();
    doReturn(false).when(byteBufMock).isReadable();
    doReturn(HttpVersion.HTTP_1_1).when(msgMock).getProtocolVersion();
    // when
    PipelineContinuationBehavior result = handler.doChannelRead(ctxMock, msgMock);
    // then
    ArgumentCaptor<RequestInfo> requestInfoArgumentCaptor = ArgumentCaptor.forClass(RequestInfo.class);
    verify(stateMock).setRequestInfo(requestInfoArgumentCaptor.capture());
    RequestInfo requestInfo = requestInfoArgumentCaptor.getValue();
    assertThat(requestInfo.getUri()).isEqualTo(uri);
    assertThat(requestInfo.isCompleteRequestWithAllChunks()).isTrue();
    assertThat(result).isEqualTo(PipelineContinuationBehavior.CONTINUE);
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) PipelineContinuationBehavior(com.nike.riposte.server.handler.base.PipelineContinuationBehavior) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) RequestInfo(com.nike.riposte.server.http.RequestInfo) Test(org.junit.Test)

Example 90 with FullHttpRequest

use of io.netty.handler.codec.http.FullHttpRequest in project riposte by Nike-Inc.

the class HttpUtilsTest method extractCookies_returns_empty_set_if_no_cookies_defined.

@Test
public void extractCookies_returns_empty_set_if_no_cookies_defined() {
    // given
    FullHttpRequest nettyRequestMock = mock(FullHttpRequest.class);
    doReturn(new DefaultHttpHeaders()).when(nettyRequestMock).headers();
    doReturn(new DefaultHttpHeaders()).when(nettyRequestMock).trailingHeaders();
    // when
    Set<Cookie> extractedCookies = HttpUtils.extractCookies(nettyRequestMock);
    // then
    assertThat(extractedCookies, notNullValue());
    assertThat(extractedCookies.isEmpty(), is(true));
}
Also used : Cookie(io.netty.handler.codec.http.cookie.Cookie) DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) Test(org.junit.Test)

Aggregations

FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)97 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)65 Test (org.junit.Test)51 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)32 AsciiString (io.netty.util.AsciiString)25 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)22 ByteBuf (io.netty.buffer.ByteBuf)19 ChannelPromise (io.netty.channel.ChannelPromise)16 HttpResponse (io.netty.handler.codec.http.HttpResponse)13 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)12 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)11 FullHttpMessage (io.netty.handler.codec.http.FullHttpMessage)10 URI (java.net.URI)10 Http2CodecUtil.getEmbeddedHttp2Exception (io.netty.handler.codec.http2.Http2CodecUtil.getEmbeddedHttp2Exception)9 Http2Runnable (io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable)9 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)8 ChannelFuture (io.netty.channel.ChannelFuture)7 Channel (io.netty.channel.Channel)6 NullDispatcher (org.elasticsearch.http.NullDispatcher)6 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)5