Search in sources :

Example 11 with Headers

use of com.netflix.zuul.message.Headers in project zuul by Netflix.

the class DebugTest method setup.

@Before
public void setup() {
    ctx = new SessionContext();
    headers = new Headers();
    headers.add("lah", "deda");
    params = new HttpQueryParams();
    params.add("k1", "v1");
    request = new HttpRequestBuilder(ctx).withMethod(HttpMethod.POST).withUri("/some/where").withHeaders(headers).withQueryParams(params).build();
    request.setBodyAsText("some text");
    request.storeInboundRequest();
    response = new HttpResponseMessageImpl(ctx, headers, request, 200);
    response.setBodyAsText("response text");
}
Also used : Headers(com.netflix.zuul.message.Headers) HttpQueryParams(com.netflix.zuul.message.http.HttpQueryParams) HttpResponseMessageImpl(com.netflix.zuul.message.http.HttpResponseMessageImpl) HttpRequestBuilder(com.netflix.zuul.message.util.HttpRequestBuilder) Before(org.junit.Before)

Example 12 with Headers

use of com.netflix.zuul.message.Headers in project zuul by Netflix.

the class GZipResponseFilter method shouldFilter.

@Override
public boolean shouldFilter(HttpResponseMessage response) {
    if (!ENABLED.get() || !response.hasBody() || response.getContext().isInBrownoutMode()) {
        return false;
    }
    if (response.getContext().get(CommonContextKeys.GZIPPER) != null) {
        return true;
    }
    // A flag on SessionContext can be set to override normal mechanism of checking if client accepts gzip.;
    final HttpRequestInfo request = response.getInboundRequest();
    final Boolean overrideIsGzipRequested = (Boolean) response.getContext().get(CommonContextKeys.OVERRIDE_GZIP_REQUESTED);
    final boolean isGzipRequested = (overrideIsGzipRequested == null) ? HttpUtils.acceptsGzip(request.getHeaders()) : overrideIsGzipRequested.booleanValue();
    // Check the headers to see if response is already gzipped.
    final Headers respHeaders = response.getHeaders();
    boolean isResponseCompressed = HttpUtils.isCompressed(respHeaders);
    // Decide what to do.;
    final boolean shouldGzip = isGzippableContentType(response) && isGzipRequested && !isResponseCompressed && isRightSizeForGzip(response);
    if (shouldGzip) {
        response.getContext().set(CommonContextKeys.GZIPPER, getGzipper());
    }
    return shouldGzip;
}
Also used : Headers(com.netflix.zuul.message.Headers) HttpRequestInfo(com.netflix.zuul.message.http.HttpRequestInfo)

Example 13 with Headers

use of com.netflix.zuul.message.Headers in project zuul by Netflix.

the class StatsManagerTest method testCollectRequestStats.

@Test
public void testCollectRequestStats() {
    final String host = "api.netflix.com";
    final String proto = "https";
    final HttpRequestInfo req = Mockito.mock(HttpRequestInfo.class);
    Headers headers = new Headers();
    when(req.getHeaders()).thenReturn(headers);
    headers.set(StatsManager.HOST_HEADER, host);
    headers.set(StatsManager.X_FORWARDED_PROTO_HEADER, proto);
    when(req.getClientIp()).thenReturn("127.0.0.1");
    final StatsManager sm = StatsManager.getManager();
    sm.collectRequestStats(req);
    final NamedCountingMonitor hostMonitor = sm.getHostMonitor(host);
    assertNotNull("hostMonitor should not be null", hostMonitor);
    final NamedCountingMonitor protoMonitor = sm.getProtocolMonitor(proto);
    assertNotNull("protoMonitor should not be null", protoMonitor);
    assertEquals(1, hostMonitor.getCount());
    assertEquals(1, protoMonitor.getCount());
}
Also used : Headers(com.netflix.zuul.message.Headers) HttpRequestInfo(com.netflix.zuul.message.http.HttpRequestInfo) Test(org.junit.Test)

Example 14 with Headers

use of com.netflix.zuul.message.Headers in project zuul by Netflix.

