Search in sources :

Example 6 with HttpRequestMessage

use of com.netflix.zuul.message.http.HttpRequestMessage in project zuul by Netflix.

the class OriginResponseReceiver method writeInternal.

private void writeInternal(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    if (!ctx.channel().isActive()) {
        ReferenceCountUtil.release(msg);
        return;
    }
    if (msg instanceof HttpRequestMessage) {
        promise.addListener((future) -> {
            if (!future.isSuccess()) {
                Throwable cause = ctx.channel().attr(SSL_HANDSHAKE_UNSUCCESS_FROM_ORIGIN_THROWABLE).get();
                if (cause != null) {
                    // Set the specific SSL handshake error if the handlers have already caught them
                    ctx.channel().attr(SSL_HANDSHAKE_UNSUCCESS_FROM_ORIGIN_THROWABLE).set(null);
                    fireWriteError("request headers", cause, ctx);
                    LOG.debug("SSLException is overridden by SSLHandshakeException caught in handler level. Original SSL exception message: ", future.cause());
                } else {
                    fireWriteError("request headers", future.cause(), ctx);
                }
            }
        });
        HttpRequestMessage zuulReq = (HttpRequestMessage) msg;
        preWriteHook(ctx, zuulReq);
        super.write(ctx, buildOriginHttpRequest(zuulReq), promise);
    } else if (msg instanceof HttpContent) {
        promise.addListener((future) -> {
            if (!future.isSuccess()) {
                fireWriteError("request content chunk", future.cause(), ctx);
            }
        });
        super.write(ctx, msg, promise);
    } else {
        // should never happen
        ReferenceCountUtil.release(msg);
        throw new ZuulException("Received invalid message from client", true);
    }
}
Also used : AttributeKey(io.netty.util.AttributeKey) HttpVersion(io.netty.handler.codec.http.HttpVersion) LoggerFactory(org.slf4j.LoggerFactory) READ_TIMEOUT(com.netflix.zuul.exception.OutboundErrorType.READ_TIMEOUT) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) ChannelPromise(io.netty.channel.ChannelPromise) ReadTimeoutException(io.netty.handler.timeout.ReadTimeoutException) SslHandshakeCompletionEvent(io.netty.handler.ssl.SslHandshakeCompletionEvent) HttpQueryParams(com.netflix.zuul.message.http.HttpQueryParams) TaskCloseable(io.perfmark.TaskCloseable) ProxyEndpoint(com.netflix.zuul.filters.endpoint.ProxyEndpoint) Header(com.netflix.zuul.message.Header) ChannelDuplexHandler(io.netty.channel.ChannelDuplexHandler) RESET_CONNECTION(com.netflix.zuul.exception.OutboundErrorType.RESET_CONNECTION) CompleteEvent(com.netflix.netty.common.HttpLifecycleChannelHandler.CompleteEvent) HttpContent(io.netty.handler.codec.http.HttpContent) OriginConnectException(com.netflix.zuul.netty.connectionpool.OriginConnectException) HttpRequest(io.netty.handler.codec.http.HttpRequest) IdleStateEvent(io.netty.handler.timeout.IdleStateEvent) Logger(org.slf4j.Logger) OutboundException(com.netflix.zuul.exception.OutboundException) HttpMethod(io.netty.handler.codec.http.HttpMethod) IOException(java.io.IOException) HttpRequestMessage(com.netflix.zuul.message.http.HttpRequestMessage) PerfMark(io.perfmark.PerfMark) ZuulException(com.netflix.zuul.exception.ZuulException) PassportState(com.netflix.zuul.passport.PassportState) ReferenceCountUtil(io.netty.util.ReferenceCountUtil) HttpResponse(io.netty.handler.codec.http.HttpResponse) CompleteReason(com.netflix.netty.common.HttpLifecycleChannelHandler.CompleteReason) ChannelUtils(com.netflix.zuul.netty.ChannelUtils) SESSION_COMPLETE(com.netflix.netty.common.HttpLifecycleChannelHandler.CompleteReason.SESSION_COMPLETE) HttpRequestMessage(com.netflix.zuul.message.http.HttpRequestMessage) ZuulException(com.netflix.zuul.exception.ZuulException) HttpContent(io.netty.handler.codec.http.HttpContent)

Example 7 with HttpRequestMessage

use of com.netflix.zuul.message.http.HttpRequestMessage in project zuul by Netflix.

the class ZuulEndPointRunner method filter.

