@@ -37,6 +37,10 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_httapi_load);
37
37
SWITCH_MODULE_SHUTDOWN_FUNCTION (mod_httapi_shutdown );
38
38
SWITCH_MODULE_DEFINITION (mod_httapi , mod_httapi_load , mod_httapi_shutdown , NULL );
39
39
40
+ #ifndef _WIN32
41
+ #define O_BINARY 0
42
+ #endif
43
+
40
44
typedef struct profile_perms_s {
41
45
switch_byte_t set_params ;
42
46
switch_byte_t set_vars ;
@@ -2374,8 +2378,9 @@ static char *load_cache_data(http_file_context_t *context, const char *url)
2374
2378
const char * ext = NULL ;
2375
2379
char * dext = NULL , * p ;
2376
2380
char digest [SWITCH_MD5_DIGEST_STRING_SIZE ] = { 0 };
2377
- char meta_buffer [ 1024 ] = "" ;
2381
+ char * meta_buffer = NULL ;
2378
2382
int fd ;
2383
+ long bytes ;
2379
2384
2380
2385
switch_md5_string (digest , (void * ) url , strlen (url ));
2381
2386
@@ -2395,33 +2400,51 @@ static char *load_cache_data(http_file_context_t *context, const char *url)
2395
2400
} else switch_safe_free (dext );
2396
2401
}
2397
2402
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
+
2398
2412
context -> cache_file_base = switch_core_sprintf (context -> pool , "%s%s%s" , globals .cache_path , SWITCH_PATH_SEPARATOR , digest );
2399
2413
context -> meta_file = switch_core_sprintf (context -> pool , "%s%s%s.meta" , globals .cache_path , SWITCH_PATH_SEPARATOR , digest );
2400
2414
2401
2415
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 );
2409
2434
}
2410
- context -> metadata = switch_core_strdup (context -> pool , p );
2411
- }
2412
2435
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
+ }
2417
2441
}
2418
2442
}
2419
-
2420
2443
}
2421
2444
close (fd );
2422
2445
}
2423
2446
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 : "" );
2425
2448
switch_safe_free (dext );
2426
2449
2427
2450
return context -> cache_file ;
@@ -2473,6 +2496,7 @@ static switch_status_t fetch_cache_data(http_file_context_t *context, const char
2473
2496
client_t * client = NULL ;
2474
2497
long code ;
2475
2498
switch_status_t status = SWITCH_STATUS_FALSE ;
2499
+ switch_time_t down_time = switch_micro_time_now ();
2476
2500
char * dup_creds = NULL , * dynamic_url = NULL , * use_url ;
2477
2501
char * ua = NULL ;
2478
2502
const char * profile_name = NULL ;
@@ -2501,7 +2525,7 @@ static switch_status_t fetch_cache_data(http_file_context_t *context, const char
2501
2525
2502
2526
if (save_path ) {
2503
2527
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 );
2505
2529
}
2506
2530
2507
2531
if (client -> fd < 0 ) {
@@ -2660,7 +2684,7 @@ static switch_status_t fetch_cache_data(http_file_context_t *context, const char
2660
2684
switch (code ) {
2661
2685
case 200 :
2662
2686
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 );
2664
2688
}
2665
2689
status = SWITCH_STATUS_SUCCESS ;
2666
2690
break ;
@@ -2692,7 +2716,6 @@ static switch_status_t write_meta_file(http_file_context_t *context, const char
2692
2716
{
2693
2717
int fd ;
2694
2718
switch_status_t status = SWITCH_STATUS_SUCCESS ;
2695
- char write_data [1024 ];
2696
2719
2697
2720
if ((fd = open (context -> meta_file , O_WRONLY | O_CREAT | O_TRUNC , S_IRUSR | S_IWUSR )) < 0 ) {
2698
2721
return SWITCH_STATUS_FALSE ;
@@ -2703,6 +2726,7 @@ static switch_status_t write_meta_file(http_file_context_t *context, const char
2703
2726
const char * cc ;
2704
2727
const char * p ;
2705
2728
int x ;
2729
+ char * write_data ;
2706
2730
2707
2731
if (context -> url_params ) {
2708
2732
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
2739
2763
context -> del_on_close = 1 ;
2740
2764
}
2741
2765
}
2742
-
2743
- switch_snprintf ( write_data , sizeof ( write_data ) ,
2766
+
2767
+ write_data = switch_core_sprintf ( context -> pool ,
2744
2768
"%" TIME_T_FMT ":%s" ,
2745
2769
switch_epoch_time_now (NULL ) + ttl ,
2746
2770
data );
2747
2771
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
+ }
2750
2778
}
2751
2779
2752
2780
close (fd );
0 commit comments