Search in sources :

Example 31 with SessionContext

use of com.netflix.zuul.context.SessionContext in project zuul by Netflix.

the class HttpRequestMessageImplTest method testGetOriginalPort.

@Test
public void testGetOriginalPort() {
    HttpQueryParams queryParams = new HttpQueryParams();
    Headers headers = new Headers();
    request = new HttpRequestMessageImpl(new SessionContext(), "HTTP/1.1", "POST", "/some/where", queryParams, headers, "192.168.0.2", "https", 7002, "localhost");
    Assert.assertEquals(7002, request.getOriginalPort());
    headers = new Headers();
    headers.add("Host", "blah.netflix.com");
    headers.add("X-Forwarded-Port", "443");
    request = new HttpRequestMessageImpl(new SessionContext(), "HTTP/1.1", "POST", "/some/where", queryParams, headers, "192.168.0.2", "https", 7002, "localhost");
    Assert.assertEquals(443, request.getOriginalPort());
    headers = new Headers();
    headers.add("Host", "blah.netflix.com:443");
    request = new HttpRequestMessageImpl(new SessionContext(), "HTTP/1.1", "POST", "/some/where", queryParams, headers, "192.168.0.2", "https", 7002, "localhost");
    Assert.assertEquals(443, request.getOriginalPort());
    headers = new Headers();
    headers.add("Host", "127.0.0.2:443");
    request = new HttpRequestMessageImpl(new SessionContext(), "HTTP/1.1", "POST", "/some/where", queryParams, headers, "192.168.0.2", "https", 7002, "localhost");
    Assert.assertEquals(443, request.getOriginalPort());
    headers = new Headers();
    headers.add("Host", "127.0.0.2");
    request = new HttpRequestMessageImpl(new SessionContext(), "HTTP/1.1", "POST", "/some/where", queryParams, headers, "192.168.0.2", "https", 7002, "localhost");
    Assert.assertEquals(7002, request.getOriginalPort());
    headers = new Headers();
    headers.add("Host", "[::2]:443");
    request = new HttpRequestMessageImpl(new SessionContext(), "HTTP/1.1", "POST", "/some/where", queryParams, headers, "192.168.0.2", "https", 7002, "localhost");
    Assert.assertEquals(443, request.getOriginalPort());
    headers = new Headers();
    headers.add("Host", "[::2]");
    request = new HttpRequestMessageImpl(new SessionContext(), "HTTP/1.1", "POST", "/some/where", queryParams, headers, "192.168.0.2", "https", 7002, "localhost");
    Assert.assertEquals(7002, request.getOriginalPort());
    headers = new Headers();
    headers.add("Host", "blah.netflix.com:443");
    headers.add("X-Forwarded-Port", "7005");
    request = new HttpRequestMessageImpl(new SessionContext(), "HTTP/1.1", "POST", "/some/where", queryParams, headers, "192.168.0.2", "https", 7002, "localhost");
    Assert.assertEquals(7005, request.getOriginalPort());
}
Also used : Headers(com.netflix.zuul.message.Headers) SessionContext(com.netflix.zuul.context.SessionContext) Test(org.junit.Test)

Example 32 with SessionContext

use of com.netflix.zuul.context.SessionContext in project zuul by Netflix.

the class HttpUtilsTest method getBodySizeIfKnown_returnsContentLengthValue.

@Test
public void getBodySizeIfKnown_returnsContentLengthValue() {
    SessionContext context = new SessionContext();
    Headers headers = new Headers();
    headers.add(com.netflix.zuul.message.http.HttpHeaderNames.CONTENT_LENGTH, "23450");
    ZuulMessage msg = new ZuulMessageImpl(context, headers);
    assertThat(HttpUtils.getBodySizeIfKnown(msg)).isEqualTo(Integer.valueOf(23450));
}
Also used : ZuulMessage(com.netflix.zuul.message.ZuulMessage) Headers(com.netflix.zuul.message.Headers) SessionContext(com.netflix.zuul.context.SessionContext) ZuulMessageImpl(com.netflix.zuul.message.ZuulMessageImpl) Test(org.junit.Test)

Example 33 with SessionContext

use of com.netflix.zuul.context.SessionContext in project zuul by Netflix.

the class HttpUtilsTest method getBodySizeIfKnown_returnsNull.

@Test
public void getBodySizeIfKnown_returnsNull() {
    SessionContext context = new SessionContext();
    Headers headers = new Headers();
    ZuulMessage msg = new ZuulMessageImpl(context, headers);
    assertThat(HttpUtils.getBodySizeIfKnown(msg)).isNull();
}
Also used : ZuulMessage(com.netflix.zuul.message.ZuulMessage) Headers(com.netflix.zuul.message.Headers) SessionContext(com.netflix.zuul.context.SessionContext) ZuulMessageImpl(com.netflix.zuul.message.ZuulMessageImpl) Test(org.junit.Test)

