Search in sources :

Example 21 with RequestInfo

use of com.nike.riposte.server.http.RequestInfo in project riposte by Nike-Inc.

the class RequestFilterHandler method handleFilterLogic.

protected PipelineContinuationBehavior handleFilterLogic(ChannelHandlerContext ctx, Object msg, BiFunction<RequestAndResponseFilter, RequestInfo, RequestInfo> normalFilterCall, BiFunction<RequestAndResponseFilter, RequestInfo, Pair<RequestInfo, Optional<ResponseInfo<?>>>> shortCircuitFilterCall) {
    HttpProcessingState state = ChannelAttributes.getHttpProcessingStateForChannel(ctx).get();
    RequestInfo<?> currentReqInfo = state.getRequestInfo();
    // Run through each filter.
    for (RequestAndResponseFilter filter : filters) {
        try {
            // See if we're supposed to do short circuit call or not
            if (filter.isShortCircuitRequestFilter()) {
                Pair<RequestInfo, Optional<ResponseInfo<?>>> result = shortCircuitFilterCall.apply(filter, currentReqInfo);
                if (result != null) {
                    currentReqInfo = requestInfoUpdateNoNulls(currentReqInfo, result.getLeft());
                    // See if we need to short circuit.
                    ResponseInfo<?> responseInfo = (result.getRight() == null) ? null : result.getRight().orElse(null);
                    if (responseInfo != null) {
                        //      full, not chunked.
                        if (responseInfo.isChunkedResponse()) {
                            throw new IllegalStateException("RequestAndResponseFilter should never return a " + "chunked ResponseInfo when short circuiting.");
                        }
                        state.setRequestInfo(currentReqInfo);
                        state.setResponseInfo(responseInfo);
                        // Fire the short-circuit event that will get the desired response info sent to the caller.
                        ctx.fireChannelRead(LastOutboundMessageSendFullResponseInfo.INSTANCE);
                        // Tell this event to stop where it is.
                        return PipelineContinuationBehavior.DO_NOT_FIRE_CONTINUE_EVENT;
                    }
                }
            } else {
                currentReqInfo = requestInfoUpdateNoNulls(currentReqInfo, normalFilterCall.apply(filter, currentReqInfo));
            }
        } catch (Throwable ex) {
            logger.error("An error occurred while processing a request filter. This error will be ignored and the " + "filtering/processing will continue normally, however this error should be fixed (filters should " + "never throw errors). filter_class={}", filter.getClass().getName(), ex);
        }
    }
    // All the filters have been processed, so set the state to whatever the current request info says.
    state.setRequestInfo(currentReqInfo);
    // No short circuit if we reach here, so continue normally.
    return PipelineContinuationBehavior.CONTINUE;
}
Also used : Optional(java.util.Optional) RequestAndResponseFilter(com.nike.riposte.server.http.filter.RequestAndResponseFilter) HttpProcessingState(com.nike.riposte.server.http.HttpProcessingState) RequestInfo(com.nike.riposte.server.http.RequestInfo)

Example 22 with RequestInfo

use of com.nike.riposte.server.http.RequestInfo in project riposte by Nike-Inc.

the class RequestInfoSetterHandler method doChannelRead.

@Override
public PipelineContinuationBehavior doChannelRead(ChannelHandlerContext ctx, Object msg) {
    try {
        HttpProcessingState state = ChannelAttributes.getHttpProcessingStateForChannel(ctx).get();
        if (state == null || state.isResponseSendingLastChunkSent()) {
            if (state == null)
                logger.error("HttpProcessingState is null for this request. This should not be possible.");
            //      anything further.
            return PipelineContinuationBehavior.DO_NOT_FIRE_CONTINUE_EVENT;
        }
        // We have a HttpProcessingState. Process the message and continue the pipeline processing.
        if (msg instanceof HttpRequest) {
            throwExceptionIfNotSuccessfullyDecoded((HttpRequest) msg);
            RequestInfo<?> requestInfo = new RequestInfoImpl<>((HttpRequest) msg);
            state.setRequestInfo(requestInfo);
        } else if (msg instanceof HttpContent) {
            HttpContent httpContentMsg = (HttpContent) msg;
            throwExceptionIfNotSuccessfullyDecoded(httpContentMsg);
            RequestInfo<?> requestInfo = state.getRequestInfo();
            if (requestInfo == null) {
                throw new IllegalStateException("Found a HttpContent msg without a RequestInfo stored in the HttpProcessingState. " + "This should be impossible");
            }
            int currentRequestLengthInBytes = requestInfo.addContentChunk(httpContentMsg);
            int configuredMaxRequestSize = getConfiguredMaxRequestSize(state.getEndpointForExecution(), globalConfiguredMaxRequestSizeInBytes);
            if (!isMaxRequestSizeValidationDisabled(configuredMaxRequestSize) && currentRequestLengthInBytes > configuredMaxRequestSize) {
                throw new TooLongFrameException("Request raw content length exceeded configured max request size of " + configuredMaxRequestSize);
            }
        }
        return PipelineContinuationBehavior.CONTINUE;
    } finally {
        // For HttpContent messages, either requestInfo.addContentChunk() has been called and the reference count
        //      increased (i.e. the RequestInfo is now responsible for releasing the content when
        //      requestInfo.releaseAllResources() is called), or an exception has been thrown. In any case, we
        //      are done with any message from a pipeline perspective and can reduce its reference count.
        ReferenceCountUtil.release(msg);
    }
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) TooLongFrameException(io.netty.handler.codec.TooLongFrameException) HttpProcessingState(com.nike.riposte.server.http.HttpProcessingState) RequestInfoImpl(com.nike.riposte.server.http.impl.RequestInfoImpl) RequestInfo(com.nike.riposte.server.http.RequestInfo) HttpContent(io.netty.handler.codec.http.HttpContent)

