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

Allow setting URL encoding/decoding to RFC 3986 #11513

Open
wants to merge 8 commits into
base: 4.8.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.micronaut.http.HttpStatus;
import io.micronaut.http.client.exceptions.HttpClientResponseException;
import io.micronaut.http.hateoas.JsonError;
import io.micronaut.http.uri.URLEncodingKind;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;

Expand Down Expand Up @@ -70,6 +71,16 @@ public interface HttpClient extends Closeable, LifeCycle<HttpClient> {
*/
<I, O, E> Publisher<HttpResponse<O>> exchange(@NonNull HttpRequest<I> request, @NonNull Argument<O> bodyType, @NonNull Argument<E> errorType);

/**
* If configuration is defined for the client this method will return the configuration.
*
* @return The configuration used to define this client.
* @since 4.8.0
*/
default Optional<URLEncodingKind> getUrlEncodingKind() {
return Optional.empty();
}

/**
* <p>Perform an HTTP request for the given request object emitting the full HTTP response from returned
* {@link Publisher} and converting the response body to the specified type.</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.micronaut.http.HttpVersion;
import io.micronaut.http.ssl.ClientSslConfiguration;
import io.micronaut.http.ssl.SslConfiguration;
import io.micronaut.http.uri.URLEncodingKind;
import io.micronaut.logging.LogLevel;
import io.micronaut.runtime.ApplicationConfiguration;

Expand Down Expand Up @@ -184,6 +185,7 @@ public abstract class HttpClientConfiguration {
private String addressResolverGroupName = null;

private String pcapLoggingPathPattern = null;
private @Nullable URLEncodingKind urlEncodingKind;

/**
* Default constructor.
Expand Down Expand Up @@ -231,6 +233,29 @@ public HttpClientConfiguration(HttpClientConfiguration copy) {
this.sslConfiguration = copy.sslConfiguration;
this.threadFactory = copy.threadFactory;
this.httpVersion = copy.httpVersion;
this.urlEncodingKind = copy.urlEncodingKind;
}
}

/**
* The method to use to encode URLs.
* @return The method to use.
*
* @since 4.8.0
*/
public @Nullable URLEncodingKind getUrlEncoding() {
return urlEncodingKind;
}

/**
* Sets the URL encoding method to use when encoding paths
*
* @param urlEncodingKind The URL encoding method.
* @since 4.8.0
*/
public void setUrlEncoding(URLEncodingKind urlEncodingKind) {
if (urlEncodingKind != null) {
this.urlEncodingKind = urlEncodingKind;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
import io.micronaut.aop.Introduction;
import io.micronaut.context.annotation.AliasFor;
import io.micronaut.core.annotation.Experimental;
import io.micronaut.core.annotation.NextMajorVersion;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.http.HttpVersion;
import io.micronaut.http.annotation.Consumes;
import io.micronaut.http.annotation.Produces;
import io.micronaut.http.client.HttpClientConfiguration;
import io.micronaut.http.client.HttpVersionSelection;
import io.micronaut.http.hateoas.JsonError;
import io.micronaut.http.uri.URLEncodingKind;
import jakarta.inject.Singleton;

import java.lang.annotation.Documented;
Expand Down Expand Up @@ -135,6 +137,20 @@ String[] alpnModes() default {
HttpVersionSelection.ALPN_HTTP_1
};

/**
* The kind of URI encoding to apply.
*
* <p>Defaults to {@link URLEncodingKind#RFC_1866}.</p>
*
* <p>Can be overridden here or via configuration globally.</p>
*
* @return The URL encoding kind.
* @since 4.8.0
* @see HttpClientConfiguration#setUrlEncoding(URLEncodingKind)
*/
@NextMajorVersion("Change default to RFC_3986")
URLEncodingKind urlEncoding() default URLEncodingKind.RFC_1866;

/**
* The interface definition type.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
import io.micronaut.http.sse.Event;
import io.micronaut.http.uri.UriBuilder;
import io.micronaut.http.uri.UriMatchTemplate;
import io.micronaut.http.uri.URLEncodingKind;
import io.micronaut.json.codec.JsonMediaTypeCodec;
import org.reactivestreams.Publisher;
import org.reactivestreams.Subscription;
Expand Down Expand Up @@ -216,7 +217,15 @@ private Object handleSynchronous(MethodInvocationContext<Object, Object> context

Class<?> javaReturnType = returnType.getType();
BlockingHttpClient blockingHttpClient = httpClient.toBlocking();
RequestBinderResult binderResult = bindRequest(context, httpMethod, httpMethodName, uriToBind, interceptedMethod, annotationMetadata);
RequestBinderResult binderResult = bindRequest(
context,
httpMethod,
httpMethodName,
uriToBind,
interceptedMethod,
annotationMetadata,
httpClient
);
String clientName = declaringType.getName();

if (binderResult.isError()) {
Expand Down Expand Up @@ -258,7 +267,7 @@ private Object handleCompletionStage(MethodInvocationContext<Object, Object> con
Class<?> declaringType) {

Publisher<RequestBinderResult> csRequestPublisher = Mono.fromCallable(() ->
bindRequest(context, httpMethod, httpMethodName, uriToBind, interceptedMethod, annotationMetadata));
bindRequest(context, httpMethod, httpMethodName, uriToBind, interceptedMethod, annotationMetadata, httpClient));
Publisher<?> csPublisher = httpClientResponsePublisher(httpClient, csRequestPublisher, returnType, errorType, valueType);
CompletableFuture<Object> future = new CompletableFuture<>();
csPublisher.subscribe(new CompletionAwareSubscriber<Object>() {
Expand Down Expand Up @@ -330,7 +339,7 @@ private Object handlePublisher(MethodInvocationContext<Object, Object> context,
HttpStatus.class == reactiveValueType;

Publisher<RequestBinderResult> requestPublisher = Mono.fromCallable(() ->
bindRequest(context, httpMethod, httpMethodName, uriToBind, interceptedMethod, annotationMetadata));
bindRequest(context, httpMethod, httpMethodName, uriToBind, interceptedMethod, annotationMetadata, httpClient));
Publisher<?> publisher;
if (!isSingle && httpClient instanceof StreamingHttpClient client) {
publisher = httpClientResponseStreamingPublisher(client, context, requestPublisher, errorType, valueType);
Expand All @@ -357,9 +366,10 @@ private RequestBinderResult bindRequest(MethodInvocationContext<Object, Object>
String httpMethodName,
String uri,
InterceptedMethod interceptedMethod,
AnnotationMetadata annotationMetadata) {
AnnotationMetadata annotationMetadata,
HttpClient httpClient) {
MutableHttpRequest<?> request = HttpRequest.create(httpMethod, "", httpMethodName);

URLEncodingKind urlEncodingKind = httpClient.getUrlEncodingKind().orElse(null);
UriMatchTemplate uriTemplate = UriMatchTemplate.of("");
if (!(uri.length() == 1 && uri.charAt(0) == '/')) {
uriTemplate = uriTemplate.nest(uri);
Expand Down Expand Up @@ -402,14 +412,14 @@ private RequestBinderResult bindRequest(MethodInvocationContext<Object, Object>
body = null;
}

uri = uriTemplate.expand(pathParams);
uri = uriTemplate.expand(pathParams, urlEncodingKind);
// Remove all the pathParams that have already been used.
// Other path parameters are added to query
uriVariables.forEach(pathParams::remove);
addParametersToQuery(pathParams, uriContext);

// The original query can be added by getting it from the request.getUri() and appending
request.uri(URI.create(appendQuery(uri, uriContext.getQueryParameters())));
request.uri(URI.create(appendQuery(uri, uriContext.getQueryParameters(), urlEncodingKind)));

final MediaType[] acceptTypes;
Collection<MediaType> accept = request.accept();
Expand Down Expand Up @@ -692,9 +702,9 @@ private void addParametersToQuery(Map<String, Object> parameters, ClientRequestU
}
}

private String appendQuery(String uri, Map<String, List<String>> queryParams) {
private String appendQuery(String uri, Map<String, List<String>> queryParams, URLEncodingKind urlEncodingKind) {
if (!queryParams.isEmpty()) {
final UriBuilder builder = UriBuilder.of(uri);
final UriBuilder builder = UriBuilder.of(uri, urlEncodingKind);
for (Map.Entry<String, List<String>> entry : queryParams.entrySet()) {
builder.queryParam(entry.getKey(), entry.getValue().toArray());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import io.micronaut.http.reactive.execution.ReactiveExecutionFlow;
import io.micronaut.http.ssl.ClientAuthentication;
import io.micronaut.http.ssl.ClientSslConfiguration;
import io.micronaut.http.uri.URLEncodingKind;
import io.micronaut.http.util.HttpHeadersUtil;
import org.reactivestreams.Publisher;
import org.slf4j.Logger;
Expand Down Expand Up @@ -107,6 +108,7 @@ abstract class AbstractJdkHttpClient {
protected final HttpClientFilterResolver<ClientFilterResolutionContext> filterResolver;
protected final List<HttpFilterResolver.FilterEntry> clientFilterEntries;
protected final CookieDecoder cookieDecoder;
protected final URLEncodingKind urlEncodingKind;
protected MediaTypeCodecRegistry mediaTypeCodecRegistry;
protected MessageBodyHandlerRegistry messageBodyHandlerRegistry;

Expand All @@ -127,6 +129,7 @@ protected AbstractJdkHttpClient(AbstractJdkHttpClient prototype) {
this.cookieDecoder = prototype.cookieDecoder;
this.mediaTypeCodecRegistry = prototype.mediaTypeCodecRegistry;
this.messageBodyHandlerRegistry = prototype.messageBodyHandlerRegistry;
this.urlEncodingKind = prototype.urlEncodingKind;
}

/**
Expand Down Expand Up @@ -157,8 +160,10 @@ protected AbstractJdkHttpClient(
String clientId,
ConversionService conversionService,
JdkClientSslBuilder sslBuilder,
CookieDecoder cookieDecoder
CookieDecoder cookieDecoder,
URLEncodingKind urlEncodingKind
) {
this.urlEncodingKind = urlEncodingKind != null ? urlEncodingKind : configuration.getUrlEncoding();
this.cookieDecoder = cookieDecoder;
this.log = configuration.getLoggerName().map(LoggerFactory::getLogger).orElse(log);
this.loadBalancer = loadBalancer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@
import io.micronaut.http.codec.MediaTypeCodecRegistry;
import io.micronaut.http.filter.HttpClientFilterResolver;
import io.micronaut.http.filter.HttpFilterResolver;
import io.micronaut.http.uri.URLEncodingKind;
import io.micronaut.json.JsonMapper;
import io.micronaut.json.codec.JsonMediaTypeCodec;
import io.micronaut.json.codec.JsonStreamMediaTypeCodec;
import io.micronaut.runtime.ApplicationConfiguration;
import java.util.Optional;
import org.reactivestreams.Publisher;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -72,7 +74,8 @@ public DefaultJdkHttpClient(
String clientId,
ConversionService conversionService,
JdkClientSslBuilder sslBuilder,
CookieDecoder cookieDecoder
CookieDecoder cookieDecoder,
URLEncodingKind urlEncodingKind
) {
super(
configuration.getLoggerName().map(LoggerFactory::getLogger).orElseGet(() -> LoggerFactory.getLogger(DefaultJdkHttpClient.class)),
Expand All @@ -88,7 +91,8 @@ public DefaultJdkHttpClient(
clientId,
conversionService,
sslBuilder,
cookieDecoder
cookieDecoder,
urlEncodingKind
);
}

Expand All @@ -106,7 +110,8 @@ public DefaultJdkHttpClient(URI uri, ConversionService conversionService) {
null,
conversionService,
new JdkClientSslBuilder(new ResourceResolver()),
new CompositeCookieDecoder(List.of(new DefaultCookieDecoder()))
new CompositeCookieDecoder(List.of(new DefaultCookieDecoder())),
null
);
}

Expand All @@ -130,7 +135,8 @@ public DefaultJdkHttpClient(
null,
conversionService,
new JdkClientSslBuilder(new ResourceResolver()),
new CompositeCookieDecoder(List.of(new DefaultCookieDecoder()))
new CompositeCookieDecoder(List.of(new DefaultCookieDecoder())),
null
);
}

Expand All @@ -143,6 +149,11 @@ private static MediaTypeCodecRegistry createDefaultMediaTypeRegistry() {
);
}

@Override
public Optional<URLEncodingKind> getUrlEncodingKind() {
return JdkHttpClient.super.getUrlEncodingKind();
}

@Override
public BlockingHttpClient toBlocking() {
return new JdkBlockingHttpClient(
Expand All @@ -158,7 +169,8 @@ public BlockingHttpClient toBlocking() {
clientId,
conversionService,
sslBuilder,
cookieDecoder
cookieDecoder,
urlEncodingKind
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import io.micronaut.http.codec.MediaTypeCodec;
import io.micronaut.http.codec.MediaTypeCodecRegistry;
import io.micronaut.http.filter.HttpClientFilterResolver;
import io.micronaut.http.uri.URLEncodingKind;
import io.micronaut.inject.InjectionPoint;
import io.micronaut.inject.qualifiers.Qualifiers;
import io.micronaut.json.JsonFeatures;
Expand Down Expand Up @@ -331,6 +332,9 @@ private DefaultJdkHttpClient buildClient(
AnnotationMetadata annotationMetadata
) {
ConversionService conversionService = beanContext.getBean(ConversionService.class);
URLEncodingKind urlEncodingKind = annotationMetadata.
enumValue(Client.class, "uriEncoding", URLEncodingKind.class)
.orElse(configuration.getUrlEncoding());
return new DefaultJdkHttpClient(
loadBalancer,
httpVersion,
Expand All @@ -347,7 +351,8 @@ private DefaultJdkHttpClient buildClient(
clientId,
conversionService,
jdkClientSslBuilder,
cookieDecoder
cookieDecoder,
urlEncodingKind
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ private static String encodeBody(Map<?, ?> mapBody, Charset characterEncoding) {
.entrySet()
.stream()
.filter(entry -> entry.getKey() != null && entry.getValue() != null)
// here we use application/x-www-form-urlencoded not RFC-3986
.map(entry -> URLEncoder.encode(entry.getKey().toString(), characterEncoding) + "=" + URLEncoder.encode(entry.getValue().toString(), characterEncoding))
.collect(Collectors.joining("&"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import io.micronaut.http.codec.MediaTypeCodecRegistry;
import io.micronaut.http.filter.HttpClientFilterResolver;
import io.micronaut.http.filter.HttpFilterResolver;
import io.micronaut.http.uri.URLEncodingKind;
import org.slf4j.LoggerFactory;

import java.util.List;
Expand All @@ -58,7 +59,8 @@ public JdkBlockingHttpClient(
String clientId,
ConversionService conversionService,
JdkClientSslBuilder sslBuilder,
CookieDecoder cookieDecoder
CookieDecoder cookieDecoder,
URLEncodingKind urlEncodingKind
) {
super(
configuration.getLoggerName().map(LoggerFactory::getLogger).orElseGet(() -> LoggerFactory.getLogger(JdkBlockingHttpClient.class)),
Expand All @@ -74,7 +76,8 @@ public JdkBlockingHttpClient(
clientId,
conversionService,
sslBuilder,
cookieDecoder
cookieDecoder,
urlEncodingKind
);
}

Expand Down
Loading
Loading