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;
}
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;
}
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();
}
}
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);
}
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));
}
Aggregations