Search in sources :

Example 86 with LastHttpContent

use of io.netty.handler.codec.http.LastHttpContent in project riposte by Nike-Inc.

the class DTraceStartHandler method doChannelRead.

@Override
public PipelineContinuationBehavior doChannelRead(ChannelHandlerContext ctx, Object msg) {
    // We only do the processing for HttpRequest which is the first message in a request chain.
    if (shouldHandleDoChannelReadMessage(msg)) {
        try {
            startTrace((HttpRequest) msg, ctx);
        } catch (Throwable t) {
            logger.error("An unexpected error occurred while starting the distributed tracing overall request span. This " + "exception will be swallowed to avoid breaking the Netty pipeline, but it should be " + "investigated as it shouldn't ever happen.", t);
        }
    }
    if (msg instanceof LastHttpContent) {
        HttpProcessingState httpProcessingState = ChannelAttributes.getHttpProcessingStateForChannel(ctx).get();
        // Add the "we received the last bytes of the request on the wire" annotation to the span if possible
        // and desired.
        Span requestSpan = handlerUtils.getOverallRequestSpan(httpProcessingState);
        if (requestSpan != null && spanNamingAndTaggingStrategy.shouldAddWireReceiveFinishAnnotation()) {
            requestSpan.addTimestampedAnnotationForCurrentTime(spanNamingAndTaggingStrategy.wireReceiveFinishAnnotationName());
        }
    }
    return PipelineContinuationBehavior.CONTINUE;
}
Also used : HttpProcessingState(com.nike.riposte.server.http.HttpProcessingState) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) Span(com.nike.wingtips.Span)

Example 87 with LastHttpContent

use of io.netty.handler.codec.http.LastHttpContent in project riposte by Nike-Inc.

the class RequestContentDeserializerHandler method doChannelRead.

@Override
public PipelineContinuationBehavior doChannelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof LastHttpContent) {
        HttpProcessingState state = ChannelAttributes.getHttpProcessingStateForChannel(ctx).get();
        Endpoint<?> endpoint = state.getEndpointForExecution();
        RequestInfo reqInfo = state.getRequestInfo();
        // Don't bother trying to deserialize until we have an endpoint and the request content has fully arrived
        if (endpoint != null && reqInfo.isCompleteRequestWithAllChunks()) {
            // Setup the content deserializer if desired
            TypeReference<?> contentTypeRef = endpoint.requestContentType();
            if (contentTypeRef != null) {
                // A non-null TypeReference is available, so deserialization is possible. Retrieve the appropriate
                // deserializer and setup the RequestInfo so that it can lazily deserialize when requested.
                ObjectMapper deserializer = endpoint.customRequestContentDeserializer(reqInfo);
                if (deserializer == null)
                    deserializer = defaultRequestContentDeserializer;
                // noinspection unchecked
                reqInfo.setupContentDeserializer(deserializer, contentTypeRef);
            }
        }
    }
    return PipelineContinuationBehavior.CONTINUE;
}
Also used : HttpProcessingState(com.nike.riposte.server.http.HttpProcessingState) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) RequestInfo(com.nike.riposte.server.http.RequestInfo) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 88 with LastHttpContent

use of io.netty.handler.codec.http.LastHttpContent in project riposte by Nike-Inc.

the class AccessLogStartHandler method doChannelRead.

