]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
[mod_http_cache] Generic HTTP API Authorization header support (#1048)
authorTaner Mansur <tmnsur@gmail.com>
Sat, 13 Feb 2021 16:32:43 +0000 (19:32 +0300)
committerGitHub <noreply@github.com>
Sat, 13 Feb 2021 16:32:43 +0000 (11:32 -0500)
[mod_http_cache] Generic HTTP API Authorization header support

src/mod/applications/mod_http_cache/common.h
src/mod/applications/mod_http_cache/mod_http_cache.c

index 9092f2a13bad04e20883d75a823e7a60205bb682..f2406983c536979eed57433d3484638dd1d3c492 100644 (file)
@@ -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);
index d8ea87578ed72c5c2afbc6752e28b20d6d8b554b..dcf1d7bff5902bed92b635cd32a96ee5bf52c49c 100644 (file)
@@ -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;
+                                                       }
+                                               }
                                        }
                                }