Skip to content

Commit 5f9bd1d

Browse files
committed
Fix inconsistencies with special headers and getHeader
Handling of Content-Type and Content-Length with getHeader, getHeaderNames and getHeaders. BZ69967
1 parent 336ebeb commit 5f9bd1d

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

java/org/apache/catalina/connector/Response.java

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,23 @@ public void setLocale(Locale locale) {
747747

748748
@Override
749749
public String getHeader(String name) {
750+
// Need special handling for Content-Type and Content-Length due to
751+
// special handling of these in coyoteResponse
752+
char cc = name.charAt(0);
753+
if (cc == 'C' || cc == 'c') {
754+
if (name.equalsIgnoreCase("Content-Type")) {
755+
// Will return null if this has not been set
756+
return getCoyoteResponse().getContentType();
757+
}
758+
if (name.equalsIgnoreCase("Content-Length")) {
759+
// -1 means not known and is not sent to client
760+
if (getCoyoteResponse().getContentLengthLong() != -1) {
761+
return String.valueOf(getCoyoteResponse().getContentLengthLong());
762+
} else {
763+
return null;
764+
}
765+
}
766+
}
750767
return getCoyoteResponse().getMimeHeaders().getHeader(name);
751768
}
752769

@@ -759,13 +776,40 @@ public Collection<String> getHeaderNames() {
759776
for (int i = 0; i < n; i++) {
760777
result.add(headers.getName(i).toString());
761778
}
779+
if (getCoyoteResponse().getContentType() != null) {
780+
result.add("Content-Type");
781+
}
782+
if (getCoyoteResponse().getContentLengthLong() != -1) {
783+
result.add("Content-Length");
784+
}
762785
return result;
763-
764786
}
765787

766788

767789
@Override
768790
public Collection<String> getHeaders(String name) {
791+
// Need special handling for Content-Type and Content-Length due to
792+
// special handling of these in coyoteResponse
793+
char cc = name.charAt(0);
794+
if (cc == 'C' || cc == 'c') {
795+
if (name.equalsIgnoreCase("Content-Type")) {
796+
// Will return null if this has not been set
797+
String contentType = getCoyoteResponse().getContentType();
798+
if (contentType != null) {
799+
return Set.of(contentType);
800+
} else {
801+
return Set.of();
802+
}
803+
}
804+
if (name.equalsIgnoreCase("Content-Length")) {
805+
// -1 means not known and is not sent to client
806+
if (getCoyoteResponse().getContentLengthLong() != -1) {
807+
return Set.of(String.valueOf(getCoyoteResponse().getContentLengthLong()));
808+
} else {
809+
return Set.of();
810+
}
811+
}
812+
}
769813
Enumeration<String> enumeration = getCoyoteResponse().getMimeHeaders().values(name);
770814
Set<String> result = new LinkedHashSet<>();
771815
while (enumeration.hasMoreElements()) {

test/org/apache/catalina/connector/TestResponse.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,24 @@ public void testSetCharacterEncoding08() {
736736
}
737737

738738

739+
@Test
740+
public void testSetContentLengthHeader() {
741+
Response response = setupResponse();
742+
743+
response.setContentLength(10);
744+
Assert.assertEquals("10", response.getHeader("Content-Length"));
745+
}
746+
747+
748+
@Test
749+
public void testSetContentTypeHeader() {
750+
Response response = setupResponse();
751+
752+
response.setContentType(TEXT_UTF_8);
753+
Assert.assertEquals(TEXT_UTF_8, response.getHeader("Content-Type"));
754+
}
755+
756+
739757
@Test
740758
public void testSetContentType01() {
741759
Response response = setupResponse();

webapps/docs/changelog.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,11 @@
191191
to <code>true</code>. (markt)
192192
</update>
193193
<!-- Entries for backport and removal before 12.0.0-M1 below this line -->
194+
<fix>
195+
<bug>69967</bug>: Fix inconsistencies related to
196+
<code>Content-Length</code> and <code>Content-Type</code> headers when
197+
accessed using the <code>getHeader</code> method and similar. (remm)
198+
</fix>
194199
</changelog>
195200
</subsection>
196201
<subsection name="Coyote">

0 commit comments

Comments
 (0)