HTTP encode. Proper Content-Length YAZ-660
authorAdam Dickmeiss <adam@indexdata.dk>
Wed, 8 May 2013 09:29:58 +0000 (11:29 +0200)
committerAdam Dickmeiss <adam@indexdata.dk>
Wed, 8 May 2013 09:29:58 +0000 (11:29 +0200)
For yaz_encode_http_response use content_len to determine
resulting Content-Length in HTTP response and NOT Content-Length
supplied in headers by user.

src/http.c

index 6f53119..d16db35 100644 (file)
@@ -573,21 +573,18 @@ int yaz_encode_http_response(ODR o, Z_HTTP_Response *hr)
             hr->code,
             z_HTTP_errmsg(hr->code));
     odr_write2(o, sbuf, strlen(sbuf));
             hr->code,
             z_HTTP_errmsg(hr->code));
     odr_write2(o, sbuf, strlen(sbuf));
-    /* apply Content-Length if not already applied */
-    if (!z_HTTP_header_lookup(hr->headers,
-                              "Content-Length"))
-    {
-        char lstr[60];
-        sprintf(lstr, "Content-Length: %d\r\n",
-                hr->content_len);
-        odr_write2(o, lstr, strlen(lstr));
-    }
+    /* use content_len for Content-Length */
+    sprintf(sbuf, "Content-Length: %d\r\n", hr->content_len);
+    odr_write2(o, sbuf, strlen(sbuf));
     for (h = hr->headers; h; h = h->next)
     {
     for (h = hr->headers; h; h = h->next)
     {
-        odr_write2(o, h->name, strlen(h->name));
-        odr_write2(o, ": ", 2);
-        odr_write2(o, h->value, strlen(h->value));
-        odr_write2(o, "\r\n", 2);
+        if (yaz_matchstr(h->name, "Content-Length"))
+        {   /* skip Content-Length if given. content_len rules */
+            odr_write2(o, h->name, strlen(h->name));
+            odr_write2(o, ": ", 2);
+            odr_write2(o, h->value, strlen(h->value));
+            odr_write2(o, "\r\n", 2);
+        }
     }
     odr_write(o, (unsigned char *) "\r\n", 2);
     if (hr->content_buf)
     }
     odr_write(o, (unsigned char *) "\r\n", 2);
     if (hr->content_buf)