@Override
public void filter(final HttpRequestMessage zuulReq) {
    if (zuulReq.getContext().isCancelled()) {
        PerfMark.event(getClass().getName(), "filterCancelled");
        zuulReq.disposeBufferedBody();
        logger.debug("Request was cancelled, UUID {}", zuulReq.getContext().getUUID());
        return;
    }
    final String endpointName = getEndPointName(zuulReq.getContext());
    try (TaskCloseable ignored = PerfMark.traceTask(this, s -> s.getClass().getSimpleName() + ".filter")) {
        Preconditions.checkNotNull(zuulReq, "input message");
        addPerfMarkTags(zuulReq);
        final ZuulFilter<HttpRequestMessage, HttpResponseMessage> endpoint = getEndpoint(endpointName, zuulReq);
        logger.debug("Got endpoint {}, UUID {}", endpoint.filterName(), zuulReq.getContext().getUUID());
        setEndpoint(zuulReq, endpoint);
        final HttpResponseMessage zuulResp = filter(endpoint, zuulReq);
        if ((zuulResp != null) && (!(endpoint instanceof ProxyEndpoint))) {
            // EdgeProxyEndpoint calls invokeNextStage internally
            logger.debug("Endpoint calling invokeNextStage, UUID {}", zuulReq.getContext().getUUID());
            invokeNextStage(zuulResp);
        }
    } catch (Exception ex) {
        handleException(zuulReq, endpointName, ex);
    }
}
Also used : HttpResponseMessage(com.netflix.zuul.message.http.HttpResponseMessage) HttpRequestMessage(com.netflix.zuul.message.http.HttpRequestMessage) ProxyEndpoint(com.netflix.zuul.filters.endpoint.ProxyEndpoint) TaskCloseable(io.perfmark.TaskCloseable)

Example 8 with HttpRequestMessage

use of com.netflix.zuul.message.http.HttpRequestMessage in project zuul by Netflix.

the class HttpUtilsTest method getBodySizeIfKnown_returnsResponseBodySize.

@Test
public void getBodySizeIfKnown_returnsResponseBodySize() {
    SessionContext context = new SessionContext();
    Headers headers = new Headers();
    HttpQueryParams queryParams = new HttpQueryParams();
    HttpRequestMessage request = new HttpRequestMessageImpl(context, "http", "GET", "/path", queryParams, headers, "127.0.0.1", "scheme", 6666, "server-name");
    request.storeInboundRequest();
    HttpResponseMessage response = new HttpResponseMessageImpl(context, request, 200);
    response.setBodyAsText("Hello world");
    assertThat(HttpUtils.getBodySizeIfKnown(response)).isEqualTo(Integer.valueOf(11));
}
Also used : HttpResponseMessage(com.netflix.zuul.message.http.HttpResponseMessage) Headers(com.netflix.zuul.message.Headers) HttpRequestMessage(com.netflix.zuul.message.http.HttpRequestMessage) HttpQueryParams(com.netflix.zuul.message.http.HttpQueryParams) HttpResponseMessageImpl(com.netflix.zuul.message.http.HttpResponseMessageImpl) SessionContext(com.netflix.zuul.context.SessionContext) HttpRequestMessageImpl(com.netflix.zuul.message.http.HttpRequestMessageImpl) Test(org.junit.Test)

Example 9 with HttpRequestMessage

use of com.netflix.zuul.message.http.HttpRequestMessage in project zuul by Netflix.

the class ClientRequestReceiverTest method multipleHostHeaders_setBadRequestStatus.

@Test
public void multipleHostHeaders_setBadRequestStatus() {
    ClientRequestReceiver receiver = new ClientRequestReceiver(null);
    EmbeddedChannel channel = new EmbeddedChannel(new HttpRequestEncoder());
    PassportLoggingHandler loggingHandler = new PassportLoggingHandler(new DefaultRegistry());
    // Required for messages
    channel.attr(SourceAddressChannelHandler.ATTR_SERVER_LOCAL_PORT).set(1234);
    channel.pipeline().addLast(new HttpServerCodec());
    channel.pipeline().addLast(receiver);
    channel.pipeline().addLast(loggingHandler);
    HttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/post");
    httpRequest.headers().add("Host", "foo.bar.com");
    httpRequest.headers().add("Host", "bar.foo.com");
    channel.writeOutbound(httpRequest);
    ByteBuf byteBuf = channel.readOutbound();
    channel.writeInbound(byteBuf);
    channel.readInbound();
    channel.close();
    HttpRequestMessage request = ClientRequestReceiver.getRequestFromChannel(channel);
    SessionContext context = request.getContext();
    assertEquals(StatusCategoryUtils.getStatusCategory(context), ZuulStatusCategory.FAILURE_CLIENT_BAD_REQUEST);
    assertEquals("Multiple Host headers", context.getError().getMessage());
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpRequestMessage(com.netflix.zuul.message.http.HttpRequestMessage) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) HttpRequestEncoder(io.netty.handler.codec.http.HttpRequestEncoder) DefaultRegistry(com.netflix.spectator.api.DefaultRegistry) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) SessionContext(com.netflix.zuul.context.SessionContext) ByteBuf(io.netty.buffer.ByteBuf) PassportLoggingHandler(com.netflix.zuul.netty.insights.PassportLoggingHandler) Test(org.junit.Test)

