Search in sources :

Example 11 with HttpResponseMessage

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

the class ZuulEndPointRunner method filter.

@Override
public void filter(final HttpRequestMessage zuulReq, final HttpContent chunk) {
    if (zuulReq.getContext().isCancelled()) {
        chunk.release();
        return;
    }
    String endpointName = "-";
    try (TaskCloseable ignored = PerfMark.traceTask(this, s -> s.getClass().getSimpleName() + ".filterChunk")) {
        addPerfMarkTags(zuulReq);
        ZuulFilter<HttpRequestMessage, HttpResponseMessage> endpoint = Preconditions.checkNotNull(getEndpoint(zuulReq), "endpoint");
        endpointName = endpoint.filterName();
        final HttpContent newChunk = endpoint.processContentChunk(zuulReq, chunk);
        if (newChunk != null) {
            // Endpoints do not directly forward content chunks to next stage in the filter chain.
            zuulReq.bufferBodyContents(newChunk);
            // deallocate original chunk if necessary
            if (newChunk != chunk) {
                chunk.release();
            }
            if (isFilterAwaitingBody(zuulReq) && zuulReq.hasCompleteBody() && !(endpoint instanceof ProxyEndpoint)) {
                // whole body has arrived, resume filter chain
                invokeNextStage(filter(endpoint, zuulReq));
            }
        }
    } catch (Exception ex) {
        handleException(zuulReq, endpointName, ex);
    }
}
Also used : HttpResponseMessage(com.netflix.zuul.message.http.HttpResponseMessage) HttpRequestMessage(com.netflix.zuul.message.http.HttpRequestMessage) ProxyEndpoint(com.netflix.zuul.filters.endpoint.ProxyEndpoint) HttpContent(io.netty.handler.codec.http.HttpContent) TaskCloseable(io.perfmark.TaskCloseable)

Example 12 with HttpResponseMessage

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

the class ZuulFilterChainRunner method filter.

@Override
public void filter(T inMesg, HttpContent chunk) {
    String filterName = "-";
    try (TaskCloseable ignored = PerfMark.traceTask(this, s -> s.getClass().getSimpleName() + ".filterChunk")) {
        addPerfMarkTags(inMesg);
        Preconditions.checkNotNull(inMesg, "input message");
        final AtomicInteger runningFilterIdx = getRunningFilterIndex(inMesg);
        final int limit = runningFilterIdx.get();
        for (int i = 0; i < limit; i++) {
            final ZuulFilter<T, T> filter = filters[i];
            filterName = filter.filterName();
            if ((!filter.isDisabled()) && (!shouldSkipFilter(inMesg, filter))) {
                final HttpContent newChunk = filter.processContentChunk(inMesg, chunk);
                if (newChunk == null) {
                    // Filter wants to break the chain and stop propagating this chunk any further
                    return;
                }
                // deallocate original chunk if necessary
                if ((newChunk != chunk) && (chunk.refCnt() > 0)) {
                    chunk.release(chunk.refCnt());
                }
                chunk = newChunk;
            }
        }
        if (limit >= filters.length) {
            // Filter chain has run to end, pass down the channel pipeline
            invokeNextStage(inMesg, chunk);
        } else {
            inMesg.bufferBodyContents(chunk);
            boolean isAwaitingBody = isFilterAwaitingBody(inMesg);
            // Record passport states for start and end of buffering bodies.
            if (isAwaitingBody) {
                CurrentPassport passport = CurrentPassport.fromSessionContext(inMesg.getContext());
                if (inMesg.hasCompleteBody()) {
                    if (inMesg instanceof HttpRequestMessage) {
                        passport.addIfNotAlready(PassportState.FILTERS_INBOUND_BUF_END);
                    } else if (inMesg instanceof HttpResponseMessage) {
                        passport.addIfNotAlready(PassportState.FILTERS_OUTBOUND_BUF_END);
                    }
                } else {
                    if (inMesg instanceof HttpRequestMessage) {
                        passport.addIfNotAlready(PassportState.FILTERS_INBOUND_BUF_START);
                    } else if (inMesg instanceof HttpResponseMessage) {
                        passport.addIfNotAlready(PassportState.FILTERS_OUTBOUND_BUF_START);
                    }
                }
            }
            if (isAwaitingBody && inMesg.hasCompleteBody()) {
                // whole body has arrived, resume filter chain
                runFilters(inMesg, runningFilterIdx);
            }
        }
    } catch (Exception ex) {
        handleException(inMesg, filterName, ex);
    }
}
Also used : CurrentPassport(com.netflix.zuul.passport.CurrentPassport) HttpResponseMessage(com.netflix.zuul.message.http.HttpResponseMessage) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpRequestMessage(com.netflix.zuul.message.http.HttpRequestMessage) HttpContent(io.netty.handler.codec.http.HttpContent) TaskCloseable(io.perfmark.TaskCloseable)

Example 13 with HttpResponseMessage

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

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

HttpResponseMessage (com.netflix.zuul.message.http.HttpResponseMessage)14 HttpRequestMessage (com.netflix.zuul.message.http.HttpRequestMessage)8 SessionContext (com.netflix.zuul.context.SessionContext)5 HttpContent (io.netty.handler.codec.http.HttpContent)5 Headers (com.netflix.zuul.message.Headers)3 HttpResponseMessageImpl (com.netflix.zuul.message.http.HttpResponseMessageImpl)3 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)3 TaskCloseable (io.perfmark.TaskCloseable)3 Test (org.junit.Test)3 ProxyEndpoint (com.netflix.zuul.filters.endpoint.ProxyEndpoint)2 HttpRequestInfo (com.netflix.zuul.message.http.HttpRequestInfo)2 CurrentPassport (com.netflix.zuul.passport.CurrentPassport)2 DefaultHttpContent (io.netty.handler.codec.http.DefaultHttpContent)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 GZIPInputStream (java.util.zip.GZIPInputStream)2 ClientException (com.netflix.client.ClientException)1 ZuulException (com.netflix.zuul.exception.ZuulException)1 ZuulFilter (com.netflix.zuul.filters.ZuulFilter)1 MissingEndpointHandlingFilter (com.netflix.zuul.filters.endpoint.MissingEndpointHandlingFilter)1