Search in sources :

Example 6 with FullHttpResponse

use of org.apache.flink.shaded.netty4.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)

Example 7 with FullHttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project elasticsearch by elastic.

the class Netty4HttpRequestSizeLimitIT method testLimitsInFlightRequests.

public void testLimitsInFlightRequests() throws Exception {
    ensureGreen();
    // we use the limit size as a (very) rough indication on how many requests we should sent to hit the limit
    int numRequests = LIMIT.bytesAsInt() / 100;
    StringBuilder bulkRequest = new StringBuilder();
    for (int i = 0; i < numRequests; i++) {
        bulkRequest.append("{\"index\": {}}");
        bulkRequest.append(System.lineSeparator());
        bulkRequest.append("{ \"field\" : \"value\" }");
        bulkRequest.append(System.lineSeparator());
    }
    @SuppressWarnings("unchecked") Tuple<String, CharSequence>[] requests = new Tuple[150];
    for (int i = 0; i < requests.length; i++) {
        requests[i] = Tuple.tuple("/index/type/_bulk", bulkRequest);
    }
    HttpServerTransport httpServerTransport = internalCluster().getInstance(HttpServerTransport.class);
    TransportAddress transportAddress = (TransportAddress) randomFrom(httpServerTransport.boundAddress().boundAddresses());
    try (Netty4HttpClient nettyHttpClient = new Netty4HttpClient()) {
        Collection<FullHttpResponse> singleResponse = nettyHttpClient.post(transportAddress.address(), requests[0]);
        assertThat(singleResponse, hasSize(1));
        assertAtLeastOnceExpectedStatus(singleResponse, HttpResponseStatus.OK);
        Collection<FullHttpResponse> multipleResponses = nettyHttpClient.post(transportAddress.address(), requests);
        assertThat(multipleResponses, hasSize(requests.length));
        assertAtLeastOnceExpectedStatus(multipleResponses, HttpResponseStatus.SERVICE_UNAVAILABLE);
    }
}
Also used : TransportAddress(org.elasticsearch.common.transport.TransportAddress) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) HttpServerTransport(org.elasticsearch.http.HttpServerTransport) Tuple(org.elasticsearch.common.collect.Tuple)

Example 8 with FullHttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project elasticsearch by elastic.

the class Netty4HttpServerPipeliningTests method testThatHttpPipeliningWorksWhenEnabled.

public void testThatHttpPipeliningWorksWhenEnabled() throws Exception {
    final Settings settings = Settings.builder().put("http.pipelining", true).put("http.port", "0").build();
    try (HttpServerTransport httpServerTransport = new CustomNettyHttpServerTransport(settings)) {
        httpServerTransport.start();
        final TransportAddress transportAddress = randomFrom(httpServerTransport.boundAddress().boundAddresses());
        final int numberOfRequests = randomIntBetween(4, 16);
        final List<String> requests = new ArrayList<>(numberOfRequests);
        for (int i = 0; i < numberOfRequests; i++) {
            if (rarely()) {
                requests.add("/slow/" + i);
            } else {
                requests.add("/" + i);
            }
        }
        try (Netty4HttpClient nettyHttpClient = new Netty4HttpClient()) {
            Collection<FullHttpResponse> responses = nettyHttpClient.get(transportAddress.address(), requests.toArray(new String[] {}));
            Collection<String> responseBodies = Netty4HttpClient.returnHttpResponseBodies(responses);
            assertThat(responseBodies, contains(requests.toArray()));
        }
    }
}
Also used : TransportAddress(org.elasticsearch.common.transport.TransportAddress) ArrayList(java.util.ArrayList) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpServerTransport(org.elasticsearch.http.HttpServerTransport) Settings(org.elasticsearch.common.settings.Settings)

Example 9 with FullHttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project vert.x by eclipse.

the class HttpServerImpl method sendError.

private void sendError(CharSequence err, HttpResponseStatus status, Channel ch) {
    FullHttpResponse resp = new DefaultFullHttpResponse(HTTP_1_1, status);
    if (status.code() == METHOD_NOT_ALLOWED.code()) {
        // SockJS requires this
        resp.headers().set(io.vertx.core.http.HttpHeaders.ALLOW, io.vertx.core.http.HttpHeaders.GET);
    }
    if (err != null) {
        resp.content().writeBytes(err.toString().getBytes(CharsetUtil.UTF_8));
        HttpHeaders.setContentLength(resp, err.length());
    } else {
        HttpHeaders.setContentLength(resp, 0);
    }
    ch.writeAndFlush(resp);
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse)

Example 10 with FullHttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project flink by apache.

the class RuntimeMonitorHandler method respondAsLeader.

@Override
protected void respondAsLeader(ChannelHandlerContext ctx, Routed routed, ActorGateway jobManager) {
    FullHttpResponse response;
    try {
        // we only pass the first element in the list to the handlers.
        Map<String, String> queryParams = new HashMap<>();
        for (String key : routed.queryParams().keySet()) {
            queryParams.put(key, routed.queryParam(key));
        }
        Map<String, String> pathParams = new HashMap<>(routed.pathParams().size());
        for (String key : routed.pathParams().keySet()) {
            pathParams.put(key, URLDecoder.decode(routed.pathParams().get(key), ENCODING.toString()));
        }
        InetSocketAddress address = (InetSocketAddress) ctx.channel().localAddress();
        queryParams.put(WEB_MONITOR_ADDRESS_KEY, (httpsEnabled ? "https://" : "http://") + address.getHostName() + ":" + address.getPort());
        response = handler.handleRequest(pathParams, queryParams, jobManager);
    } catch (NotFoundException e) {
        // this should result in a 404 error code (not found)
        ByteBuf message = e.getMessage() == null ? Unpooled.buffer(0) : Unpooled.wrappedBuffer(e.getMessage().getBytes(ENCODING));
        response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND, message);
        response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
        response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
        LOG.debug("Error while handling request", e);
    } catch (Exception e) {
        byte[] bytes = ExceptionUtils.stringifyException(e).getBytes(ENCODING);
        response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR, Unpooled.wrappedBuffer(bytes));
        response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
        response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
        LOG.debug("Error while handling request", e);
    }
    response.headers().set(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_ORIGIN, "*");
    // Content-Encoding:utf-8
    response.headers().set(HttpHeaders.Names.CONTENT_ENCODING, ENCODING.name());
    KeepAliveWrite.flush(ctx, routed.request(), response);
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)260 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)174 ByteBuf (io.netty.buffer.ByteBuf)53 Test (org.junit.Test)50 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)38 HttpRequest (io.netty.handler.codec.http.HttpRequest)34 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)24 HttpObject (io.netty.handler.codec.http.HttpObject)23 IOException (java.io.IOException)23 HttpTrade (org.jocean.http.server.HttpServerBuilder.HttpTrade)23 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)22 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)21 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)21 Test (org.junit.jupiter.api.Test)21 ChannelFuture (io.netty.channel.ChannelFuture)20 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)20 HttpResponse (io.netty.handler.codec.http.HttpResponse)20 HttpInitiator (org.jocean.http.client.HttpClient.HttpInitiator)20 Subscription (rx.Subscription)20 LocalAddress (io.netty.channel.local.LocalAddress)19