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

remm pushed a commit to branch 11.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/11.0.x by this push:
     new 01e2507e3a Fix inconsistencies with special headers and getHeader
01e2507e3a is described below

commit 01e2507e3ae7b9700c637a80bc2ebed27e127ed6
Author: remm <[email protected]>
AuthorDate: Wed Mar 4 00:15:20 2026 +0100

    Fix inconsistencies with special headers and getHeader
    
    Handling of Content-Type and Content-Length with getHeader,
    getHeaderNames and getHeaders.
    BZ69967
---
 java/org/apache/catalina/connector/Response.java   | 46 +++++++++++++++++++++-
 .../apache/catalina/connector/TestResponse.java    | 18 +++++++++
 webapps/docs/changelog.xml                         |  9 +++++
 3 files changed, 72 insertions(+), 1 deletion(-)

diff --git a/java/org/apache/catalina/connector/Response.java 
b/java/org/apache/catalina/connector/Response.java
index b02e336f5b..05193bcf5a 100644
--- a/java/org/apache/catalina/connector/Response.java
+++ b/java/org/apache/catalina/connector/Response.java
@@ -749,6 +749,23 @@ public class Response implements HttpServletResponse {
 
     @Override
     public String getHeader(String name) {
+        // Need special handling for Content-Type and Content-Length due to
+        // special handling of these in coyoteResponse
+        char cc = name.charAt(0);
+        if (cc == 'C' || cc == 'c') {
+            if (name.equalsIgnoreCase("Content-Type")) {
+                // Will return null if this has not been set
+                return getCoyoteResponse().getContentType();
+            }
+            if (name.equalsIgnoreCase("Content-Length")) {
+                // -1 means not known and is not sent to client
+                if (getCoyoteResponse().getContentLengthLong() != -1) {
+                    return 
String.valueOf(getCoyoteResponse().getContentLengthLong());
+                } else {
+                    return null;
+                }
+            }
+        }
         return getCoyoteResponse().getMimeHeaders().getHeader(name);
     }
 
@@ -761,13 +778,40 @@ public class Response implements HttpServletResponse {
         for (int i = 0; i < n; i++) {
             result.add(headers.getName(i).toString());
         }
+        if (getCoyoteResponse().getContentType() != null) {
+            result.add("Content-Type");
+        }
+        if (getCoyoteResponse().getContentLengthLong() != -1) {
+            result.add("Content-Length");
+        }
         return result;
-
     }
 
 
     @Override
     public Collection<String> getHeaders(String name) {
+        // Need special handling for Content-Type and Content-Length due to
+        // special handling of these in coyoteResponse
+        char cc = name.charAt(0);
+        if (cc == 'C' || cc == 'c') {
+            if (name.equalsIgnoreCase("Content-Type")) {
+                // Will return null if this has not been set
+                String contentType = getCoyoteResponse().getContentType();
+                if (contentType != null) {
+                    return Set.of(contentType);
+                } else {
+                    return Set.of();
+                }
+            }
+            if (name.equalsIgnoreCase("Content-Length")) {
+                // -1 means not known and is not sent to client
+                if (getCoyoteResponse().getContentLengthLong() != -1) {
+                    return 
Set.of(String.valueOf(getCoyoteResponse().getContentLengthLong()));
+                } else {
+                    return Set.of();
+                }
+            }
+        }
         Enumeration<String> enumeration = 
getCoyoteResponse().getMimeHeaders().values(name);
         Set<String> result = new LinkedHashSet<>();
         while (enumeration.hasMoreElements()) {
diff --git a/test/org/apache/catalina/connector/TestResponse.java 
b/test/org/apache/catalina/connector/TestResponse.java
index f79a3cebf8..9f0eb25a6b 100644
--- a/test/org/apache/catalina/connector/TestResponse.java
+++ b/test/org/apache/catalina/connector/TestResponse.java
@@ -736,6 +736,24 @@ public class TestResponse extends TomcatBaseTest {
     }
 
 
+    @Test
+    public void testSetContentLengthHeader() {
+        Response response = setupResponse();
+
+        response.setContentLength(10);
+        Assert.assertEquals("10", response.getHeader("Content-Length"));
+    }
+
+
+    @Test
+    public void testSetContentTypeHeader() {
+        Response response = setupResponse();
+
+        response.setContentType(TEXT_UTF_8);
+        Assert.assertEquals(TEXT_UTF_8, response.getHeader("Content-Type"));
+    }
+
+
     @Test
     public void testSetContentType01() {
         Response response = setupResponse();
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 6435b5daac..16bc068507 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.19 (markt)" rtext="in development">
+  <subsection name="Catalina">
+    <changelog>
+      <fix>
+        <bug>69967</bug>: Fix inconsistencies related to
+        <code>Content-Length</code> and <code>Content-Type</code> headers when
+        accessed using the <code>getHeader</code> method and similar. (remm)
+      </fix>
+    </changelog>
+  </subsection>
   <subsection name="Coyote">
     <changelog>
       <add>


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

Reply via email to