From nobody Mon Nov 25 11:32:55 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1715691486; cv=none; d=zohomail.com; s=zohoarc; b=B+ayEhkY9qxQMtvmv6G6iW2iFXY/AoFxjxEuNZ/1JWAJXS760K9QSzuocUqrHQSq9vU39kB+VFl4+npBXsfvz3pgVKHnCVZmPspHd0OFHkID8jxkgVt8nKgbQ5JxbwsQF59guJ/CN+EAHX7D1Zad2QPf/RpqfIbtnfDSfGiaPas= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1715691486; h=Content-Transfer-Encoding:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To:Cc; bh=sNmwDsALBheVCqjJ6O/+VPFPRWLoJbc0qD/jpQh6/DM=; b=kKkKlnVXWSsLi2WnV7DJU/lehR94l8y87jiAjSzLFx0batiHaklnhnqWaCseut7no6kYzJTehtNOcNdHHVi4CZG6DDv2KEnAtyLviVxQGE0Abm7fbmAV+5el5Yj0zsgewc4ALsMgYVfvSg+5BUn9+KFHrqDMtniqtcDeAFKnmFU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1715691486609215.17217828424089; Tue, 14 May 2024 05:58:06 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1s6riw-0004Hf-9i; Tue, 14 May 2024 08:57:44 -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 1s6riY-0003yD-2h for qemu-devel@nongnu.org; Tue, 14 May 2024 08:57:22 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1s6rhu-00064f-K9 for qemu-devel@nongnu.org; Tue, 14 May 2024 08:56:45 -0400 Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-510-MlIahdrbNe2nxyVfdTnhtw-1; Tue, 14 May 2024 08:51:36 -0400 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id DA3643C00084; Tue, 14 May 2024 12:51:35 +0000 (UTC) Received: from thuth-p1g4.redhat.com (unknown [10.39.193.26]) by smtp.corp.redhat.com (Postfix) with ESMTP id 55ABD400EAC; Tue, 14 May 2024 12:51:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1715691397; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=sNmwDsALBheVCqjJ6O/+VPFPRWLoJbc0qD/jpQh6/DM=; b=hTd/y9MSzxdMP3mDnTxWaFfDnDw/qnMuZbU2ZS5Kj3708FANrP+NgALUA0ToiMHyQoWWn/ nKUf7X0PFu9vAjG29YiVF7Pk30AhvxmsaN7oM11ptUxdZCzAyXBZHO5IE6TONPB1ZDokmU ADtlyKO1GsEkFLJyiVP4F05tog3+dSo= X-MC-Unique: MlIahdrbNe2nxyVfdTnhtw-1 From: Thomas Huth To: qemu-devel@nongnu.org, Richard Henderson Subject: [PULL 11/11] util/uri: Remove the old URI parsing code Date: Tue, 14 May 2024 14:51:19 +0200 Message-ID: <20240514125119.284638-12-thuth@redhat.com> In-Reply-To: <20240514125119.284638-1-thuth@redhat.com> References: <20240514125119.284638-1-thuth@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 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+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=thuth@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, T_SPF_TEMPERROR=0.01 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1715691488709100003 Content-Type: text/plain; charset="utf-8" Now that we switched all consumers of the URI code to use the URI parsing functions from glib instead, we can remove our internal URI parsing code since it is not used anymore. Reviewed-by: Eric Blake Message-ID: <20240418101056.302103-14-thuth@redhat.com> Signed-off-by: Thomas Huth --- include/qemu/uri.h | 99 --- util/uri.c | 1466 -------------------------------------------- util/meson.build | 2 +- 3 files changed, 1 insertion(+), 1566 deletions(-) delete mode 100644 include/qemu/uri.h delete mode 100644 util/uri.c diff --git a/include/qemu/uri.h b/include/qemu/uri.h deleted file mode 100644 index 255e61f452..0000000000 --- a/include/qemu/uri.h +++ /dev/null @@ -1,99 +0,0 @@ -/** - * Summary: library of generic URI related routines - * Description: library of generic URI related routines - * Implements RFC 2396 - * - * Copyright (C) 1998-2003 Daniel Veillard. All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a= copy - * of this software and associated documentation files (the "Software"), t= o deal - * in the Software without restriction, including without limitation the r= ights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or se= ll - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included= in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS= OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHAL= L THE - * DANIEL VEILLARD BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WH= ETHER - * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWA= RE. - * - * Except as contained in this notice, the name of Daniel Veillard shall n= ot - * be used in advertising or otherwise to promote the sale, use or other - * dealings in this Software without prior written authorization from him. - * - * Author: Daniel Veillard - ** - * Copyright (C) 2007 Red Hat, Inc. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library. If not, see . - * - * Authors: - * Richard W.M. Jones - * - * Utility functions to help parse and assemble query strings. - */ - -#ifndef QEMU_URI_H -#define QEMU_URI_H - -/** - * URI: - * - * A parsed URI reference. This is a struct containing the various fields - * as described in RFC 2396 but separated for further processing. - */ -typedef struct URI { - char *scheme; /* the URI scheme */ - char *opaque; /* opaque part */ - char *authority; /* the authority part */ - char *server; /* the server part */ - char *user; /* the user part */ - int port; /* the port number */ - char *path; /* the path string */ - char *fragment; /* the fragment identifier */ - int cleanup; /* parsing potentially unclean URI */ - char *query; /* the query string (as it appears in the URI) */ -} URI; - -URI *uri_new(void); -URI *uri_parse(const char *str); -URI *uri_parse_raw(const char *str, int raw); -int uri_parse_into(URI *uri, const char *str); -char *uri_to_string(URI *uri); -void uri_free(URI *uri); - -/* Single web service query parameter 'name=3Dvalue'. */ -typedef struct QueryParam { - char *name; /* Name (unescaped). */ - char *value; /* Value (unescaped). */ - int ignore; /* Ignore this field in qparam_get_query */ -} QueryParam; - -/* Set of parameters. */ -typedef struct QueryParams { - int n; /* number of parameters used */ - int alloc; /* allocated space */ - QueryParam *p; /* array of parameters */ -} QueryParams; - -QueryParams *query_params_new(int init_alloc); -QueryParams *query_params_parse(const char *query); -void query_params_free(QueryParams *ps); - -#endif /* QEMU_URI_H */ diff --git a/util/uri.c b/util/uri.c deleted file mode 100644 index 573174bf47..0000000000 --- a/util/uri.c +++ /dev/null @@ -1,1466 +0,0 @@ -/** - * uri.c: set of generic URI related routines - * - * Reference: RFCs 3986, 2732 and 2373 - * - * Copyright (C) 1998-2003 Daniel Veillard. All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a= copy - * of this software and associated documentation files (the "Software"), t= o deal - * in the Software without restriction, including without limitation the r= ights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or se= ll - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included= in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS= OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHAL= L THE - * DANIEL VEILLARD BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WH= ETHER - * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWA= RE. - * - * Except as contained in this notice, the name of Daniel Veillard shall n= ot - * be used in advertising or otherwise to promote the sale, use or other - * dealings in this Software without prior written authorization from him. - * - * daniel@veillard.com - * - ** - * - * Copyright (C) 2007, 2009-2010 Red Hat, Inc. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library. If not, see . - * - * Authors: - * Richard W.M. Jones - * - */ - -#include "qemu/osdep.h" -#include "qemu/cutils.h" - -#include "qemu/uri.h" - -static void uri_clean(URI *uri); - -/* - * Old rule from 2396 used in legacy handling code - * alpha =3D lowalpha | upalpha - */ -#define IS_ALPHA(x) (IS_LOWALPHA(x) || IS_UPALPHA(x)) - -/* - * lowalpha =3D "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | - * "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | - * "u" | "v" | "w" | "x" | "y" | "z" - */ - -#define IS_LOWALPHA(x) (((x) >=3D 'a') && ((x) <=3D 'z')) - -/* - * upalpha =3D "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | - * "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | - * "U" | "V" | "W" | "X" | "Y" | "Z" - */ -#define IS_UPALPHA(x) (((x) >=3D 'A') && ((x) <=3D 'Z')) - -#ifdef IS_DIGIT -#undef IS_DIGIT -#endif -/* - * digit =3D "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" - */ -#define IS_DIGIT(x) (((x) >=3D '0') && ((x) <=3D '9')) - -/* - * alphanum =3D alpha | digit - */ - -#define IS_ALPHANUM(x) (IS_ALPHA(x) || IS_DIGIT(x)) - -/* - * mark =3D "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")" - */ - -#define IS_MARK(x) (((x) =3D=3D '-') || ((x) =3D=3D '_') || ((x) =3D=3D '.= ') || \ - ((x) =3D=3D '!') || ((x) =3D=3D '~') || ((x) =3D=3D '*') || ((x) =3D= =3D '\'') || \ - ((x) =3D=3D '(') || ((x) =3D=3D ')')) - -/* - * unwise =3D "{" | "}" | "|" | "\" | "^" | "`" - */ - -#define IS_UNWISE(p) = \ - (((*(p) =3D=3D '{')) || ((*(p) =3D=3D '}')) || ((*(p) =3D=3D '|')) || = \ - ((*(p) =3D=3D '\\')) || ((*(p) =3D=3D '^')) || ((*(p) =3D=3D '[')) ||= \ - ((*(p) =3D=3D ']')) || ((*(p) =3D=3D '`'))) -/* - * reserved =3D ";" | "/" | "?" | ":" | "@" | "&" | "=3D" | "+" | "$" | ",= " | - * "[" | "]" - */ - -#define IS_RESERVED(x) (((x) =3D=3D ';') || ((x) =3D=3D '/') || ((x) =3D= =3D '?') || \ - ((x) =3D=3D ':') || ((x) =3D=3D '@') || ((x) =3D=3D '&') || ((x) =3D= =3D '=3D') || \ - ((x) =3D=3D '+') || ((x) =3D=3D '$') || ((x) =3D=3D ',') || ((x) =3D= =3D '[') || \ - ((x) =3D=3D ']')) - -/* - * unreserved =3D alphanum | mark - */ - -#define IS_UNRESERVED(x) (IS_ALPHANUM(x) || IS_MARK(x)) - -/* - * Skip to next pointer char, handle escaped sequences - */ - -#define NEXT(p) ((*p =3D=3D '%') ? p +=3D 3 : p++) - -/* - * Productions from the spec. - * - * authority =3D server | reg_name - * reg_name =3D 1*( unreserved | escaped | "$" | "," | - * ";" | ":" | "@" | "&" | "=3D" | "+" ) - * - * path =3D [ abs_path | opaque_part ] - */ - -/************************************************************************ - * * - * RFC 3986 parser * - * * - ************************************************************************/ - -#define ISA_DIGIT(p) ((*(p) >=3D '0') && (*(p) <=3D '9')) -#define ISA_ALPHA(p) (((*(p) >=3D 'a') && (*(p) <=3D 'z')) || = \ - ((*(p) >=3D 'A') && (*(p) <=3D 'Z'))) -#define ISA_HEXDIG(p) = \ - (ISA_DIGIT(p) || ((*(p) >=3D 'a') && (*(p) <=3D 'f')) || = \ - ((*(p) >=3D 'A') && (*(p) <=3D 'F'))) - -/* - * sub-delims =3D "!" / "$" / "&" / "'" / "(" / ")" - * / "*" / "+" / "," / ";" / "=3D" - */ -#define ISA_SUB_DELIM(p) = \ - (((*(p) =3D=3D '!')) || ((*(p) =3D=3D '$')) || ((*(p) =3D=3D '&')) || = \ - ((*(p) =3D=3D '(')) || ((*(p) =3D=3D ')')) || ((*(p) =3D=3D '*')) || = \ - ((*(p) =3D=3D '+')) || ((*(p) =3D=3D ',')) || ((*(p) =3D=3D ';')) || = \ - ((*(p) =3D=3D '=3D')) || ((*(p) =3D=3D '\''))) - -/* - * unreserved =3D ALPHA / DIGIT / "-" / "." / "_" / "~" - */ -#define ISA_UNRESERVED(p) = \ - ((ISA_ALPHA(p)) || (ISA_DIGIT(p)) || ((*(p) =3D=3D '-')) || = \ - ((*(p) =3D=3D '.')) || ((*(p) =3D=3D '_')) || ((*(p) =3D=3D '~'))) - -/* - * pct-encoded =3D "%" HEXDIG HEXDIG - */ -#define ISA_PCT_ENCODED(p) = \ - ((*(p) =3D=3D '%') && (ISA_HEXDIG(p + 1)) && (ISA_HEXDIG(p + 2))) - -/* - * pchar =3D unreserved / pct-encoded / sub-delims / ":" / "@" - */ -#define ISA_PCHAR(p) = \ - (ISA_UNRESERVED(p) || ISA_PCT_ENCODED(p) || ISA_SUB_DELIM(p) || = \ - ((*(p) =3D=3D ':')) || ((*(p) =3D=3D '@'))) - -/** - * rfc3986_parse_scheme: - * @uri: pointer to an URI structure - * @str: pointer to the string to analyze - * - * Parse an URI scheme - * - * ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) - * - * Returns 0 or the error code - */ -static int rfc3986_parse_scheme(URI *uri, const char **str) -{ - const char *cur; - - if (str =3D=3D NULL) { - return -1; - } - - cur =3D *str; - if (!ISA_ALPHA(cur)) { - return 2; - } - cur++; - while (ISA_ALPHA(cur) || ISA_DIGIT(cur) || (*cur =3D=3D '+') || (*cur = =3D=3D '-') || - (*cur =3D=3D '.')) { - cur++; - } - if (uri !=3D NULL) { - g_free(uri->scheme); - uri->scheme =3D g_strndup(*str, cur - *str); - } - *str =3D cur; - return 0; -} - -/** - * rfc3986_parse_fragment: - * @uri: pointer to an URI structure - * @str: pointer to the string to analyze - * - * Parse the query part of an URI - * - * fragment =3D *( pchar / "/" / "?" ) - * NOTE: the strict syntax as defined by 3986 does not allow '[' and ']' - * in the fragment identifier but this is used very broadly for - * xpointer scheme selection, so we are allowing it here to not break - * for example all the DocBook processing chains. - * - * Returns 0 or the error code - */ -static int rfc3986_parse_fragment(URI *uri, const char **str) -{ - const char *cur; - - if (str =3D=3D NULL) { - return -1; - } - - cur =3D *str; - - while ((ISA_PCHAR(cur)) || (*cur =3D=3D '/') || (*cur =3D=3D '?') || - (*cur =3D=3D '[') || (*cur =3D=3D ']') || - ((uri !=3D NULL) && (uri->cleanup & 1) && (IS_UNWISE(cur)))) { - NEXT(cur); - } - if (uri !=3D NULL) { - g_free(uri->fragment); - if (uri->cleanup & 2) { - uri->fragment =3D g_strndup(*str, cur - *str); - } else { - uri->fragment =3D g_uri_unescape_segment(*str, cur, NULL); - } - } - *str =3D cur; - return 0; -} - -/** - * rfc3986_parse_query: - * @uri: pointer to an URI structure - * @str: pointer to the string to analyze - * - * Parse the query part of an URI - * - * query =3D *uric - * - * Returns 0 or the error code - */ -static int rfc3986_parse_query(URI *uri, const char **str) -{ - const char *cur; - - if (str =3D=3D NULL) { - return -1; - } - - cur =3D *str; - - while ((ISA_PCHAR(cur)) || (*cur =3D=3D '/') || (*cur =3D=3D '?') || - ((uri !=3D NULL) && (uri->cleanup & 1) && (IS_UNWISE(cur)))) { - NEXT(cur); - } - if (uri !=3D NULL) { - g_free(uri->query); - uri->query =3D g_strndup(*str, cur - *str); - } - *str =3D cur; - return 0; -} - -/** - * rfc3986_parse_port: - * @uri: pointer to an URI structure - * @str: the string to analyze - * - * Parse a port part and fills in the appropriate fields - * of the @uri structure - * - * port =3D *DIGIT - * - * Returns 0 or the error code - */ -static int rfc3986_parse_port(URI *uri, const char **str) -{ - const char *cur =3D *str; - int port =3D 0; - - if (ISA_DIGIT(cur)) { - while (ISA_DIGIT(cur)) { - port =3D port * 10 + (*cur - '0'); - if (port > 65535) { - return 1; - } - cur++; - } - if (uri) { - uri->port =3D port; - } - *str =3D cur; - return 0; - } - return 1; -} - -/** - * rfc3986_parse_user_info: - * @uri: pointer to an URI structure - * @str: the string to analyze - * - * Parse a user information part and fill in the appropriate fields - * of the @uri structure - * - * userinfo =3D *( unreserved / pct-encoded / sub-delims / ":" ) - * - * Returns 0 or the error code - */ -static int rfc3986_parse_user_info(URI *uri, const char **str) -{ - const char *cur; - - cur =3D *str; - while (ISA_UNRESERVED(cur) || ISA_PCT_ENCODED(cur) || ISA_SUB_DELIM(cu= r) || - (*cur =3D=3D ':')) { - NEXT(cur); - } - if (*cur =3D=3D '@') { - if (uri !=3D NULL) { - g_free(uri->user); - if (uri->cleanup & 2) { - uri->user =3D g_strndup(*str, cur - *str); - } else { - uri->user =3D g_uri_unescape_segment(*str, cur, NULL); - } - } - *str =3D cur; - return 0; - } - return 1; -} - -/** - * rfc3986_parse_dec_octet: - * @str: the string to analyze - * - * dec-octet =3D DIGIT ; 0-9 - * / %x31-39 DIGIT ; 10-99 - * / "1" 2DIGIT ; 100-199 - * / "2" %x30-34 DIGIT ; 200-249 - * / "25" %x30-35 ; 250-255 - * - * Skip a dec-octet. - * - * Returns 0 if found and skipped, 1 otherwise - */ -static int rfc3986_parse_dec_octet(const char **str) -{ - const char *cur =3D *str; - - if (!(ISA_DIGIT(cur))) { - return 1; - } - if (!ISA_DIGIT(cur + 1)) { - cur++; - } else if ((*cur !=3D '0') && (ISA_DIGIT(cur + 1)) && (!ISA_DIGIT(cur = + 2))) { - cur +=3D 2; - } else if ((*cur =3D=3D '1') && (ISA_DIGIT(cur + 1)) && (ISA_DIGIT(cur= + 2))) { - cur +=3D 3; - } else if ((*cur =3D=3D '2') && (*(cur + 1) >=3D '0') && (*(cur + 1) <= =3D '4') && - (ISA_DIGIT(cur + 2))) { - cur +=3D 3; - } else if ((*cur =3D=3D '2') && (*(cur + 1) =3D=3D '5') && (*(cur + 2)= >=3D '0') && - (*(cur + 1) <=3D '5')) { - cur +=3D 3; - } else { - return 1; - } - *str =3D cur; - return 0; -} -/** - * rfc3986_parse_host: - * @uri: pointer to an URI structure - * @str: the string to analyze - * - * Parse an host part and fills in the appropriate fields - * of the @uri structure - * - * host =3D IP-literal / IPv4address / reg-name - * IP-literal =3D "[" ( IPv6address / IPvFuture ) "]" - * IPv4address =3D dec-octet "." dec-octet "." dec-octet "." dec-octet - * reg-name =3D *( unreserved / pct-encoded / sub-delims ) - * - * Returns 0 or the error code - */ -static int rfc3986_parse_host(URI *uri, const char **str) -{ - const char *cur =3D *str; - const char *host; - - host =3D cur; - /* - * IPv6 and future addressing scheme are enclosed between brackets - */ - if (*cur =3D=3D '[') { - cur++; - while ((*cur !=3D ']') && (*cur !=3D 0)) { - cur++; - } - if (*cur !=3D ']') { - return 1; - } - cur++; - goto found; - } - /* - * try to parse an IPv4 - */ - if (ISA_DIGIT(cur)) { - if (rfc3986_parse_dec_octet(&cur) !=3D 0) { - goto not_ipv4; - } - if (*cur !=3D '.') { - goto not_ipv4; - } - cur++; - if (rfc3986_parse_dec_octet(&cur) !=3D 0) { - goto not_ipv4; - } - if (*cur !=3D '.') { - goto not_ipv4; - } - if (rfc3986_parse_dec_octet(&cur) !=3D 0) { - goto not_ipv4; - } - if (*cur !=3D '.') { - goto not_ipv4; - } - if (rfc3986_parse_dec_octet(&cur) !=3D 0) { - goto not_ipv4; - } - goto found; - not_ipv4: - cur =3D *str; - } - /* - * then this should be a hostname which can be empty - */ - while (ISA_UNRESERVED(cur) || ISA_PCT_ENCODED(cur) || ISA_SUB_DELIM(cu= r)) { - NEXT(cur); - } -found: - if (uri !=3D NULL) { - g_free(uri->authority); - uri->authority =3D NULL; - g_free(uri->server); - if (cur !=3D host) { - if (uri->cleanup & 2) { - uri->server =3D g_strndup(host, cur - host); - } else { - uri->server =3D g_uri_unescape_segment(host, cur, NULL); - } - } else { - uri->server =3D NULL; - } - } - *str =3D cur; - return 0; -} - -/** - * rfc3986_parse_authority: - * @uri: pointer to an URI structure - * @str: the string to analyze - * - * Parse an authority part and fills in the appropriate fields - * of the @uri structure - * - * authority =3D [ userinfo "@" ] host [ ":" port ] - * - * Returns 0 or the error code - */ -static int rfc3986_parse_authority(URI *uri, const char **str) -{ - const char *cur; - int ret; - - cur =3D *str; - /* - * try to parse a userinfo and check for the trailing @ - */ - ret =3D rfc3986_parse_user_info(uri, &cur); - if ((ret !=3D 0) || (*cur !=3D '@')) { - cur =3D *str; - } else { - cur++; - } - ret =3D rfc3986_parse_host(uri, &cur); - if (ret !=3D 0) { - return ret; - } - if (*cur =3D=3D ':') { - cur++; - ret =3D rfc3986_parse_port(uri, &cur); - if (ret !=3D 0) { - return ret; - } - } - *str =3D cur; - return 0; -} - -/** - * rfc3986_parse_segment: - * @str: the string to analyze - * @forbid: an optional forbidden character - * @empty: allow an empty segment - * - * Parse a segment and fills in the appropriate fields - * of the @uri structure - * - * segment =3D *pchar - * segment-nz =3D 1*pchar - * segment-nz-nc =3D 1*( unreserved / pct-encoded / sub-delims / "@" ) - * ; non-zero-length segment without any colon ":" - * - * Returns 0 or the error code - */ -static int rfc3986_parse_segment(const char **str, char forbid, int empty) -{ - const char *cur; - - cur =3D *str; - if (!ISA_PCHAR(cur)) { - if (empty) { - return 0; - } - return 1; - } - while (ISA_PCHAR(cur) && (*cur !=3D forbid)) { - NEXT(cur); - } - *str =3D cur; - return 0; -} - -/** - * rfc3986_parse_path_ab_empty: - * @uri: pointer to an URI structure - * @str: the string to analyze - * - * Parse an path absolute or empty and fills in the appropriate fields - * of the @uri structure - * - * path-abempty =3D *( "/" segment ) - * - * Returns 0 or the error code - */ -static int rfc3986_parse_path_ab_empty(URI *uri, const char **str) -{ - const char *cur; - int ret; - - cur =3D *str; - - while (*cur =3D=3D '/') { - cur++; - ret =3D rfc3986_parse_segment(&cur, 0, 1); - if (ret !=3D 0) { - return ret; - } - } - if (uri !=3D NULL) { - g_free(uri->path); - if (*str !=3D cur) { - if (uri->cleanup & 2) { - uri->path =3D g_strndup(*str, cur - *str); - } else { - uri->path =3D g_uri_unescape_segment(*str, cur, NULL); - } - } else { - uri->path =3D NULL; - } - } - *str =3D cur; - return 0; -} - -/** - * rfc3986_parse_path_absolute: - * @uri: pointer to an URI structure - * @str: the string to analyze - * - * Parse an path absolute and fills in the appropriate fields - * of the @uri structure - * - * path-absolute =3D "/" [ segment-nz *( "/" segment ) ] - * - * Returns 0 or the error code - */ -static int rfc3986_parse_path_absolute(URI *uri, const char **str) -{ - const char *cur; - int ret; - - cur =3D *str; - - if (*cur !=3D '/') { - return 1; - } - cur++; - ret =3D rfc3986_parse_segment(&cur, 0, 0); - if (ret =3D=3D 0) { - while (*cur =3D=3D '/') { - cur++; - ret =3D rfc3986_parse_segment(&cur, 0, 1); - if (ret !=3D 0) { - return ret; - } - } - } - if (uri !=3D NULL) { - g_free(uri->path); - if (cur !=3D *str) { - if (uri->cleanup & 2) { - uri->path =3D g_strndup(*str, cur - *str); - } else { - uri->path =3D g_uri_unescape_segment(*str, cur, NULL); - } - } else { - uri->path =3D NULL; - } - } - *str =3D cur; - return 0; -} - -/** - * rfc3986_parse_path_rootless: - * @uri: pointer to an URI structure - * @str: the string to analyze - * - * Parse an path without root and fills in the appropriate fields - * of the @uri structure - * - * path-rootless =3D segment-nz *( "/" segment ) - * - * Returns 0 or the error code - */ -static int rfc3986_parse_path_rootless(URI *uri, const char **str) -{ - const char *cur; - int ret; - - cur =3D *str; - - ret =3D rfc3986_parse_segment(&cur, 0, 0); - if (ret !=3D 0) { - return ret; - } - while (*cur =3D=3D '/') { - cur++; - ret =3D rfc3986_parse_segment(&cur, 0, 1); - if (ret !=3D 0) { - return ret; - } - } - if (uri !=3D NULL) { - g_free(uri->path); - if (cur !=3D *str) { - if (uri->cleanup & 2) { - uri->path =3D g_strndup(*str, cur - *str); - } else { - uri->path =3D g_uri_unescape_segment(*str, cur, NULL); - } - } else { - uri->path =3D NULL; - } - } - *str =3D cur; - return 0; -} - -/** - * rfc3986_parse_path_no_scheme: - * @uri: pointer to an URI structure - * @str: the string to analyze - * - * Parse an path which is not a scheme and fills in the appropriate fields - * of the @uri structure - * - * path-noscheme =3D segment-nz-nc *( "/" segment ) - * - * Returns 0 or the error code - */ -static int rfc3986_parse_path_no_scheme(URI *uri, const char **str) -{ - const char *cur; - int ret; - - cur =3D *str; - - ret =3D rfc3986_parse_segment(&cur, ':', 0); - if (ret !=3D 0) { - return ret; - } - while (*cur =3D=3D '/') { - cur++; - ret =3D rfc3986_parse_segment(&cur, 0, 1); - if (ret !=3D 0) { - return ret; - } - } - if (uri !=3D NULL) { - g_free(uri->path); - if (cur !=3D *str) { - if (uri->cleanup & 2) { - uri->path =3D g_strndup(*str, cur - *str); - } else { - uri->path =3D g_uri_unescape_segment(*str, cur, NULL); - } - } else { - uri->path =3D NULL; - } - } - *str =3D cur; - return 0; -} - -/** - * rfc3986_parse_hier_part: - * @uri: pointer to an URI structure - * @str: the string to analyze - * - * Parse an hierarchical part and fills in the appropriate fields - * of the @uri structure - * - * hier-part =3D "//" authority path-abempty - * / path-absolute - * / path-rootless - * / path-empty - * - * Returns 0 or the error code - */ -static int rfc3986_parse_hier_part(URI *uri, const char **str) -{ - const char *cur; - int ret; - - cur =3D *str; - - if ((*cur =3D=3D '/') && (*(cur + 1) =3D=3D '/')) { - cur +=3D 2; - ret =3D rfc3986_parse_authority(uri, &cur); - if (ret !=3D 0) { - return ret; - } - ret =3D rfc3986_parse_path_ab_empty(uri, &cur); - if (ret !=3D 0) { - return ret; - } - *str =3D cur; - return 0; - } else if (*cur =3D=3D '/') { - ret =3D rfc3986_parse_path_absolute(uri, &cur); - if (ret !=3D 0) { - return ret; - } - } else if (ISA_PCHAR(cur)) { - ret =3D rfc3986_parse_path_rootless(uri, &cur); - if (ret !=3D 0) { - return ret; - } - } else { - /* path-empty is effectively empty */ - if (uri !=3D NULL) { - g_free(uri->path); - uri->path =3D NULL; - } - } - *str =3D cur; - return 0; -} - -/** - * rfc3986_parse_relative_ref: - * @uri: pointer to an URI structure - * @str: the string to analyze - * - * Parse an URI string and fills in the appropriate fields - * of the @uri structure - * - * relative-ref =3D relative-part [ "?" query ] [ "#" fragment ] - * relative-part =3D "//" authority path-abempty - * / path-absolute - * / path-noscheme - * / path-empty - * - * Returns 0 or the error code - */ -static int rfc3986_parse_relative_ref(URI *uri, const char *str) -{ - int ret; - - if ((*str =3D=3D '/') && (*(str + 1) =3D=3D '/')) { - str +=3D 2; - ret =3D rfc3986_parse_authority(uri, &str); - if (ret !=3D 0) { - return ret; - } - ret =3D rfc3986_parse_path_ab_empty(uri, &str); - if (ret !=3D 0) { - return ret; - } - } else if (*str =3D=3D '/') { - ret =3D rfc3986_parse_path_absolute(uri, &str); - if (ret !=3D 0) { - return ret; - } - } else if (ISA_PCHAR(str)) { - ret =3D rfc3986_parse_path_no_scheme(uri, &str); - if (ret !=3D 0) { - return ret; - } - } else { - /* path-empty is effectively empty */ - if (uri !=3D NULL) { - g_free(uri->path); - uri->path =3D NULL; - } - } - - if (*str =3D=3D '?') { - str++; - ret =3D rfc3986_parse_query(uri, &str); - if (ret !=3D 0) { - return ret; - } - } - if (*str =3D=3D '#') { - str++; - ret =3D rfc3986_parse_fragment(uri, &str); - if (ret !=3D 0) { - return ret; - } - } - if (*str !=3D 0) { - uri_clean(uri); - return 1; - } - return 0; -} - -/** - * rfc3986_parse: - * @uri: pointer to an URI structure - * @str: the string to analyze - * - * Parse an URI string and fills in the appropriate fields - * of the @uri structure - * - * scheme ":" hier-part [ "?" query ] [ "#" fragment ] - * - * Returns 0 or the error code - */ -static int rfc3986_parse(URI *uri, const char *str) -{ - int ret; - - ret =3D rfc3986_parse_scheme(uri, &str); - if (ret !=3D 0) { - return ret; - } - if (*str !=3D ':') { - return 1; - } - str++; - ret =3D rfc3986_parse_hier_part(uri, &str); - if (ret !=3D 0) { - return ret; - } - if (*str =3D=3D '?') { - str++; - ret =3D rfc3986_parse_query(uri, &str); - if (ret !=3D 0) { - return ret; - } - } - if (*str =3D=3D '#') { - str++; - ret =3D rfc3986_parse_fragment(uri, &str); - if (ret !=3D 0) { - return ret; - } - } - if (*str !=3D 0) { - uri_clean(uri); - return 1; - } - return 0; -} - -/** - * rfc3986_parse_uri_reference: - * @uri: pointer to an URI structure - * @str: the string to analyze - * - * Parse an URI reference string and fills in the appropriate fields - * of the @uri structure - * - * URI-reference =3D URI / relative-ref - * - * Returns 0 or the error code - */ -static int rfc3986_parse_uri_reference(URI *uri, const char *str) -{ - int ret; - - if (str =3D=3D NULL) { - return -1; - } - uri_clean(uri); - - /* - * Try first to parse absolute refs, then fallback to relative if - * it fails. - */ - ret =3D rfc3986_parse(uri, str); - if (ret !=3D 0) { - uri_clean(uri); - ret =3D rfc3986_parse_relative_ref(uri, str); - if (ret !=3D 0) { - uri_clean(uri); - return ret; - } - } - return 0; -} - -/** - * uri_parse: - * @str: the URI string to analyze - * - * Parse an URI based on RFC 3986 - * - * URI-reference =3D [ absoluteURI | relativeURI ] [ "#" fragment ] - * - * Returns a newly built URI or NULL in case of error - */ -URI *uri_parse(const char *str) -{ - URI *uri; - int ret; - - if (str =3D=3D NULL) { - return NULL; - } - uri =3D uri_new(); - ret =3D rfc3986_parse_uri_reference(uri, str); - if (ret) { - uri_free(uri); - return NULL; - } - return uri; -} - -/** - * uri_parse_into: - * @uri: pointer to an URI structure - * @str: the string to analyze - * - * Parse an URI reference string based on RFC 3986 and fills in the - * appropriate fields of the @uri structure - * - * URI-reference =3D URI / relative-ref - * - * Returns 0 or the error code - */ -int uri_parse_into(URI *uri, const char *str) -{ - return rfc3986_parse_uri_reference(uri, str); -} - -/** - * uri_parse_raw: - * @str: the URI string to analyze - * @raw: if 1 unescaping of URI pieces are disabled - * - * Parse an URI but allows to keep intact the original fragments. - * - * URI-reference =3D URI / relative-ref - * - * Returns a newly built URI or NULL in case of error - */ -URI *uri_parse_raw(const char *str, int raw) -{ - URI *uri; - int ret; - - if (str =3D=3D NULL) { - return NULL; - } - uri =3D uri_new(); - if (raw) { - uri->cleanup |=3D 2; - } - ret =3D uri_parse_into(uri, str); - if (ret) { - uri_free(uri); - return NULL; - } - return uri; -} - -/************************************************************************ - * * - * Generic URI structure functions * - * * - ************************************************************************/ - -/** - * uri_new: - * - * Simply creates an empty URI - * - * Returns the new structure or NULL in case of error - */ -URI *uri_new(void) -{ - return g_new0(URI, 1); -} - -/** - * realloc2n: - * - * Function to handle properly a reallocation when saving an URI - * Also imposes some limit on the length of an URI string output - */ -static char *realloc2n(char *ret, int *max) -{ - char *temp; - int tmp; - - tmp =3D *max * 2; - temp =3D g_realloc(ret, (tmp + 1)); - *max =3D tmp; - return temp; -} - -/** - * uri_to_string: - * @uri: pointer to an URI - * - * Save the URI as an escaped string - * - * Returns a new string (to be deallocated by caller) - */ -char *uri_to_string(URI *uri) -{ - char *ret =3D NULL; - char *temp; - const char *p; - int len; - int max; - - if (uri =3D=3D NULL) { - return NULL; - } - - max =3D 80; - ret =3D g_malloc(max + 1); - len =3D 0; - - if (uri->scheme !=3D NULL) { - p =3D uri->scheme; - while (*p !=3D 0) { - if (len >=3D max) { - temp =3D realloc2n(ret, &max); - ret =3D temp; - } - ret[len++] =3D *p++; - } - if (len >=3D max) { - temp =3D realloc2n(ret, &max); - ret =3D temp; - } - ret[len++] =3D ':'; - } - if (uri->opaque !=3D NULL) { - p =3D uri->opaque; - while (*p !=3D 0) { - if (len + 3 >=3D max) { - temp =3D realloc2n(ret, &max); - ret =3D temp; - } - if (IS_RESERVED(*(p)) || IS_UNRESERVED(*(p))) { - ret[len++] =3D *p++; - } else { - int val =3D *(unsigned char *)p++; - int hi =3D val / 0x10, lo =3D val % 0x10; - ret[len++] =3D '%'; - ret[len++] =3D hi + (hi > 9 ? 'A' - 10 : '0'); - ret[len++] =3D lo + (lo > 9 ? 'A' - 10 : '0'); - } - } - } else { - if (uri->server !=3D NULL) { - if (len + 3 >=3D max) { - temp =3D realloc2n(ret, &max); - ret =3D temp; - } - ret[len++] =3D '/'; - ret[len++] =3D '/'; - if (uri->user !=3D NULL) { - p =3D uri->user; - while (*p !=3D 0) { - if (len + 3 >=3D max) { - temp =3D realloc2n(ret, &max); - ret =3D temp; - } - if ((IS_UNRESERVED(*(p))) || ((*(p) =3D=3D ';')) || - ((*(p) =3D=3D ':')) || ((*(p) =3D=3D '&')) || ((*(= p) =3D=3D '=3D')) || - ((*(p) =3D=3D '+')) || ((*(p) =3D=3D '$')) || ((*(= p) =3D=3D ','))) { - ret[len++] =3D *p++; - } else { - int val =3D *(unsigned char *)p++; - int hi =3D val / 0x10, lo =3D val % 0x10; - ret[len++] =3D '%'; - ret[len++] =3D hi + (hi > 9 ? 'A' - 10 : '0'); - ret[len++] =3D lo + (lo > 9 ? 'A' - 10 : '0'); - } - } - if (len + 3 >=3D max) { - temp =3D realloc2n(ret, &max); - ret =3D temp; - } - ret[len++] =3D '@'; - } - p =3D uri->server; - while (*p !=3D 0) { - if (len >=3D max) { - temp =3D realloc2n(ret, &max); - ret =3D temp; - } - ret[len++] =3D *p++; - } - if (uri->port > 0) { - if (len + 10 >=3D max) { - temp =3D realloc2n(ret, &max); - ret =3D temp; - } - len +=3D snprintf(&ret[len], max - len, ":%d", uri->port); - } - } else if (uri->authority !=3D NULL) { - if (len + 3 >=3D max) { - temp =3D realloc2n(ret, &max); - ret =3D temp; - } - ret[len++] =3D '/'; - ret[len++] =3D '/'; - p =3D uri->authority; - while (*p !=3D 0) { - if (len + 3 >=3D max) { - temp =3D realloc2n(ret, &max); - ret =3D temp; - } - if ((IS_UNRESERVED(*(p))) || ((*(p) =3D=3D '$')) || - ((*(p) =3D=3D ',')) || ((*(p) =3D=3D ';')) || ((*(p) = =3D=3D ':')) || - ((*(p) =3D=3D '@')) || ((*(p) =3D=3D '&')) || ((*(p) = =3D=3D '=3D')) || - ((*(p) =3D=3D '+'))) { - ret[len++] =3D *p++; - } else { - int val =3D *(unsigned char *)p++; - int hi =3D val / 0x10, lo =3D val % 0x10; - ret[len++] =3D '%'; - ret[len++] =3D hi + (hi > 9 ? 'A' - 10 : '0'); - ret[len++] =3D lo + (lo > 9 ? 'A' - 10 : '0'); - } - } - } else if (uri->scheme !=3D NULL) { - if (len + 3 >=3D max) { - temp =3D realloc2n(ret, &max); - ret =3D temp; - } - ret[len++] =3D '/'; - ret[len++] =3D '/'; - } - if (uri->path !=3D NULL) { - p =3D uri->path; - /* - * the colon in file:///d: should not be escaped or - * Windows accesses fail later. - */ - if ((uri->scheme !=3D NULL) && (p[0] =3D=3D '/') && - (((p[1] >=3D 'a') && (p[1] <=3D 'z')) || - ((p[1] >=3D 'A') && (p[1] <=3D 'Z'))) && - (p[2] =3D=3D ':') && (!strcmp(uri->scheme, "file"))) { - if (len + 3 >=3D max) { - temp =3D realloc2n(ret, &max); - ret =3D temp; - } - ret[len++] =3D *p++; - ret[len++] =3D *p++; - ret[len++] =3D *p++; - } - while (*p !=3D 0) { - if (len + 3 >=3D max) { - temp =3D realloc2n(ret, &max); - ret =3D temp; - } - if ((IS_UNRESERVED(*(p))) || ((*(p) =3D=3D '/')) || - ((*(p) =3D=3D ';')) || ((*(p) =3D=3D '@')) || ((*(p) = =3D=3D '&')) || - ((*(p) =3D=3D '=3D')) || ((*(p) =3D=3D '+')) || ((*(p)= =3D=3D '$')) || - ((*(p) =3D=3D ','))) { - ret[len++] =3D *p++; - } else { - int val =3D *(unsigned char *)p++; - int hi =3D val / 0x10, lo =3D val % 0x10; - ret[len++] =3D '%'; - ret[len++] =3D hi + (hi > 9 ? 'A' - 10 : '0'); - ret[len++] =3D lo + (lo > 9 ? 'A' - 10 : '0'); - } - } - } - if (uri->query !=3D NULL) { - if (len + 1 >=3D max) { - temp =3D realloc2n(ret, &max); - ret =3D temp; - } - ret[len++] =3D '?'; - p =3D uri->query; - while (*p !=3D 0) { - if (len + 1 >=3D max) { - temp =3D realloc2n(ret, &max); - ret =3D temp; - } - ret[len++] =3D *p++; - } - } - } - if (uri->fragment !=3D NULL) { - if (len + 3 >=3D max) { - temp =3D realloc2n(ret, &max); - ret =3D temp; - } - ret[len++] =3D '#'; - p =3D uri->fragment; - while (*p !=3D 0) { - if (len + 3 >=3D max) { - temp =3D realloc2n(ret, &max); - ret =3D temp; - } - if ((IS_UNRESERVED(*(p))) || (IS_RESERVED(*(p)))) { - ret[len++] =3D *p++; - } else { - int val =3D *(unsigned char *)p++; - int hi =3D val / 0x10, lo =3D val % 0x10; - ret[len++] =3D '%'; - ret[len++] =3D hi + (hi > 9 ? 'A' - 10 : '0'); - ret[len++] =3D lo + (lo > 9 ? 'A' - 10 : '0'); - } - } - } - if (len >=3D max) { - temp =3D realloc2n(ret, &max); - ret =3D temp; - } - ret[len] =3D 0; - return ret; -} - -/** - * uri_clean: - * @uri: pointer to an URI - * - * Make sure the URI struct is free of content - */ -static void uri_clean(URI *uri) -{ - if (uri =3D=3D NULL) { - return; - } - - g_free(uri->scheme); - uri->scheme =3D NULL; - g_free(uri->server); - uri->server =3D NULL; - g_free(uri->user); - uri->user =3D NULL; - g_free(uri->path); - uri->path =3D NULL; - g_free(uri->fragment); - uri->fragment =3D NULL; - g_free(uri->opaque); - uri->opaque =3D NULL; - g_free(uri->authority); - uri->authority =3D NULL; - g_free(uri->query); - uri->query =3D NULL; -} - -/** - * uri_free: - * @uri: pointer to an URI, NULL is ignored - * - * Free up the URI struct - */ -void uri_free(URI *uri) -{ - uri_clean(uri); - g_free(uri); -} - -/************************************************************************ - * * - * Public functions * - * * - ************************************************************************/ - -/* - * Utility functions to help parse and assemble query strings. - */ - -struct QueryParams *query_params_new(int init_alloc) -{ - struct QueryParams *ps; - - if (init_alloc <=3D 0) { - init_alloc =3D 1; - } - - ps =3D g_new(QueryParams, 1); - ps->n =3D 0; - ps->alloc =3D init_alloc; - ps->p =3D g_new(QueryParam, ps->alloc); - - return ps; -} - -/* Ensure there is space to store at least one more parameter - * at the end of the set. - */ -static int query_params_append(struct QueryParams *ps, const char *name, - const char *value) -{ - if (ps->n >=3D ps->alloc) { - ps->p =3D g_renew(QueryParam, ps->p, ps->alloc * 2); - ps->alloc *=3D 2; - } - - ps->p[ps->n].name =3D g_strdup(name); - ps->p[ps->n].value =3D g_strdup(value); - ps->p[ps->n].ignore =3D 0; - ps->n++; - - return 0; -} - -void query_params_free(struct QueryParams *ps) -{ - int i; - - for (i =3D 0; i < ps->n; ++i) { - g_free(ps->p[i].name); - g_free(ps->p[i].value); - } - g_free(ps->p); - g_free(ps); -} - -struct QueryParams *query_params_parse(const char *query) -{ - struct QueryParams *ps; - const char *end, *eq; - - ps =3D query_params_new(0); - if (!query || query[0] =3D=3D '\0') { - return ps; - } - - while (*query) { - char *name =3D NULL, *value =3D NULL; - - /* Find the next separator, or end of the string. */ - end =3D strchr(query, '&'); - if (!end) { - end =3D qemu_strchrnul(query, ';'); - } - - /* Find the first '=3D' character between here and end. */ - eq =3D strchr(query, '=3D'); - if (eq && eq >=3D end) { - eq =3D NULL; - } - - /* Empty section (eg. "&&"). */ - if (end =3D=3D query) { - goto next; - } - - /* If there is no '=3D' character, then we have just "name" - * and consistent with CGI.pm we assume value is "". - */ - else if (!eq) { - name =3D g_uri_unescape_segment(query, end, NULL); - value =3D NULL; - } - /* Or if we have "name=3D" here (works around annoying - * problem when calling uri_string_unescape with len =3D 0). - */ - else if (eq + 1 =3D=3D end) { - name =3D g_uri_unescape_segment(query, eq, NULL); - value =3D g_new0(char, 1); - } - /* If the '=3D' character is at the beginning then we have - * "=3Dvalue" and consistent with CGI.pm we _ignore_ this. - */ - else if (query =3D=3D eq) { - goto next; - } - - /* Otherwise it's "name=3Dvalue". */ - else { - name =3D g_uri_unescape_segment(query, eq, NULL); - value =3D g_uri_unescape_segment(eq + 1, end, NULL); - } - - /* Append to the parameter set. */ - query_params_append(ps, name, value); - g_free(name); - g_free(value); - - next: - query =3D end; - if (*query) { - query++; /* skip '&' separator */ - } - } - - return ps; -} diff --git a/util/meson.build b/util/meson.build index 2ad57b10ba..72b505df11 100644 --- a/util/meson.build +++ b/util/meson.build @@ -93,7 +93,7 @@ if have_block util_ss.add(files('hbitmap.c')) util_ss.add(files('hexdump.c')) util_ss.add(files('iova-tree.c')) - util_ss.add(files('iov.c', 'uri.c')) + util_ss.add(files('iov.c')) util_ss.add(files('nvdimm-utils.c')) util_ss.add(files('block-helpers.c')) util_ss.add(files('qemu-coroutine-sleep.c')) --=20 2.45.0