[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 01/16] block/curl: rewrite http header parsing function
From: |
Michael Tokarev |
Subject: |
[PULL 01/16] block/curl: rewrite http header parsing function |
Date: |
Wed, 17 Jul 2024 14:06:25 +0300 |
Existing code was long, unclear and twisty.
This also relaxes the rules a tiny bit: allows to have
whitespace before header name and colon and makes the
header value match to be case-insensitive.
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
---
block/curl.c | 44 ++++++++++++++++++--------------------------
1 file changed, 18 insertions(+), 26 deletions(-)
diff --git a/block/curl.c b/block/curl.c
index ef5252d00b..0fdb6d39ac 100644
--- a/block/curl.c
+++ b/block/curl.c
@@ -210,37 +210,29 @@ 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 *header = (char *)ptr;
- const char *end = header + realsize;
- const char *accept_ranges = "accept-ranges:";
- const char *bytes = "bytes";
+ const char *p = ptr;
+ const char *end = p + realsize;
+ const char *t = "accept-ranges : bytes "; /* A lowercase template */
- if (realsize >= strlen(accept_ranges)
- && g_ascii_strncasecmp(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;
+ /* check if header matches the "t" template */
+ for (;;) {
+ if (*t == ' ') { /* space in t matches any amount of isspace in p */
+ if (p < end && g_ascii_isspace(*p)) {
+ ++p;
+ } else {
+ ++t;
}
+ } else if (*t && p < end && *t == g_ascii_tolower(*p)) {
+ ++p, ++t;
+ } else {
+ break;
}
}
+ if (!*t && p == end) { /* if we managed to reach ends of both strings */
+ s->accept_range = true;
+ }
+
return realsize;
}
--
2.39.2
- [PULL 00/16] Trivial patches for 2024-07-17, Michael Tokarev, 2024/07/17
- [PULL 02/16] README.rst: add the missing punctuations, Michael Tokarev, 2024/07/17
- [PULL 01/16] block/curl: rewrite http header parsing function,
Michael Tokarev <=
- [PULL 03/16] accel/kvm/kvm-all: Fix superfluous trailing semicolon, Michael Tokarev, 2024/07/17
- [PULL 06/16] target/hexagon/imported/mmvec: Fix superfluous trailing semicolon, Michael Tokarev, 2024/07/17
- [PULL 07/16] doc/net/l2tpv3: Update boolean fields' description to avoid short-form use, Michael Tokarev, 2024/07/17
- [PULL 05/16] util/oslib-posix: Fix superfluous trailing semicolon, Michael Tokarev, 2024/07/17
- [PULL 09/16] hw/i386/sgx: Get rid of qemu_open_old(), Michael Tokarev, 2024/07/17
- [PULL 04/16] hw/i386/x86: Fix superfluous trailing semicolon, Michael Tokarev, 2024/07/17
- [PULL 11/16] hw/usb/u2f-passthru: Get rid of qemu_open_old(), Michael Tokarev, 2024/07/17
- [PULL 08/16] tests/avocado: Remove the non-working virtio_check_params test, Michael Tokarev, 2024/07/17
- [PULL 12/16] hw/vfio/container: Get rid of qemu_open_old(), Michael Tokarev, 2024/07/17
- [PULL 10/16] hw/usb/host-libusb: Get rid of qemu_open_old(), Michael Tokarev, 2024/07/17