res_http_media_cache.c: Parse media URLs to find extensions.

Use cURL's URL parsing API, falling back to the urlparser library, to
parse playback URLs in order to find their file extensions.

For backwards compatibility, we first look at the full URL, then at
any Content-Type header, and finally at just the path portion of the
URL.

ASTERISK-27871 #close

Change-Id: I16d0682f6d794be96539261b3e48f237909139cb
This commit is contained in:
Sean Bright
2021-07-02 11:15:05 -04:00
committed by Joshua Colp
parent 785e4afc20
commit d568326807
3 changed files with 276 additions and 51 deletions

View File

@@ -41,6 +41,14 @@
#include "asterisk/bucket.h"
#include "asterisk/test.h"
#undef INCLUDE_URI_PARSING_TESTS
#if defined(HAVE_CURL)
# include <curl/curl.h>
#endif
#if (defined(HAVE_CURL) && LIBCURL_VERSION_NUM >= 0x073e00) || defined(HAVE_URIPARSER)
# define INCLUDE_URI_PARSING_TESTS 1
#endif
#define CATEGORY "/res/http_media_cache/"
#define TEST_URI "test_media_cache"
@@ -57,6 +65,7 @@ struct test_options {
struct timeval expires;
const char *status_text;
const char *etag;
const char *content_type;
};
static struct test_options options;
@@ -125,6 +134,10 @@ static int http_callback(struct ast_tcptls_session_instance *ser, const struct a
}
}
if (!ast_strlen_zero(options.content_type)) {
ast_str_append(&http_header, 0, "Content-Type: %s\r\n", options.content_type);
}
if (options.cache_control.maxage) {
SET_OR_APPEND_CACHE_CONTROL(cache_control);
ast_str_append(&cache_control, 0, "max-age=%d", options.cache_control.maxage);
@@ -220,6 +233,77 @@ static void bucket_file_cleanup(void *obj)
}
}
AST_TEST_DEFINE(retrieve_content_type)
{
RAII_VAR(struct ast_bucket_file *, bucket_file, NULL, bucket_file_cleanup);
char uri[1024];
switch (cmd) {
case TEST_INIT:
info->name = __func__;
info->category = CATEGORY;
info->summary = "Test retrieval of a resource with a Content-Type header";
info->description =
"This test covers retrieval of a resource whose URL does not end with\n"
"a parseable extension and whose response includes a Content-Type\n"
"header that we recognize.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
options.send_file = 1;
options.status_code = 200;
options.status_text = "OK";
options.content_type = "audio/wav";
snprintf(uri, sizeof(uri), "%s/%s", server_uri, "foo.wav?account_id=1234");
bucket_file = ast_bucket_file_retrieve(uri);
ast_test_validate(test, bucket_file != NULL);
ast_test_validate(test, !strcmp(uri, ast_sorcery_object_get_id(bucket_file)));
ast_test_validate(test, !ast_strlen_zero(bucket_file->path));
VALIDATE_STR_METADATA(test, bucket_file, "ext", ".wav");
return AST_TEST_PASS;
}
#ifdef INCLUDE_URI_PARSING_TESTS
AST_TEST_DEFINE(retrieve_parsed_uri)
{
RAII_VAR(struct ast_bucket_file *, bucket_file, NULL, bucket_file_cleanup);
char uri[1024];
switch (cmd) {
case TEST_INIT:
info->name = __func__;
info->category = CATEGORY;
info->summary = "Test retrieval of a resource with a complex URI";
info->description =
"This test covers retrieval of a resource whose URL does not end with\n"
"a parseable extension, but the path portion of the URL does end with\n"
"parseable extension.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
options.send_file = 1;
options.status_code = 200;
options.status_text = "OK";
snprintf(uri, sizeof(uri), "%s/%s", server_uri, "foo.wav?account_id=1234");
bucket_file = ast_bucket_file_retrieve(uri);
ast_test_validate(test, bucket_file != NULL);
ast_test_validate(test, !strcmp(uri, ast_sorcery_object_get_id(bucket_file)));
ast_test_validate(test, !ast_strlen_zero(bucket_file->path));
VALIDATE_STR_METADATA(test, bucket_file, "ext", ".wav");
return AST_TEST_PASS;
}
#endif
AST_TEST_DEFINE(retrieve_cache_control_directives)
{
RAII_VAR(struct ast_bucket_file *, bucket_file, NULL, bucket_file_cleanup);
@@ -670,6 +754,11 @@ static int load_module(void)
AST_TEST_REGISTER(retrieve_etag_expired);
AST_TEST_REGISTER(retrieve_cache_control_age);
AST_TEST_REGISTER(retrieve_cache_control_directives);
AST_TEST_REGISTER(retrieve_content_type);
#ifdef INCLUDE_URI_PARSING_TESTS
AST_TEST_REGISTER(retrieve_parsed_uri);
#endif
ast_test_register_init(CATEGORY, pre_test_cb);
@@ -688,6 +777,11 @@ static int unload_module(void)
AST_TEST_UNREGISTER(retrieve_etag_expired);
AST_TEST_UNREGISTER(retrieve_cache_control_age);
AST_TEST_UNREGISTER(retrieve_cache_control_directives);
AST_TEST_REGISTER(retrieve_content_type);
#ifdef INCLUDE_URI_PARSING_TESTS
AST_TEST_REGISTER(retrieve_parsed_uri);
#endif
return 0;
}