# HG changeset patch # User Adam Chlipala # Date 1386861878 18000 # Node ID 420d129c41740789fa525832098745c13252812e # Parent 8e6e3d9cea228dd30315bee5338b7b23fdac6c64 HTTP: avoid duplicate Content-length diff -r 8e6e3d9cea22 -r 420d129c4174 include/urweb/urweb_cpp.h --- 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); diff -r 8e6e3d9cea22 -r 420d129c4174 src/c/http.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); } diff -r 8e6e3d9cea22 -r 420d129c4174 src/c/urweb.c --- 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;