Asterisk - The Open Source Telephony Project GIT-master-754dea3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
res_http_media_cache.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2015, Matt Jordan
5 *
6 * Matt Jordan <mjordan@digium.com>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19/*!
20 * \file
21 * \brief
22 *
23 * \author \verbatim Matt Jordan <mjordan@digium.com> \endverbatim
24 *
25 * HTTP backend for the core media cache
26 */
27
28/*** MODULEINFO
29 <depend>curl</depend>
30 <depend>res_curl</depend>
31 <support_level>core</support_level>
32 ***/
33
34/*** DOCUMENTATION
35 <configInfo name="res_http_media_cache" language="en_US">
36 <synopsis>HTTP media cache</synopsis>
37 <configFile name="res_http_media_cache.conf">
38 <configObject name="general">
39 <since>
40 <version>18.18.0</version>
41 <version>20.3.0</version>
42 </since>
43 <synopsis>General configuration</synopsis>
44 <configOption name="timeout_secs" default="180">
45 <since>
46 <version>18.18.0</version>
47 <version>20.3.0</version>
48 </since>
49 <synopsis>The maximum time the transfer is allowed to complete in seconds. See https://curl.se/libcurl/c/CURLOPT_TIMEOUT.html for details.</synopsis>
50 </configOption>
51 <configOption name="user_agent">
52 <since>
53 <version>18.18.0</version>
54 <version>20.3.0</version>
55 </since>
56 <synopsis>The HTTP User-Agent to use for requests. See https://curl.se/libcurl/c/CURLOPT_USERAGENT.html for details.</synopsis>
57 </configOption>
58 <configOption name="follow_location" default="1">
59 <since>
60 <version>18.18.0</version>
61 <version>20.3.0</version>
62 </since>
63 <synopsis>Follow HTTP 3xx redirects on requests. See https://curl.se/libcurl/c/CURLOPT_FOLLOWLOCATION.html for details.</synopsis>
64 </configOption>
65 <configOption name="max_redirects" default="8">
66 <since>
67 <version>18.18.0</version>
68 <version>20.3.0</version>
69 </since>
70 <synopsis>The maximum number of redirects to follow. See https://curl.se/libcurl/c/CURLOPT_MAXREDIRS.html for details.</synopsis>
71 </configOption>
72 <configOption name="proxy">
73 <since>
74 <version>18.18.0</version>
75 <version>20.3.0</version>
76 </since>
77 <synopsis>The proxy to use for requests. See https://curl.se/libcurl/c/CURLOPT_PROXY.html for details.</synopsis>
78 </configOption>
79 <configOption name="protocols">
80 <since>
81 <version>18.18.0</version>
82 <version>20.3.0</version>
83 </since>
84 <synopsis>The comma separated list of allowed protocols for the request. Available with cURL 7.85.0 or later. See https://curl.se/libcurl/c/CURLOPT_PROTOCOLS_STR.html for details.</synopsis>
85 </configOption>
86 <configOption name="redirect_protocols">
87 <since>
88 <version>18.18.0</version>
89 <version>20.3.0</version>
90 </since>
91 <synopsis>The comma separated list of allowed protocols for redirects. Available with cURL 7.85.0 or later. See https://curl.se/libcurl/c/CURLOPT_REDIR_PROTOCOLS_STR.html for details.</synopsis>
92 </configOption>
93 <configOption name="dns_cache_timeout_secs" default="60">
94 <since>
95 <version>18.18.0</version>
96 <version>20.3.0</version>
97 </since>
98 <synopsis>The life-time for DNS cache entries. See https://curl.se/libcurl/c/CURLOPT_DNS_CACHE_TIMEOUT.html for details.</synopsis>
99 </configOption>
100 </configObject>
101 </configFile>
102 </configInfo>
103***/
104
105#include "asterisk.h"
106
107#include <curl/curl.h>
108
109#include "asterisk/file.h"
110#include "asterisk/module.h"
111#include "asterisk/bucket.h"
112#include "asterisk/sorcery.h"
114#include "asterisk/uri.h"
115
116#define MAX_HEADER_LENGTH 1023
117
118#ifdef CURL_AT_LEAST_VERSION
119#if CURL_AT_LEAST_VERSION(7, 85, 0)
120#define AST_CURL_HAS_PROTOCOLS_STR 1
121#endif
122#endif
123
124static int http_media_cache_config_pre_apply(void);
125
126/*! \brief General configuration options for http media cache. */
128 /*! \brief Request timeout to use */
130
131 /*! \brief Follow 3xx redirects automatically. */
133
134 /*! \brief Number of redirects to follow for one request. */
136
137 /*! \brief Life-time of CURL DNS cache entries. */
139
141 AST_STRING_FIELD(curl_useragent); /*! \brief User-agent to use for requests. */
142 AST_STRING_FIELD(curl_proxy); /*! \brief Proxy to use for requests. None by default. */
143 AST_STRING_FIELD(curl_protocols); /*! \brief Allowed protocols to use for requests. All by default. */
144 AST_STRING_FIELD(curl_redir_protocols); /*! \brief Allowed protocols to use on redirect. All by default. */
145 );
146};
147
148/*! \brief All configuration options for http media cache. */
149struct conf {
150 /*! The general section configuration options. */
152};
153
154/*! \brief Locking container for safe configuration access. */
156
157/*! \brief Mapping of the http media cache conf struct's general to the general context in the config file. */
158static struct aco_type general_option = {
159 .type = ACO_GLOBAL,
160 .name = "general",
161 .item_offset = offsetof(struct conf, general),
162 .category = "general",
163 .category_match = ACO_WHITELIST_EXACT,
164};
165
167
168/*! \brief Disposes of the http media cache conf object */
169static void conf_destructor(void *obj)
170{
171 struct conf *cfg = obj;
173 ao2_cleanup(cfg->general);
174}
175
176/*! \brief Creates the http media cache conf object. */
177static void *conf_alloc(void)
178{
179 struct conf *cfg;
180
181 if (!(cfg = ao2_alloc(sizeof(*cfg), conf_destructor))) {
182 return NULL;
183 }
184
185 if (!(cfg->general = ao2_alloc(sizeof(*cfg->general), NULL))) {
186 ao2_ref(cfg, -1);
187 return NULL;
188 }
189
190 if (ast_string_field_init(cfg->general, 256)) {
191 ao2_ref(cfg, -1);
192 return NULL;
193 }
194
195 return cfg;
196}
197
198/*! \brief The conf file that's processed for the module. */
199static struct aco_file conf_file = {
200 /*! The config file name. */
201 .filename = "res_http_media_cache.conf",
202 /*! The mapping object types to be processed. */
203 .types = ACO_TYPES(&general_option),
204};
205
207 .pre_apply_config = http_media_cache_config_pre_apply,
208 .files = ACO_FILES(&conf_file));
209
210/*!
211 * \brief Pre-apply callback for the config framework.
212 *
213 * This validates that used options match the ones supported by CURL.
214 */
216{
217#ifndef AST_CURL_HAS_PROTOCOLS_STR
218 struct conf *cfg = aco_pending_config(&cfg_info);
219
221 ast_log(AST_LOG_ERROR, "'protocols' not supported by linked CURL library. Please recompile against newer CURL.\n");
222 return -1;
223 }
224
226 ast_log(AST_LOG_ERROR, "'redirect_protocols' not supported by linked CURL library. Please recompile against newer CURL.\n");
227 return -1;
228 }
229#endif
230
231 return 0;
232}
233
234
235/*! \brief Data passed to cURL callbacks */
237 /*! The \c ast_bucket_file object that caused the operation */
239 /*! File to write data to */
240 FILE *out_file;
241};
242
243/*!
244 * \internal \brief The cURL header callback function
245 */
246static size_t curl_header_callback(char *buffer, size_t size, size_t nitems, void *data)
247{
248 struct curl_bucket_file_data *cb_data = data;
249 size_t realsize;
250 char *value;
251 char *header;
252
253 realsize = size * nitems;
254
255 if (realsize > MAX_HEADER_LENGTH) {
256 ast_log(LOG_WARNING, "cURL header length of '%zu' is too large: max %d\n",
257 realsize, MAX_HEADER_LENGTH);
258 return 0;
259 }
260
261 /* buffer may not be NULL terminated */
262 header = ast_alloca(realsize + 1);
263 memcpy(header, buffer, realsize);
264 header[realsize] = '\0';
265 value = strchr(header, ':');
266 if (!value) {
267 /* Not a header we care about; bail */
268 return realsize;
269 }
270 *value++ = '\0';
271
272 if (strcasecmp(header, "ETag")
273 && strcasecmp(header, "Cache-Control")
274 && strcasecmp(header, "Last-Modified")
275 && strcasecmp(header, "Content-Type")
276 && strcasecmp(header, "Expires")) {
277 return realsize;
278 }
279
282
284
285 return realsize;
286}
287
288/*!
289 * \internal \brief The cURL body callback function
290 */
291static size_t curl_body_callback(void *ptr, size_t size, size_t nitems, void *data)
292{
293 struct curl_bucket_file_data *cb_data = data;
294 size_t realsize;
295
296 realsize = fwrite(ptr, size, nitems, cb_data->out_file);
297
298 return realsize;
299}
300
301/*!
302 * \internal \brief Set the expiration metadata on the bucket file based on HTTP caching rules
303 */
305{
306 struct ast_bucket_metadata *metadata;
307 char time_buf[32], secs[AST_TIME_T_LEN];
308 struct timeval actual_expires = ast_tvnow();
309
310 metadata = ast_bucket_file_metadata_get(bucket_file, "cache-control");
311 if (metadata) {
312 char *str_max_age;
313
314 str_max_age = strstr(metadata->value, "s-maxage");
315 if (!str_max_age) {
316 str_max_age = strstr(metadata->value, "max-age");
317 }
318
319 if (str_max_age) {
320 unsigned int max_age;
321 char *equal = strchr(str_max_age, '=');
322 if (equal && (sscanf(equal + 1, "%30u", &max_age) == 1)) {
323 actual_expires.tv_sec += max_age;
324 }
325 }
326 ao2_ref(metadata, -1);
327 } else {
328 metadata = ast_bucket_file_metadata_get(bucket_file, "expires");
329 if (metadata) {
330 struct tm expires_time;
331
332 strptime(metadata->value, "%a, %d %b %Y %T %z", &expires_time);
333 expires_time.tm_isdst = -1;
334 actual_expires.tv_sec = mktime(&expires_time);
335
336 ao2_ref(metadata, -1);
337 }
338 }
339
340 /* Use 'now' if we didn't get an expiration time */
341 ast_time_t_to_string(actual_expires.tv_sec, secs, sizeof(secs));
342 snprintf(time_buf, sizeof(time_buf), "%30s", secs);
343
344 ast_bucket_file_metadata_set(bucket_file, "__actual_expires", time_buf);
345}
346
347static char *file_extension_from_string(const char *str, char *buffer, size_t capacity)
348{
349 const char *ext;
350
351 ext = strrchr(str, '.');
352 if (ext && ast_get_format_for_file_ext(ext + 1)) {
353 ast_debug(3, "Found extension '%s' at end of string\n", ext);
354 ast_copy_string(buffer, ext, capacity);
355 return buffer;
356 }
357
358 return NULL;
359}
360
361/*!
362 * \internal
363 * \brief Normalize the value of a Content-Type header
364 *
365 * This will trim off any optional parameters after the type/subtype.
366 *
367 * \return 0 if no normalization occurred, otherwise true (non-zero)
368 */
369static int normalize_content_type_header(char *content_type)
370{
371 char *params = strchr(content_type, ';');
372
373 if (params) {
374 *params-- = 0;
375 while (params > content_type && (*params == ' ' || *params == '\t')) {
376 *params-- = 0;
377 }
378 return 1;
379 }
380
381 return 0;
382}
383
384static int derive_extension_from_mime_type(const char *mime_type, char *buffer, size_t capacity)
385{
386 int res = 0;
387
388 /* Compare the provided Content-Type directly, parameters and all */
389 res = ast_get_extension_for_mime_type(mime_type, buffer, sizeof(buffer));
390 if (!res) {
391 char *m = ast_strdupa(mime_type);
392 /* Strip MIME type parameters and then check */
394 res = ast_get_extension_for_mime_type(m, buffer, sizeof(buffer));
395 }
396 }
397
398 return res;
399}
400
401static char *file_extension_from_content_type(struct ast_bucket_file *bucket_file, char *buffer, size_t capacity)
402{
403 /* Check for the extension based on the MIME type passed in the Content-Type
404 * header.
405 *
406 * If a match is found then retrieve the extension from the supported list
407 * corresponding to the mime-type and use that to rename the file */
408
410
411 header = ast_bucket_file_metadata_get(bucket_file, "content-type");
412 if (!header) {
413 return NULL;
414 }
415
416 if (derive_extension_from_mime_type(header->value, buffer, capacity)) {
417 ast_debug(3, "Derived extension '%s' from MIME type %s\n",
418 buffer,
419 header->value);
420 ao2_ref(header, -1);
421 return buffer;
422 }
423
424 ao2_ref(header, -1);
425
426 return NULL;
427}
428
429static char *file_extension_from_url_path(struct ast_bucket_file *bucket_file, char *buffer, size_t capacity)
430{
431 const char *path;
432 struct ast_uri *uri;
433
435 if (!uri) {
436 ast_log(LOG_ERROR, "Failed to parse URI: %s\n",
437 ast_sorcery_object_get_id(bucket_file));
438 return NULL;
439 }
440
442 if (!path) {
444 return NULL;
445 }
446
447 /* Just parse it as a string like before, but without the extra cruft */
448 buffer = file_extension_from_string(path, buffer, capacity);
450 return buffer;
451}
452
453static void bucket_file_set_extension(struct ast_bucket_file *bucket_file)
454{
455 /* Using Content-Type first allows for the most flexibility for whomever
456 * is serving up the audio file. If that doesn't turn up anything useful
457 * we'll try to parse the URL and use the extension */
458
459 char buffer[64];
460
461 if (file_extension_from_content_type(bucket_file, buffer, sizeof(buffer))
462 || file_extension_from_url_path(bucket_file, buffer, sizeof(buffer))) {
463 ast_bucket_file_metadata_set(bucket_file, "ext", buffer);
464 }
465}
466
467/*! \internal
468 * \brief Return whether or not we should always revalidate against the server
469 */
470static int bucket_file_always_revalidate(struct ast_bucket_file *bucket_file)
471{
472 RAII_VAR(struct ast_bucket_metadata *, metadata,
473 ast_bucket_file_metadata_get(bucket_file, "cache-control"),
475
476 if (!metadata) {
477 return 0;
478 }
479
480 if (strstr(metadata->value, "no-cache")
481 || strstr(metadata->value, "must-revalidate")) {
482 return 1;
483 }
484
485 return 0;
486}
487
488/*! \internal
489 * \brief Return whether or not the item has expired
490 */
491static int bucket_file_expired(struct ast_bucket_file *bucket_file)
492{
493 RAII_VAR(struct ast_bucket_metadata *, metadata,
494 ast_bucket_file_metadata_get(bucket_file, "__actual_expires"),
496 struct timeval current_time = ast_tvnow();
497 struct timeval expires = { .tv_sec = 0, .tv_usec = 0 };
498
499 if (!metadata) {
500 return 1;
501 }
502
503 if ((expires.tv_sec = ast_string_to_time_t(metadata->value)) == -1) {
504 return 1;
505 }
506
507 return ast_tvcmp(current_time, expires) == -1 ? 0 : 1;
508}
509
510/*!
511 * \internal \brief Obtain a CURL handle with common setup options
512 */
513static CURL *get_curl_instance(struct curl_bucket_file_data *cb_data)
514{
516 CURLcode rc;
517 CURL *curl;
518
519 curl = curl_easy_init();
520 if (!curl) {
521 return NULL;
522 }
523
524 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
525 curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, curl_header_callback);
526 curl_easy_setopt(curl, CURLOPT_URL, ast_sorcery_object_get_id(cb_data->bucket_file));
527 curl_easy_setopt(curl, CURLOPT_HEADERDATA, cb_data);
528
529 curl_easy_setopt(curl, CURLOPT_TIMEOUT, cfg->general->curl_timeout);
530 curl_easy_setopt(curl, CURLOPT_USERAGENT, cfg->general->curl_useragent);
531 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, cfg->general->curl_followlocation ? 1 : 0);
532 curl_easy_setopt(curl, CURLOPT_MAXREDIRS, cfg->general->curl_maxredirs);
533
534 if (!ast_strlen_zero(cfg->general->curl_proxy)) {
535 curl_easy_setopt(curl, CURLOPT_PROXY, cfg->general->curl_proxy);
536 }
537
538 if (!ast_strlen_zero(cfg->general->curl_protocols)) {
539#ifdef AST_CURL_HAS_PROTOCOLS_STR
540 CURLcode rc = curl_easy_setopt(curl, CURLOPT_PROTOCOLS_STR, cfg->general->curl_protocols);
541 if (rc != CURLE_OK) {
542 ast_log(AST_LOG_ERROR, "Setting protocols to '%s' failed: %d\n", cfg->general->curl_protocols, rc);
543 curl_easy_cleanup(curl);
544 return NULL;
545 }
546#endif
547 }
548 if (!ast_strlen_zero(cfg->general->curl_redir_protocols)) {
549#ifdef AST_CURL_HAS_PROTOCOLS_STR
550 CURLcode rc = curl_easy_setopt(curl, CURLOPT_REDIR_PROTOCOLS_STR, cfg->general->curl_redir_protocols);
551 if (rc != CURLE_OK) {
552 ast_log(AST_LOG_ERROR, "Setting redirect_protocols to '%s' failed: %d\n", cfg->general->curl_redir_protocols, rc);
553 curl_easy_cleanup(curl);
554 return NULL;
555 }
556#endif
557 }
558
559 rc = curl_easy_setopt(curl, CURLOPT_DNS_CACHE_TIMEOUT, cfg->general->curl_dns_cache_timeout);
560 if (rc != CURLE_OK) {
561 ast_log(AST_LOG_ERROR, "Setting dns_cache_timeout to '%d' failed: %d\n", cfg->general->curl_dns_cache_timeout, rc);
562 curl_easy_cleanup(curl);
563 return NULL;
564 }
565
566 return curl;
567}
568
569/*!
570 * \brief Execute the CURL
571 */
572static long execute_curl_instance(CURL *curl)
573{
574 char curl_errbuf[CURL_ERROR_SIZE + 1];
575 long http_code;
576
577 curl_errbuf[CURL_ERROR_SIZE] = '\0';
578 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errbuf);
579
580 if (curl_easy_perform(curl)) {
581 ast_log(LOG_WARNING, "%s\n", curl_errbuf);
582 return -1;
583 }
584
585 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
586
587 curl_easy_cleanup(curl);
588
589 return http_code;
590}
591
592/*!
593 * \internal \brief CURL the URI specified by the bucket_file and store it in the provided path
594 */
595static int bucket_file_run_curl(struct ast_bucket_file *bucket_file)
596{
597 struct curl_bucket_file_data cb_data = {
599 };
600 long http_code;
601 CURL *curl;
602
603 cb_data.out_file = fopen(bucket_file->path, "wb");
604 if (!cb_data.out_file) {
605 ast_log(LOG_WARNING, "Failed to open file '%s' for writing: %s (%d)\n",
606 bucket_file->path, strerror(errno), errno);
607 return -1;
608 }
609
610 curl = get_curl_instance(&cb_data);
611 if (!curl) {
612 fclose(cb_data.out_file);
613 return -1;
614 }
615
616 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_body_callback);
617 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&cb_data);
618
619 http_code = execute_curl_instance(curl);
620
621 fclose(cb_data.out_file);
622
623 if (http_code / 100 == 2) {
626 return 0;
627 } else {
628 ast_log(LOG_WARNING, "Failed to retrieve URL '%s': server returned %ld\n",
630 }
631
632 return -1;
633}
634
635static int bucket_http_wizard_is_stale(const struct ast_sorcery *sorcery, void *data, void *object)
636{
637 struct ast_bucket_file *bucket_file = object;
638 struct ast_bucket_metadata *metadata;
639 struct curl_slist *header_list = NULL;
640 long http_code;
641 CURL *curl;
642 struct curl_bucket_file_data cb_data = {
644 };
645 char etag_buf[256];
646
648 return 0;
649 }
650
651 /* See if we have an ETag for this item. If not, it's stale. */
652 metadata = ast_bucket_file_metadata_get(bucket_file, "etag");
653 if (!metadata) {
654 return 1;
655 }
656
657 curl = get_curl_instance(&cb_data);
658
659 /* Set the ETag header on our outgoing request */
660 snprintf(etag_buf, sizeof(etag_buf), "If-None-Match: %s", metadata->value);
661 header_list = curl_slist_append(header_list, etag_buf);
662 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header_list);
663 curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
664 ao2_ref(metadata, -1);
665
666 http_code = execute_curl_instance(curl);
667
668 curl_slist_free_all(header_list);
669
670 if (http_code == 304) {
672 return 0;
673 }
674
675 return 1;
676}
677
678static int bucket_http_wizard_create(const struct ast_sorcery *sorcery, void *data,
679 void *object)
680{
681 struct ast_bucket_file *bucket_file = object;
682
683 return bucket_file_run_curl(bucket_file);
684}
685
687 void *data, const char *type, const char *id)
688{
689 struct ast_bucket_file *bucket_file;
690
691 if (strcmp(type, "file")) {
692 ast_log(LOG_WARNING, "Failed to create storage: invalid bucket type '%s'\n", type);
693 return NULL;
694 }
695
696 if (ast_strlen_zero(id)) {
697 ast_log(LOG_WARNING, "Failed to create storage: no URI\n");
698 return NULL;
699 }
700
701 bucket_file = ast_bucket_file_alloc(id);
702 if (!bucket_file) {
703 ast_log(LOG_WARNING, "Failed to create storage for '%s'\n", id);
704 return NULL;
705 }
706
707 if (ast_bucket_file_temporary_create(bucket_file)) {
708 ast_log(LOG_WARNING, "Failed to create temporary storage for '%s'\n", id);
709 ast_sorcery_delete(sorcery, bucket_file);
710 ao2_ref(bucket_file, -1);
711 return NULL;
712 }
713
714 if (bucket_file_run_curl(bucket_file)) {
715 ast_sorcery_delete(sorcery, bucket_file);
716 ao2_ref(bucket_file, -1);
717 return NULL;
718 }
719
720 return bucket_file;
721}
722
723static int bucket_http_wizard_delete(const struct ast_sorcery *sorcery, void *data,
724 void *object)
725{
726 struct ast_bucket_file *bucket_file = object;
727
728 unlink(bucket_file->path);
729
730 return 0;
731}
732
734 .name = "http",
736 .retrieve_id = bucket_http_wizard_retrieve_id,
738 .is_stale = bucket_http_wizard_is_stale,
739};
740
742 .name = "http",
744 .retrieve_id = bucket_http_wizard_retrieve_id,
746 .is_stale = bucket_http_wizard_is_stale,
747};
748
750 .name = "https",
752 .retrieve_id = bucket_http_wizard_retrieve_id,
754 .is_stale = bucket_http_wizard_is_stale,
755};
756
758 .name = "https",
760 .retrieve_id = bucket_http_wizard_retrieve_id,
762 .is_stale = bucket_http_wizard_is_stale,
763};
764
765static int unload_module(void)
766{
767 aco_info_destroy(&cfg_info);
769 return 0;
770}
771
772static int load_module(void)
773{
774 if (aco_info_init(&cfg_info)) {
775 aco_info_destroy(&cfg_info);
777 }
778
779
780 aco_option_register(&cfg_info, "timeout_secs", ACO_EXACT, general_options,
781 "180", OPT_INT_T, 0,
782 FLDSET(struct conf_general_options, curl_timeout));
783
784 aco_option_register(&cfg_info, "user_agent", ACO_EXACT, general_options,
786 STRFLDSET(struct conf_general_options, curl_useragent));
787
788 aco_option_register(&cfg_info, "follow_location", ACO_EXACT, general_options,
789 "yes", OPT_BOOL_T, 1,
790 FLDSET(struct conf_general_options, curl_followlocation));
791
792 aco_option_register(&cfg_info, "max_redirects", ACO_EXACT, general_options,
793 "8", OPT_INT_T, 0,
794 FLDSET(struct conf_general_options, curl_maxredirs));
795
796 aco_option_register(&cfg_info, "proxy", ACO_EXACT, general_options,
798 STRFLDSET(struct conf_general_options, curl_proxy));
799
800 aco_option_register(&cfg_info, "dns_cache_timeout_secs", ACO_EXACT, general_options,
801 "60", OPT_INT_T, 0,
802 FLDSET(struct conf_general_options, curl_dns_cache_timeout));
803
804 aco_option_register(&cfg_info, "protocols", ACO_EXACT, general_options,
806 STRFLDSET(struct conf_general_options, curl_protocols));
807
808 aco_option_register(&cfg_info, "redirect_protocols", ACO_EXACT, general_options,
810 STRFLDSET(struct conf_general_options, curl_redir_protocols));
811
812
813 if (aco_process_config(&cfg_info, 0) == ACO_PROCESS_ERROR) {
814 struct conf *cfg;
815
816 ast_log(LOG_NOTICE, "Could not load res_http_media_cache config; using defaults\n");
817 cfg = conf_alloc();
818 if (!cfg) {
819 aco_info_destroy(&cfg_info);
821 }
822
823 if (aco_set_defaults(&general_option, "general", cfg->general)) {
824 ast_log(LOG_ERROR, "Failed to initialize res_http_media_cache defaults.\n");
825 ao2_ref(cfg, -1);
826 aco_info_destroy(&cfg_info);
828 }
829
831 ao2_ref(cfg, -1);
832 }
833
835 NULL, NULL)) {
836 ast_log(LOG_ERROR, "Failed to register Bucket HTTP wizard scheme implementation\n");
838 }
839
841 NULL, NULL)) {
842 ast_log(LOG_ERROR, "Failed to register Bucket HTTPS wizard scheme implementation\n");
844 }
845
847}
848
849AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "HTTP Media Cache Backend",
850 .support_level = AST_MODULE_SUPPORT_CORE,
851 .load = load_module,
852 .unload = unload_module,
853 .requires = "res_curl",
854 );
const char * str
Definition: app_jack.c:150
Asterisk main include file. File version handling, generic pbx functions.
#define AST_CURL_USER_AGENT
Definition: asterisk.h:44
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:288
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define ast_log
Definition: astobj2.c:42
#define ao2_global_obj_replace_unref(holder, obj)
Replace an ao2 object in the global holder, throwing away any old object.
Definition: astobj2.h:901
#define ao2_cleanup(obj)
Definition: astobj2.h:1934
#define ao2_global_obj_ref(holder)
Get a reference to the object stored in the global holder.
Definition: astobj2.h:918
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
#define ao2_global_obj_release(holder)
Release the ao2 object held in the global holder.
Definition: astobj2.h:859
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:409
Bucket File API.
int ast_bucket_file_metadata_set(struct ast_bucket_file *file, const char *name, const char *value)
Set a metadata attribute on a file to a specific value.
Definition: bucket.c:304
struct ast_bucket_file * ast_bucket_file_alloc(const char *uri)
Allocate a new bucket file.
Definition: bucket.c:633
#define ast_bucket_scheme_register(name, bucket, file, create_cb, destroy_cb)
Register support for a specific scheme.
Definition: bucket.h:137
int ast_bucket_file_temporary_create(struct ast_bucket_file *file)
Common file snapshot creation callback for creating a temporary file.
Definition: bucket.c:869
struct ast_bucket_metadata * ast_bucket_file_metadata_get(struct ast_bucket_file *file, const char *name)
Retrieve a metadata attribute from a file.
Definition: bucket.c:329
static const char type[]
Definition: chan_ooh323.c:109
@ ACO_EXACT
int aco_set_defaults(struct aco_type *type, const char *category, void *obj)
Set all default options of obj.
void aco_info_destroy(struct aco_info *info)
Destroy an initialized aco_info struct.
@ ACO_PROCESS_ERROR
Their was an error and no changes were applied.
#define STRFLDSET(type,...)
Convert a struct and a list of stringfield fields to an argument list of field offsets.
int aco_info_init(struct aco_info *info)
Initialize an aco_info structure.
#define FLDSET(type,...)
Convert a struct and list of fields to an argument list of field offsets.
#define aco_option_register(info, name, matchtype, types, default_val, opt_type, flags,...)
Register a config option.
#define ACO_FILES(...)
@ OPT_BOOL_T
Type for default option handler for bools (ast_true/ast_false)
@ OPT_INT_T
Type for default option handler for signed integers.
@ OPT_STRINGFIELD_T
Type for default option handler for stringfields.
@ ACO_GLOBAL
@ ACO_WHITELIST_EXACT
void * aco_pending_config(struct aco_info *info)
Get pending config changes.
enum aco_process_status aco_process_config(struct aco_info *info, int reload)
Process a config info via the options registered with an aco_info.
#define ACO_TYPES(...)
A helper macro to ensure that aco_info types always have a sentinel.
Generic File Format Support. Should be included by clients of the file handling routines....
struct ast_format * ast_get_format_for_file_ext(const char *file_ext)
Get the ast_format associated with the given file extension.
Definition: file.c:2014
int ast_get_extension_for_mime_type(const char *mime_type, char *buffer, size_t capacity)
Get a suitable filename extension for the given MIME type.
Definition: file.c:2027
const char * ext
Definition: http.c:150
#define AST_LOG_ERROR
#define ast_debug(level,...)
Log a DEBUG message.
#define LOG_ERROR
#define LOG_NOTICE
#define LOG_WARNING
int errno
Asterisk module definitions.
@ AST_MODFLAG_DEFAULT
Definition: module.h:329
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:557
@ AST_MODULE_SUPPORT_CORE
Definition: module.h:121
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
static int http_media_cache_config_pre_apply(void)
Pre-apply callback for the config framework.
static char * file_extension_from_string(const char *str, char *buffer, size_t capacity)
static void bucket_file_set_expiration(struct ast_bucket_file *bucket_file)
static void * conf_alloc(void)
Creates the http media cache conf object.
static int normalize_content_type_header(char *content_type)
static AO2_GLOBAL_OBJ_STATIC(confs)
Locking container for safe configuration access.
static char * file_extension_from_content_type(struct ast_bucket_file *bucket_file, char *buffer, size_t capacity)
static struct aco_file conf_file
The conf file that's processed for the module.
static void bucket_file_set_extension(struct ast_bucket_file *bucket_file)
static int bucket_file_expired(struct ast_bucket_file *bucket_file)
static struct aco_type general_option
Mapping of the http media cache conf struct's general to the general context in the config file.
static size_t curl_body_callback(void *ptr, size_t size, size_t nitems, void *data)
static struct ast_sorcery_wizard http_bucket_wizard
static void conf_destructor(void *obj)
Disposes of the http media cache conf object.
CONFIG_INFO_STANDARD(cfg_info, confs, conf_alloc,.pre_apply_config=http_media_cache_config_pre_apply,.files=ACO_FILES(&conf_file))
static struct aco_type * general_options[]
static CURL * get_curl_instance(struct curl_bucket_file_data *cb_data)
static int bucket_http_wizard_delete(const struct ast_sorcery *sorcery, void *data, void *object)
static struct ast_sorcery_wizard http_bucket_file_wizard
static struct ast_sorcery_wizard https_bucket_wizard
static struct ast_sorcery_wizard https_bucket_file_wizard
static int bucket_http_wizard_create(const struct ast_sorcery *sorcery, void *data, void *object)
static int load_module(void)
static int bucket_file_always_revalidate(struct ast_bucket_file *bucket_file)
static char * file_extension_from_url_path(struct ast_bucket_file *bucket_file, char *buffer, size_t capacity)
static void * bucket_http_wizard_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id)
static long execute_curl_instance(CURL *curl)
Execute the CURL.
static int unload_module(void)
static int bucket_http_wizard_is_stale(const struct ast_sorcery *sorcery, void *data, void *object)
static int bucket_file_run_curl(struct ast_bucket_file *bucket_file)
static size_t curl_header_callback(char *buffer, size_t size, size_t nitems, void *data)
#define MAX_HEADER_LENGTH
static int derive_extension_from_mime_type(const char *mime_type, char *buffer, size_t capacity)
static struct ast_sorcery * sorcery
#define NULL
Definition: resample.c:96
Sorcery Data Access Layer API.
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition: sorcery.c:2317
int ast_sorcery_delete(const struct ast_sorcery *sorcery, void *object)
Delete an object.
Definition: sorcery.c:2238
#define AST_DECLARE_STRING_FIELDS(field_list)
Declare the fields needed in a structure.
Definition: stringfields.h:341
#define AST_STRING_FIELD(name)
Declare a string field.
Definition: stringfields.h:303
#define ast_string_field_init(x, size)
Initialize a field pool and fields.
Definition: stringfields.h:359
#define ast_string_field_free_memory(x)
free all memory - to be called before destroying the object
Definition: stringfields.h:374
static force_inline char * ast_str_to_lower(char *str)
Convert a string to all lower-case.
Definition: strings.h:1321
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
char * ast_trim_blanks(char *str)
Trims trailing whitespace characters from a string.
Definition: strings.h:186
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
char * ast_skip_blanks(const char *str)
Gets a pointer to the first non-whitespace character in a string.
Definition: strings.h:161
The representation of a single configuration file to be processed.
const char * filename
Type information about a category-level configurable object.
enum aco_type_t type
Bucket file structure, contains reference to file and information about it.
Definition: bucket.h:78
char path[PATH_MAX]
Local path to this file.
Definition: bucket.h:95
Bucket metadata structure, AO2 key value pair.
Definition: bucket.h:47
const char * value
Value of the attribute.
Definition: bucket.h:51
Interface for a sorcery wizard.
Definition: sorcery.h:276
const char * name
Name of the wizard.
Definition: sorcery.h:278
Full structure for sorcery.
Definition: sorcery.c:230
Stores parsed uri information.
Definition: uri.c:30
char * path
Definition: uri.c:40
char uri[0]
Definition: uri.c:44
General configuration options for http media cache.
const ast_string_field curl_useragent
int curl_dns_cache_timeout
Life-time of CURL DNS cache entries.
const ast_string_field curl_protocols
const ast_string_field curl_proxy
int curl_maxredirs
Number of redirects to follow for one request.
int curl_followlocation
Follow 3xx redirects automatically.
int curl_timeout
Request timeout to use.
const ast_string_field curl_redir_protocols
All configuration options for http media cache.
struct conf_general_options * general
Data passed to cURL callbacks.
struct ast_bucket_file * bucket_file
const ast_string_field value
int value
Definition: syslog.c:37
Definitions to aid in the use of thread local storage.
int ast_time_t_to_string(time_t time, char *buf, size_t length)
Converts to a string representation of a time_t as decimal seconds since the epoch....
Definition: time.c:152
time_t ast_string_to_time_t(const char *str)
Returns a time_t from a string containing seconds since the epoch.
Definition: time.c:163
int ast_tvcmp(struct timeval _a, struct timeval _b)
Compress two struct timeval instances returning -1, 0, 1 if the first arg is smaller,...
Definition: time.h:137
#define AST_TIME_T_LEN
Definition: time.h:45
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:159
const char * ast_uri_path(const struct ast_uri *uri)
Retrieve the uri path.
Definition: uri.c:135
struct ast_uri * ast_uri_parse(const char *uri)
Parse the given uri into a structure.
Definition: uri.c:195
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:941