Skip to content

Commit

Permalink
Minor client optimizations (micronaut-projects#11295)
Browse files Browse the repository at this point in the history
* Migrate netty HttpClient to ExecutionFlow
Also adds cancellation support to ExecutionFlow.

* review

* fix double cancel

* Remove context propagation filter
Implement "proper" context propagation in the client to get rid of the old ClientServerContextFilter.

Because context propagation uses its own key for the reactor context instead of ServerRequestContext.KEY, I've deprecated ServerRequestContext.KEY and added a more general API that can use the propagated context from the ContextView.

* review

* Minor client optimizations

* Minor client optimizations

* trigger build
  • Loading branch information
yawkat authored Nov 8, 2024
1 parent 11cb0f0 commit 0ce7262
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 18 deletions.
35 changes: 26 additions & 9 deletions core/src/main/java/io/micronaut/core/util/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -366,18 +366,35 @@ public static String convertDotToUnderscore(String dottedProperty, boolean upper
* @return A combined uri string
*/
public static String prependUri(String baseUri, String uri) {
if (!uri.startsWith("/") && !uri.startsWith("?")) {
uri = "/" + uri;
StringBuilder builder = new StringBuilder(baseUri);
if (!uri.isEmpty() && (uri.length() != 1 || uri.charAt(0) != '/')) {
if (!uri.startsWith("/") && !uri.startsWith("?")) {
builder.append('/');
}
builder.append(uri);
}
if (builder.isEmpty()) {
return "";
}
if (uri.length() == 1 && uri.charAt(0) == '/') {
uri = "";

int i = 0;
if (builder.charAt(0) != '/' && builder.indexOf("://") != -1) {
// skip until after scheme
while (i < builder.length() && builder.charAt(i) != ':') {
i++;
}
i += 2;
}
uri = baseUri + uri;
if (uri.startsWith("/")) {
return uri.replaceAll("/{2,}", "/");
} else {
return uri.replaceAll("(?<=[^:])/{2,}", "/");
// replace double slashes
for (; i < builder.length() - 1; i++) {
if (builder.charAt(i) == '/' && builder.charAt(i + 1) == '/') {
builder.deleteCharAt(i);
i--;
} else if (builder.charAt(i) == '?') {
break;
}
}
return builder.toString();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1284,14 +1284,14 @@ private NettyByteBody buildNettyRequest(
boolean permitsBody,
EventLoop eventLoop) throws HttpPostRequestEncoder.ErrorDataEncoderException {

if (!request.getHeaders().contains(io.micronaut.http.HttpHeaders.HOST)) {
if (!request.getHeaders().contains(HttpHeaderNames.HOST)) {
request.getHeaders().set(HttpHeaderNames.HOST, getHostHeader(requestURI));
}

if (permitsBody) {
Optional<?> body = request.getBody();
if (body.isPresent()) {
if (!request.getHeaders().contains(io.micronaut.http.HttpHeaders.CONTENT_TYPE)) {
if (!request.getHeaders().contains(HttpHeaderNames.CONTENT_TYPE)) {
MediaType mediaType = request.getContentType().orElse(MediaType.APPLICATION_JSON_TYPE);
request.getHeaders().set(HttpHeaderNames.CONTENT_TYPE, mediaType);
}
Expand Down Expand Up @@ -1971,6 +1971,14 @@ private static MessageBodyHandlerRegistry createDefaultMessageBodyHandlerRegistr
}

static boolean isSecureScheme(String scheme) {
// fast path
if (scheme.equals("http")) {
return false;
}
if (scheme.equals("https")) {
return true;
}
// actual case-insensitive check
return io.micronaut.http.HttpRequest.SCHEME_HTTPS.equalsIgnoreCase(scheme) || SCHEME_WSS.equalsIgnoreCase(scheme);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ public final boolean contains(String name) {
return nettyHeaders.contains(name);
}

@Override
public final boolean contains(CharSequence name) {
return nettyHeaders.contains(name);
}

@Override
public <T> Optional<T> get(CharSequence name, ArgumentConversionContext<T> conversionContext) {
List<String> values = nettyHeaders.getAll(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import io.micronaut.core.type.MutableHeaders;
import io.micronaut.http.ByteBodyHttpResponse;
import io.micronaut.http.ByteBodyHttpResponseWrapper;
import io.micronaut.http.HttpHeaders;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.MediaType;
import io.micronaut.http.MutableHttpHeaders;
Expand Down Expand Up @@ -63,7 +62,7 @@ public ByteBodyHttpResponse<?> write(ByteBufferFactory<?, ?> bufferFactory, Http
ByteBufUtil.writeUtf8(ByteBufAllocator.DEFAULT, object) :
ByteBufUtil.encodeString(ByteBufAllocator.DEFAULT, CharBuffer.wrap(object), charset);
NettyHttpHeaders nettyHttpHeaders = (NettyHttpHeaders) headers;
if (!nettyHttpHeaders.contains(HttpHeaders.CONTENT_TYPE)) {
if (!nettyHttpHeaders.contains(HttpHeaderNames.CONTENT_TYPE)) {
nettyHttpHeaders.set(HttpHeaderNames.CONTENT_TYPE, mediaType);
}
return ByteBodyHttpResponseWrapper.wrap(outgoingResponse, new AvailableNettyByteBody(byteBuf));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import io.micronaut.core.type.MutableHeaders;
import io.micronaut.http.ByteBodyHttpResponse;
import io.micronaut.http.ByteBodyHttpResponseWrapper;
import io.micronaut.http.HttpHeaders;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.MediaType;
import io.micronaut.http.MutableHttpHeaders;
Expand All @@ -41,6 +40,7 @@
import io.micronaut.runtime.ApplicationConfiguration;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.ByteBufOutputStream;
import io.netty.handler.codec.http.HttpHeaderNames;
import jakarta.inject.Singleton;
import org.reactivestreams.Publisher;

Expand Down Expand Up @@ -76,7 +76,7 @@ public boolean isBlocking() {
@Override
public ByteBodyHttpResponse<?> write(ByteBufferFactory<?, ?> bufferFactory, HttpRequest<?> request, MutableHttpResponse<Writable> outgoingResponse, Argument<Writable> type, MediaType mediaType, Writable object) throws CodecException {
MutableHttpHeaders outgoingHeaders = outgoingResponse.getHeaders();
if (mediaType != null && !outgoingHeaders.contains(HttpHeaders.CONTENT_TYPE)) {
if (mediaType != null && !outgoingHeaders.contains(HttpHeaderNames.CONTENT_TYPE)) {
outgoingHeaders.contentType(mediaType);
}
ByteBufOutputStream outputStream = new ByteBufOutputStream(ByteBufAllocator.DEFAULT.buffer());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.micronaut.http.netty.body.AvailableNettyByteBody;
import io.micronaut.http.server.netty.configuration.NettyHttpServerConfiguration;
import io.micronaut.http.server.types.files.FileCustomizableResponseType;
import io.netty.handler.codec.http.HttpHeaderNames;

import java.time.LocalDateTime;
import java.time.ZonedDateTime;
Expand Down Expand Up @@ -70,8 +71,8 @@ protected boolean handleIfModifiedAndHeaders(HttpRequest<?> request, MutableHttp
}
}

if (!response.getHeaders().contains(HttpHeaders.CONTENT_TYPE)) {
response.header(HttpHeaders.CONTENT_TYPE, systemFile.getMediaType().toString());
if (!response.getHeaders().contains(HttpHeaderNames.CONTENT_TYPE)) {
response.header(HttpHeaderNames.CONTENT_TYPE, systemFile.getMediaType().toString());
}
setDateAndCacheHeaders(response, lastModified);
systemFile.process(nettyResponse);
Expand All @@ -86,7 +87,7 @@ protected void setDateAndCacheHeaders(MutableHttpResponse response, long lastMod
// Date header
MutableHttpHeaders headers = response.getHeaders();
LocalDateTime now = LocalDateTime.now();
if (!headers.contains(HttpHeaders.DATE)) {
if (!headers.contains(HttpHeaderNames.DATE)) {
headers.date(now);
}

Expand Down
11 changes: 11 additions & 0 deletions http/src/main/java/io/micronaut/http/HttpHeaders.java
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,17 @@ public interface HttpHeaders extends Headers {
X_AUTH_TOKEN
));

/**
* Whether the given key is contained within these values.
*
* @param name The key name
* @return True if it is
* @since 4.8.0
*/
default boolean contains(CharSequence name) {
return contains(name.toString());
}

/**
* Obtain the date header.
*
Expand Down

0 comments on commit 0ce7262

Please sign in to comment.