Example 10 with HttpRequestMessage

use of com.netflix.zuul.message.http.HttpRequestMessage in project zuul by Netflix.

the class ClientRequestReceiverTest method maxHeaderSizeExceeded_setBadRequestStatus.

@Test
public void maxHeaderSizeExceeded_setBadRequestStatus() {
    int maxInitialLineLength = BaseZuulChannelInitializer.MAX_INITIAL_LINE_LENGTH.get();
    int maxHeaderSize = 10;
    int maxChunkSize = BaseZuulChannelInitializer.MAX_CHUNK_SIZE.get();
    ClientRequestReceiver receiver = new ClientRequestReceiver(null);
    EmbeddedChannel channel = new EmbeddedChannel(new HttpRequestEncoder());
    PassportLoggingHandler loggingHandler = new PassportLoggingHandler(new DefaultRegistry());
    // Required for messages
    channel.attr(SourceAddressChannelHandler.ATTR_SERVER_LOCAL_PORT).set(1234);
    channel.pipeline().addLast(new HttpServerCodec(maxInitialLineLength, maxHeaderSize, maxChunkSize, false));
    channel.pipeline().addLast(receiver);
    channel.pipeline().addLast(loggingHandler);
    String str = "test-header-value";
    ByteBuf buf = Unpooled.buffer(1);
    HttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/post", buf);
    for (int i = 0; i < 100; i++) {
        httpRequest.headers().add("test-header" + i, str);
    }
    channel.writeOutbound(httpRequest);
    ByteBuf byteBuf = channel.readOutbound();
    channel.writeInbound(byteBuf);
    channel.readInbound();
    channel.close();
    HttpRequestMessage request = ClientRequestReceiver.getRequestFromChannel(channel);
    assertEquals(StatusCategoryUtils.getStatusCategory(request.getContext()), ZuulStatusCategory.FAILURE_CLIENT_BAD_REQUEST);
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpRequestMessage(com.netflix.zuul.message.http.HttpRequestMessage) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) HttpRequestEncoder(io.netty.handler.codec.http.HttpRequestEncoder) DefaultRegistry(com.netflix.spectator.api.DefaultRegistry) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) ByteBuf(io.netty.buffer.ByteBuf) PassportLoggingHandler(com.netflix.zuul.netty.insights.PassportLoggingHandler) Test(org.junit.Test)

Aggregations

HttpRequestMessage (com.netflix.zuul.message.http.HttpRequestMessage)16 HttpResponseMessage (com.netflix.zuul.message.http.HttpResponseMessage)8 SessionContext (com.netflix.zuul.context.SessionContext)6 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)4 HttpContent (io.netty.handler.codec.http.HttpContent)4 HttpRequest (io.netty.handler.codec.http.HttpRequest)4 TaskCloseable (io.perfmark.TaskCloseable)4 Test (org.junit.Test)4 ProxyEndpoint (com.netflix.zuul.filters.endpoint.ProxyEndpoint)3 ByteBuf (io.netty.buffer.ByteBuf)3 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)3 CompleteEvent (com.netflix.netty.common.HttpLifecycleChannelHandler.CompleteEvent)2 DefaultRegistry (com.netflix.spectator.api.DefaultRegistry)2 Headers (com.netflix.zuul.message.Headers)2 HttpQueryParams (com.netflix.zuul.message.http.HttpQueryParams)2 HttpRequestMessageImpl (com.netflix.zuul.message.http.HttpRequestMessageImpl)2 PassportLoggingHandler (com.netflix.zuul.netty.insights.PassportLoggingHandler)2 CurrentPassport (com.netflix.zuul.passport.CurrentPassport)2 HttpRequestEncoder (io.netty.handler.codec.http.HttpRequestEncoder)2 HttpServerCodec (io.netty.handler.codec.http.HttpServerCodec)2