@Nullable @Override public String getRequestPath(@Nullable HttpRequest request) { if (request == null) { return null; } String result = HttpUtils.extractPath(request.uri()); if (StringUtils.isBlank(result)) { return null; } return result; }
@Test public void convertContentChunksToRawString_and_convertContentChunksToRawBytes_returns_null_if_chunks_arg_is_null() { // when String resultString = HttpUtils.convertContentChunksToRawString(CharsetUtil.UTF_8, null); byte[] resultBytes = HttpUtils.convertContentChunksToRawBytes(null); // then assertThat(resultString, nullValue()); assertThat(resultBytes, nullValue()); }
public static String convertContentChunksToRawString(Charset contentCharset, Collection<HttpContent> contentChunks) { byte[] rawBytes = convertContentChunksToRawBytes(contentChunks); if (rawBytes == null) return null; return convertRawBytesToString(contentCharset, rawBytes); }
private void throwExceptionIfContentLengthHeaderIsLargerThanConfiguredMaxRequestSize(HttpRequest msg, Endpoint<?> endpoint) { int configuredMaxRequestSize = getConfiguredMaxRequestSize(endpoint, globalConfiguredMaxRequestSizeInBytes); if (!isMaxRequestSizeValidationDisabled(configuredMaxRequestSize) && HttpHeaders.isContentLengthSet(msg) && HttpHeaders.getContentLength(msg) > configuredMaxRequestSize) { throw new RequestTooBigException( "Content-Length header value exceeded configured max request size of " + configuredMaxRequestSize ); } }
public RequestInfoImpl(HttpRequest request) { this(request.uri(), request.method(), request.headers(), HttpUtils.extractTrailingHeadersIfPossible(request), null, HttpUtils.extractCookies(request), null, HttpUtils.extractContentChunks(request), request.protocolVersion(), HttpUtil.isKeepAlive(request), (request instanceof FullHttpRequest), HttpPostRequestDecoder.isMultipart(request)); }
this.path = QueryStringDecoder.decodeComponent(HttpUtils.extractPath(uri)); this.method = method; this.headers = headers; this.cookies = cookies; setPathParams(pathParams); this.contentCharset = HttpUtils.determineCharsetFromContentType(headers, DEFAULT_CONTENT_CHARSET); if (contentChunks != null) { contentChunks.forEach(this::addContentChunk);
protected Charset determineCharsetToUse(ResponseInfo<?> responseInfo) { // Figure out what charset to use. If responseInfo has one specified via desiredContentWriterEncoding then use // that, otherwise attempt to extract it from the response headers, and use // ResponseInfo.DEFAULT_CONTENT_ENCODING as the "nobody has an opinion" default. return (responseInfo.getDesiredContentWriterEncoding() == null) ? HttpUtils .determineCharsetFromContentType(responseInfo.getHeaders(), ResponseInfo.DEFAULT_CONTENT_ENCODING) : responseInfo.getDesiredContentWriterEncoding(); }
@Override public CompletableFuture<DownstreamRequestFirstChunkInfo> getDownstreamRequestFirstChunkInfo( RequestInfo<?> request, Executor longRunningTaskExecutor, ChannelHandlerContext ctx ) { return CompletableFuture.completedFuture( new DownstreamRequestFirstChunkInfo( downstreamDestinationHost, downstreamDestinationPort, isDownstreamCallHttps, generateSimplePassthroughRequest( request, HttpUtils.replaceUriPathVariables(request, downstreamDestinationUriPath), request.getMethod(), ctx ), customCircuitBreaker, disableCircuitBreaker ) ); } }
/** * {@inheritDoc} */ @Override public synchronized String getRawContent() { if (!isCompleteRequestWithAllChunks) return null; if (rawContent != null) return rawContent; // The raw content string has not been loaded/cached yet. Do that now. rawContent = HttpUtils.convertRawBytesToString(getContentCharset(), getRawContentBytes()); return rawContent; }
@Test public void extractCookies_does_not_use_trailing_headers_if_trailing_headers_is_null() { // given HttpRequest nettyRequestMock = mock(HttpRequest.class); doReturn(new DefaultHttpHeaders()).when(nettyRequestMock).headers(); // when Set<Cookie> extractedCookies = HttpUtils.extractCookies(nettyRequestMock); // then assertThat(extractedCookies, notNullValue()); assertThat(extractedCookies.isEmpty(), is(true)); }
@Test public void extractContentChunks_returns_null_for_request_that_is_not_HttpContent() { // given HttpRequest request = mock(HttpRequest.class); // when List<HttpContent> result = HttpUtils.extractContentChunks(request); // then assertThat(result, nullValue()); }
/** * {@inheritDoc} */ @Override public synchronized byte[] getRawContentBytes() { if (!isCompleteRequestWithAllChunks) return null; if (!contentChunks.isEmpty()) { rawContentBytes = HttpUtils.convertContentChunksToRawBytes(contentChunks); releaseContentChunks(); } return rawContentBytes; }
@Override public String getQueryString() { return HttpUtils.extractQueryString(request.getUri()); }
protected Charset determineCharsetToUse(ResponseInfo<?> responseInfo) { // Figure out what charset to use. If responseInfo has one specified via desiredContentWriterEncoding then use // that, otherwise attempt to extract it from the response headers, and use // ResponseInfo.DEFAULT_CONTENT_ENCODING as the "nobody has an opinion" default. return (responseInfo.getDesiredContentWriterEncoding() == null) ? HttpUtils .determineCharsetFromContentType(responseInfo.getHeaders(), ResponseInfo.DEFAULT_CONTENT_ENCODING) : responseInfo.getDesiredContentWriterEncoding(); }
private void throwExceptionIfContentLengthHeaderIsLargerThanConfiguredMaxRequestSize(HttpRequest msg, Endpoint<?> endpoint) { int configuredMaxRequestSize = getConfiguredMaxRequestSize(endpoint, globalConfiguredMaxRequestSizeInBytes); if (!isMaxRequestSizeValidationDisabled(configuredMaxRequestSize) && HttpHeaders.isContentLengthSet(msg) && HttpHeaders.getContentLength(msg) > configuredMaxRequestSize) { throw new RequestTooBigException( "Content-Length header value exceeded configured max request size of " + configuredMaxRequestSize ); } }
@Override public CompletableFuture<DownstreamRequestFirstChunkInfo> getDownstreamRequestFirstChunkInfo( RequestInfo<?> request, Executor longRunningTaskExecutor, ChannelHandlerContext ctx ) { return CompletableFuture.completedFuture( new DownstreamRequestFirstChunkInfo( downstreamDestinationHost, downstreamDestinationPort, isDownstreamCallHttps, generateSimplePassthroughRequest( request, HttpUtils.replaceUriPathVariables(request, downstreamDestinationUriPath), request.getMethod(), ctx ), customCircuitBreaker, disableCircuitBreaker ) ); } }
@Test public void convertRawBytesToString_returns_null_when_byte_array_is_empty() { // expect assertThat(HttpUtils.convertRawBytesToString(CharsetUtil.UTF_8, new byte[0]), is("")); }
@Test public void extractCookies_returns_empty_set_if_no_cookies_defined() { // given FullHttpRequest nettyRequestMock = mock(FullHttpRequest.class); doReturn(new DefaultHttpHeaders()).when(nettyRequestMock).headers(); doReturn(new DefaultHttpHeaders()).when(nettyRequestMock).trailingHeaders(); // when Set<Cookie> extractedCookies = HttpUtils.extractCookies(nettyRequestMock); // then assertThat(extractedCookies, notNullValue()); assertThat(extractedCookies.isEmpty(), is(true)); }
@Test public void extractContentChunks_works_for_request_that_is_also_HttpContent() { // given HttpRequest request = mock(FullHttpRequest.class); // when List<HttpContent> result = HttpUtils.extractContentChunks(request); // then assertThat(result, notNullValue()); assertThat(result.size(), is(1)); assertThat(result.get(0), is(request)); }
@Test public void convertContentChunksToRawBytes_returns_null_if_total_bytes_is_zero() { // given Collection<HttpContent> chunkCollection = Arrays.asList(new DefaultHttpContent(new EmptyByteBuf(ByteBufAllocator.DEFAULT)), new DefaultHttpContent(new EmptyByteBuf(ByteBufAllocator.DEFAULT))); // when byte[] resultBytes = HttpUtils.convertContentChunksToRawBytes(chunkCollection); // then assertThat(resultBytes, nullValue()); }