qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 0/2] block/curl: Improve HTTP header parsing
@ 2020-02-24 10:13 David Edmondson
  2020-02-24 10:13 ` [PATCH v3 1/2] block/curl: HTTP header fields allow whitespace around values David Edmondson
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: David Edmondson @ 2020-02-24 10:13 UTC (permalink / raw)
  To: qemu-devel; +Cc: Kevin Wolf, David Edmondson, qemu-block, Max Reitz

An HTTP object store of my acquaintance returns "accept-ranges: bytes"
(all lower case) as a header, causing the QEMU curl backend to refuse
to talk to it. RFC 7230 says that HTTP headers are case insensitive,
so update the curl backend accordingly.

At the same time, allow for arbitrary white space around the HTTP
header field value, as required by the RFC.

v3:
- fix line length.

v2:
- strncasecmp -> g_ascii_strncasecmp (Philippe).
- isspace -> g_ascii_isspace, for good measure.

David Edmondson (2):
  block/curl: HTTP header fields allow whitespace around values
  block/curl: HTTP header field names are case insensitive

 block/curl.c | 32 ++++++++++++++++++++++++++++----
 1 file changed, 28 insertions(+), 4 deletions(-)

-- 
2.24.1



^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v3 1/2] block/curl: HTTP header fields allow whitespace around values
  2020-02-24 10:13 [PATCH v3 0/2] block/curl: Improve HTTP header parsing David Edmondson
@ 2020-02-24 10:13 ` David Edmondson
  2020-02-25 17:13   ` Max Reitz
  2020-02-24 10:13 ` [PATCH v3 2/2] block/curl: HTTP header field names are case insensitive David Edmondson
  2020-02-25 17:18 ` [PATCH v3 0/2] block/curl: Improve HTTP header parsing Max Reitz
  2 siblings, 1 reply; 6+ messages in thread
From: David Edmondson @ 2020-02-24 10:13 UTC (permalink / raw)
  To: qemu-devel; +Cc: Kevin Wolf, David Edmondson, qemu-block, Max Reitz

RFC 7230 section 3.2 indicates that whitespace is permitted between
the field name and field value and after the field value.

Signed-off-by: David Edmondson <david.edmondson@oracle.com>
---
 block/curl.c | 31 +++++++++++++++++++++++++++----
 1 file changed, 27 insertions(+), 4 deletions(-)

diff --git a/block/curl.c b/block/curl.c
index f86299378e38..f9ffb7f4e2bf 100644
--- a/block/curl.c
+++ b/block/curl.c
@@ -214,11 +214,34 @@ static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
 {
     BDRVCURLState *s = opaque;
     size_t realsize = size * nmemb;
-    const char *accept_line = "Accept-Ranges: bytes";
+    const char *header = (char *)ptr;
+    const char *end = header + realsize;
+    const char *accept_ranges = "Accept-Ranges:";
+    const char *bytes = "bytes";
 
-    if (realsize >= strlen(accept_line)
-        && strncmp((char *)ptr, accept_line, strlen(accept_line)) == 0) {
-        s->accept_range = true;
+    if (realsize >= strlen(accept_ranges)
+        && strncmp(header, accept_ranges, strlen(accept_ranges)) == 0) {
+
+        char *p = strchr(header, ':') + 1;
+
+        /* Skip whitespace between the header name and value. */
+        while (p < end && *p && g_ascii_isspace(*p)) {
+            p++;
+        }
+
+        if (end - p >= strlen(bytes)
+            && strncmp(p, bytes, strlen(bytes)) == 0) {
+
+            /* Check that there is nothing but whitespace after the value. */
+            p += strlen(bytes);
+            while (p < end && *p && g_ascii_isspace(*p)) {
+                p++;
+            }
+
+            if (p == end || !*p) {
+                s->accept_range = true;
+            }
+        }
     }
 
     return realsize;
-- 
2.24.1



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v3 2/2] block/curl: HTTP header field names are case insensitive
  2020-02-24 10:13 [PATCH v3 0/2] block/curl: Improve HTTP header parsing David Edmondson
  2020-02-24 10:13 ` [PATCH v3 1/2] block/curl: HTTP header fields allow whitespace around values David Edmondson