the class ClientRequestReceiver method write.

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    try (TaskCloseable ignored = PerfMark.traceTask("CRR.write")) {
        if (msg instanceof HttpResponse) {
            promise.addListener((future) -> {
                if (!future.isSuccess()) {
                    fireWriteError("response headers", future.cause(), ctx);
                }
            });
            super.write(ctx, msg, promise);
        } else if (msg instanceof HttpContent) {
            promise.addListener((future) -> {
                if (!future.isSuccess()) {
                    fireWriteError("response content", future.cause(), ctx);
                }
            });
            super.write(ctx, msg, promise);
        } else {
            // should never happen
            ReferenceCountUtil.release(msg);
            throw new ZuulException("Attempt to write invalid content type to client: " + msg.getClass().getSimpleName(), true);
        }
    }
}
Also used : AttributeKey(io.netty.util.AttributeKey) SocketAddress(java.net.SocketAddress) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) LoggerFactory(org.slf4j.LoggerFactory) SslHandshakeInfo(com.netflix.netty.common.ssl.SslHandshakeInfo) Debug(com.netflix.zuul.context.Debug) CurrentPassport(com.netflix.zuul.passport.CurrentPassport) Matcher(java.util.regex.Matcher) ChannelPromise(io.netty.channel.ChannelPromise) Spectator(com.netflix.spectator.api.Spectator) Map(java.util.Map) TaskCloseable(io.perfmark.TaskCloseable) ChannelDuplexHandler(io.netty.channel.ChannelDuplexHandler) HttpRequest(io.netty.handler.codec.http.HttpRequest) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) InetSocketAddress(java.net.InetSocketAddress) PerfMark(io.perfmark.PerfMark) ZuulException(com.netflix.zuul.exception.ZuulException) List(java.util.List) HttpResponseMessage(com.netflix.zuul.message.http.HttpResponseMessage) PassportState(com.netflix.zuul.passport.PassportState) ReferenceCountUtil(io.netty.util.ReferenceCountUtil) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) CommonContextKeys(com.netflix.zuul.context.CommonContextKeys) HttpResponse(io.netty.handler.codec.http.HttpResponse) SessionContextDecorator(com.netflix.zuul.context.SessionContextDecorator) Pattern(java.util.regex.Pattern) SslHandshakeInfoHandler(com.netflix.zuul.netty.server.ssl.SslHandshakeInfoHandler) ChannelUtils(com.netflix.zuul.netty.ChannelUtils) StatusCategoryUtils(com.netflix.zuul.stats.status.StatusCategoryUtils) RejectionUtils(com.netflix.netty.common.throttle.RejectionUtils) HttpVersion(io.netty.handler.codec.http.HttpVersion) Errors(io.netty.channel.unix.Errors) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ZuulStatusCategory(com.netflix.zuul.stats.status.ZuulStatusCategory) ByteBuf(io.netty.buffer.ByteBuf) PROTOCOL_NAME(com.netflix.zuul.netty.server.http2.Http2OrHttpHandler.PROTOCOL_NAME) HttpQueryParams(com.netflix.zuul.message.http.HttpQueryParams) Headers(com.netflix.zuul.message.Headers) CompleteEvent(com.netflix.netty.common.HttpLifecycleChannelHandler.CompleteEvent) SourceAddressChannelHandler(com.netflix.netty.common.SourceAddressChannelHandler) HttpContent(io.netty.handler.codec.http.HttpContent) Logger(org.slf4j.Logger) HttpUtils(com.netflix.zuul.util.HttpUtils) HttpRequestMessageImpl(com.netflix.zuul.message.http.HttpRequestMessageImpl) SessionContext(com.netflix.zuul.context.SessionContext) HttpRequestMessage(com.netflix.zuul.message.http.HttpRequestMessage) ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) HAProxyMessage(io.netty.handler.codec.haproxy.HAProxyMessage) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) CompleteReason(com.netflix.netty.common.HttpLifecycleChannelHandler.CompleteReason) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) SESSION_COMPLETE(com.netflix.netty.common.HttpLifecycleChannelHandler.CompleteReason.SESSION_COMPLETE) HttpUtil(io.netty.handler.codec.http.HttpUtil) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) ZuulException(com.netflix.zuul.exception.ZuulException) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) TaskCloseable(io.perfmark.TaskCloseable)

Example 15 with Headers

use of com.netflix.zuul.message.Headers in project zuul by Netflix.

the class ProxyUtils method addXForwardedHeaders.

public static void addXForwardedHeaders(HttpRequestMessage request) {
    // Add standard Proxy request headers.
    Headers headers = request.getHeaders();
    addXForwardedHeader(headers, HttpHeaderNames.X_FORWARDED_HOST, request.getOriginalHost());
    addXForwardedHeader(headers, HttpHeaderNames.X_FORWARDED_PORT, Integer.toString(request.getPort()));
    addXForwardedHeader(headers, HttpHeaderNames.X_FORWARDED_PROTO, request.getScheme());
    addXForwardedHeader(headers, HttpHeaderNames.X_FORWARDED_FOR, request.getClientIp());
}
Also used : Headers(com.netflix.zuul.message.Headers)

Aggregations

Headers (com.netflix.zuul.message.Headers)26 SessionContext (com.netflix.zuul.context.SessionContext)17 Test (org.junit.Test)16 HttpResponseMessageImpl (com.netflix.zuul.message.http.HttpResponseMessageImpl)5 HttpQueryParams (com.netflix.zuul.message.http.HttpQueryParams)4 HttpResponseMessage (com.netflix.zuul.message.http.HttpResponseMessage)4 InetSocketAddress (java.net.InetSocketAddress)4 HttpRequestMessage (com.netflix.zuul.message.http.HttpRequestMessage)3 HttpRequestMessageImpl (com.netflix.zuul.message.http.HttpRequestMessageImpl)3 ZuulMessage (com.netflix.zuul.message.ZuulMessage)2 ZuulMessageImpl (com.netflix.zuul.message.ZuulMessageImpl)2 HttpRequestInfo (com.netflix.zuul.message.http.HttpRequestInfo)2 ByteBuf (io.netty.buffer.ByteBuf)2 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)2 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)2 SocketAddress (java.net.SocketAddress)2 Map (java.util.Map)2 Before (org.junit.Before)2 ClientException (com.netflix.client.ClientException)1 CompleteEvent (com.netflix.netty.common.HttpLifecycleChannelHandler.CompleteEvent)1