Search in sources :

Example 1 with NullDispatcher

use of org.elasticsearch.http.NullDispatcher in project elasticsearch by elastic.

the class Netty4HttpChannelTests method testConnectionClose.

public void testConnectionClose() throws Exception {
    final Settings settings = Settings.builder().build();
    try (Netty4HttpServerTransport httpServerTransport = new Netty4HttpServerTransport(settings, networkService, bigArrays, threadPool, xContentRegistry(), new NullDispatcher())) {
        httpServerTransport.start();
        final FullHttpRequest httpRequest;
        final boolean close = randomBoolean();
        if (randomBoolean()) {
            httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/");
            if (close) {
                httpRequest.headers().add(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
            }
        } else {
            httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, "/");
            if (!close) {
                httpRequest.headers().add(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
            }
        }
        final EmbeddedChannel embeddedChannel = new EmbeddedChannel();
        final Netty4HttpRequest request = new Netty4HttpRequest(xContentRegistry(), httpRequest, embeddedChannel);
        // send a response, the channel close status should match
        assertTrue(embeddedChannel.isOpen());
        final Netty4HttpChannel channel = new Netty4HttpChannel(httpServerTransport, request, null, randomBoolean(), threadPool.getThreadContext());
        final TestResponse resp = new TestResponse();
        channel.sendResponse(resp);
        assertThat(embeddedChannel.isOpen(), equalTo(!close));
    }
}
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) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Settings(org.elasticsearch.common.settings.Settings) HttpTransportSettings(org.elasticsearch.http.HttpTransportSettings)

Example 2 with NullDispatcher

use of org.elasticsearch.http.NullDispatcher 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 3 with NullDispatcher

use of org.elasticsearch.http.NullDispatcher in project elasticsearch by elastic.

the class Netty4HttpServerTransportTests method testBindUnavailableAddress.

public void testBindUnavailableAddress() {
    try (Netty4HttpServerTransport transport = new Netty4HttpServerTransport(Settings.EMPTY, networkService, bigArrays, threadPool, xContentRegistry(), new NullDispatcher())) {
        transport.start();
        TransportAddress remoteAddress = randomFrom(transport.boundAddress().boundAddresses());
        Settings settings = Settings.builder().put("http.port", remoteAddress.getPort()).build();
        try (Netty4HttpServerTransport otherTransport = new Netty4HttpServerTransport(settings, networkService, bigArrays, threadPool, xContentRegistry(), new NullDispatcher())) {
            BindHttpException bindHttpException = expectThrows(BindHttpException.class, () -> otherTransport.start());
            assertEquals("Failed to bind to [" + remoteAddress.getPort() + "]", bindHttpException.getMessage());
        }
    }
}
Also used : NullDispatcher(org.elasticsearch.http.NullDispatcher) TransportAddress(org.elasticsearch.common.transport.TransportAddress) BindHttpException(org.elasticsearch.http.BindHttpException) Settings(org.elasticsearch.common.settings.Settings) HttpTransportSettings(org.elasticsearch.http.HttpTransportSettings)

Example 4 with NullDispatcher

use of org.elasticsearch.http.NullDispatcher in project elasticsearch by elastic.

the class Netty4HttpChannelTests method testReleaseOnSendToClosedChannel.

public void testReleaseOnSendToClosedChannel() {
    final Settings settings = Settings.builder().build();
    final NamedXContentRegistry registry = xContentRegistry();
    try (Netty4HttpServerTransport httpServerTransport = new Netty4HttpServerTransport(settings, networkService, bigArrays, threadPool, registry, new NullDispatcher())) {
        final FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/");
        final EmbeddedChannel embeddedChannel = new EmbeddedChannel();
        final Netty4HttpRequest request = new Netty4HttpRequest(registry, httpRequest, embeddedChannel);
        final HttpPipelinedRequest pipelinedRequest = randomBoolean() ? new HttpPipelinedRequest(request.request(), 1) : null;
        final Netty4HttpChannel channel = new Netty4HttpChannel(httpServerTransport, request, pipelinedRequest, randomBoolean(), threadPool.getThreadContext());
        final TestResponse response = new TestResponse(bigArrays);
        assertThat(response.content(), instanceOf(Releasable.class));
        embeddedChannel.close();
        channel.sendResponse(response);
    // ESTestCase#after will invoke ensureAllArraysAreReleased which will fail if the response content was not released
    }
}
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) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) HttpPipelinedRequest(org.elasticsearch.http.netty4.pipelining.HttpPipelinedRequest) Releasable(org.elasticsearch.common.lease.Releasable) NamedXContentRegistry(org.elasticsearch.common.xcontent.NamedXContentRegistry) Settings(org.elasticsearch.common.settings.Settings) HttpTransportSettings(org.elasticsearch.http.HttpTransportSettings)

Example 5 with NullDispatcher

use of org.elasticsearch.http.NullDispatcher 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)

Aggregations

NullDispatcher (org.elasticsearch.http.NullDispatcher)5 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)4 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)4 Settings (org.elasticsearch.common.settings.Settings)4 HttpTransportSettings (org.elasticsearch.http.HttpTransportSettings)4 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)2 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)2 HttpResponse (io.netty.handler.codec.http.HttpResponse)1 Releasable (org.elasticsearch.common.lease.Releasable)1 TransportAddress (org.elasticsearch.common.transport.TransportAddress)1 NamedXContentRegistry (org.elasticsearch.common.xcontent.NamedXContentRegistry)1 BindHttpException (org.elasticsearch.http.BindHttpException)1 HttpPipelinedRequest (org.elasticsearch.http.netty4.pipelining.HttpPipelinedRequest)1