Example 23 with RequestInfo

use of com.nike.riposte.server.http.RequestInfo in project riposte by Nike-Inc.

the class RoutingHandler method doChannelRead.

@Override
public PipelineContinuationBehavior doChannelRead(ChannelHandlerContext ctx, Object msg) {
    if (msg instanceof HttpRequest) {
        HttpProcessingState state = ChannelAttributes.getHttpProcessingStateForChannel(ctx).get();
        RequestInfo request = state.getRequestInfo();
        Pair<Endpoint<?>, String> endpointForExecution = findSingleEndpointForExecution(request);
        throwExceptionIfContentLengthHeaderIsLargerThanConfiguredMaxRequestSize((HttpRequest) msg, endpointForExecution.getLeft());
        request.setPathParamsBasedOnPathTemplate(endpointForExecution.getRight());
        state.setEndpointForExecution(endpointForExecution.getLeft(), endpointForExecution.getRight());
    }
    return PipelineContinuationBehavior.CONTINUE;
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) Endpoint(com.nike.riposte.server.http.Endpoint) HttpProcessingState(com.nike.riposte.server.http.HttpProcessingState) RequestInfo(com.nike.riposte.server.http.RequestInfo)

Example 24 with RequestInfo

use of com.nike.riposte.server.http.RequestInfo in project riposte by Nike-Inc.

the class RequestInfoSetterHandlerTest method doChannelRead_creates_and_sets_RequestInfo_on_state_and_RequestInfo_is_marked_as_complete_with_all_chunks_if_msg_is_FullHttpRequest.

@Test
public void doChannelRead_creates_and_sets_RequestInfo_on_state_and_RequestInfo_is_marked_as_complete_with_all_chunks_if_msg_is_FullHttpRequest() {
    // given
    FullHttpRequest msgMock = mock(FullHttpRequest.class);
    String uri = "/some/url";
    HttpHeaders headers = new DefaultHttpHeaders();
    doReturn(uri).when(msgMock).getUri();
    doReturn(headers).when(msgMock).headers();
    doReturn(headers).when(msgMock).trailingHeaders();
    doReturn(byteBufMock).when(msgMock).content();
    doReturn(false).when(byteBufMock).isReadable();
    doReturn(HttpVersion.HTTP_1_1).when(msgMock).getProtocolVersion();
    // when
    PipelineContinuationBehavior result = handler.doChannelRead(ctxMock, msgMock);
    // then
    ArgumentCaptor<RequestInfo> requestInfoArgumentCaptor = ArgumentCaptor.forClass(RequestInfo.class);
    verify(stateMock).setRequestInfo(requestInfoArgumentCaptor.capture());
    RequestInfo requestInfo = requestInfoArgumentCaptor.getValue();
    assertThat(requestInfo.getUri()).isEqualTo(uri);
    assertThat(requestInfo.isCompleteRequestWithAllChunks()).isTrue();
    assertThat(result).isEqualTo(PipelineContinuationBehavior.CONTINUE);
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) PipelineContinuationBehavior(com.nike.riposte.server.handler.base.PipelineContinuationBehavior) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) RequestInfo(com.nike.riposte.server.http.RequestInfo) Test(org.junit.Test)

Example 25 with RequestInfo

use of com.nike.riposte.server.http.RequestInfo in project riposte by Nike-Inc.

the class BasicAuthSecurityValidatorTest method validateNullAuthHeader.

@Test(expected = Unauthorized401Exception.class)
public void validateNullAuthHeader() {
    RequestInfo mockRequest = mock(RequestInfo.class);
    doReturn(mock(HttpHeaders.class)).when(mockRequest).getHeaders();
    when(mockRequest.getHeaders().get("Authorization")).thenReturn(null);
    underTest.validateSecureRequestForEndpoint(mockRequest, mockEndpoint1);
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) RequestInfo(com.nike.riposte.server.http.RequestInfo) Test(org.junit.Test)

Aggregations

RequestInfo (com.nike.riposte.server.http.RequestInfo)39 Test (org.junit.Test)30 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)13 HttpProcessingState (com.nike.riposte.server.http.HttpProcessingState)10 ResponseInfo (com.nike.riposte.server.http.ResponseInfo)9 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 PipelineContinuationBehavior (com.nike.riposte.server.handler.base.PipelineContinuationBehavior)6 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)6 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)6 Pair (com.nike.internal.util.Pair)5 RequestInfoForLogging (com.nike.backstopper.handler.RequestInfoForLogging)4 Endpoint (com.nike.riposte.server.http.Endpoint)4 HttpMethod (io.netty.handler.codec.http.HttpMethod)4 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)4 Optional (java.util.Optional)4 Assertions.catchThrowable (org.assertj.core.api.Assertions.catchThrowable)4 LastOutboundMessageSendFullResponseInfo (com.nike.riposte.server.channelpipeline.message.LastOutboundMessageSendFullResponseInfo)3 ChannelAttributes (com.nike.riposte.server.channelpipeline.ChannelAttributes)2 BaseInboundHandlerWithTracingAndMdcSupport (com.nike.riposte.server.handler.base.BaseInboundHandlerWithTracingAndMdcSupport)2 ProxyRouterProcessingState (com.nike.riposte.server.http.ProxyRouterProcessingState)2