Skip to content

Commit de1a8cf

Browse files
authored
Adds config flag to disable header copying in JaxRsService (helidon-io#9990)
* Adds config flag to disable header copying in JaxRsService. Signed-off-by: Santiago Pericas-Geertsen <santiago.pericasgeertsen@oracle.com>
1 parent f4686dc commit de1a8cf

3 files changed

Lines changed: 23 additions & 2 deletions

File tree

microprofile/server/src/main/java/io/helidon/microprofile/server/JaxRsService.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,12 @@ private void handle(ServerRequest req, ServerResponse res) {
202202
}
203203

204204
private void doHandle(Context ctx, ServerRequest req, ServerResponse res) {
205-
ServerResponseHeaders savedResponseHeaders = ServerResponseHeaders.create(res.headers());
205+
// save headers in case MP request processing fails
206+
ServerResponseHeaders savedResponseHeaders = null;
207+
if (req.listenerContext().config().restoreResponseHeaders()) {
208+
savedResponseHeaders = ServerResponseHeaders.create(res.headers());
209+
}
210+
206211
BaseUriRequestUri uris = BaseUriRequestUri.resolve(req);
207212
ContainerRequest requestContext = new ContainerRequest(uris.baseUri,
208213
uris.requestUri,
@@ -252,7 +257,9 @@ private void doHandle(Context ctx, ServerRequest req, ServerResponse res) {
252257
if (res instanceof RoutingResponse routing) {
253258
if (routing.reset()) {
254259
res.status(Status.OK_200);
255-
savedResponseHeaders.forEach(res::header);
260+
if (savedResponseHeaders != null) {
261+
savedResponseHeaders.forEach(res::header);
262+
}
256263
routing.next();
257264
}
258265
}

tests/integration/mp-jaxrs-preserve-headers/src/main/resources/application.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@
1414
# limitations under the License.
1515
#
1616
server:
17+
restore-response-headers: true
1718
features:
1819
header-adjustment:

webserver/webserver/src/main/java/io/helidon/webserver/ListenerConfigBlueprint.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,4 +408,17 @@ default void configureSocket(ServerSocket socket) {
408408
@Option.Configured
409409
@Option.DefaultMethod("create")
410410
ErrorHandling errorHandling();
411+
412+
/**
413+
* Copy and restore response headers before and after passing a request to Jersey
414+
* for processing. If Jersey fails to handle the request, and the Webserver continues
415+
* processing the request, it needs to make sure the original headers are restored.
416+
* Turn off this flag to avoid the extra overhead of copying headers when no handler
417+
* executes after Jersey returns.
418+
*
419+
* @return copy/restore header setting
420+
*/
421+
@Option.Configured
422+
@Option.Default("true")
423+
boolean restoreResponseHeaders();
411424
}

0 commit comments

Comments
 (0)