From f5388e444c94fed707acef983555bfe6ddd069da Mon Sep 17 00:00:00 2001 From: Taner Mansur Date: Sat, 13 Feb 2021 19:32:43 +0300 Subject: [PATCH] [mod_http_cache] Generic HTTP API Authorization header support (#1048) [mod_http_cache] Generic HTTP API Authorization header support --- src/mod/applications/mod_http_cache/common.h | 4 +- .../mod_http_cache/mod_http_cache.c | 55 +++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/src/mod/applications/mod_http_cache/common.h b/src/mod/applications/mod_http_cache/common.h index 9092f2a13b..f2406983c5 100644 --- a/src/mod/applications/mod_http_cache/common.h +++ b/src/mod/applications/mod_http_cache/common.h @@ -45,7 +45,9 @@ struct http_profile { char *region; // AWS region. Used by AWS S3 switch_time_t expires; // Expiration time in seconds for URL signature. Default is 604800 seconds. Used by AWS S3 switch_size_t bytes_per_block; - + int header_count; + char** header_names; + char** header_values; // function to be called to add the profile specific headers to the GET/PUT requests switch_curl_slist_t *(*append_headers_ptr)(struct http_profile *profile, switch_curl_slist_t *headers, const char *verb, unsigned int content_length, const char *content_type, const char *url, const unsigned int block_num, char **query_string); diff --git a/src/mod/applications/mod_http_cache/mod_http_cache.c b/src/mod/applications/mod_http_cache/mod_http_cache.c index d8ea87578e..dcf1d7bff5 100644 --- a/src/mod/applications/mod_http_cache/mod_http_cache.c +++ b/src/mod/applications/mod_http_cache/mod_http_cache.c @@ -1532,6 +1532,51 @@ static void *SWITCH_THREAD_FUNC prefetch_thread(switch_thread_t *thread, void *o return NULL; } +static switch_curl_slist_t *default_append_headers(http_profile_t *profile, switch_curl_slist_t *headers, + const char *verb, unsigned int content_length, const char *content_type, const char *url, const unsigned int block_num, char **query_string) +{ + char header[1024]; + int i; + + for (i = 0; i < profile->header_count; i++) { + switch_snprintf(header, sizeof(header), "%s: %s", profile->header_names[i], profile->header_values[i]); + + headers = switch_curl_slist_append(headers, header); + } + + return headers; +} + +static switch_status_t default_config_profile(switch_xml_t xml, http_profile_t *profile, switch_memory_pool_t *pool) +{ + int i, header_count = 0; + switch_xml_t header; + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Configuring default profile\n"); + + for (header = switch_xml_child(xml, "header"); header; header = header->next) { + header_count++; + } + + profile->header_count = header_count; + profile->header_names = switch_core_alloc(pool, sizeof(char*) * header_count); + profile->header_values = switch_core_alloc(pool, sizeof(char*) * header_count); + + for (i = 0, header = switch_xml_child(xml, "header"); header; i++, header = header->next) { + char *header_name = (char *) switch_xml_attr_soft(header, "name"); + char *header_value = (char *) switch_xml_txt(header); + + profile->header_names[i] = switch_core_strdup(pool, header_name); + profile->header_values[i] = switch_core_strdup(pool, header_value); + } + + profile->append_headers_ptr = default_append_headers; + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Configured default profile\n"); + + return SWITCH_STATUS_SUCCESS; +} + /** * Configure the module * @param cache to configure @@ -1632,6 +1677,9 @@ static switch_status_t do_config(url_cache_t *cache) profile_obj->secret_access_key = NULL; profile_obj->base_domain = NULL; profile_obj->bytes_per_block = 0; + profile_obj->header_count = 0; + profile_obj->header_names = NULL; + profile_obj->header_values = NULL; profile_obj->append_headers_ptr = NULL; profile_obj->finalise_put_ptr = NULL; @@ -1646,6 +1694,13 @@ static switch_status_t do_config(url_cache_t *cache) if (azure_blob_config_profile(profile_xml, profile_obj) == SWITCH_STATUS_FALSE) { continue; } + } else { + profile_xml = switch_xml_child(profile, "default"); + if (profile_xml) { + if (default_config_profile(profile_xml, profile_obj, cache->pool) == SWITCH_STATUS_FALSE) { + continue; + } + } } }