Search in sources :

Example 1 with HttpRequestMessageImpl

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

the class ClientRequestReceiver method buildZuulHttpRequest.

// Build a ZuulMessage from the netty request.
private HttpRequestMessage buildZuulHttpRequest(final HttpRequest nativeRequest, final ChannelHandlerContext clientCtx) {
    PerfMark.attachTag("path", nativeRequest, HttpRequest::uri);
    // Setup the context for this request.
    final SessionContext context;
    if (decorator != null) {
        // Optionally decorate the context.
        SessionContext tempContext = new SessionContext();
        // Store the netty channel in SessionContext.
        tempContext.set(CommonContextKeys.NETTY_SERVER_CHANNEL_HANDLER_CONTEXT, clientCtx);
        context = decorator.decorate(tempContext);
        // We expect the UUID is present after decoration
        PerfMark.attachTag("uuid", context, SessionContext::getUUID);
    } else {
        context = new SessionContext();
    }
    // Get the client IP (ignore XFF headers at this point, as that can be app specific).
    final Channel channel = clientCtx.channel();
    final String clientIp = channel.attr(SourceAddressChannelHandler.ATTR_SOURCE_ADDRESS).get();
    // This is the only way I found to get the port of the request with netty...
    final int port = channel.attr(SourceAddressChannelHandler.ATTR_SERVER_LOCAL_PORT).get();
    final String serverName = channel.attr(SourceAddressChannelHandler.ATTR_SERVER_LOCAL_ADDRESS).get();
    final SocketAddress clientDestinationAddress = channel.attr(SourceAddressChannelHandler.ATTR_LOCAL_ADDR).get();
    final InetSocketAddress proxyProtocolDestinationAddress = channel.attr(SourceAddressChannelHandler.ATTR_PROXY_PROTOCOL_DESTINATION_ADDRESS).get();
    if (proxyProtocolDestinationAddress != null) {
        context.set(CommonContextKeys.PROXY_PROTOCOL_DESTINATION_ADDRESS, proxyProtocolDestinationAddress);
    }
    // Store info about the SSL handshake if applicable, and choose the http scheme.
    String scheme = SCHEME_HTTP;
    final SslHandshakeInfo sslHandshakeInfo = channel.attr(SslHandshakeInfoHandler.ATTR_SSL_INFO).get();
    if (sslHandshakeInfo != null) {
        context.set(CommonContextKeys.SSL_HANDSHAKE_INFO, sslHandshakeInfo);
        scheme = SCHEME_HTTPS;
    }
    // Decide if this is HTTP/1 or HTTP/2.
    String protocol = channel.attr(PROTOCOL_NAME).get();
    if (protocol == null) {
        protocol = nativeRequest.protocolVersion().text();
    }
    // Strip off the query from the path.
    String path = parsePath(nativeRequest.uri());
    // Setup the req/resp message objects.
    final HttpRequestMessage request = new HttpRequestMessageImpl(context, protocol, nativeRequest.method().asciiName().toString().toLowerCase(), path, copyQueryParams(nativeRequest), copyHeaders(nativeRequest), clientIp, scheme, port, serverName, clientDestinationAddress, false);
    // a LastHttpContent without any prior HttpContent's.
    if (HttpUtils.hasChunkedTransferEncodingHeader(request) || HttpUtils.hasNonZeroContentLengthHeader(request)) {
        request.setHasBody(true);
    }
    // Store this original request info for future reference (ie. for metrics and access logging purposes).
    request.storeInboundRequest();
    // Store the netty request for use later.
    context.set(CommonContextKeys.NETTY_HTTP_REQUEST, nativeRequest);
    // Store zuul request on netty channel for later use.
    channel.attr(ATTR_ZUUL_REQ).set(request);
    if (nativeRequest instanceof DefaultFullHttpRequest) {
        final ByteBuf chunk = ((DefaultFullHttpRequest) nativeRequest).content();
        request.bufferBodyContents(new DefaultLastHttpContent(chunk));
    }
    return request;
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) InetSocketAddress(java.net.InetSocketAddress) HttpRequestMessage(com.netflix.zuul.message.http.HttpRequestMessage) Channel(io.netty.channel.Channel) HttpRequestMessageImpl(com.netflix.zuul.message.http.HttpRequestMessageImpl) ByteBuf(io.netty.buffer.ByteBuf) SslHandshakeInfo(com.netflix.netty.common.ssl.SslHandshakeInfo) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) SessionContext(com.netflix.zuul.context.SessionContext) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 2 with HttpRequestMessageImpl

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

the class ZuulFilterChainRunnerTest method before.