@ 2020-02-24 10:13 ` David Edmondson
  2020-02-25 17:17   ` Max Reitz
  2020-02-25 17:18 ` [PATCH v3 0/2] block/curl: Improve HTTP header parsing Max Reitz
  2 siblings, 1 reply; 6+ messages in thread
From: David Edmondson @ 2020-02-24 10:13 UTC (permalink / raw)
  To: qemu-devel; +Cc: Kevin Wolf, David Edmondson, qemu-block, Max Reitz

RFC 7230 section 3.2 indicates that HTTP header field names are case
insensitive.

Signed-off-by: David Edmondson <david.edmondson@oracle.com>
---
 block/curl.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/block/curl.c b/block/curl.c
index f9ffb7f4e2bf..6e325901dc68 100644
--- a/block/curl.c
+++ b/block/curl.c
@@ -216,11 +216,12 @@ static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
     size_t realsize = size * nmemb;
     const char *header = (char *)ptr;
     const char *end = header + realsize;
-    const char *accept_ranges = "Accept-Ranges:";
+    const char *accept_ranges = "accept-ranges:";
     const char *bytes = "bytes";
 
     if (realsize >= strlen(accept_ranges)
-        && strncmp(header, accept_ranges, strlen(accept_ranges)) == 0) {
+        && g_ascii_strncasecmp(header, accept_ranges,
+                               strlen(accept_ranges)) == 0) {
 
         char *p = strchr(header, ':') + 1;
 
-- 
2.24.1



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v3 1/2] block/curl: HTTP header fields allow whitespace around values
  2020-02-24 10:13 ` [PATCH v3 1/2] block/curl: HTTP header fields allow whitespace around values David Edmondson
@ 2020-02-25 17:13   ` Max Reitz
  0 siblings, 0 replies; 6+ messages in thread
From: Max Reitz @ 2020-02-25 17:13 UTC (permalink / raw)
  To: David Edmondson, qemu-devel; +Cc: Kevin Wolf, qemu-block


[-- Attachment #1.1: Type: text/plain, Size: 404 bytes --]

On 24.02.20 11:13, David Edmondson wrote:
> RFC 7230 section 3.2 indicates that whitespace is permitted between
> the field name and field value and after the field value.
> 
> Signed-off-by: David Edmondson <david.edmondson@oracle.com>
> ---
>  block/curl.c | 31 +++++++++++++++++++++++++++----
>  1 file changed, 27 insertions(+), 4 deletions(-)

Reviewed-by: Max Reitz <mreitz@redhat.com>


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v3 2/2] block/curl: HTTP header field names are case insensitive
  2020-02-24 10:13 ` [PATCH v3 2/2] block/curl: HTTP header field names are case insensitive David Edmondson
@ 2020-02-25 17:17   ` Max Reitz
  0 siblings, 0 replies; 6+ messages in thread
From: Max Reitz @ 2020-02-25 17:17 UTC (permalink / raw)
  To: David Edmondson, qemu-devel; +Cc: Kevin Wolf, qemu-block


[-- Attachment #1.1: Type: text/plain, Size: 332 bytes --]

On 24.02.20 11:13, David Edmondson wrote:
> RFC 7230 section 3.2 indicates that HTTP header field names are case
> insensitive.
> 
> Signed-off-by: David Edmondson <david.edmondson@oracle.com>
> ---
>  block/curl.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)

Reviewed-by: Max Reitz <mreitz@redhat.com>


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v3 0/2] block/curl: Improve HTTP header parsing
  2020-02-24 10:13 [PATCH v3 0/2] block/curl: Improve HTTP header parsing David Edmondson
  2020-02-24 10:13 ` [PATCH v3 1/2] block/curl: HTTP header fields allow whitespace around values David Edmondson
  2020-02-24 10:13 ` [PATCH v3 2/2] block/curl: HTTP header field names are case insensitive David Edmondson
@ 2020-02-25 17:18 ` Max Reitz
  2 siblings, 0 replies; 6+ messages in thread
From: Max Reitz @ 2020-02-25 17:18 UTC (permalink / raw)
  To: David Edmondson, qemu-devel; +Cc: Kevin Wolf, qemu-block


[-- Attachment #1.1: Type: text/plain, Size: 534 bytes --]

On 24.02.20 11:13, David Edmondson wrote:
> An HTTP object store of my acquaintance returns "accept-ranges: bytes"
> (all lower case) as a header, causing the QEMU curl backend to refuse
> to talk to it. RFC 7230 says that HTTP headers are case insensitive,
> so update the curl backend accordingly.
> 
> At the same time, allow for arbitrary white space around the HTTP
> header field value, as required by the RFC.

Thanks, applied to my block branch:

https://git.xanclic.moe/XanClic/qemu/commits/branch/block

Max


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2020-02-25 17:20 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-24 10:13 [PATCH v3 0/2] block/curl: Improve HTTP header parsing David Edmondson
2020-02-24 10:13 ` [PATCH v3 1/2] block/curl: HTTP header fields allow whitespace around values David Edmondson
2020-02-25 17:13   ` Max Reitz
2020-02-24 10:13 ` [PATCH v3 2/2] block/curl: HTTP header field names are case insensitive David Edmondson
2020-02-25 17:17   ` Max Reitz
2020-02-25 17:18 ` [PATCH v3 0/2] block/curl: Improve HTTP header parsing Max Reitz

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).