Skip to content

Commit

Permalink
Introduce quarkus.http.header
Browse files Browse the repository at this point in the history
This allows to explicitly set fixed headers on HTTP responses
  • Loading branch information
gastaldi committed Oct 31, 2021
1 parent 9aaa192 commit 8a6da3b
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 1 deletion.
12 changes: 12 additions & 0 deletions docs/src/main/asciidoc/http-reference.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,18 @@ values:

NOTE: if you use `redirect` or `disabled` and have not added a SSL certificate or keystore, your server will not start!

== Additional HTTP Headers

To enable HTTP headers to be sent on every response, add the following properties:

[source, properties]
----
quarkus.http.header."X-Content-Type-Options"=nosniff
----

This will include the `X-Content-Type-Options: nosniff` HTTP Header on responses for requests performed on any resource in the application.


== HTTP/2 Support

HTTP/2 is enabled by default, and will be used by browsers if SSL is in use on JDK11 or higher. JDK8 does not support
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,12 @@ public class HttpConfiguration {
@ConfigItem
public Optional<PayloadHint> unhandledErrorContentTypeDefault;

/**
* Additional HTTP Headers always sent in the response
*/
@ConfigItem
public Map<String, String> header;

public ProxyConfig proxy;

public int determinePort(LaunchMode launchMode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,17 @@ public void handle(Void e) {
}
});
}
// Headers sent on any request, regardless of the response
Map<String, String> headers = httpConfiguration.header;
if (!headers.isEmpty()) {
httpRouteRouter.route().order(Integer.MIN_VALUE).handler(new Handler<RoutingContext>() {
@Override
public void handle(RoutingContext event) {
event.response().headers().addAll(headers);
event.next();
}
});
}

Handler<HttpServerRequest> root;
if (rootPath.equals("/")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

/**
*
Expand All @@ -13,4 +14,16 @@ public class SimpleResource {
public String accessLogTest() {
return "passed";
}

@GET
@Path("/headers")
public String headers() {
return "ok";
}

@GET
@Path("/headers-override")
public Response headersOverride() {
return Response.ok("ok").header("foo", "abc").build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ quarkus.http.access-log.enabled=true
quarkus.http.access-log.log-to-file=true
quarkus.http.access-log.base-file-name=quarkus-access-log
quarkus.http.access-log.log-directory=target
quarkus.native.additional-build-args=-H:IncludeResources=.*\\.jks,-H:EnableURLProtocols=http\\,https
quarkus.http.header.foo=bar
quarkus.native.additional-build-args=-H:IncludeResources=.*\\.jks,-H:EnableURLProtocols=http\\,https
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.quarkus.it.vertx;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.is;

import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusTest;

@QuarkusTest
public class HeadersTestCase {

@Test
void testAdditionalHeaders() {
given()
.get("/simple/headers")
.then()
.header("foo", is("bar"))
.body(is("ok"));

}

@Test
void testAdditionalHeadersOverride() {
given()
.get("/simple/headers-override")
.then()
.header("foo", is("abc"))
.body(is("ok"));

}
}

0 comments on commit 8a6da3b

Please sign in to comment.