This is an automated email from the ASF dual-hosted git repository. markt pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/main by this push: new 3c3337ac19 Allow applications to trigger sending of 103 early hints (#764) 3c3337ac19 is described below commit 3c3337ac190baa5749c72f0e08f56f27bf0ca31a Author: Christopher Schultz <ch...@christopherschultz.net> AuthorDate: Wed Nov 6 05:38:27 2024 -0500 Allow applications to trigger sending of 103 early hints (#764) * Allow applications to trigger sending of 103 early hints * Add unit tests --- java/org/apache/catalina/connector/Response.java | 42 +++++++++-- .../apache/catalina/connector/ResponseFacade.java | 40 +++++++++-- .../apache/coyote/http11/TestHttp11Processor.java | 81 +++++++++++++++++++++- 3 files changed, 151 insertions(+), 12 deletions(-) diff --git a/java/org/apache/catalina/connector/Response.java b/java/org/apache/catalina/connector/Response.java index 3a3b4237d3..a291edcec9 100644 --- a/java/org/apache/catalina/connector/Response.java +++ b/java/org/apache/catalina/connector/Response.java @@ -1052,12 +1052,36 @@ public class Response implements HttpServletResponse { } + /** + * {@inheritDoc} + * + * <i>Deprecated functionality</i>: calling <code>sendError</code> with a status + * code of 103 differs from the usual behavior. Sending 103 will trigger + * the container to send a "103 Early Hints" informational response + * including all current headers. The application can continue to use + * the request and response after calling sendError with a 103 status code, + * including triggering a more typical response of any type. + * + * Starting with Tomcat 12, applications should use {@link #sendEarlyHints}. + */ @Override public void sendError(int status) throws IOException { sendError(status, null); } + /** + * {@inheritDoc} + * + * <i>Deprecated functionality</i>: calling <code>sendError</code> with a status + * code of 103 differs from the usual behavior. Sending 103 will trigger + * the container to send a "103 Early Hints" informational response + * including all current headers. The application can continue to use + * the request and response after calling sendError with a 103 status code, + * including triggering a more typical response of any type. + * + * Starting with Tomcat 12, applications should use {@link #sendEarlyHints}. + */ @Override public void sendError(int status, String message) throws IOException { @@ -1070,16 +1094,20 @@ public class Response implements HttpServletResponse { return; } - setError(); + if (HttpServletResponse.SC_EARLY_HINTS == status) { + sendEarlyHints(); + } else { + setError(); - getCoyoteResponse().setStatus(status); - getCoyoteResponse().setMessage(message); + getCoyoteResponse().setStatus(status); + getCoyoteResponse().setMessage(message); - // Clear any data content that has been buffered - resetBuffer(); + // Clear any data content that has been buffered + resetBuffer(); - // Cause the response to be finished (from the application perspective) - setSuspended(true); + // Cause the response to be finished (from the application perspective) + setSuspended(true); + } } diff --git a/java/org/apache/catalina/connector/ResponseFacade.java b/java/org/apache/catalina/connector/ResponseFacade.java index 6c12853f1f..3d03b1f739 100644 --- a/java/org/apache/catalina/connector/ResponseFacade.java +++ b/java/org/apache/catalina/connector/ResponseFacade.java @@ -250,19 +250,51 @@ public class ResponseFacade implements HttpServletResponse { response.sendEarlyHints(); } + /** + * {@inheritDoc} + * + * <i>Deprecated functionality</i>: calling <code>sendError</code> with a status + * code of 103 differs from the usual behavior. Sending 103 will trigger + * the container to send a "103 Early Hints" informational response + * including all current headers. The application can continue to use + * the request and response after calling sendError with a 103 status code, + * including triggering a more typical response of any type. + * + * Starting with Tomcat 12, applications should use {@link #sendEarlyHints}. + */ @Override public void sendError(int sc, String msg) throws IOException { checkCommitted("coyoteResponse.sendError.ise"); - response.setAppCommitted(true); - response.sendError(sc, msg); + if (HttpServletResponse.SC_EARLY_HINTS == sc) { + sendEarlyHints(); + } else { + response.setAppCommitted(true); + response.sendError(sc, msg); + } } + /** + * {@inheritDoc} + * + * <i>Deprecated functionality</i>: calling <code>sendError</code> with a status + * code of 103 differs from the usual behavior. Sending 103 will trigger + * the container to send a "103 Early Hints" informational response + * including all current headers. The application can continue to use + * the request and response after calling sendError with a 103 status code, + * including triggering a more typical response of any type. + * + * Starting with Tomcat 12, applications should use {@link #sendEarlyHints}. + */ @Override public void sendError(int sc) throws IOException { checkCommitted("coyoteResponse.sendError.ise"); - response.setAppCommitted(true); - response.sendError(sc); + if (HttpServletResponse.SC_EARLY_HINTS == sc) { + sendEarlyHints(); + } else { + response.setAppCommitted(true); + response.sendError(sc); + } } diff --git a/test/org/apache/coyote/http11/TestHttp11Processor.java b/test/org/apache/coyote/http11/TestHttp11Processor.java index fed616435b..0d8a9d766c 100644 --- a/test/org/apache/coyote/http11/TestHttp11Processor.java +++ b/test/org/apache/coyote/http11/TestHttp11Processor.java @@ -1946,16 +1946,95 @@ public class TestHttp11Processor extends TomcatBaseTest { Assert.assertEquals(HttpServletResponse.SC_OK, client.getStatusCode()); } + @Test + public void testEarlyHintsSendError() throws Exception { + Tomcat tomcat = getTomcatInstance(); + + // No file system docBase required + Context ctx = getProgrammaticRootContext(); + + // Add servlet + Tomcat.addServlet(ctx, "EarlyHintsServlet", new EarlyHintsServlet(true, null)); + ctx.addServletMappingDecoded("/ehs", "EarlyHintsServlet"); + + tomcat.start(); + + String request = "GET /ehs HTTP/1.1" + SimpleHttpClient.CRLF + + "Host: localhost:" + getPort() + SimpleHttpClient.CRLF + + SimpleHttpClient.CRLF; + + Client client = new Client(tomcat.getConnector().getLocalPort()); + client.setRequest(new String[] { request }); + + client.connect(600000, 600000); + client.processRequest(false); + + Assert.assertEquals(103, client.getStatusCode()); + + client.readResponse(false); + Assert.assertEquals(HttpServletResponse.SC_OK, client.getStatusCode()); + } + + + @Test + public void testEarlyHintsSendErrorWithMessage() throws Exception { + Tomcat tomcat = getTomcatInstance(); + + // No file system docBase required + Context ctx = getProgrammaticRootContext(); + + // Add servlet + Tomcat.addServlet(ctx, "EarlyHintsServlet", new EarlyHintsServlet(true, "ignored")); + ctx.addServletMappingDecoded("/ehs", "EarlyHintsServlet"); + + tomcat.start(); + + String request = "GET /ehs HTTP/1.1" + SimpleHttpClient.CRLF + + "Host: localhost:" + getPort() + SimpleHttpClient.CRLF + + SimpleHttpClient.CRLF; + + Client client = new Client(tomcat.getConnector().getLocalPort()); + client.setRequest(new String[] { request }); + + client.connect(600000, 600000); + client.processRequest(false); + + Assert.assertEquals(103, client.getStatusCode()); + + client.readResponse(false); + Assert.assertEquals(HttpServletResponse.SC_OK, client.getStatusCode()); + } + + private static class EarlyHintsServlet extends HttpServlet { private static final long serialVersionUID = 1L; + private final boolean useSendError; + private final String errorString; + + EarlyHintsServlet() { + this(false, null); + } + + EarlyHintsServlet(boolean useSendError, String errorString) { + this.useSendError = useSendError; + this.errorString = errorString; + } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.addHeader("Link", "</style.css>; rel=preload; as=style"); - ((ResponseFacade) resp).sendEarlyHints(); + if (useSendError) { + if (null == errorString) { + resp.sendError(103); + } else { + resp.sendError(103, errorString); + } + } else { + ((ResponseFacade) resp).sendEarlyHints(); + } resp.setCharacterEncoding(StandardCharsets.UTF_8); resp.setContentType("text/plain"); --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org