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

Using optimized Vertx HTTP header names #37615

Merged
merged 1 commit into from
Dec 11, 2023
Merged
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkus.vertx.web.runtime;

import static io.vertx.core.http.HttpHeaders.CONTENT_TYPE;

import io.vertx.core.Handler;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.ext.web.RoutingContext;
Expand All @@ -9,20 +11,20 @@ public final class RouteHandlers {
private RouteHandlers() {
}

static final String CONTENT_TYPE = "content-type";

public static void setContentType(RoutingContext context, String defaultContentType) {
HttpServerResponse response = context.response();
context.addHeadersEndHandler(new Handler<Void>() {
@Override
public void handle(Void aVoid) {
var headers = response.headers();
//use a listener to set the content type if it has not been set
if (response.headers().get(CONTENT_TYPE) == null) {
if (!headers.contains(CONTENT_TYPE)) {
String acceptableContentType = context.getAcceptableContentType();
// we can use add because we know already there's no content type
if (acceptableContentType != null) {
response.putHeader(CONTENT_TYPE, acceptableContentType);
headers.add(CONTENT_TYPE, acceptableContentType);
} else if (defaultContentType != null) {
response.putHeader(CONTENT_TYPE, defaultContentType);
headers.add(CONTENT_TYPE, defaultContentType);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import jakarta.validation.Validator;

import io.quarkus.arc.ArcContainer;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
Expand Down Expand Up @@ -70,7 +71,7 @@ private static JsonObject generateJsonResponse(Set<ConstraintViolation<?>> viola
public static void handleViolationException(ConstraintViolationException ex, RoutingContext rc, boolean forceJsonEncoding) {
String accept = rc.request().getHeader(ACCEPT_HEADER);
if (forceJsonEncoding || accept != null && accept.contains(APPLICATION_JSON)) {
rc.response().putHeader(RouteHandlers.CONTENT_TYPE, APPLICATION_JSON);
rc.response().putHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON);
JsonObject json = generateJsonResponse(ex.getConstraintViolations(), false);
rc.response().setStatusCode(json.getInteger(PROBLEM_STATUS));
rc.response().end(json.encode());
Expand Down