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 6323898cba Add support for PATCH 6323898cba is described below commit 6323898cba4223fd462eb388273d06fa451b9e2d Author: Mark Thomas <ma...@apache.org> AuthorDate: Wed Mar 1 15:55:50 2023 +0000 Add support for PATCH --- java/jakarta/servlet/http/HttpServlet.java | 58 ++++++++++++++++++++++- java/jakarta/servlet/http/LocalStrings.properties | 1 + webapps/docs/changelog.xml | 9 ++++ 3 files changed, 66 insertions(+), 2 deletions(-) diff --git a/java/jakarta/servlet/http/HttpServlet.java b/java/jakarta/servlet/http/HttpServlet.java index aec57d5e56..5f8f90dd8b 100644 --- a/java/jakarta/servlet/http/HttpServlet.java +++ b/java/jakarta/servlet/http/HttpServlet.java @@ -74,6 +74,7 @@ public abstract class HttpServlet extends GenericServlet { private static final String METHOD_HEAD = "HEAD"; private static final String METHOD_GET = "GET"; private static final String METHOD_OPTIONS = "OPTIONS"; + private static final String METHOD_PATCH = "PATCH"; private static final String METHOD_POST = "POST"; private static final String METHOD_PUT = "PUT"; private static final String METHOD_TRACE = "TRACE"; @@ -133,7 +134,7 @@ public abstract class HttpServlet extends GenericServlet { * Overriding this method to support a GET request also automatically supports an HTTP HEAD request. A HEAD request * is a GET request that returns no body in the response, only the request header fields. * <p> - * When overriding this method, read the request data, write the response headers, get the response's noBodyWriter + * When overriding this method, read the request data, write the response headers, get the response's Writer * or output stream object, and finally, write the response data. It's best to include content type and encoding. * When using a <code>PrintWriter</code> object to return the response, set the content type before accessing the * <code>PrintWriter</code> object. @@ -225,12 +226,52 @@ public abstract class HttpServlet extends GenericServlet { } + /** + * Called by the server (via the <code>service</code> method) to allow a servlet to handle a PATCH request. The HTTP + * PATCH method allows the client to partially modify an existing resource. + * <p> + * When overriding this method, read the request data and write the response headers, get the response's Writer + * or output stream object, and finally, write the response data. It's best to include content type and encoding. + * When using a <code>PrintWriter</code> object to return the response, set the content type before accessing the + * <code>PrintWriter</code> object. + * <p> + * The servlet container must write the headers before committing the response, because in HTTP the headers must be + * sent before the response body. + * <p> + * Where possible, set the Content-Length header (with the {@link jakarta.servlet.ServletResponse#setContentLength} + * method), to allow the servlet container to use a persistent connection to return its response to the client, + * improving performance. The content length is automatically set if the entire response fits inside the response + * buffer. + * <p> + * When using HTTP 1.1 chunked encoding (which means that the response has a Transfer-Encoding header), do not set + * the Content-Length header. + * <p> + * This method does not need to be either safe or idempotent. Operations requested through POST can have side + * effects for which the user can be held accountable, for example, updating stored data or buying items online. + * <p> + * If the HTTP POST request is incorrectly formatted, <code>doPost</code> returns an HTTP "Bad Request" message. + * + * @param req an {@link HttpServletRequest} object that contains the request the client has made of the servlet + * @param resp an {@link HttpServletResponse} object that contains the response the servlet sends to the client + * + * @exception IOException if an input or output error is detected when the servlet handles the request + * @exception ServletException if the request for the POST could not be handled + * + * @see jakarta.servlet.ServletOutputStream + * @see jakarta.servlet.ServletResponse#setContentType + */ + protected void doPatch(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + String msg = lStrings.getString("http.method_patch_not_supported"); + sendMethodNotAllowed(req, resp, msg); + } + + /** * Called by the server (via the <code>service</code> method) to allow a servlet to handle a POST request. The HTTP * POST method allows the client to send data of unlimited length to the Web server a single time and is useful when * posting information such as credit card numbers. * <p> - * When overriding this method, read the request data, write the response headers, get the response's noBodyWriter + * When overriding this method, read the request data, write the response headers, get the response's Writer * or output stream object, and finally, write the response data. It's best to include content type and encoding. * When using a <code>PrintWriter</code> object to return the response, set the content type before accessing the * <code>PrintWriter</code> object. @@ -346,6 +387,7 @@ public abstract class HttpServlet extends GenericServlet { boolean allowGet = false; boolean allowHead = false; + boolean allowPatch = false; boolean allowPost = false; boolean allowPut = false; boolean allowDelete = false; @@ -357,6 +399,10 @@ public abstract class HttpServlet extends GenericServlet { allowHead = true; break; } + case "doPatch": { + allowPatch = true; + break; + } case "doPost": { allowPost = true; break; @@ -387,6 +433,11 @@ public abstract class HttpServlet extends GenericServlet { allow.append(", "); } + if (allowPatch) { + allow.append(METHOD_PATCH); + allow.append(", "); + } + if (allowPost) { allow.append(METHOD_POST); allow.append(", "); @@ -574,6 +625,9 @@ public abstract class HttpServlet extends GenericServlet { } else if (method.equals(METHOD_TRACE)) { doTrace(req, resp); + } else if (method.equals(METHOD_PATCH)) { + doPatch(req, resp); + } else { // // Note that this means NO servlet supports whatever diff --git a/java/jakarta/servlet/http/LocalStrings.properties b/java/jakarta/servlet/http/LocalStrings.properties index f9fbd6c7a0..492548841f 100644 --- a/java/jakarta/servlet/http/LocalStrings.properties +++ b/java/jakarta/servlet/http/LocalStrings.properties @@ -26,6 +26,7 @@ err.state.commit=Not permitted once response has been committed http.method_delete_not_supported=HTTP method DELETE is not supported by this URL http.method_get_not_supported=HTTP method GET is not supported by this URL http.method_not_implemented=Method [{0}] is not implemented by this Servlet for this URI +http.method_patch_not_supported=HTTP method PATCH is not supported by this URL http.method_post_not_supported=HTTP method POST is not supported by this URL http.method_put_not_supported=HTTP method PUT is not supported by this URL http.non_http=Non HTTP request or response diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index 70ba58b1be..06dabcd930 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -105,6 +105,15 @@ issues do not "pop up" wrt. others). --> <section name="Tomcat 11.0.0-M5 (markt)" rtext="in development"> + <subsection name="Catalina"> + <changelog> + <add> + Add a <code>doPatch</code> method to <code>HttpServlet</code> to provide + support for the HTTP <code>PATCH</code> method as defined in RFC 5789. + This is one of the changes in the Servlet 6.1 API. (markt) + </add> + </changelog> + </subsection> </section> <section name="Tomcat 11.0.0-M4 (markt)" rtext="release in progress"> <subsection name="General"> --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org