httpd-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Luca Toscano <toscano.l...@gmail.com>
Subject Content-Length header for HTTP 204 and 1xx status codes
Date Wed, 30 Nov 2016 17:46:21 GMT
Hi everybody,

while working on https://bz.apache.org/bugzilla/show_bug.cgi?id=51350 a
user asked why httpd send the "Content-Length: 0" header for HTTP 204
responses given the following statement in the RFC:

https://tools.ietf.org/html/rfc7230#page-30
"A server MUST NOT send a Content-Length header field in any response with
a status code of 1xx (Informational) or 204 (No Content)."

I tried with a simple PHP script returning an HTTP 204 header (via
mod_proxy_fcgi) and indeed I can see the Content-Length: 0. After a bit of
digging it seems that ap_content_length_filter in protocol.c adds the
header when it evaluates:

        if (!(r->header_only
              && !r->bytes_sent
              && (r->sent_bodyct
                  || conf->http_cl_head_zero != AP_HTTP_CL_HEAD_ZERO_ENABLE
                  || apr_table_get(r->headers_out, "Content-Length")))) {
            ap_set_content_length(r, r->bytes_sent);
        }

An idea to fix the issue after a bit of chat on IRC could be the following
snippet, even if it might no be the right move. Since I am not expert
enough to attempt any code change to http_filters.c or protocol.c, I'll
wait for some feedback about how to proceed (that might also be "don't do
anything, it is fine in this way" :)

Thanks!

Luca

Index: modules/http/http_filters.c
===================================================================
--- modules/http/http_filters.c (revision 1772052)
+++ modules/http/http_filters.c (working copy)
@@ -1296,6 +1296,10 @@
         apr_table_unset(r->headers_out, "Content-Length");
     }

+    if ((r->status == HTTP_NO_CONTENT || ap_is_HTTP_INFO(r->status)) &&
!r->bytes_sent) {
+        apr_table_unset(r->headers_out, "Content-Length");
+    }
+
     ctype = ap_make_content_type(r, r->content_type);
     if (ctype) {
         apr_table_setn(r->headers_out, "Content-Type", ctype);

Mime
View raw message