Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude URI query from remaining WebClient checkpoints #31992

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -195,6 +195,18 @@ private static <T> Mono<T> releaseIfNotConsumed(ClientResponse response, Throwab
return response.releaseBody().onErrorComplete().then(Mono.error(ex));
}

private static URI getUriToLog(URI uri) {
if (StringUtils.hasText(uri.getQuery())) {
try {
uri = new URI(uri.getScheme(), null, uri.getHost(), uri.getPort(), uri.getPath(), null, null);
}
catch (URISyntaxException ex) {
// ignore
}
}
return uri;
}


private class DefaultRequestBodyUriSpec implements RequestBodyUriSpec {

Expand Down Expand Up @@ -457,7 +469,7 @@ public Mono<ClientResponse> exchange() {
observationContext.setRequest(request);
Mono<ClientResponse> responseMono = filterFunction.apply(exchangeFunction)
.exchange(request)
.checkpoint("Request to " + this.httpMethod.name() + " " + this.uri + " [DefaultWebClient]")
.checkpoint("Request to " + this.httpMethod.name() + " " + getUriToLog(request.url()) + " [DefaultWebClient]")
.switchIfEmpty(NO_HTTP_CLIENT_RESPONSE_ERROR);
if (this.contextModifier != null) {
responseMono = responseMono.contextWrite(this.contextModifier);
Expand Down Expand Up @@ -698,18 +710,6 @@ private <T> Mono<T> applyStatusHandlers(ClientResponse response) {
return null;
}

private static URI getUriToLog(URI uri) {
if (StringUtils.hasText(uri.getQuery())) {
try {
uri = new URI(uri.getScheme(), null, uri.getHost(), uri.getPort(), uri.getPath(), null, null);
}
catch (URISyntaxException ex) {
// ignore
}
}
return uri;
}


private static class StatusHandler {

Expand Down
Expand Up @@ -16,6 +16,8 @@

package org.springframework.web.reactive.function.client;

import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;
Expand All @@ -30,6 +32,7 @@
import org.springframework.http.HttpStatusCode;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
* Exceptions that contain actual HTTP response data.
Expand Down Expand Up @@ -99,7 +102,19 @@ public WebClientResponseException(

private static String initMessage(HttpStatusCode status, String reasonPhrase, @Nullable HttpRequest request) {
return status.value() + " " + reasonPhrase +
(request != null ? " from " + request.getMethod() + " " + request.getURI() : "");
(request != null ? " from " + request.getMethod() + " " + getUriToLog(request.getURI()) : "");
}

private static URI getUriToLog(URI uri) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this method should go in WebClientUtils? 🤔

if (StringUtils.hasText(uri.getQuery())) {
try {
uri = new URI(uri.getScheme(), null, uri.getHost(), uri.getPort(), uri.getPath(), null, null);
}
catch (URISyntaxException ex) {
// ignore
}
}
return uri;
}

/**
Expand Down
Expand Up @@ -17,6 +17,7 @@
package org.springframework.web.reactive.function.client;

import java.net.InetSocketAddress;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
Expand All @@ -34,7 +35,9 @@
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpRange;
import org.springframework.http.HttpRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
Expand All @@ -43,6 +46,7 @@
import org.springframework.http.client.reactive.ClientHttpResponse;
import org.springframework.http.codec.DecoderHttpMessageReader;
import org.springframework.http.codec.json.Jackson2JsonDecoder;
import org.springframework.lang.Nullable;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

Expand All @@ -67,13 +71,16 @@ class DefaultClientResponseTests {

private final ExchangeStrategies mockExchangeStrategies = mock();

@Nullable
private HttpRequest httpRequest = null;

private DefaultClientResponse defaultClientResponse;


@BeforeEach
void configureMocks() {
given(mockResponse.getHeaders()).willReturn(this.httpHeaders);
defaultClientResponse = new DefaultClientResponse(mockResponse, mockExchangeStrategies, "", "", () -> null);
defaultClientResponse = new DefaultClientResponse(mockResponse, mockExchangeStrategies, "", "", () -> httpRequest);
}


Expand Down Expand Up @@ -302,13 +309,30 @@ void createException() {
given(mockResponse.getStatusCode()).willReturn(HttpStatus.NOT_FOUND);
given(mockResponse.getBody()).willReturn(Flux.just(dataBuffer));

httpRequest = new HttpRequest() {
@Override
public HttpMethod getMethod() {
return HttpMethod.valueOf("UNKNOWN");
}

@Override
public URI getURI() {
return URI.create("https://user:pass@example.org:9999/app/path?token=secret#fragment");
}

@Override
public HttpHeaders getHeaders() {
return HttpHeaders.EMPTY;
}
};

given(mockExchangeStrategies.messageReaders()).willReturn(
List.of(new DecoderHttpMessageReader<>(new ByteArrayDecoder())));

Mono<WebClientResponseException> resultMono = defaultClientResponse.createException();
WebClientResponseException exception = resultMono.block();
assertThat(exception.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
assertThat(exception.getMessage()).isEqualTo("404 Not Found");
assertThat(exception.getMessage()).isEqualTo("404 Not Found from UNKNOWN https://example.org:9999/app/path");
assertThat(exception.getHeaders()).containsExactly(entry("Content-Type", List.of("text/plain")));
assertThat(exception.getResponseBodyAsByteArray()).isEqualTo(bytes);
}
Expand Down