Search in sources :

Example 1 with HttpResponse

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

the class Netty4CorsHandler method handlePreflight.

private void handlePreflight(final ChannelHandlerContext ctx, final HttpRequest request) {
    final HttpResponse response = new DefaultFullHttpResponse(request.protocolVersion(), HttpResponseStatus.OK, true, true);
    if (setOrigin(response)) {
        setAllowMethods(response);
        setAllowHeaders(response);
        setAllowCredentials(response);
        setMaxAge(response);
        setPreflightHeaders(response);
        ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
    } else {
        forbidden(ctx, request);
    }
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse)

Example 2 with HttpResponse

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

the class Netty4HttpChannelTests method testCorsEnabledWithAllowOrigins.

public void testCorsEnabledWithAllowOrigins() {
    final String originValue = "remote-host";
    // create a http transport with CORS enabled and allow origin configured
    Settings settings = Settings.builder().put(SETTING_CORS_ENABLED.getKey(), true).put(SETTING_CORS_ALLOW_ORIGIN.getKey(), originValue).build();
    HttpResponse response = executeRequest(settings, originValue, "request-host");
    // inspect response and validate
    assertThat(response.headers().get(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN), notNullValue());
    String allowedOrigins = response.headers().get(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN);
    assertThat(allowedOrigins, is(originValue));
}
Also used : 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 3 with HttpResponse

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

the class Netty4HttpChannelTests method testCorsEnabledWithoutAllowOrigins.

public void testCorsEnabledWithoutAllowOrigins() {
    // Set up a HTTP transport with only the CORS enabled setting
    Settings settings = Settings.builder().put(HttpTransportSettings.SETTING_CORS_ENABLED.getKey(), true).build();
    HttpResponse response = executeRequest(settings, "remote-host", "request-host");
    // inspect response and validate
    assertThat(response.headers().get(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN), nullValue());
}
Also used : 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 4 with HttpResponse

use of io.netty.handler.codec.http.HttpResponse in project jersey by jersey.

the class JerseyClientHandler method channelRead0.

@Override
public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) {
    if (msg instanceof HttpResponse) {
        final HttpResponse response = (HttpResponse) msg;
        final ClientResponse jerseyResponse = new ClientResponse(new Response.StatusType() {

            @Override
            public int getStatusCode() {
                return response.status().code();
            }

            @Override
            public Response.Status.Family getFamily() {
                return Response.Status.Family.familyOf(response.status().code());
            }

            @Override
            public String getReasonPhrase() {
                return response.status().reasonPhrase();
            }
        }, jerseyRequest);
        for (Map.Entry<String, String> entry : response.headers().entries()) {
            jerseyResponse.getHeaders().add(entry.getKey(), entry.getValue());
        }
        // request entity handling.
        if ((response.headers().contains(HttpHeaderNames.CONTENT_LENGTH) && HttpUtil.getContentLength(response) > 0) || HttpUtil.isTransferEncodingChunked(response)) {
            ctx.channel().closeFuture().addListener(new GenericFutureListener<Future<? super Void>>() {

                @Override
                public void operationComplete(Future<? super Void> future) throws Exception {
                    isList.add(NettyInputStream.END_OF_INPUT_ERROR);
                }
            });
            jerseyResponse.setEntityStream(new NettyInputStream(isList));
        } else {
            jerseyResponse.setEntityStream(new InputStream() {

                @Override
                public int read() throws IOException {
                    return -1;
                }
            });
        }
        if (asyncConnectorCallback != null) {
            connector.executorService.execute(new Runnable() {

                @Override
                public void run() {
                    asyncConnectorCallback.response(jerseyResponse);
                    future.complete(jerseyResponse);
                }
            });
        }
    }
    if (msg instanceof HttpContent) {
        HttpContent httpContent = (HttpContent) msg;
        ByteBuf content = httpContent.content();
        if (content.isReadable()) {
            // copy bytes - when netty reads last chunk, it automatically closes the channel, which invalidates all
            // relates ByteBuffs.
            byte[] bytes = new byte[content.readableBytes()];
            content.getBytes(content.readerIndex(), bytes);
            isList.add(new ByteArrayInputStream(bytes));
        }
        if (msg instanceof LastHttpContent) {
            isList.add(NettyInputStream.END_OF_INPUT);
        }
    }
}
Also used : ClientResponse(org.glassfish.jersey.client.ClientResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) NettyInputStream(org.glassfish.jersey.netty.connector.internal.NettyInputStream) InputStream(java.io.InputStream) HttpResponse(io.netty.handler.codec.http.HttpResponse) IOException(java.io.IOException) NettyInputStream(org.glassfish.jersey.netty.connector.internal.NettyInputStream) ByteBuf(io.netty.buffer.ByteBuf) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) IOException(java.io.IOException) ClientResponse(org.glassfish.jersey.client.ClientResponse) Response(javax.ws.rs.core.Response) HttpResponse(io.netty.handler.codec.http.HttpResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) CompletableFuture(java.util.concurrent.CompletableFuture) Future(io.netty.util.concurrent.Future) Map(java.util.Map) HttpContent(io.netty.handler.codec.http.HttpContent) LastHttpContent(io.netty.handler.codec.http.LastHttpContent)

Example 5 with HttpResponse

use of io.netty.handler.codec.http.HttpResponse in project flink by apache.

the class HandlerRedirectUtils method getUnavailableResponse.

public static HttpResponse getUnavailableResponse() {
    String result = "Service temporarily unavailable due to an ongoing leader election. Please refresh.";
    byte[] bytes = result.getBytes(ConfigConstants.DEFAULT_CHARSET);
    HttpResponse unavailableResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.SERVICE_UNAVAILABLE, Unpooled.wrappedBuffer(bytes));
    unavailableResponse.headers().set(HttpHeaders.Names.CONTENT_LENGTH, bytes.length);
    unavailableResponse.headers().set(HttpHeaders.Names.CONTENT_TYPE, MimeTypes.getMimeTypeForExtension("txt"));
    return unavailableResponse;
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse)

Aggregations

HttpResponse (io.netty.handler.codec.http.HttpResponse)230 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)103 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)56 Test (org.junit.jupiter.api.Test)56 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)54 HttpRequest (io.netty.handler.codec.http.HttpRequest)52 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)47 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)40 Test (org.junit.Test)39 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)38 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)35 HttpContent (io.netty.handler.codec.http.HttpContent)32 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)25 ByteBuf (io.netty.buffer.ByteBuf)18 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)17 ChannelFuture (io.netty.channel.ChannelFuture)16 UtilsTest (com.github.ambry.utils.UtilsTest)15 IOException (java.io.IOException)15 Map (java.util.Map)15 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)13