Search in sources :

Example 6 with FullHttpResponse

use of io.netty.handler.codec.http.FullHttpResponse in project elasticsearch by elastic.

the class Netty4HttpChannelTests method executeRequest.

private FullHttpResponse executeRequest(final Settings settings, final String originValue, final String host) {
    // construct request and send it over the transport layer
    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, "/");
        if (originValue != null) {
            httpRequest.headers().add(HttpHeaderNames.ORIGIN, originValue);
        }
        httpRequest.headers().add(HttpHeaderNames.HOST, host);
        final WriteCapturingChannel writeCapturingChannel = new WriteCapturingChannel();
        final Netty4HttpRequest request = new Netty4HttpRequest(xContentRegistry(), httpRequest, writeCapturingChannel);
        Netty4HttpChannel channel = new Netty4HttpChannel(httpServerTransport, request, null, randomBoolean(), threadPool.getThreadContext());
        channel.sendResponse(new TestResponse());
        // get the response
        List<Object> writtenObjects = writeCapturingChannel.getWrittenObjects();
        assertThat(writtenObjects.size(), is(1));
        return (FullHttpResponse) writtenObjects.get(0);
    }
}
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) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse)

Example 7 with FullHttpResponse

use of io.netty.handler.codec.http.FullHttpResponse in project elasticsearch by elastic.

the class Netty4HttpServerTransportTests method testBadRequest.

public void testBadRequest() throws InterruptedException {
    final AtomicReference<Throwable> causeReference = new AtomicReference<>();
    final HttpServerTransport.Dispatcher dispatcher = new HttpServerTransport.Dispatcher() {

        @Override
        public void dispatchRequest(final RestRequest request, final RestChannel channel, final ThreadContext threadContext) {
            throw new AssertionError();
        }

        @Override
        public void dispatchBadRequest(final RestRequest request, final RestChannel channel, final ThreadContext threadContext, final Throwable cause) {
            causeReference.set(cause);
            try {
                final ElasticsearchException e = new ElasticsearchException("you sent a bad request and you should feel bad");
                channel.sendResponse(new BytesRestResponse(channel, BAD_REQUEST, e));
            } catch (final IOException e) {
                throw new AssertionError(e);
            }
        }
    };
    final Settings settings;
    final int maxInitialLineLength;
    final Setting<ByteSizeValue> httpMaxInitialLineLengthSetting = HttpTransportSettings.SETTING_HTTP_MAX_INITIAL_LINE_LENGTH;
    if (randomBoolean()) {
        maxInitialLineLength = httpMaxInitialLineLengthSetting.getDefault(Settings.EMPTY).bytesAsInt();
        settings = Settings.EMPTY;
    } else {
        maxInitialLineLength = randomIntBetween(1, 8192);
        settings = Settings.builder().put(httpMaxInitialLineLengthSetting.getKey(), maxInitialLineLength + "b").build();
    }
    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 String url = "/" + new String(new byte[maxInitialLineLength], Charset.forName("UTF-8"));
            final FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, url);
            final FullHttpResponse response = client.post(remoteAddress.address(), request);
            assertThat(response.status(), equalTo(HttpResponseStatus.BAD_REQUEST));
            assertThat(new String(response.content().array(), Charset.forName("UTF-8")), containsString("you sent a bad request and you should feel bad"));
        }
    }
    assertNotNull(causeReference.get());
    assertThat(causeReference.get(), instanceOf(TooLongFrameException.class));
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) TooLongFrameException(io.netty.handler.codec.TooLongFrameException) TransportAddress(org.elasticsearch.common.transport.TransportAddress) ByteSizeValue(org.elasticsearch.common.unit.ByteSizeValue) ElasticsearchException(org.elasticsearch.ElasticsearchException) Matchers.containsString(org.hamcrest.Matchers.containsString) Strings.collectionToDelimitedString(org.elasticsearch.common.Strings.collectionToDelimitedString) NullDispatcher(org.elasticsearch.http.NullDispatcher) HttpServerTransport(org.elasticsearch.http.HttpServerTransport) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Settings(org.elasticsearch.common.settings.Settings) HttpTransportSettings(org.elasticsearch.http.HttpTransportSettings) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) ThreadContext(org.elasticsearch.common.util.concurrent.ThreadContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) RestChannel(org.elasticsearch.rest.RestChannel) IOException(java.io.IOException) RestRequest(org.elasticsearch.rest.RestRequest)