Example 34 with SessionContext

use of com.netflix.zuul.context.SessionContext in project zuul by Netflix.

the class BaseZuulFilterRunner method recordFilterCompletion.

protected void recordFilterCompletion(final ExecutionStatus status, final ZuulFilter<I, O> filter, long startTime, final ZuulMessage zuulMesg, final ZuulMessage startSnapshot) {
    final SessionContext zuulCtx = zuulMesg.getContext();
    final long execTimeNs = System.nanoTime() - startTime;
    final long execTimeMs = execTimeNs / 1_000_000L;
    if (execTimeMs >= FILTER_EXCESSIVE_EXEC_TIME.get()) {
        registry.timer(filterExcessiveTimerId.withTag("id", filter.filterName()).withTag("status", status.name())).record(execTimeMs, TimeUnit.MILLISECONDS);
    }
    // Record the execution summary in context.
    switch(status) {
        case FAILED:
            if (logger.isDebugEnabled()) {
                zuulCtx.addFilterExecutionSummary(filter.filterName(), FAILED.name(), execTimeMs);
            }
            break;
        case SUCCESS:
            if (logger.isDebugEnabled()) {
                zuulCtx.addFilterExecutionSummary(filter.filterName(), SUCCESS.name(), execTimeMs);
            }
            if (startSnapshot != null) {
                // debugRouting == true
                Debug.addRoutingDebug(zuulCtx, "Filter {" + filter.filterName() + " TYPE:" + filter.filterType().toString() + " ORDER:" + filter.filterOrder() + "} Execution time = " + execTimeMs + "ms");
                Debug.compareContextState(filter.filterName(), zuulCtx, startSnapshot.getContext());
            }
            break;
        default:
            break;
    }
    logger.debug("Filter {} completed with status {}, UUID {}", filter.filterName(), status.name(), zuulMesg.getContext().getUUID());
    // Notify configured listener.
    usageNotifier.notify(filter, status);
}
Also used : SessionContext(com.netflix.zuul.context.SessionContext)

Example 35 with SessionContext

use of com.netflix.zuul.context.SessionContext in project zuul by Netflix.

the class BaseZuulFilterRunner method recordFilterError.

protected void recordFilterError(final I inMesg, final ZuulFilter<I, O> filter, final Throwable t) {
    // Add a log statement for this exception.
    final String errorMsg = "Filter Exception: filter=" + filter.filterName() + ", request-info=" + inMesg.getInfoForLogging() + ", msg=" + String.valueOf(t.getMessage());
    if (t instanceof ZuulException && !((ZuulException) t).shouldLogAsError()) {
        logger.warn(errorMsg);
    } else {
        logger.error(errorMsg, t);
    }
    // Store this filter error for possible future use. But we still continue with next filter in the chain.
    final SessionContext zuulCtx = inMesg.getContext();
    zuulCtx.getFilterErrors().add(new FilterError(filter.filterName(), filter.filterType().toString(), t));
    if (zuulCtx.debugRouting()) {
        Debug.addRoutingDebug(zuulCtx, "Running Filter failed " + filter.filterName() + " type:" + filter.filterType() + " order:" + filter.filterOrder() + " " + t.getMessage());
    }
}
Also used : ZuulException(com.netflix.zuul.exception.ZuulException) SessionContext(com.netflix.zuul.context.SessionContext) FilterError(com.netflix.zuul.filters.FilterError)

Aggregations

SessionContext (com.netflix.zuul.context.SessionContext)43 Test (org.junit.Test)23 Headers (com.netflix.zuul.message.Headers)16 HttpRequestMessage (com.netflix.zuul.message.http.HttpRequestMessage)6 HttpResponseMessage (com.netflix.zuul.message.http.HttpResponseMessage)5 HttpResponseMessageImpl (com.netflix.zuul.message.http.HttpResponseMessageImpl)5 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)5 HttpQueryParams (com.netflix.zuul.message.http.HttpQueryParams)4 InetSocketAddress (java.net.InetSocketAddress)4 ClientException (com.netflix.client.ClientException)3 ZuulException (com.netflix.zuul.exception.ZuulException)3 HttpRequestMessageImpl (com.netflix.zuul.message.http.HttpRequestMessageImpl)3 StatusCategory (com.netflix.zuul.stats.status.StatusCategory)3 ByteBuf (io.netty.buffer.ByteBuf)3 DefaultHttpContent (io.netty.handler.codec.http.DefaultHttpContent)3 ErrorType (com.netflix.zuul.exception.ErrorType)2 OutboundException (com.netflix.zuul.exception.OutboundException)2 ZuulMessage (com.netflix.zuul.message.ZuulMessage)2 ZuulMessageImpl (com.netflix.zuul.message.ZuulMessageImpl)2 CurrentPassport (com.netflix.zuul.passport.CurrentPassport)2