@Before
public void before() {
    SessionContext context = new SessionContext();
    Headers headers = new Headers();
    ChannelHandlerContext chc = mock(ChannelHandlerContext.class);
    when(chc.executor()).thenReturn(ImmediateEventExecutor.INSTANCE);
    context.put(NETTY_SERVER_CHANNEL_HANDLER_CONTEXT, chc);
    request = new HttpRequestMessageImpl(context, "http", "GET", "/foo/bar", new HttpQueryParams(), headers, "127.0.0.1", "http", 8080, "server123");
    request.storeInboundRequest();
    response = new HttpResponseMessageImpl(context, request, 200);
}
Also used : Headers(com.netflix.zuul.message.Headers) HttpQueryParams(com.netflix.zuul.message.http.HttpQueryParams) HttpResponseMessageImpl(com.netflix.zuul.message.http.HttpResponseMessageImpl) SessionContext(com.netflix.zuul.context.SessionContext) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HttpRequestMessageImpl(com.netflix.zuul.message.http.HttpRequestMessageImpl) Before(org.junit.Before)

Example 3 with HttpRequestMessageImpl

use of com.netflix.zuul.message.http.HttpRequestMessageImpl 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 4 with HttpRequestMessageImpl

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

the class ClientRequestReceiverTest method proxyProtocol_portSetInSessionContextAndInHttpRequestMessageImpl.

@Test
public void proxyProtocol_portSetInSessionContextAndInHttpRequestMessageImpl() {
    EmbeddedChannel channel = new EmbeddedChannel(new ClientRequestReceiver(null));
    channel.attr(SourceAddressChannelHandler.ATTR_SERVER_LOCAL_PORT).set(1234);
    InetSocketAddress hapmDestinationAddress = new InetSocketAddress(InetAddresses.forString("2.2.2.2"), 444);
    channel.attr(SourceAddressChannelHandler.ATTR_PROXY_PROTOCOL_DESTINATION_ADDRESS).set(hapmDestinationAddress);
    channel.attr(SourceAddressChannelHandler.ATTR_LOCAL_ADDR).set(hapmDestinationAddress);
    HttpRequestMessageImpl result;
    {
        channel.writeInbound(new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/post", Unpooled.buffer()));
        result = channel.readInbound();
        result.disposeBufferedBody();
    }
    assertEquals((int) result.getClientDestinationPort().get(), hapmDestinationAddress.getPort());
    int destinationPort = ((InetSocketAddress) result.getContext().get(CommonContextKeys.PROXY_PROTOCOL_DESTINATION_ADDRESS)).getPort();
    assertEquals(destinationPort, 444);
    assertEquals(result.getOriginalPort(), 444);
    channel.close();
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) InetSocketAddress(java.net.InetSocketAddress) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) HttpRequestMessageImpl(com.netflix.zuul.message.http.HttpRequestMessageImpl) Test(org.junit.Test)

Example 5 with HttpRequestMessageImpl

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

the class ClientRequestReceiverTest method largeResponse_aboveLimit.

@Test
public void largeResponse_aboveLimit() {
    ClientRequestReceiver receiver = new ClientRequestReceiver(null);
    EmbeddedChannel channel = new EmbeddedChannel(receiver);
    // Required for messages
    channel.attr(SourceAddressChannelHandler.ATTR_SERVER_LOCAL_PORT).set(1234);
    int maxSize;
    // Figure out the max size, since it isn't public.
    {
        ByteBuf buf = Unpooled.buffer(1).writeByte('a');
        channel.writeInbound(new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/post", buf));
        HttpRequestMessageImpl res = channel.readInbound();
        maxSize = res.getMaxBodySize();
        res.disposeBufferedBody();
    }
    HttpRequestMessageImpl result;
    {
        ByteBuf buf = Unpooled.buffer(maxSize + 1);
        buf.writerIndex(maxSize + 1);
        channel.writeInbound(new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/post", buf));
        result = channel.readInbound();
        result.disposeBufferedBody();
    }
    assertNotNull(result.getContext().getError());
    assertTrue(result.getContext().getError().getMessage().contains("too large"));
    assertTrue(result.getContext().shouldSendErrorResponse());
    channel.close();
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) HttpRequestMessageImpl(com.netflix.zuul.message.http.HttpRequestMessageImpl) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Aggregations

HttpRequestMessageImpl (com.netflix.zuul.message.http.HttpRequestMessageImpl)10 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)8 Test (org.junit.Test)8 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)7 SessionContext (com.netflix.zuul.context.SessionContext)3 ByteBuf (io.netty.buffer.ByteBuf)3 Headers (com.netflix.zuul.message.Headers)2 HttpQueryParams (com.netflix.zuul.message.http.HttpQueryParams)2 HttpRequestMessage (com.netflix.zuul.message.http.HttpRequestMessage)2 HttpResponseMessageImpl (com.netflix.zuul.message.http.HttpResponseMessageImpl)2 InetSocketAddress (java.net.InetSocketAddress)2 SslHandshakeInfo (com.netflix.netty.common.ssl.SslHandshakeInfo)1 HttpResponseMessage (com.netflix.zuul.message.http.HttpResponseMessage)1 Channel (io.netty.channel.Channel)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)1 HttpRequest (io.netty.handler.codec.http.HttpRequest)1 SocketAddress (java.net.SocketAddress)1 Before (org.junit.Before)1