changeset 1915:52e88e139b25

Add Connection and Content-length headers to raw HTTP responses
author Adam Chlipala <adam@chlipala.net>
date Wed, 27 Nov 2013 15:28:55 -0500
parents 6a2cc604a322
children e2345c438f08
files include/urweb/urweb_cpp.h src/c/http.c src/c/urweb.c
diffstat 3 files changed, 13 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/include/urweb/urweb_cpp.h	Wed Nov 27 08:59:57 2013 -0500
+++ b/include/urweb/urweb_cpp.h	Wed Nov 27 15:28:55 2013 -0500
@@ -58,6 +58,7 @@
 int uw_send(struct uw_context *, int sock);
 int uw_print(struct uw_context *, int fd);
 int uw_output(struct uw_context * ctx, int (*output)(void *data, const char *buf, size_t len), void *data);
+int uw_pagelen(struct uw_context *);
 
 int uw_set_input(struct uw_context *, const char *name, char *value);
 int uw_set_file_input(struct uw_context *, char *name, uw_Basis_file);
--- a/src/c/http.c	Wed Nov 27 08:59:57 2013 -0500
+++ b/src/c/http.c	Wed Nov 27 15:28:55 2013 -0500
@@ -204,7 +204,14 @@
                         on_success, on_failure,
                         NULL, log_error, log_debug,
                         sock, uw_really_send, close);
-        if (rr != KEEP_OPEN) uw_send(ctx, sock);
+        if (rr != KEEP_OPEN) {
+          char clen[100];
+
+          uw_write_header(ctx, "Connection: close\r\n");
+          sprintf(clen, "Content-length: %d\r\n", uw_pagelen(ctx));
+          uw_write_header(ctx, clen);
+          uw_send(ctx, sock);
+        }
 
         if (rr == SERVED || rr == FAILED)
           close(sock);
--- a/src/c/urweb.c	Wed Nov 27 08:59:57 2013 -0500
+++ b/src/c/urweb.c	Wed Nov 27 15:28:55 2013 -0500
@@ -1282,6 +1282,10 @@
   printf("Heap: %lu/%lu\n", (unsigned long)uw_buffer_used(&ctx->heap), (unsigned long)uw_buffer_avail(&ctx->heap));
 }
 
+int uw_pagelen(uw_context ctx) {
+  return ctx->page.front - ctx->page.start;
+}
+
 int uw_send(uw_context ctx, int sock) {
   int n = uw_really_send(sock, ctx->outHeaders.start, ctx->outHeaders.front - ctx->outHeaders.start);