From 02f54e2751802950c5499dcf83ecdefe038aaad5 Mon Sep 17 00:00:00 2001 From: Sean Bright <sean.bright@gmail.com> Date: Fri, 10 Sep 2021 10:40:00 -0400 Subject: [PATCH] res_http_media_cache.c: Compare unaltered MIME types. Rather than stripping parameters from Content-Type headers before comparison, first try to compare the whole string. If no match is found, strip the parameters and try that way. ASTERISK-29275 #close Change-Id: I2963c8ecbb3a9605b78b6421c415108d77a66a0f --- res/res_http_media_cache.c | 46 +++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/res/res_http_media_cache.c b/res/res_http_media_cache.c index 9f560c7c81..c0363b1702 100644 --- a/res/res_http_media_cache.c +++ b/res/res_http_media_cache.c @@ -176,8 +176,10 @@ static char *file_extension_from_string(const char *str, char *buffer, size_t ca * \brief Normalize the value of a Content-Type header * * This will trim off any optional parameters after the type/subtype. + * + * \return 0 if no normalization occurred, otherwise true (non-zero) */ -static void normalize_content_type_header(char *content_type) +static int normalize_content_type_header(char *content_type) { char *params = strchr(content_type, ';'); @@ -186,7 +188,27 @@ static void normalize_content_type_header(char *content_type) while (params > content_type && (*params == ' ' || *params == '\t')) { *params-- = 0; } + return 1; + } + + return 0; +} + +static int derive_extension_from_mime_type(const char *mime_type, char *buffer, size_t capacity) +{ + int res = 0; + + /* Compare the provided Content-Type directly, parameters and all */ + res = ast_get_extension_for_mime_type(mime_type, buffer, sizeof(buffer)); + if (!res) { + char *m = ast_strdupa(mime_type); + /* Strip MIME type parameters and then check */ + if (normalize_content_type_header(m)) { + res = ast_get_extension_for_mime_type(m, buffer, sizeof(buffer)); + } } + + return res; } static char *file_extension_from_content_type(struct ast_bucket_file *bucket_file, char *buffer, size_t capacity) @@ -198,28 +220,20 @@ static char *file_extension_from_content_type(struct ast_bucket_file *bucket_fil * corresponding to the mime-type and use that to rename the file */ struct ast_bucket_metadata *header; - char *mime_type; header = ast_bucket_file_metadata_get(bucket_file, "content-type"); if (!header) { return NULL; } - mime_type = ast_strdup(header->value); - if (mime_type) { - normalize_content_type_header(mime_type); - if (!ast_strlen_zero(mime_type)) { - if (ast_get_extension_for_mime_type(mime_type, buffer, sizeof(buffer))) { - ast_debug(3, "Derived extension '%s' from MIME type %s\n", - buffer, - mime_type); - ast_free(mime_type); - ao2_ref(header, -1); - return buffer; - } - } + if (derive_extension_from_mime_type(header->value, buffer, capacity)) { + ast_debug(3, "Derived extension '%s' from MIME type %s\n", + buffer, + header->value); + ao2_ref(header, -1); + return buffer; } - ast_free(mime_type); + ao2_ref(header, -1); return NULL; -- GitLab