From 1b70a706ff7744eb23f167fa3114e83f5c54c426 Mon Sep 17 00:00:00 2001 From: Joshua Gigg Date: Thu, 27 Aug 2020 13:27:40 +0100 Subject: [PATCH 1/5] [mod_httapi] Update cache file if the extension changes If a HTTP request changes the extension (by using Content-Type header) of the existing cached file, remove the existing cached file, and allow the new one to save with the correct extension. --- src/mod/applications/mod_httapi/mod_httapi.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/mod/applications/mod_httapi/mod_httapi.c b/src/mod/applications/mod_httapi/mod_httapi.c index abc9bb37d2..985d85aca9 100644 --- a/src/mod/applications/mod_httapi/mod_httapi.c +++ b/src/mod/applications/mod_httapi/mod_httapi.c @@ -2813,13 +2813,18 @@ static switch_status_t locate_url_file(http_file_context_t *context, const char } } - if (zstr(ext) && headers && (ct = switch_event_get_header(headers, "content-type"))) { + if (headers && (ct = switch_event_get_header(headers, "content-type"))) { newext = switch_core_mime_type2ext(ct); } - if (newext) { + if (newext && (zstr(ext) || strcmp(ext, newext) != 0)) { + /* + * HTTP Request has returned the file with a different extension + * Update the cache_file path and delete the original file + */ ext = newext; - context->cache_file = switch_core_sprintf(context->pool, "%s.%s", context->cache_file, newext); + unlink(context->cache_file); + context->cache_file = switch_core_sprintf(context->pool, "%s.%s", context->cache_file_base, newext); } From 59eaa4c221b8859d839017807c90c04436de7796 Mon Sep 17 00:00:00 2001 From: Joshua Gigg Date: Fri, 4 Sep 2020 11:29:02 +0100 Subject: [PATCH 2/5] Don't remove the old file if the extension changes Avoids any chance of a race condition removing a file that is about to be played --- src/mod/applications/mod_httapi/mod_httapi.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/mod/applications/mod_httapi/mod_httapi.c b/src/mod/applications/mod_httapi/mod_httapi.c index 985d85aca9..28c892d206 100644 --- a/src/mod/applications/mod_httapi/mod_httapi.c +++ b/src/mod/applications/mod_httapi/mod_httapi.c @@ -2823,7 +2823,6 @@ static switch_status_t locate_url_file(http_file_context_t *context, const char * Update the cache_file path and delete the original file */ ext = newext; - unlink(context->cache_file); context->cache_file = switch_core_sprintf(context->pool, "%s.%s", context->cache_file_base, newext); } From c475f5d8ab0c95d0d61ce05ae4aa75f4ee8286fb Mon Sep 17 00:00:00 2001 From: Joshua Gigg Date: Thu, 10 Sep 2020 20:47:00 +0100 Subject: [PATCH 3/5] Save to a temp file first, then rename This allows the GET request to return a different mime type (and extension) to what was previously stored --- src/mod/applications/mod_httapi/mod_httapi.c | 26 +++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/mod/applications/mod_httapi/mod_httapi.c b/src/mod/applications/mod_httapi/mod_httapi.c index 28c892d206..c424597495 100644 --- a/src/mod/applications/mod_httapi/mod_httapi.c +++ b/src/mod/applications/mod_httapi/mod_httapi.c @@ -2781,8 +2781,12 @@ static switch_status_t locate_url_file(http_file_context_t *context, const char switch_status_t status = SWITCH_STATUS_FALSE; time_t now = switch_epoch_time_now(NULL); char *metadata; + const char *cache_file_temp; const char *ext = NULL; const char *err_msg = NULL; + const char *ct = NULL; + const char *newext = NULL; + load_cache_data(context, url); @@ -2852,10 +2856,30 @@ static switch_status_t locate_url_file(http_file_context_t *context, const char } - if ((status = fetch_cache_data(context, url, &headers, context->cache_file, &err_msg)) != SWITCH_STATUS_SUCCESS) { + /* + * Save to a temp file first, then rename + * Just in case the extension changes + */ + cache_file_temp = switch_core_sprintf(context->pool, "%s.tmp", context->cache_file_base); + if ((status = fetch_cache_data(context, url, &headers, cache_file_temp, &err_msg)) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error fetching file at URL \"%s\" (%s)\n", url, err_msg ? err_msg : ""); goto end; } + + if (headers && (ct = switch_event_get_header(headers, "content-type"))) { + newext = switch_core_mime_type2ext(ct); + } + + if (newext && (zstr(ext) || strcmp(ext, newext) != 0)) { + /* + * HTTP Request has returned the file with a different extension + * Update the cache_file path for when the rename happens + */ + ext = newext; + context->cache_file = switch_core_sprintf(context->pool, "%s.%s", context->cache_file_base, newext); + } + + rename(cache_file_temp, context->cache_file); metadata = switch_core_sprintf(context->pool, "%s:%s:%s:%s:%s", From 32e7e516527f718a1d1da0260241ca7f945d026c Mon Sep 17 00:00:00 2001 From: Joshua Gigg Date: Thu, 10 Sep 2020 20:47:56 +0100 Subject: [PATCH 4/5] Fix duplicate variable declaration --- src/mod/applications/mod_httapi/mod_httapi.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/mod/applications/mod_httapi/mod_httapi.c b/src/mod/applications/mod_httapi/mod_httapi.c index c424597495..56d9818dd4 100644 --- a/src/mod/applications/mod_httapi/mod_httapi.c +++ b/src/mod/applications/mod_httapi/mod_httapi.c @@ -2803,9 +2803,6 @@ static switch_status_t locate_url_file(http_file_context_t *context, const char } if (!context->url_params || !switch_true(switch_event_get_header(context->url_params, "nohead"))) { - const char *ct = NULL; - const char *newext = NULL; - if ((status = fetch_cache_data(context, url, &headers, NULL, NULL)) != SWITCH_STATUS_SUCCESS) { if (status == SWITCH_STATUS_NOTFOUND) { unreachable = 2; From 1c749fbed7a3a0e19935c4d506475a73feeccc02 Mon Sep 17 00:00:00 2001 From: Joshua Gigg Date: Thu, 10 Sep 2020 20:50:16 +0100 Subject: [PATCH 5/5] Fix comment --- src/mod/applications/mod_httapi/mod_httapi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mod/applications/mod_httapi/mod_httapi.c b/src/mod/applications/mod_httapi/mod_httapi.c index 56d9818dd4..4b604904ad 100644 --- a/src/mod/applications/mod_httapi/mod_httapi.c +++ b/src/mod/applications/mod_httapi/mod_httapi.c @@ -2821,7 +2821,7 @@ static switch_status_t locate_url_file(http_file_context_t *context, const char if (newext && (zstr(ext) || strcmp(ext, newext) != 0)) { /* * HTTP Request has returned the file with a different extension - * Update the cache_file path and delete the original file + * Update the cache_file path */ ext = newext; context->cache_file = switch_core_sprintf(context->pool, "%s.%s", context->cache_file_base, newext);