Skip to content

Commit

Permalink
Improved HTTP 1.1 ClassRoutingHandler::handle due to un-optimized hea…
Browse files Browse the repository at this point in the history
…ders names lookup
  • Loading branch information
franz1981 committed Sep 15, 2023
1 parent dc176d0 commit 4756828
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ public void accept(ResteasyReactiveRequestContext context) {
private static final String TYPE_LOWER = "type";
private static final String LENGTH = "Length";
private static final String LENGTH_LOWER = "length";
private static final String CONTENT_TYPE = CONTENT + "-" + TYPE; // use this instead of the Vert.x constant because the TCK expects upper case
private static final String CONTENT_TYPE = HttpHeaders.CONTENT_TYPE;

// use this instead of the Vert.x constant because the TCK expects upper case

static {
primitivesToWrappers.put(boolean.class, Boolean.class);
Expand All @@ -95,6 +97,7 @@ public void accept(ResteasyReactiveRequestContext context) {
primitivesToWrappers.put(long.class, Long.class);
primitivesToWrappers.put(float.class, Float.class);
primitivesToWrappers.put(double.class, Double.class);
assert CONTENT_TYPE.equals(CONTENT + '-' + TYPE) : "TCK expect upper case Content-Type: Jakarta ones should match";
}

public final static List<Serialisers.BuiltinReader> BUILTIN_READERS = List.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.http.impl.Http1xServerRequest;
import io.vertx.core.http.impl.Http1xServerResponse;
import io.vertx.core.net.impl.ConnectionBase;
import io.vertx.ext.web.RoutingContext;
Expand Down Expand Up @@ -146,6 +147,19 @@ public boolean resumeExternalProcessing() {

@Override
public String getRequestHeader(CharSequence name) {
final HttpServerRequest request = this.request;
if (request instanceof Http1xServerRequest) {
// this is an HTTP 1.1 fast path optimization to enable cached ASCII keys constants:
// see ClassRoutingHandler::handle's HttpHeaders used.
// We're not using switch on purpose: we don't want to force String::hashCode
// for not constant/cached String(s)
if (name == HttpHeaders.CONTENT_TYPE) {
return request.headers().get(HttpHeaderNames.CONTENT_TYPE);
}
if (name == HttpHeaders.ACCEPT) {
return request.headers().get(HttpHeaderNames.ACCEPT);
}
}
return request.headers().get(name);
}

Expand All @@ -156,6 +170,19 @@ public Iterable<Map.Entry<String, String>> getAllRequestHeaders() {

@Override
public List<String> getAllRequestHeaders(String name) {
final HttpServerRequest request = this.request;
if (request instanceof Http1xServerRequest) {
// this is an HTTP 1.1 fast path optimization to enable cached ASCII keys constants:
// see ClassRoutingHandler::handle's HttpHeaders used.
// We're not using switch on purpose: we don't want to force String::hashCode
// for not constant/cached String(s)
if (name == HttpHeaders.CONTENT_TYPE) {
return request.headers().getAll(HttpHeaderNames.CONTENT_TYPE);
}
if (name == HttpHeaders.ACCEPT) {
return request.headers().getAll(HttpHeaderNames.ACCEPT);
}
}
return request.headers().getAll(name);
}

Expand Down Expand Up @@ -379,7 +406,13 @@ public ServerHttpResponse addResponseHeader(CharSequence name, CharSequence valu

@Override
public ServerHttpResponse setResponseHeader(CharSequence name, CharSequence value) {
response.headers().set(name, value);
final HttpServerResponse response = this.response;
if (response instanceof Http1xServerResponse && name == HttpHeaders.CONTENT_TYPE) {
// this is an HTTP 1.1 fast path optimization to enable cached ASCII keys constants
response.headers().set(HttpHeaderNames.CONTENT_TYPE, value);
} else {
response.headers().set(name, value);
}
return this;
}

Expand Down

0 comments on commit 4756828

Please sign in to comment.