diff --git a/jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/HttpFields.java b/jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/HttpFields.java index 8f057820ee5..393732e4c21 100644 --- a/jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/HttpFields.java +++ b/jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/HttpFields.java @@ -29,6 +29,7 @@ import java.util.Spliterators; import java.util.function.BiFunction; import java.util.function.BiPredicate; import java.util.function.Function; +import java.util.function.Supplier; import java.util.function.ToIntFunction; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -41,7 +42,7 @@ import java.util.stream.StreamSupport; * To implement this interface, only the {@link #iterator()} method * need be implemented, however default implementations may be inefficient */ -public interface HttpFields extends Iterable +public interface HttpFields extends Iterable, Supplier { HttpField EXPIRES_01JAN1970 = new PreEncodedHttpField(HttpHeader.EXPIRES, DateGenerator.__01Jan1970); HttpField CONNECTION_CLOSE = new PreEncodedHttpField(HttpHeader.CONNECTION, HttpHeaderValue.CLOSE.asString()); @@ -90,6 +91,16 @@ public interface HttpFields extends Iterable return HttpFields.build(this).asImmutable(); } + /** + *

Supplies this instance, typically used to supply HTTP trailers.

+ * @return this instance + */ + @Override + default HttpFields get() + { + return this; + } + /** * Efficiently take the fields as an Immutable that cannot be changed by any further mutations * to this instance. diff --git a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/ConnectorTimeoutTest.java b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/ConnectorTimeoutTest.java index fc5ae265513..82f3e3e396e 100644 --- a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/ConnectorTimeoutTest.java +++ b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/ConnectorTimeoutTest.java @@ -212,7 +212,7 @@ public abstract class ConnectorTimeoutTest extends HttpServerTestFixture startServer(new EchoHandler() { @Override - public boolean handle(Request request, Response response, Callback callback) throws Exception + public boolean handle(Request request, Response response, Callback callback) { try { diff --git a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/HttpChannelTest.java b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/HttpChannelTest.java index f4a3c5d2d3f..19df0a1fee3 100644 --- a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/HttpChannelTest.java +++ b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/HttpChannelTest.java @@ -1257,7 +1257,7 @@ public class HttpChannelTest EchoHandler echoHandler = new EchoHandler() { @Override - public boolean handle(Request request, Response response, Callback callback) throws Exception + public boolean handle(Request request, Response response, Callback callback) { request.addHttpStreamWrapper(s -> new HttpStream.Wrapper(s) { diff --git a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/HttpServerTestBase.java b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/HttpServerTestBase.java index cd3dd3f9147..63f832d67ed 100644 --- a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/HttpServerTestBase.java +++ b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/HttpServerTestBase.java @@ -1872,7 +1872,7 @@ public abstract class HttpServerTestBase extends HttpServerTestFixture } @Override - public boolean handle(Request request, Response response, Callback callback) throws Exception + public boolean handle(Request request, Response response, Callback callback) { AtomicBoolean hasContent = new AtomicBoolean(); Request.Wrapper wrapper = new Request.Wrapper(request) diff --git a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/handler/EchoHandler.java b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/handler/EchoHandler.java index 4d345513d3f..2bd0b840f40 100644 --- a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/handler/EchoHandler.java +++ b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/handler/EchoHandler.java @@ -13,14 +13,13 @@ package org.eclipse.jetty.server.handler; +import org.eclipse.jetty.http.HttpField; import org.eclipse.jetty.http.HttpFields; -import org.eclipse.jetty.http.HttpHeader; import org.eclipse.jetty.io.Content; import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Response; import org.eclipse.jetty.util.Callback; -import org.eclipse.jetty.util.StringUtil; /** * Dump request handler. @@ -32,26 +31,32 @@ public class EchoHandler extends Handler.Abstract.NonBlocking public EchoHandler() { } - + @Override - public boolean handle(Request request, Response response, Callback callback) throws Exception + public boolean handle(Request request, Response response, Callback callback) { response.setStatus(200); - String contentType = request.getHeaders().get(HttpHeader.CONTENT_TYPE); - if (StringUtil.isNotBlank(contentType)) - response.getHeaders().put(HttpHeader.CONTENT_TYPE, contentType); - if (request.getHeaders().contains(HttpHeader.TRAILER)) + long contentLength = -1; + for (HttpField field : request.getHeaders()) { - HttpFields.Mutable responseTrailers = HttpFields.build(); - response.setTrailersSupplier(() -> responseTrailers); + if (field.getHeader() != null) + { + switch (field.getHeader()) + { + case CONTENT_LENGTH -> + { + response.getHeaders().add(field); + contentLength = field.getLongValue(); + } + case CONTENT_TYPE -> response.getHeaders().add(field); + case TRAILER -> response.setTrailersSupplier(HttpFields.build()); + case TRANSFER_ENCODING -> contentLength = Long.MAX_VALUE; + } + } } - long contentLength = request.getHeaders().getLongField(HttpHeader.CONTENT_LENGTH); - if (contentLength >= 0) - response.getHeaders().put(HttpHeader.CONTENT_LENGTH, contentLength); - - if (contentLength > 0 || contentLength == -1 && request.getHeaders().contains(HttpHeader.TRANSFER_ENCODING)) + if (contentLength > 0) Content.copy(request, response, Response.newTrailersChunkProcessor(response), callback); else callback.succeeded(); diff --git a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/ssl/SslContextFactoryReloadTest.java b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/ssl/SslContextFactoryReloadTest.java index fcf6ffb542f..417c9dfac5d 100644 --- a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/ssl/SslContextFactoryReloadTest.java +++ b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/ssl/SslContextFactoryReloadTest.java @@ -248,7 +248,7 @@ public class SslContextFactoryReloadTest private static class TestHandler extends EchoHandler { @Override - public boolean handle(Request request, Response response, Callback callback) throws Exception + public boolean handle(Request request, Response response, Callback callback) { if (HttpMethod.POST.is(request.getMethod())) return super.handle(request, response, callback);