From 6ef27f19176ad02b07d852bc62580ab494215bb8 Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Thu, 5 Jan 2012 16:52:37 -0600 Subject: [PATCH] add cache-control support --- src/mod/applications/mod_httapi/mod_httapi.c | 23 +++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/mod/applications/mod_httapi/mod_httapi.c b/src/mod/applications/mod_httapi/mod_httapi.c index 3d45a2f390..5d68c548f2 100644 --- a/src/mod/applications/mod_httapi/mod_httapi.c +++ b/src/mod/applications/mod_httapi/mod_httapi.c @@ -1381,7 +1381,7 @@ static switch_status_t do_config(void) } else if (!strcasecmp(var, "file-cache-ttl")) { int tmp = atoi(val); - if (tmp > 0) { + if (tmp > -1) { globals.cache_ttl = tmp; } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid value [%s]for file-cache-ttl\n", val); @@ -1974,7 +1974,7 @@ static switch_status_t fetch_cache_data(const char *url, switch_event_t **header return status; } -static switch_status_t write_meta_file(http_file_context_t *context, const char *data) +static switch_status_t write_meta_file(http_file_context_t *context, const char *data, switch_event_t *headers) { int fd; switch_status_t status = SWITCH_STATUS_SUCCESS; @@ -1985,10 +1985,23 @@ static switch_status_t write_meta_file(http_file_context_t *context, const char } if (!zstr(data)) { + int ttl = globals.cache_ttl; + const char *cc; + const char *p; + + if (headers && (cc = switch_event_get_header(headers, "Cache-Control")) && (p = switch_stristr("max-age=", cc))) { + p += 8; + + if (p) { + ttl = atoi(p); + if (ttl < 0) ttl = globals.cache_ttl; + } + } + switch_snprintf(write_data, sizeof(write_data), "%" SWITCH_TIME_T_FMT ":%s", - switch_epoch_time_now(NULL) + globals.cache_ttl, + switch_epoch_time_now(NULL) + ttl, data); @@ -2068,7 +2081,7 @@ static switch_status_t locate_url_file(http_file_context_t *context, const char ); if (!strcmp(metadata, context->metadata)) { - write_meta_file(context, metadata); + write_meta_file(context, metadata, headers); switch_goto_status(SWITCH_STATUS_SUCCESS, end); } } @@ -2082,7 +2095,7 @@ static switch_status_t locate_url_file(http_file_context_t *context, const char switch_event_get_header_nil(headers, "content-length") ); - write_meta_file(context, metadata); + write_meta_file(context, metadata, headers); if (switch_file_exists(context->cache_file, context->pool) == SWITCH_STATUS_SUCCESS) { status = SWITCH_STATUS_SUCCESS;