| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 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" |
| 113 |
#include "asterisk/threadstorage.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 |
|
| 124 |
static int http_media_cache_config_pre_apply(void); |
| 125 |
|
| 126 |
|
| 127 |
struct conf_general_options { |
| 128 |
|
| 129 |
int curl_timeout; |
| 130 |
|
| 131 |
|
| 132 |
int curl_followlocation; |
| 133 |
|
| 134 |
|
| 135 |
int curl_maxredirs; |
| 136 |
|
| 137 |
|
| 138 |
int curl_dns_cache_timeout; |
| 139 |
|
| 140 |
AST_DECLARE_STRING_FIELDS( |
| 141 |
AST_STRING_FIELD(curl_useragent); |
| 142 |
AST_STRING_FIELD(curl_proxy); |
| 143 |
AST_STRING_FIELD(curl_protocols); |
| 144 |
AST_STRING_FIELD(curl_redir_protocols); |
| 145 |
); |
| 146 |
}; |
| 147 |
|
| 148 |
|
| 149 |
struct conf { |
| 150 |
|
| 151 |
struct conf_general_options *general; |
| 152 |
}; |
| 153 |
|
| 154 |
|
| 155 |
static AO2_GLOBAL_OBJ_STATIC(confs); |
| 156 |
|
| 157 |
|
| 158 |
static 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 |
|
| 166 |
static struct aco_type *general_options[] = ACO_TYPES(&general_option); |
| 167 |
|
| 168 |
|
| 169 |
static void conf_destructor(void *obj) |
| 170 |
{ |
| 171 |
struct conf *cfg = obj; |
| 172 |
ast_string_field_free_memory(cfg->general); |
| 173 |
ao2_cleanup(cfg->general); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
static 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 |
|
| 199 |
static struct aco_file conf_file = { |
| 200 |
|
| 201 |
.filename = "res_http_media_cache.conf", |
| 202 |
|
| 203 |
.types = ACO_TYPES(&general_option), |
| 204 |
}; |
| 205 |
|
| 206 |
CONFIG_INFO_STANDARD(cfg_info, confs, conf_alloc, |
| 207 |
.pre_apply_config = http_media_cache_config_pre_apply, |
| 208 |
.files = ACO_FILES(&conf_file)); |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
static int http_media_cache_config_pre_apply(void) |
| 216 |
{ |
| 217 |
#ifndef AST_CURL_HAS_PROTOCOLS_STR |
| 218 |
struct conf *cfg = aco_pending_config(&cfg_info); |
| 219 |
|
| 220 |
if (!ast_strlen_zero(cfg->general->curl_protocols)) { |
| 221 |
ast_log(AST_LOG_ERROR, "'protocols' not supported by linked CURL library. Please recompile against newer CURL.\n"); |
| 222 |
return -1; |
| 223 |
} |
| 224 |
|
| 225 |
if (!ast_strlen_zero(cfg->general->curl_redir_protocols)) { |
| 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 |
|
| 236 |
struct curl_bucket_file_data { |
| 237 |
|
| 238 |
struct ast_bucket_file *bucket_file; |
| 239 |
|
| 240 |
FILE *out_file; |
| 241 |
}; |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
static 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 |
|
| 262 |
header = ast_alloca(realsize + 1); |
| 263 |
memcpy(header, buffer, realsize); |
| 264 |
header[realsize] = '\0'; |
| 265 |
value = strchr(header, ':'); |
| 266 |
if (!value) { |
| 267 |
|
| 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 |
|
| 280 |
value = ast_trim_blanks(ast_skip_blanks(value)); |
| 281 |
header = ast_str_to_lower(header); |
| 282 |
|
| 283 |
ast_bucket_file_metadata_set(cb_data->bucket_file, header, value); |
| 284 |
|
| 285 |
return realsize; |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
static 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 |
|
| 303 |
|
| 304 |
static void bucket_file_set_expiration(struct ast_bucket_file *bucket_file) |
| 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 |
const 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 |
const 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 |
|
| 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 |
|
| 347 |
static 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 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
static 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 |
|
| 384 |
static int derive_extension_from_mime_type(const char *mime_type, char *buffer, size_t capacity) |
| 385 |
{ |
| 386 |
int res = 0; |
| 387 |
|
| 388 |
|
| 389 |
res = ast_get_extension_for_mime_type(mime_type, buffer, sizeof(buffer)); |
| 390 |
if (!res) { |
| 391 |
char *m = ast_strdupa(mime_type); |
| 392 |
|
| 393 |
if (normalize_content_type_header(m)) { |
| 394 |
res = ast_get_extension_for_mime_type(m, buffer, sizeof(buffer)); |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
return res; |
| 399 |
} |
| 400 |
|
| 401 |
static char *file_extension_from_content_type(struct ast_bucket_file *bucket_file, char *buffer, size_t capacity) |
| 402 |
{ |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
struct ast_bucket_metadata *header; |
| 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 |
|
| 429 |
static 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 |
|
| 434 |
uri = ast_uri_parse(ast_sorcery_object_get_id(bucket_file)); |
| 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 |
|
| 441 |
path = ast_uri_path(uri); |
| 442 |
if (!path) { |
| 443 |
ao2_cleanup(uri); |
| 444 |
return NULL; |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
buffer = file_extension_from_string(path, buffer, capacity); |
| 449 |
ao2_cleanup(uri); |
| 450 |
return buffer; |
| 451 |
} |
| 452 |
|
| 453 |
static void bucket_file_set_extension(struct ast_bucket_file *bucket_file) |
| 454 |
{ |
| 455 |
|
| 456 |
|
| 457 |
|
| 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 |
|
| 468 |
|
| 469 |
|
| 470 |
static 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"), |
| 474 |
ao2_cleanup); |
| 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 |
|
| 489 |
|
| 490 |
|
| 491 |
static 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"), |
| 495 |
ao2_cleanup); |
| 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 |
|
| 512 |
|
| 513 |
static CURL *get_curl_instance(struct curl_bucket_file_data *cb_data) |
| 514 |
{ |
| 515 |
RAII_VAR(struct conf *, cfg, ao2_global_obj_ref(confs), ao2_cleanup); |
| 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 |
|
| 571 |
|
| 572 |
static 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 |
|
| 594 |
|
| 595 |
static int bucket_file_run_curl(struct ast_bucket_file *bucket_file) |
| 596 |
{ |
| 597 |
struct curl_bucket_file_data cb_data = { |
| 598 |
.bucket_file = bucket_file, |
| 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) { |
| 624 |
bucket_file_set_expiration(bucket_file); |
| 625 |
bucket_file_set_extension(bucket_file); |
| 626 |
return 0; |
| 627 |
} else { |
| 628 |
ast_log(LOG_WARNING, "Failed to retrieve URL '%s': server returned %ld\n", |
| 629 |
ast_sorcery_object_get_id(bucket_file), http_code); |
| 630 |
} |
| 631 |
|
| 632 |
return -1; |
| 633 |
} |
| 634 |
|
| 635 |
static 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 = { |
| 643 |
.bucket_file = bucket_file |
| 644 |
}; |
| 645 |
char etag_buf[256]; |
| 646 |
|
| 647 |
if (!bucket_file_expired(bucket_file) && !bucket_file_always_revalidate(bucket_file)) { |
| 648 |
return 0; |
| 649 |
} |
| 650 |
|
| 651 |
|
| 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 |
|
| 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) { |
| 671 |
bucket_file_set_expiration(bucket_file); |
| 672 |
return 0; |
| 673 |
} |
| 674 |
|
| 675 |
return 1; |
| 676 |
} |
| 677 |
|
| 678 |
static 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 |
|
| 686 |
static void *bucket_http_wizard_retrieve_id(const struct ast_sorcery *sorcery, |
| 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 |
|
| 723 |
static 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 |
|
| 733 |
static struct ast_sorcery_wizard http_bucket_wizard = { |
| 734 |
.name = "http", |
| 735 |
.create = bucket_http_wizard_create, |
| 736 |
.retrieve_id = bucket_http_wizard_retrieve_id, |
| 737 |
.delete = bucket_http_wizard_delete, |
| 738 |
.is_stale = bucket_http_wizard_is_stale, |
| 739 |
}; |
| 740 |
|
| 741 |
static struct ast_sorcery_wizard http_bucket_file_wizard = { |
| 742 |
.name = "http", |
| 743 |
.create = bucket_http_wizard_create, |
| 744 |
.retrieve_id = bucket_http_wizard_retrieve_id, |
| 745 |
.delete = bucket_http_wizard_delete, |
| 746 |
.is_stale = bucket_http_wizard_is_stale, |
| 747 |
}; |
| 748 |
|
| 749 |
static struct ast_sorcery_wizard https_bucket_wizard = { |
| 750 |
.name = "https", |
| 751 |
.create = bucket_http_wizard_create, |
| 752 |
.retrieve_id = bucket_http_wizard_retrieve_id, |
| 753 |
.delete = bucket_http_wizard_delete, |
| 754 |
.is_stale = bucket_http_wizard_is_stale, |
| 755 |
}; |
| 756 |
|
| 757 |
static struct ast_sorcery_wizard https_bucket_file_wizard = { |
| 758 |
.name = "https", |
| 759 |
.create = bucket_http_wizard_create, |
| 760 |
.retrieve_id = bucket_http_wizard_retrieve_id, |
| 761 |
.delete = bucket_http_wizard_delete, |
| 762 |
.is_stale = bucket_http_wizard_is_stale, |
| 763 |
}; |
| 764 |
|
| 765 |
static int unload_module(void) |
| 766 |
{ |
| 767 |
aco_info_destroy(&cfg_info); |
| 768 |
ao2_global_obj_release(confs); |
| 769 |
return 0; |
| 770 |
} |
| 771 |
|
| 772 |
static int load_module(void) |
| 773 |
{ |
| 774 |
if (aco_info_init(&cfg_info)) { |
| 775 |
aco_info_destroy(&cfg_info); |
| 776 |
return AST_MODULE_LOAD_DECLINE; |
| 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, |
| 785 |
AST_CURL_USER_AGENT, OPT_STRINGFIELD_T, 0, |
| 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, |
| 797 |
NULL, OPT_STRINGFIELD_T, 1, |
| 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, |
| 805 |
NULL, OPT_STRINGFIELD_T, 1, |
| 806 |
STRFLDSET(struct conf_general_options, curl_protocols)); |
| 807 |
|
| 808 |
aco_option_register(&cfg_info, "redirect_protocols", ACO_EXACT, general_options, |
| 809 |
NULL, OPT_STRINGFIELD_T, 1, |
| 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); |
| 820 |
return AST_MODULE_LOAD_DECLINE; |
| 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); |
| 827 |
return AST_MODULE_LOAD_DECLINE; |
| 828 |
} |
| 829 |
|
| 830 |
ao2_global_obj_replace_unref(confs, cfg); |
| 831 |
ao2_ref(cfg, -1); |
| 832 |
} |
| 833 |
|
| 834 |
if (ast_bucket_scheme_register("http", &http_bucket_wizard, &http_bucket_file_wizard, |
| 835 |
NULL, NULL)) { |
| 836 |
ast_log(LOG_ERROR, "Failed to register Bucket HTTP wizard scheme implementation\n"); |
| 837 |
return AST_MODULE_LOAD_DECLINE; |
| 838 |
} |
| 839 |
|
| 840 |
if (ast_bucket_scheme_register("https", &https_bucket_wizard, &https_bucket_file_wizard, |
| 841 |
NULL, NULL)) { |
| 842 |
ast_log(LOG_ERROR, "Failed to register Bucket HTTPS wizard scheme implementation\n"); |
| 843 |
return AST_MODULE_LOAD_DECLINE; |
| 844 |
} |
| 845 |
|
| 846 |
return AST_MODULE_LOAD_SUCCESS; |
| 847 |
} |
| 848 |
|
| 849 |
AST_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 |
); |