Search in sources :

Example 16 with HttpRequestMessage

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

Aggregations

HttpRequestMessage (com.netflix.zuul.message.http.HttpRequestMessage)16 HttpResponseMessage (com.netflix.zuul.message.http.HttpResponseMessage)8 SessionContext (com.netflix.zuul.context.SessionContext)6 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)4 HttpContent (io.netty.handler.codec.http.HttpContent)4 HttpRequest (io.netty.handler.codec.http.HttpRequest)4 TaskCloseable (io.perfmark.TaskCloseable)4 Test (org.junit.Test)4 ProxyEndpoint (com.netflix.zuul.filters.endpoint.ProxyEndpoint)3 ByteBuf (io.netty.buffer.ByteBuf)3 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)3 CompleteEvent (com.netflix.netty.common.HttpLifecycleChannelHandler.CompleteEvent)2 DefaultRegistry (com.netflix.spectator.api.DefaultRegistry)2 Headers (com.netflix.zuul.message.Headers)2 HttpQueryParams (com.netflix.zuul.message.http.HttpQueryParams)2 HttpRequestMessageImpl (com.netflix.zuul.message.http.HttpRequestMessageImpl)2 PassportLoggingHandler (com.netflix.zuul.netty.insights.PassportLoggingHandler)2 CurrentPassport (com.netflix.zuul.passport.CurrentPassport)2 HttpRequestEncoder (io.netty.handler.codec.http.HttpRequestEncoder)2 HttpServerCodec (io.netty.handler.codec.http.HttpServerCodec)2