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;
}
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);
}
}
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;
}
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);
}
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);
}
Aggregations