Search in sources :

Example 21 with Headers

use of com.netflix.zuul.message.Headers 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 22 with Headers

use of com.netflix.zuul.message.Headers 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 23 with Headers

use of com.netflix.zuul.message.Headers 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 24 with Headers

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

the class ProxyEndpoint method buildZuulHttpResponse.

private HttpResponseMessage buildZuulHttpResponse(final HttpResponse httpResponse, final StatusCategory statusCategory, final Throwable ex) {
    startedSendingResponseToClient = true;
    // Translate the netty HttpResponse into a zuul HttpResponseMessage.
    final SessionContext zuulCtx = context;
    final int respStatus = httpResponse.status().code();
    final HttpResponseMessage zuulResponse = new HttpResponseMessageImpl(zuulCtx, zuulRequest, respStatus);
    final Headers respHeaders = zuulResponse.getHeaders();
    for (Map.Entry<String, String> entry : httpResponse.headers()) {
        respHeaders.add(entry.getKey(), entry.getValue());
    }
    // a LastHttpContent without any prior HttpContent's.
    if (HttpUtils.hasChunkedTransferEncodingHeader(zuulResponse) || HttpUtils.hasNonZeroContentLengthHeader(zuulResponse)) {
        zuulResponse.setHasBody(true);
    }
    // Store this original response info for future reference (ie. for metrics and access logging purposes).
    zuulResponse.storeInboundResponse();
    channelCtx.channel().attr(ATTR_ZUUL_RESP).set(zuulResponse);
    if (httpResponse instanceof DefaultFullHttpResponse) {
        final ByteBuf chunk = ((DefaultFullHttpResponse) httpResponse).content();
        zuulResponse.bufferBodyContents(new DefaultLastHttpContent(chunk));
    }
    // Request was a success even if server may have responded with an error code 5XX, except for 503.
    if (originConn != null) {
        if (statusCategory == ZuulStatusCategory.FAILURE_ORIGIN_THROTTLED) {
            origin.onRequestExecutionFailed(zuulRequest, originConn.getServer(), attemptNum, new ClientException(ClientException.ErrorType.SERVER_THROTTLED));
        } else {
            origin.onRequestExecutionSuccess(zuulRequest, zuulResponse, originConn.getServer(), attemptNum);
        }
    }
    // Collect some info about the received response.
    origin.recordFinalResponse(zuulResponse);
    origin.recordFinalError(zuulRequest, ex);
    zuulCtx.put(CommonContextKeys.STATUS_CATGEORY, statusCategory);
    zuulCtx.setError(ex);
    zuulCtx.put("origin_http_status", Integer.toString(respStatus));
    return transformResponse(zuulResponse);
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponseMessage(com.netflix.zuul.message.http.HttpResponseMessage) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) Headers(com.netflix.zuul.message.Headers) HttpResponseMessageImpl(com.netflix.zuul.message.http.HttpResponseMessageImpl) SessionContext(com.netflix.zuul.context.SessionContext) ClientException(com.netflix.client.ClientException) ByteBuf(io.netty.buffer.ByteBuf) Map(java.util.Map) HashMap(java.util.HashMap)

Example 25 with Headers

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

the class ZuulFilterChainHandler method sendResponse.

private void sendResponse(final StatusCategory statusCategory, final int status, ChannelHandlerContext ctx) {
    if (zuulRequest == null) {
        ctx.close();
    } else {
        final SessionContext zuulCtx = zuulRequest.getContext();
        StatusCategoryUtils.storeStatusCategoryIfNotAlreadyFailure(zuulCtx, statusCategory);
        final HttpResponseMessage zuulResponse = new HttpResponseMessageImpl(zuulCtx, zuulRequest, status);
        final Headers headers = zuulResponse.getHeaders();
        headers.add("Connection", "close");
        headers.add("Content-Length", "0");
        zuulResponse.finishBufferedBodyIfIncomplete();
        responseFilterChain.filter(zuulResponse);
        fireEndpointFinish(true);
    }
}
Also used : HttpResponseMessage(com.netflix.zuul.message.http.HttpResponseMessage) Headers(com.netflix.zuul.message.Headers) HttpResponseMessageImpl(com.netflix.zuul.message.http.HttpResponseMessageImpl) SessionContext(com.netflix.zuul.context.SessionContext)

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