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;
}
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);
}
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));
}
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();
}
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();
}
Aggregations