Skip to content

Commit

Permalink
curl: Case insensitive check for accept-ranges (RHBZ#1837337).
Browse files Browse the repository at this point in the history
When accessing an HTTP/2 server we read lowercase headers so the
existing test for byte range support did not work.  You would see an
error like this:

  nbdkit: curl[1]: error: server does not support 'range' (byte range) requests

This commit copies the bug fix which was recently added to qemu’s
block/curl.c.

qemu commits:

  commit 69032253c33ae1774233c63cedf36d32242a85fc
  Author: David Edmondson <david.edmondson@oracle.com>
  Date:   Mon Feb 24 10:13:10 2020 +0000

    block/curl: HTTP header field names are case insensitive

  commit 7788a319399f17476ff1dd43164c869e320820a2
  Author: David Edmondson <david.edmondson@oracle.com>
  Date:   Mon Feb 24 10:13:09 2020 +0000

    block/curl: HTTP header fields allow whitespace around values

Thanks: David Edmondson, Pino Toscano, Zi Liu
  • Loading branch information
rwmjones committed May 19, 2020
1 parent 9f34db7 commit c1260ec
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
1 change: 1 addition & 0 deletions plugins/curl/Makefile.am
Expand Up @@ -44,6 +44,7 @@ nbdkit_curl_plugin_la_SOURCES = \

nbdkit_curl_plugin_la_CPPFLAGS = \
-I$(top_srcdir)/include \
-I$(top_srcdir)/common/include \
-I$(top_srcdir)/common/utils \
$(NULL)
nbdkit_curl_plugin_la_CFLAGS = \
Expand Down
31 changes: 25 additions & 6 deletions plugins/curl/curl.c
Expand Up @@ -57,6 +57,7 @@
#include <nbdkit-plugin.h>

#include "cleanup.h"
#include "ascii-ctype.h"

static const char *url = NULL; /* required */

Expand Down Expand Up @@ -555,12 +556,30 @@ header_cb (void *ptr, size_t size, size_t nmemb, void *opaque)
{
struct curl_handle *h = opaque;
size_t realsize = size * nmemb;
const char *accept_line = "Accept-Ranges: bytes";
const char *line = ptr;

if (realsize >= strlen (accept_line) &&
strncmp (line, accept_line, strlen (accept_line)) == 0)
h->accept_range = true;
const char *header = ptr;
const char *end = header + realsize;
const char *accept_ranges = "accept-ranges:";
const char *bytes = "bytes";

if (realsize >= strlen (accept_ranges) &&
strncasecmp (header, accept_ranges, strlen (accept_ranges)) == 0) {
const char *p = strchr (header, ':') + 1;

/* Skip whitespace between the header name and value. */
while (p < end && *p && 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 && ascii_isspace (*p))
p++;

if (p == end || !*p)
h->accept_range = true;
}
}

return realsize;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/web-server.c
Expand Up @@ -235,7 +235,7 @@ handle_request (int s, bool headers_only)
const char response1_ok[] = "HTTP/1.1 200 OK\r\n";
const char response1_partial[] = "HTTP/1.1 206 Partial Content\r\n";
const char response2[] =
"Accept-Ranges: bytes\r\n"
"Accept-rANGES: bytes\r\n" /* See RHBZ#1837337 */
"Connection: keep-alive\r\n"
"Content-Type: application/octet-stream\r\n";
char response3[64];
Expand Down

0 comments on commit c1260ec

Please sign in to comment.