Search in sources :

Example 1 with HttpRequestInfo

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

the class ClientResponseWriter method buildHttpResponse.

private HttpResponse buildHttpResponse(final HttpResponseMessage zuulResp) {
    final HttpRequestInfo zuulRequest = zuulResp.getInboundRequest();
    HttpVersion responseHttpVersion;
    final String inboundProtocol = zuulRequest.getProtocol();
    if (inboundProtocol.startsWith("HTTP/1")) {
        responseHttpVersion = HttpVersion.valueOf(inboundProtocol);
    } else {
        // Default to 1.1. We do this to cope with HTTP/2 inbound requests.
        responseHttpVersion = HttpVersion.HTTP_1_1;
    }
    // Create the main http response to send, with body.
    final DefaultHttpResponse nativeResponse = new DefaultHttpResponse(responseHttpVersion, HttpResponseStatus.valueOf(zuulResp.getStatus()), false, false);
    // Now set all of the response headers - note this is a multi-set in keeping with HTTP semantics
    final HttpHeaders nativeHeaders = nativeResponse.headers();
    for (Header entry : zuulResp.getHeaders().entries()) {
        nativeHeaders.add(entry.getKey(), entry.getValue());
    }
    // Netty does not automatically add Content-Length or Transfer-Encoding: chunked. So we add here if missing.
    if (!HttpUtil.isContentLengthSet(nativeResponse) && !HttpUtil.isTransferEncodingChunked(nativeResponse)) {
        nativeResponse.headers().add(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
    }
    final HttpRequest nativeReq = (HttpRequest) zuulResp.getContext().get(CommonContextKeys.NETTY_HTTP_REQUEST);
    if (!closeConnection && HttpUtil.isKeepAlive(nativeReq)) {
        HttpUtil.setKeepAlive(nativeResponse, true);
    } else {
        // Send a Connection: close response header (only needed for HTTP/1.0 but no harm in doing for 1.1 too).
        nativeResponse.headers().set("Connection", "close");
    }
    // TODO - temp hack for http/2 handling.
    if (nativeReq.headers().contains(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text())) {
        String streamId = nativeReq.headers().get(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text());
        nativeResponse.headers().set(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text(), streamId);
    }
    return nativeResponse;
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) Header(com.netflix.zuul.message.Header) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) HttpRequestInfo(com.netflix.zuul.message.http.HttpRequestInfo) HttpVersion(io.netty.handler.codec.http.HttpVersion)

Example 2 with HttpRequestInfo

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

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

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

the class BaseZuulFilterRunner method handleException.

protected void handleException(final ZuulMessage zuulMesg, final String filterName, final Exception ex) {
    HttpRequestInfo zuulReq = null;
    if (zuulMesg instanceof HttpRequestMessage) {
        zuulReq = (HttpRequestMessage) zuulMesg;
    } else if (zuulMesg instanceof HttpResponseMessage) {
        zuulReq = ((HttpResponseMessage) zuulMesg).getInboundRequest();
    }
    final String path = (zuulReq != null) ? zuulReq.getPathAndQuery() : "-";
    final String method = (zuulReq != null) ? zuulReq.getMethod() : "-";
    final String errMesg = "Error with filter: " + filterName + ", path: " + path + ", method: " + method;
    logger.error(errMesg, ex);
    getChannelHandlerContext(zuulMesg).fireExceptionCaught(ex);
}
Also used : HttpResponseMessage(com.netflix.zuul.message.http.HttpResponseMessage) HttpRequestMessage(com.netflix.zuul.message.http.HttpRequestMessage) HttpRequestInfo(com.netflix.zuul.message.http.HttpRequestInfo)

Example 5 with HttpRequestInfo

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

the class BaseZuulFilterRunner method addPerfMarkTags.

protected final void addPerfMarkTags(ZuulMessage inMesg) {
    HttpRequestInfo req = null;
    if (inMesg instanceof HttpRequestInfo) {
        req = (HttpRequestInfo) inMesg;
    }
    if (inMesg instanceof HttpResponseMessage) {
        HttpResponseMessage msg = (HttpResponseMessage) inMesg;
        req = msg.getOutboundRequest();
        attachTag("statuscode", msg.getStatus());
    }
    if (req != null) {
        attachTag("path", req, HttpRequestInfo::getPath);
        attachTag("originalhost", req, HttpRequestInfo::getOriginalHost);
    }
    attachTag("uuid", inMesg, m -> m.getContext().getUUID());
}
Also used : HttpResponseMessage(com.netflix.zuul.message.http.HttpResponseMessage) HttpRequestInfo(com.netflix.zuul.message.http.HttpRequestInfo)

Aggregations

HttpRequestInfo (com.netflix.zuul.message.http.HttpRequestInfo)6 Headers (com.netflix.zuul.message.Headers)2 HttpResponseMessage (com.netflix.zuul.message.http.HttpResponseMessage)2 ClientException (com.netflix.client.ClientException)1 OutboundException (com.netflix.zuul.exception.OutboundException)1 ZuulException (com.netflix.zuul.exception.ZuulException)1 Header (com.netflix.zuul.message.Header)1 HttpRequestMessage (com.netflix.zuul.message.http.HttpRequestMessage)1 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)1 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)1 HttpRequest (io.netty.handler.codec.http.HttpRequest)1 HttpVersion (io.netty.handler.codec.http.HttpVersion)1 TaskCloseable (io.perfmark.TaskCloseable)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Test (org.junit.Test)1