Skip to content

Commit a0995a8

Browse files
authored
Merge pull request #30 from cdevelop/fix_mod_httpapi_url
fix[mod_httapi] url超过1024字节无法缓存
2 parents 782d7aa + 5f68f2c commit a0995a8

File tree

1 file changed

+51
-23
lines changed

1 file changed

+51
-23
lines changed

src/mod/applications/mod_httapi/mod_httapi.c

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_httapi_load);
3737
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_httapi_shutdown);
3838
SWITCH_MODULE_DEFINITION(mod_httapi, mod_httapi_load, mod_httapi_shutdown, NULL);
3939

40+
#ifndef _WIN32
41+
#define O_BINARY 0
42+
#endif
43+
4044
typedef struct profile_perms_s {
4145
switch_byte_t set_params;
4246
switch_byte_t set_vars;
@@ -2374,8 +2378,9 @@ static char *load_cache_data(http_file_context_t *context, const char *url)
23742378
const char *ext = NULL;
23752379
char *dext = NULL, *p;
23762380
char digest[SWITCH_MD5_DIGEST_STRING_SIZE] = { 0 };
2377-
char meta_buffer[1024] = "";
2381+
char *meta_buffer = NULL;
23782382
int fd;
2383+
long bytes;
23792384

23802385
switch_md5_string(digest, (void *) url, strlen(url));
23812386

@@ -2395,33 +2400,51 @@ static char *load_cache_data(http_file_context_t *context, const char *url)
23952400
} else switch_safe_free(dext);
23962401
}
23972402

2403+
if (!zstr(ext)) {
2404+
switch_file_interface_t* file_interface = switch_loadable_module_get_file_interface(ext, NULL);
2405+
if (file_interface) {
2406+
UNPROTECT_INTERFACE(file_interface);
2407+
} else {
2408+
ext = NULL;
2409+
}
2410+
}
2411+
23982412
context->cache_file_base = switch_core_sprintf(context->pool, "%s%s%s", globals.cache_path, SWITCH_PATH_SEPARATOR, digest);
23992413
context->meta_file = switch_core_sprintf(context->pool, "%s%s%s.meta", globals.cache_path, SWITCH_PATH_SEPARATOR, digest);
24002414

