This is an automated email from the ASF dual-hosted git repository.

markt-asf pushed a commit to branch 9.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/9.0.x by this push:
     new 3589c02e3e Do not depend on Content-Length to process request bodies
3589c02e3e is described below

commit 3589c02e3ed72f125054ffe0f6def9eb419cc293
Author: Mark Thomas <[email protected]>
AuthorDate: Wed Oct 28 08:39:26 2026 +0000

    Do not depend on Content-Length to process request bodies
---
 java/org/apache/catalina/servlets/CGIServlet.java     | 19 ++++++++++++-------
 .../apache/catalina/servlets/LocalStrings.properties  |  1 +
 webapps/docs/changelog.xml                            |  4 ++++
 3 files changed, 17 insertions(+), 7 deletions(-)

diff --git a/java/org/apache/catalina/servlets/CGIServlet.java 
b/java/org/apache/catalina/servlets/CGIServlet.java
index 3b13c745c6..c2f40c5b1e 100644
--- a/java/org/apache/catalina/servlets/CGIServlet.java
+++ b/java/org/apache/catalina/servlets/CGIServlet.java
@@ -1411,7 +1411,7 @@ public final class CGIServlet extends HttpServlet {
             InputStream cgiOutput = null;
             BufferedReader commandsStdErr = null;
             Thread errReaderThread = null;
-            BufferedOutputStream commandsStdIn;
+            BufferedOutputStream commandsStdIn = null;
             Process proc = null;
             int bufRead = -1;
 
@@ -1429,14 +1429,12 @@ public final class CGIServlet extends HttpServlet {
                 rt = Runtime.getRuntime();
                 proc = rt.exec(cmdAndArgs.toArray(new String[0]), 
hashToStringArray(env), wd);
 
-                String sContentLength = env.get("CONTENT_LENGTH");
-
-                if (!"".equals(sContentLength)) {
-                    commandsStdIn = new 
BufferedOutputStream(proc.getOutputStream());
+                commandsStdIn = new 
BufferedOutputStream(proc.getOutputStream());
+                if (stdin != null) {
                     IOTools.flow(stdin, commandsStdIn);
-                    commandsStdIn.flush();
-                    commandsStdIn.close();
                 }
+                commandsStdIn.close();
+                commandsStdIn = null;
 
                 /*
                  * we want to wait for the process to exit, Process.waitFor() 
is useless in our situation; see
@@ -1541,6 +1539,13 @@ public final class CGIServlet extends HttpServlet {
                         
log.warn(sm.getString("cgiServlet.runOutputStreamFail"), ioe);
                     }
                 }
+                if (commandsStdIn != null) {
+                    try {
+                        commandsStdIn.close();
+                    } catch (IOException ioe) {
+                        log.warn(sm.getString("cgiServlet.commandsStdInFail"), 
ioe);
+                    }
+                }
                 // Close the error stream reader if used
                 if (commandsStdErr != null) {
                     try {
diff --git a/java/org/apache/catalina/servlets/LocalStrings.properties 
b/java/org/apache/catalina/servlets/LocalStrings.properties
index b62e7345ce..25ce0bfade 100644
--- a/java/org/apache/catalina/servlets/LocalStrings.properties
+++ b/java/org/apache/catalina/servlets/LocalStrings.properties
@@ -16,6 +16,7 @@
 # Do not edit this file directly.
 # To edit translations see: 
https://tomcat.apache.org/getinvolved.html#Translations
 
+cgiServlet.commandsStdInFail=I/O problems closing stdin writer
 cgiServlet.emptyEnvVarName=Empty environment variable name in initialisation 
parameter [environment-variable-]
 cgiServlet.expandCloseFail=Failed to close input stream for script at path 
[{0}]
 cgiServlet.expandCreateDirFail=Failed to create destination directory [{0}] 
for script expansion
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index d44fa90a2c..f08eb525ed 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -319,6 +319,10 @@
         <code>serverAuthContext.secureResponse()</code> should result in an
         internal server error (500) response. (markt)
       </fix>
+      <fix>
+        Do not depend on the content length being defined when processing
+        request bodies with the CGI Servlet. (csutherl)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Coyote">


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to