use of io.netty.handler.codec.http.DefaultFullHttpRequest in project elasticsearch by elastic.
the class Netty4HttpClient method get.
public Collection<FullHttpResponse> get(SocketAddress remoteAddress, String... uris) throws InterruptedException {
Collection<HttpRequest> requests = new ArrayList<>(uris.length);
for (int i = 0; i < uris.length; i++) {
final HttpRequest httpRequest = new DefaultFullHttpRequest(HTTP_1_1, HttpMethod.GET, uris[i]);
httpRequest.headers().add(HOST, "localhost");
httpRequest.headers().add("X-Opaque-ID", String.valueOf(i));
requests.add(httpRequest);
}
return sendRequests(remoteAddress, requests);
}
use of io.netty.handler.codec.http.DefaultFullHttpRequest in project elasticsearch by elastic.
the class Netty4HttpChannelTests method testHeadersSet.
public void testHeadersSet() {
Settings settings = Settings.builder().build();
try (Netty4HttpServerTransport httpServerTransport = new Netty4HttpServerTransport(settings, networkService, bigArrays, threadPool, xContentRegistry(), new NullDispatcher())) {
httpServerTransport.start();
final FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/");
httpRequest.headers().add(HttpHeaderNames.ORIGIN, "remote");
final WriteCapturingChannel writeCapturingChannel = new WriteCapturingChannel();
Netty4HttpRequest request = new Netty4HttpRequest(xContentRegistry(), httpRequest, writeCapturingChannel);
// send a response
Netty4HttpChannel channel = new Netty4HttpChannel(httpServerTransport, request, null, randomBoolean(), threadPool.getThreadContext());
TestResponse resp = new TestResponse();
final String customHeader = "custom-header";
final String customHeaderValue = "xyz";
resp.addHeader(customHeader, customHeaderValue);
channel.sendResponse(resp);
// inspect what was written
List<Object> writtenObjects = writeCapturingChannel.getWrittenObjects();
assertThat(writtenObjects.size(), is(1));
HttpResponse response = (HttpResponse) writtenObjects.get(0);
assertThat(response.headers().get("non-existent-header"), nullValue());
assertThat(response.headers().get(customHeader), equalTo(customHeaderValue));
assertThat(response.headers().get(HttpHeaderNames.CONTENT_LENGTH), equalTo(Integer.toString(resp.content().length())));
assertThat(response.headers().get(HttpHeaderNames.CONTENT_TYPE), equalTo(resp.contentType()));
}
}
use of io.netty.handler.codec.http.DefaultFullHttpRequest in project elasticsearch by elastic.
the class Netty4HttpServerTransportTests method runExpectHeaderTest.
private void runExpectHeaderTest(final Settings settings, final String expectation, final int contentLength, final HttpResponseStatus expectedStatus) throws InterruptedException {
final HttpServerTransport.Dispatcher dispatcher = new HttpServerTransport.Dispatcher() {
@Override
public void dispatchRequest(RestRequest request, RestChannel channel, ThreadContext threadContext) {
channel.sendResponse(new BytesRestResponse(OK, BytesRestResponse.TEXT_CONTENT_TYPE, new BytesArray("done")));
}
@Override
public void dispatchBadRequest(RestRequest request, RestChannel channel, ThreadContext threadContext, Throwable cause) {
throw new AssertionError();
}
};
try (Netty4HttpServerTransport transport = new Netty4HttpServerTransport(settings, networkService, bigArrays, threadPool, xContentRegistry(), dispatcher)) {
transport.start();
final TransportAddress remoteAddress = randomFrom(transport.boundAddress().boundAddresses());
try (Netty4HttpClient client = new Netty4HttpClient()) {
final FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
request.headers().set(HttpHeaderNames.EXPECT, expectation);
HttpUtil.setContentLength(request, contentLength);
final FullHttpResponse response = client.post(remoteAddress.address(), request);
assertThat(response.status(), equalTo(expectedStatus));
if (expectedStatus.equals(HttpResponseStatus.CONTINUE)) {
final FullHttpRequest continuationRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/", Unpooled.EMPTY_BUFFER);
final FullHttpResponse continuationResponse = client.post(remoteAddress.address(), continuationRequest);
assertThat(continuationResponse.status(), is(HttpResponseStatus.OK));
assertThat(new String(ByteBufUtil.getBytes(continuationResponse.content()), StandardCharsets.UTF_8), is("done"));
}
}
}
}
use of io.netty.handler.codec.http.DefaultFullHttpRequest in project rest.li by linkedin.
the class NettyRequestAdapter method toNettyRequest.
/**
* Adapts a RestRequest to Netty's HttpRequest
* @param request R2 rest request
* @return Adapted HttpRequest.
*/
static HttpRequest toNettyRequest(RestRequest request) throws Exception {
HttpMethod nettyMethod = HttpMethod.valueOf(request.getMethod());
URL url = new URL(request.getURI().toString());
String path = url.getFile();
// it MUST be given as "/" (the server root).
if (path.isEmpty()) {
path = "/";
}
ByteBuf content = Unpooled.wrappedBuffer(request.getEntity().asByteBuffer());
HttpRequest nettyRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, nettyMethod, path, content);
nettyRequest.headers().set(HttpConstants.CONTENT_LENGTH, request.getEntity().length());
for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) {
nettyRequest.headers().set(entry.getKey(), entry.getValue());
}
nettyRequest.headers().set(HttpHeaderNames.HOST, url.getAuthority());
nettyRequest.headers().set(HttpConstants.REQUEST_COOKIE_HEADER_NAME, request.getCookies());
return nettyRequest;
}
use of io.netty.handler.codec.http.DefaultFullHttpRequest in project netty by netty.
the class SpdyHttpDecoder method createHttpRequest.
private static FullHttpRequest createHttpRequest(SpdyHeadersFrame requestFrame, ByteBufAllocator alloc) throws Exception {
// Create the first line of the request from the name/value pairs
SpdyHeaders headers = requestFrame.headers();
HttpMethod method = HttpMethod.valueOf(headers.getAsString(METHOD));
String url = headers.getAsString(PATH);
HttpVersion httpVersion = HttpVersion.valueOf(headers.getAsString(VERSION));
headers.remove(METHOD);
headers.remove(PATH);
headers.remove(VERSION);
boolean release = true;
ByteBuf buffer = alloc.buffer();
try {
FullHttpRequest req = new DefaultFullHttpRequest(httpVersion, method, url, buffer);
// Remove the scheme header
headers.remove(SCHEME);
// Replace the SPDY host header with the HTTP host header
CharSequence host = headers.get(HOST);
headers.remove(HOST);
req.headers().set(HttpHeaderNames.HOST, host);
for (Map.Entry<CharSequence, CharSequence> e : requestFrame.headers()) {
req.headers().add(e.getKey(), e.getValue());
}
// The Connection and Keep-Alive headers are no longer valid
HttpUtil.setKeepAlive(req, true);
// Transfer-Encoding header is not valid
req.headers().remove(HttpHeaderNames.TRANSFER_ENCODING);
release = false;
return req;
} finally {
if (release) {
buffer.release();
}
}
}
Aggregations