24012415
if (switch_file_exists(context->meta_file, context->pool) == SWITCH_STATUS_SUCCESS && ((fd = open(context->meta_file, O_RDONLY, 0)) > -1)) {
2402-
if (read(fd, meta_buffer, sizeof(meta_buffer)) > 0) {
2403-
char *p;
2404-
2405-
if ((p = strchr(meta_buffer, ':'))) {
2406-
*p++ = '\0';
2407-
if (context->expires != 1) {
2408-
context->expires = (time_t) atol(meta_buffer);
2416+
bytes = lseek(fd, 0, SEEK_END);
2417+
lseek(fd, 0, SEEK_SET);
2418+
2419+
#ifndef CURL_MAX_INPUT_LENGTH
2420+
#define CURL_MAX_INPUT_LENGTH 10000000
2421+
#endif
2422+
2423+
if (bytes < CURL_MAX_INPUT_LENGTH && bytes > 0) {
2424+
meta_buffer = switch_core_alloc(context->pool, bytes + 1);
2425+
if ((bytes = read(fd, meta_buffer, bytes)) > 0) {
2426+
char *p;
2427+
2428+
if ((p = strchr(meta_buffer, ':'))) {
2429+
*p++ = '\0';
2430+
if (context->expires != 1) {
2431+
context->expires = (time_t) atol(meta_buffer);
2432+
}
2433+
context->metadata = switch_core_strdup(context->pool, p);
24092434
}
2410-
context->metadata = switch_core_strdup(context->pool, p);
2411-
}
24122435

2413-
if ((p = strrchr(context->metadata, ':'))) {
2414-
p++;
2415-
if (!zstr(p)) {
2416-
ext = p;
2436+
if ((p = strrchr(context->metadata, ':'))) {
2437+
p++;
2438+
if (!zstr(p)) {
2439+
ext = p;
2440+
}
24172441
}
24182442
}
2419-
24202443
}
24212444
close(fd);
24222445
}
24232446

2424-
context->cache_file = switch_core_sprintf(context->pool, "%s%s%s%s%s", globals.cache_path, SWITCH_PATH_SEPARATOR, digest, ext ? "." : "", ext ? ext : "");
2447+
context->cache_file = switch_core_sprintf(context->pool, "%s%s%s%s%s", globals.cache_path, SWITCH_PATH_SEPARATOR, digest, !zstr(ext) ? "." : "", !zstr(ext) ? ext : "");
24252448
switch_safe_free(dext);
24262449

24272450
return context->cache_file;
@@ -2473,6 +2496,7 @@ static switch_status_t fetch_cache_data(http_file_context_t *context, const char
24732496
client_t *client = NULL;
24742497
long code;
24752498
switch_status_t status = SWITCH_STATUS_FALSE;
2499+
switch_time_t down_time = switch_micro_time_now();
24762500
char *dup_creds = NULL, *dynamic_url = NULL, *use_url;
24772501
char *ua = NULL;
24782502
const char *profile_name = NULL;
@@ -2501,7 +2525,7 @@ static switch_status_t fetch_cache_data(http_file_context_t *context, const char
25012525

25022526
if (save_path) {
25032527
while(--tries && (client->fd == 0 || client->fd == -1)) {
2504-
client->fd = open(save_path, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR);
2528+
client->fd = open(save_path, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, S_IRUSR | S_IWUSR);
25052529
}
25062530

25072531
if (client->fd < 0) {
@@ -2660,7 +2684,7 @@ static switch_status_t fetch_cache_data(http_file_context_t *context, const char
26602684
switch(code) {
26612685
case 200:
26622686
if (save_path) {
2663-
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "caching: url:%s to %s (%" SWITCH_SIZE_T_FMT " bytes)\n", url, save_path, client->bytes);
2687+
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "caching: url:%s to %s (%" SWITCH_SIZE_T_FMT " bytes) elapsed:%" SWITCH_TIME_T_FMT "\n", url, save_path, client->bytes, switch_micro_time_now() - down_time);
26642688
}
26652689
status = SWITCH_STATUS_SUCCESS;
26662690
break;
@@ -2692,7 +2716,6 @@ static switch_status_t write_meta_file(http_file_context_t *context, const char
26922716
{
26932717
int fd;
26942718
switch_status_t status = SWITCH_STATUS_SUCCESS;
2695-
char write_data[1024];
26962719

26972720
if ((fd = open(context->meta_file, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)) < 0) {
26982721
return SWITCH_STATUS_FALSE;
@@ -2703,6 +2726,7 @@ static switch_status_t write_meta_file(http_file_context_t *context, const char
27032726
const char *cc;
27042727
const char *p;
27052728
int x;
2729+
char *write_data;
27062730

27072731
if (context->url_params) {
27082732
if ((cc = switch_event_get_header(context->url_params, "abs_cache_control"))) {
@@ -2739,14 +2763,18 @@ static switch_status_t write_meta_file(http_file_context_t *context, const char
27392763
context->del_on_close = 1;
27402764
}
27412765
}
2742-
2743-
switch_snprintf(write_data, sizeof(write_data),
2766+
2767+
write_data = switch_core_sprintf(context->pool,
27442768
"%" TIME_T_FMT ":%s",
27452769
switch_epoch_time_now(NULL) + ttl,
27462770
data);
27472771

2748-
2749-
status = write(fd, write_data, (int)strlen(write_data) + 1) > 0 ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE;
2772+
if (write_data) {
2773+
status =
2774+
write(fd, write_data, (int)strlen(write_data) + 1) > 0 ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE;
2775+
} else {
2776+
status = SWITCH_STATUS_FALSE;
2777+
}
27502778
}
27512779

27522780
close(fd);

0 commit comments

Comments
 (0)