Browse Source

do not generate Content-Length header for 1xx/204/304 replies, also suppress transmission of message body in these cases

Christian Grothoff 9 years ago
parent
commit
3d0516f447
3 changed files with 19 additions and 6 deletions
  1. 3 0
      ChangeLog
  2. 1 1
      src/include/microhttpd.h
  3. 15 5
      src/microhttpd/connection.c

+ 3 - 0
ChangeLog

@@ -1,3 +1,6 @@
+Wed Jun  1 21:59:34 CEST 2016
+	Do not send "Content-Length" header for 1xx/204/304 status codes. -CG
+
 Tue May 17 13:32:21 CEST 2016
 	Allow clients to determine whether a connection is suspended;
 	introduces MHD_CONNECTION_INFO_CONNECTION_SUSPENDED. -CG/FC

+ 1 - 1
src/include/microhttpd.h

@@ -130,7 +130,7 @@ typedef intptr_t ssize_t;
  * Current version of the library.
  * 0x01093001 = 1.9.30-1.
  */
-#define MHD_VERSION 0x00094903
+#define MHD_VERSION 0x00094904
 
 /**
  * MHD-internal return code for "YES".

+ 15 - 5
src/microhttpd/connection.c

@@ -876,9 +876,9 @@ build_header_response (struct MHD_Connection *connection)
       connection->write_buffer_size = 0;
       return MHD_YES;
     }
+  rc = connection->responseCode & (~MHD_ICY_FLAG);
   if (MHD_CONNECTION_FOOTERS_RECEIVED == connection->state)
     {
-      rc = connection->responseCode & (~MHD_ICY_FLAG);
       reason_phrase = MHD_get_reason_phrase_for (rc);
       sprintf (code,
                "%s %u %s\r\n",
@@ -988,7 +988,13 @@ build_header_response (struct MHD_Connection *connection)
       have_content_length = MHD_get_response_header (connection->response,
                                                      MHD_HTTP_HEADER_CONTENT_LENGTH);
 
+      /* MHD_HTTP_NO_CONTENT, MHD_HTTP_NOT_MODIFIED and 1xx-status
+         codes SHOULD NOT have a Content-Length according to spec;
+         also chunked encoding / unknown length or CONNECT... */
       if ( (MHD_SIZE_UNKNOWN != connection->response->total_size) &&
+           (MHD_HTTP_NO_CONTENT != rc) &&
+           (MHD_HTTP_NOT_MODIFIED != rc) &&
+           (MHD_HTTP_OK < rc) &&
            (NULL == have_content_length) &&
            ( (NULL == connection->method) ||
              (! MHD_str_equal_caseless_ (connection->method,
@@ -3101,11 +3107,15 @@ MHD_queue_response (struct MHD_Connection *connection,
   MHD_increment_response_rc (response);
   connection->response = response;
   connection->responseCode = status_code;
-  if ( (NULL != connection->method) &&
-       (MHD_str_equal_caseless_ (connection->method, MHD_HTTP_METHOD_HEAD)) )
+  if ( ( (NULL != connection->method) &&
+         (MHD_str_equal_caseless_ (connection->method, MHD_HTTP_METHOD_HEAD)) ) ||
+       (MHD_HTTP_OK > status_code) ||
+       (MHD_HTTP_NO_CONTENT == status_code) ||
+       (MHD_HTTP_NOT_MODIFIED == status_code) )
     {
-      /* if this is a "HEAD" request, pretend that we
-         have already sent the full message body */
+      /* if this is a "HEAD" request, or a status code for
+         which a body is not allowed, pretend that we
+         have already sent the full message body. */
       connection->response_write_position = response->total_size;
     }
   if ( (MHD_CONNECTION_HEADERS_PROCESSED == connection->state) &&