From nobody Sat Dec 28 10:08:30 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1719671224171761.9640910314057; Sat, 29 Jun 2024 07:27:04 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sNZ1l-0006q2-Jc; Sat, 29 Jun 2024 10:26:09 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sNZ1j-0006pJ-Vw; Sat, 29 Jun 2024 10:26:07 -0400 Received: from isrv.corpit.ru ([86.62.121.231]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sNZ1h-0004MF-T0; Sat, 29 Jun 2024 10:26:07 -0400 Received: from tsrv.corpit.ru (tsrv.tls.msk.ru [192.168.177.2]) by isrv.corpit.ru (Postfix) with ESMTP id 1A9FF7540A; Sat, 29 Jun 2024 17:25:55 +0300 (MSK) Received: from tls.msk.ru (mjt.wg.tls.msk.ru [192.168.177.130]) by tsrv.corpit.ru (Postfix) with SMTP id 40A04F9C61; Sat, 29 Jun 2024 17:26:00 +0300 (MSK) Received: (nullmailer pid 1088710 invoked by uid 1000); Sat, 29 Jun 2024 14:26:00 -0000 From: Michael Tokarev To: Vladimir Sementsov-Ogievskiy , qemu-block@nongnu.org Cc: Michael Tokarev , qemu-devel@nongnu.org, hreitz@redhat.com, kwolf@redhat.com, qemu-trivial@nongnu.org Subject: [PATCH] block/curl: rewrite http header parsing function Date: Sat, 29 Jun 2024 17:25:42 +0300 Message-Id: <20240629142542.1086076-1-mjt@tls.msk.ru> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=86.62.121.231; envelope-from=mjt@tls.msk.ru; helo=isrv.corpit.ru X-Spam_score_int: -68 X-Spam_score: -6.9 X-Spam_bar: ------ X-Spam_report: (-6.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_HI=-5, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZM-MESSAGEID: 1719671231670100001 Content-Type: text/plain; charset="utf-8" Existing code was long, unclear and twisty. Signed-off-by: Michael Tokarev Reviewed-by: Vladimir Sementsov-Ogievskiy --- block/curl.c | 44 ++++++++++++++++++-------------------------- 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/block/curl.c b/block/curl.c index 419f7c89ef..9802d0319d 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 =3D opaque; size_t realsize =3D size * nmemb; - const char *header =3D (char *)ptr; - const char *end =3D header + realsize; - const char *accept_ranges =3D "accept-ranges:"; - const char *bytes =3D "bytes"; + const char *p =3D ptr; + const char *end =3D p + realsize; + const char *t =3D "accept-ranges : bytes "; /* A lowercase template */ =20 - if (realsize >=3D strlen(accept_ranges) - && g_ascii_strncasecmp(header, accept_ranges, - strlen(accept_ranges)) =3D=3D 0) { - - char *p =3D strchr(header, ':') + 1; - - /* Skip whitespace between the header name and value. */ - while (p < end && *p && g_ascii_isspace(*p)) { - p++; - } - - if (end - p >=3D strlen(bytes) - && strncmp(p, bytes, strlen(bytes)) =3D=3D 0) { - - /* Check that there is nothing but whitespace after the value.= */ - p +=3D strlen(bytes); - while (p < end && *p && g_ascii_isspace(*p)) { - p++; - } - - if (p =3D=3D end || !*p) { - s->accept_range =3D true; + /* check if header matches the "t" template */ + for (;;) { + if (*t =3D=3D ' ') { /* space in t matches any amount of isspace i= n p */ + if (p < end && g_ascii_isspace(*p)) { + ++p; + } else { + ++t; } + } else if (*t && p < end && *t =3D=3D g_ascii_tolower(*p)) { + ++p, ++t; + } else { + break; } } =20 + if (!*t && p =3D=3D end) { /* if we managed to reach ends of both stri= ngs */ + s->accept_range =3D true; + } + return realsize; } =20 --=20 2.39.2