changeset 1934:420d129c4174

HTTP: avoid duplicate Content-length
author Adam Chlipala <adam@chlipala.net>
date Thu, 12 Dec 2013 10:24:38 -0500
parents 8e6e3d9cea22
children fda9d5af69e7
files include/urweb/urweb_cpp.h src/c/http.c src/c/urweb.c
diffstat 3 files changed, 14 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/include/urweb/urweb_cpp.h	Wed Dec 11 18:54:42 2013 -0500
+++ b/include/urweb/urweb_cpp.h	Thu Dec 12 10:24:38 2013 -0500
@@ -209,6 +209,7 @@
 
 void uw_write_header(struct uw_context *, uw_Basis_string);
 void uw_clear_headers(struct uw_context *);
+int uw_has_contentLength(struct uw_context *);
 void uw_Basis_clear_page(struct uw_context *);
 
 uw_Basis_string uw_Basis_get_cookie(struct uw_context *, uw_Basis_string c);
--- a/src/c/http.c	Wed Dec 11 18:54:42 2013 -0500
+++ b/src/c/http.c	Thu Dec 12 10:24:38 2013 -0500
@@ -233,8 +233,6 @@
                         sock, uw_really_send, close);
 
         if (rr != KEEP_OPEN) {
-          char clen[100];
-
           if (keepalive) {
             char *connection = uw_Basis_requestHeader(ctx, "Connection");
 
@@ -244,8 +242,13 @@
           if (!should_keepalive)
             uw_write_header(ctx, "Connection: close\r\n");
 
-          sprintf(clen, "Content-length: %d\r\n", uw_pagelen(ctx));
-          uw_write_header(ctx, clen);
+          if (!uw_has_contentLength(ctx)) {
+            char clen[100];
+
+            sprintf(clen, "Content-length: %d\r\n", uw_pagelen(ctx));
+            uw_write_header(ctx, clen);
+          }
+
           uw_send(ctx, sock);
         }
 
--- a/src/c/urweb.c	Wed Dec 11 18:54:42 2013 -0500
+++ b/src/c/urweb.c	Thu Dec 12 10:24:38 2013 -0500
@@ -1347,6 +1347,10 @@
   ctx->outHeaders.front += len;
 }
 
+int uw_has_contentLength(uw_context ctx) {
+  return strstr(ctx->outHeaders.start, "Content-length: ") != NULL;
+}
+
 void uw_clear_headers(uw_context ctx) {
   uw_buffer_reset(&ctx->outHeaders);
 }
@@ -3723,7 +3727,7 @@
   uw_write_header(ctx, on_success);
   uw_write_header(ctx, "Content-Type: ");
   uw_write_header(ctx, mimeType);
-  uw_write_header(ctx, "\r\nContent-Length: ");
+  uw_write_header(ctx, "\r\nContent-length: ");
   ctx_uw_buffer_check(ctx, "headers", &ctx->outHeaders, INTS_MAX);
   sprintf(ctx->outHeaders.front, "%lu%n", (unsigned long)b.size, &len);
   ctx->outHeaders.front += len;
@@ -3755,7 +3759,7 @@
   uw_write_header(ctx, on_success);
   uw_write_header(ctx, "Content-Type: ");
   uw_write_header(ctx, mimeType);
-  uw_write_header(ctx, "\r\nContent-Length: ");
+  uw_write_header(ctx, "\r\nContent-length: ");
   ctx_uw_buffer_check(ctx, "headers", &ctx->outHeaders, INTS_MAX);
   sprintf(ctx->outHeaders.front, "%lu%n", (unsigned long)uw_buffer_used(&ctx->page), &len);
   ctx->outHeaders.front += len;