@Override
public PipelineContinuationBehavior doChannelRead(ChannelHandlerContext ctx, Object msg) {
    if (msg instanceof HttpRequest) {
        HttpProcessingState httpProcessingState = ChannelAttributes.getHttpProcessingStateForChannel(ctx).get();
        if (httpProcessingState != null) {
            httpProcessingState.setRequestStartTime(Instant.now());
            httpProcessingState.setRequestStartTimeNanos(System.nanoTime());
        } else {
            runnableWithTracingAndMdc(() -> logger.warn("HttpProcessingState is null reading HttpRequest. This shouldn't happen."), ctx).run();
        }
    }
    if (msg instanceof LastHttpContent) {
        HttpProcessingState httpProcessingState = ChannelAttributes.getHttpProcessingStateForChannel(ctx).get();
        if (httpProcessingState != null) {
            // Capture the current nano time as the time when the last chunk of the payload was received.
            long lastChunkArrivedTimeNanos = System.nanoTime();
            httpProcessingState.setRequestLastChunkArrivedTimeNanos(lastChunkArrivedTimeNanos);
            // Calculate and set a request attribute for the payload transfer time from the caller.
            // Note that this might be both a HttpRequest and LastHttpContent (e.g. in the case of
            // a FullHttpRequest). If it's an HttpRequest then the RequestInfo might not have been
            // set on the HttpProcessingState yet, so we'll call RiposteHandlerInternalUtil to create
            // a RequestInfo and set it on the HttpProcessingState if necessary.
            RequestInfo<?> requestInfo = (msg instanceof HttpRequest) ? handlerUtils.createRequestInfoFromNettyHttpRequestAndHandleStateSetupIfNecessary((HttpRequest) msg, httpProcessingState) : httpProcessingState.getRequestInfo();
            if (requestInfo != null) {
                Long reqStartTimeNanos = httpProcessingState.getRequestStartTimeNanos();
                if (reqStartTimeNanos != null) {
                    requestInfo.addRequestAttribute(REQUEST_PAYLOAD_TRANSFER_TIME_NANOS_REQUEST_ATTR_KEY, (lastChunkArrivedTimeNanos - reqStartTimeNanos));
                } else {
                    runnableWithTracingAndMdc(() -> logger.warn("HttpProcessingState.getRequestStartTimeNanos() is null. " + "This shouldn't happen."), ctx).run();
                }
            } else {
                runnableWithTracingAndMdc(() -> logger.warn("RequestInfo is null. This shouldn't happen."), ctx).run();
            }
        } else {
            runnableWithTracingAndMdc(() -> logger.warn("HttpProcessingState is null reading LastHttpContent. This shouldn't happen."), ctx).run();
        }
    }
    return PipelineContinuationBehavior.CONTINUE;
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) HttpProcessingState(com.nike.riposte.server.http.HttpProcessingState) LastHttpContent(io.netty.handler.codec.http.LastHttpContent)

Example 89 with LastHttpContent

use of io.netty.handler.codec.http.LastHttpContent in project riposte by Nike-Inc.

the class RequestInfoImplTest method addContentChunk_does_not_throw_IllegalStateException_if_requestInfo_trailingHeaders_is_already_populated_when_last_chunk_arrives_if_same_instance.

@Test
public void addContentChunk_does_not_throw_IllegalStateException_if_requestInfo_trailingHeaders_is_already_populated_when_last_chunk_arrives_if_same_instance() {
    // given
    RequestInfoImpl<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests();
    requestInfo.isCompleteRequestWithAllChunks = false;
    LastHttpContent lastChunk = new DefaultLastHttpContent(Unpooled.copiedBuffer(UUID.randomUUID().toString(), CharsetUtil.UTF_8));
    lastChunk.trailingHeaders().add("somekey", "someval");
    requestInfo.trailingHeaders = lastChunk.trailingHeaders();
    // when
    requestInfo.addContentChunk(lastChunk);
    // then
    assertThat(requestInfo.trailingHeaders, is(lastChunk.trailingHeaders()));
}
Also used : DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) Test(org.junit.Test)

Example 90 with LastHttpContent

use of io.netty.handler.codec.http.LastHttpContent in project riposte by Nike-Inc.

the class RequestInfoImplTest method addContentChunk_throws_IllegalStateException_if_requestInfo_trailingHeaders_is_already_populated_when_last_chunk_arrives.

@Test(expected = IllegalStateException.class)
public void addContentChunk_throws_IllegalStateException_if_requestInfo_trailingHeaders_is_already_populated_when_last_chunk_arrives() {
    // given
    RequestInfoImpl<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests();
    requestInfo.isCompleteRequestWithAllChunks = false;
    LastHttpContent lastChunk = new DefaultLastHttpContent(Unpooled.copiedBuffer(UUID.randomUUID().toString(), CharsetUtil.UTF_8));
    requestInfo.trailingHeaders.add("somekey", "someval");
    // expect
    requestInfo.addContentChunk(lastChunk);
    fail("Expected an IllegalStateException, but no exception was thrown");
}
Also used : DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) Test(org.junit.Test)

Aggregations

LastHttpContent (io.netty.handler.codec.http.LastHttpContent)137 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)63 HttpContent (io.netty.handler.codec.http.HttpContent)55 ByteBuf (io.netty.buffer.ByteBuf)49 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)43 HttpResponse (io.netty.handler.codec.http.HttpResponse)42 HttpRequest (io.netty.handler.codec.http.HttpRequest)34 Test (org.junit.Test)27 Test (org.junit.jupiter.api.Test)24 DefaultHttpContent (io.netty.handler.codec.http.DefaultHttpContent)23 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)20 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)18 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)13 ChannelFuture (io.netty.channel.ChannelFuture)11 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)10 HttpObject (io.netty.handler.codec.http.HttpObject)10 JsonObjectDecoder (io.netty.handler.codec.json.JsonObjectDecoder)10 IOException (java.io.IOException)10 HttpProcessingState (com.nike.riposte.server.http.HttpProcessingState)9 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)9