Search in sources :

Example 46 with DefaultFullHttpRequest

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);
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) ArrayList(java.util.ArrayList)

Example 47 with DefaultFullHttpRequest

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()));
    }
}
Also used : NullDispatcher(org.elasticsearch.http.NullDispatcher) 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) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Settings(org.elasticsearch.common.settings.Settings) HttpTransportSettings(org.elasticsearch.http.HttpTransportSettings)

Example 48 with DefaultFullHttpRequest

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"));
            }
        }
    }
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) TransportAddress(org.elasticsearch.common.transport.TransportAddress) ThreadContext(org.elasticsearch.common.util.concurrent.ThreadContext) RestChannel(org.elasticsearch.rest.RestChannel) Matchers.containsString(org.hamcrest.Matchers.containsString) Strings.collectionToDelimitedString(org.elasticsearch.common.Strings.collectionToDelimitedString) NullDispatcher(org.elasticsearch.http.NullDispatcher) HttpServerTransport(org.elasticsearch.http.HttpServerTransport) RestRequest(org.elasticsearch.rest.RestRequest) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse)

Example 49 with DefaultFullHttpRequest

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;
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) AsciiString(io.netty.util.AsciiString) ByteBuf(io.netty.buffer.ByteBuf) Map(java.util.Map) HttpMethod(io.netty.handler.codec.http.HttpMethod) URL(java.net.URL)

Example 50 with DefaultFullHttpRequest

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();
        }
    }
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) ByteBuf(io.netty.buffer.ByteBuf) HttpVersion(io.netty.handler.codec.http.HttpVersion) HashMap(java.util.HashMap) Map(java.util.Map) HttpMethod(io.netty.handler.codec.http.HttpMethod)

Aggregations

DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)92 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)53 Test (org.junit.Test)53 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)31 AsciiString (io.netty.util.AsciiString)28 ByteBuf (io.netty.buffer.ByteBuf)27 HttpRequest (io.netty.handler.codec.http.HttpRequest)22 ChannelPromise (io.netty.channel.ChannelPromise)15 FullHttpMessage (io.netty.handler.codec.http.FullHttpMessage)11 Http2CodecUtil.getEmbeddedHttp2Exception (io.netty.handler.codec.http2.Http2CodecUtil.getEmbeddedHttp2Exception)11 Http2Runnable (io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable)11 URI (java.net.URI)9 Channel (io.netty.channel.Channel)8 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)8 Bootstrap (io.netty.bootstrap.Bootstrap)7 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)7 File (java.io.File)7 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)6 Map (java.util.Map)6 NullDispatcher (org.elasticsearch.http.NullDispatcher)6