Handle spurious CR/LFs between responses
authorDan Winship <danw@gnome.org>
Mon, 14 Dec 2009 20:39:28 +0000 (15:39 -0500)
committerDan Winship <danw@gnome.org>
Mon, 14 Dec 2009 20:39:28 +0000 (15:39 -0500)
based on a patch from Alexander V. Butenko.
https://bugzilla.gnome.org/show_bug.cgi?id=602863

libsoup/soup-headers.c
tests/header-parsing.c

index 465c4f7..9835392 100644 (file)
@@ -188,10 +188,12 @@ soup_headers_parse_request (const char          *str,
        /* RFC 2616 4.1 "servers SHOULD ignore any empty line(s)
         * received where a Request-Line is expected."
         */
-       while (*str == '\r' || *str == '\n') {
+       while ((*str == '\r' || *str == '\n') && len > 0) {
                str++;
                len--;
        }
+       if (!len)
+               return SOUP_STATUS_BAD_REQUEST;
 
        /* RFC 2616 19.3 "[servers] SHOULD accept any amount of SP or
         * HT characters between [Request-Line] fields"
@@ -361,6 +363,17 @@ soup_headers_parse_response (const char          *str,
 
        g_return_val_if_fail (str && *str, FALSE);
 
+       /* Workaround for broken servers that send extra line breaks
+        * after a response, which we then see prepended to the next
+        * response on that connection.
+        */
+       while ((*str == '\r' || *str == '\n') && len > 0) {
+               str++;
+               len--;
+       }
+       if (!len)
+               return FALSE;
+
        if (!soup_headers_parse (str, len, headers)) 
                return FALSE;
 
index 4c45140..0086b22 100644 (file)
@@ -470,6 +470,14 @@ static struct ResponseTest {
          }
        },
 
+       /* qv bug 602863 */
+       { "HTTP 1.1 response with leading line break",
+         "\nHTTP/1.1 200 ok\r\nFoo: bar\r\n", -1,
+         SOUP_HTTP_1_1, SOUP_STATUS_OK, "ok",
+         { { "Foo", "bar" },
+           { NULL } }
+       },
+
        /*************************/
        /*** INVALID RESPONSES ***/
        /*************************/