From 6ef6de6c5c13a8614d334e9e49233326b18c8025 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Thu, 16 Jul 2020 00:19:48 +0200 Subject: [PATCH] fix Accept patterns with semicolons --- src/mhd/mhd_legal.c | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/src/mhd/mhd_legal.c b/src/mhd/mhd_legal.c index 19a21475f..f46f97420 100644 --- a/src/mhd/mhd_legal.c +++ b/src/mhd/mhd_legal.c @@ -1,6 +1,6 @@ /* This file is part of TALER - Copyright (C) 2019 Taler Systems SA + Copyright (C) 2019, 2020 Taler Systems SA TALER is free software; you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software @@ -80,7 +80,8 @@ struct TALER_MHD_Legal /** * Check if @a mime matches the @a accept_pattern. * - * @param accept_pattern a mime pattern like text/plain or image/STAR + * @param accept_pattern a mime pattern like "text/plain" + * or "image/STAR" * @param mime the mime type to match * @return true if @a mime matches the @a accept_pattern */ @@ -107,6 +108,38 @@ mime_matches (const char *accept_pattern, } +/** + * Check if @a mime matches the @a accept_pattern. For this function, the @a + * accept_pattern may include multiple values separated by ";". + * + * @param accept_pattern a mime pattern like "text/plain" + * or "image/STAR" or "text/plain; text/xml" + * @param mime the mime type to match + * @return true if @a mime matches the @a accept_pattern + */ +static bool +xmime_matches (const char *accept_pattern, + const char *mime) +{ + char *ap = GNUNET_strdup (accept_pattern); + char *sptr; + + for (const char *tok = strtok_r (ap, ";", &sptr); + NULL != tok; + tok = strtok_r (NULL, ";", &sptr)) + { + if (mime_matches (tok, + mime)) + { + GNUNET_free (ap); + return true; + } + } + GNUNET_free (ap); + return false; +} + + /** * Check if @a lang matches the @a language_pattern, and if so with * which preference. @@ -218,12 +251,12 @@ TALER_MHD_reply_legal (struct MHD_Connection *conn, struct Terms *p = &legal->terms[i]; if ( (NULL == t) || - (mime_matches (mime, - p->mime_type)) ) + (xmime_matches (mime, + p->mime_type)) ) { if ( (NULL == t) || - (! mime_matches (mime, - t->mime_type)) || + (! xmime_matches (mime, + t->mime_type)) || (language_matches (lang, p->language) > language_matches (lang,