Example 8 with FullHttpResponse

use of io.netty.handler.codec.http.FullHttpResponse in project elasticsearch by elastic.

the class Netty4PipeliningDisabledIT method testThatNettyHttpServerDoesNotSupportPipelining.

public void testThatNettyHttpServerDoesNotSupportPipelining() throws Exception {
    ensureGreen();
    String[] requests = new String[] { "/", "/_nodes/stats", "/", "/_cluster/state", "/", "/_nodes", "/" };
    HttpServerTransport httpServerTransport = internalCluster().getInstance(HttpServerTransport.class);
    TransportAddress[] boundAddresses = httpServerTransport.boundAddress().boundAddresses();
    TransportAddress transportAddress = (TransportAddress) randomFrom(boundAddresses);
    try (Netty4HttpClient nettyHttpClient = new Netty4HttpClient()) {
        Collection<FullHttpResponse> responses = nettyHttpClient.get(transportAddress.address(), requests);
        assertThat(responses, hasSize(requests.length));
        List<String> opaqueIds = new ArrayList<>(Netty4HttpClient.returnOpaqueIds(responses));
        assertResponsesOutOfOrder(opaqueIds);
    }
}
Also used : TransportAddress(org.elasticsearch.common.transport.TransportAddress) ArrayList(java.util.ArrayList) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) HttpServerTransport(org.elasticsearch.http.HttpServerTransport)

Example 9 with FullHttpResponse

use of io.netty.handler.codec.http.FullHttpResponse in project elasticsearch by elastic.

the class Netty4PipeliningEnabledIT method testThatNettyHttpServerSupportsPipelining.

public void testThatNettyHttpServerSupportsPipelining() throws Exception {
    String[] requests = new String[] { "/", "/_nodes/stats", "/", "/_cluster/state", "/" };
    HttpServerTransport httpServerTransport = internalCluster().getInstance(HttpServerTransport.class);
    TransportAddress[] boundAddresses = httpServerTransport.boundAddress().boundAddresses();
    TransportAddress transportAddress = (TransportAddress) randomFrom(boundAddresses);
    try (Netty4HttpClient nettyHttpClient = new Netty4HttpClient()) {
        Collection<FullHttpResponse> responses = nettyHttpClient.get(transportAddress.address(), requests);
        assertThat(responses, hasSize(5));
        Collection<String> opaqueIds = Netty4HttpClient.returnOpaqueIds(responses);
        assertOpaqueIdsInOrder(opaqueIds);
    }
}
Also used : TransportAddress(org.elasticsearch.common.transport.TransportAddress) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) HttpServerTransport(org.elasticsearch.http.HttpServerTransport)

Example 10 with FullHttpResponse

use of io.netty.handler.codec.http.FullHttpResponse in project elasticsearch by elastic.

the class Netty4HttpClient method sendRequests.

private synchronized Collection<FullHttpResponse> sendRequests(final SocketAddress remoteAddress, final Collection<HttpRequest> requests) throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(requests.size());
    final Collection<FullHttpResponse> content = Collections.synchronizedList(new ArrayList<>(requests.size()));
    clientBootstrap.handler(new CountDownLatchHandler(latch, content));
    ChannelFuture channelFuture = null;
    try {
        channelFuture = clientBootstrap.connect(remoteAddress);
        channelFuture.sync();
        for (HttpRequest request : requests) {
            channelFuture.channel().writeAndFlush(request);
        }
        latch.await(10, TimeUnit.SECONDS);
    } finally {
        if (channelFuture != null) {
            channelFuture.channel().close().sync();
        }
    }
    return content;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)83 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)50 ByteBuf (io.netty.buffer.ByteBuf)24 Test (org.junit.Test)16 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)13 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)11 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)11 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)11 TransportAddress (org.elasticsearch.common.transport.TransportAddress)8 HttpServerTransport (org.elasticsearch.http.HttpServerTransport)8 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)7 HttpRequest (io.netty.handler.codec.http.HttpRequest)6 ChannelFuture (io.netty.channel.ChannelFuture)5 ChannelPipeline (io.netty.channel.ChannelPipeline)5 HttpClientCodec (io.netty.handler.codec.http.HttpClientCodec)5 HashMap (java.util.HashMap)5 Channel (com.weibo.api.motan.transport.Channel)4 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)4 Map (java.util.Map)4 Pair (com.nike.internal.util.Pair)3