From d8a02dc2449769870c400688f77f78054eb05b30 Mon Sep 17 00:00:00 2001 From: Seven Du Date: Tue, 30 Jul 2013 16:35:46 +0800 Subject: [PATCH] add timeout options to mod_curl --- src/mod/applications/mod_curl/mod_curl.c | 36 +++++++++++++++++++++--- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/src/mod/applications/mod_curl/mod_curl.c b/src/mod/applications/mod_curl/mod_curl.c index bcc5335513..74ab3c1d88 100644 --- a/src/mod/applications/mod_curl/mod_curl.c +++ b/src/mod/applications/mod_curl/mod_curl.c @@ -25,6 +25,7 @@ * * Rupa Schomaker * Yossi Neiman + * Seven Du * * mod_curl.c -- API for performing http queries * @@ -106,6 +107,12 @@ struct callback_obj { }; typedef struct callback_obj callback_t; +struct curl_options_obj { + long connect_timeout; + long timeout; +}; +typedef struct curl_options_obj curl_options_t; + static size_t file_callback(void *ptr, size_t size, size_t nmemb, void *data) { register unsigned int realsize = (unsigned int) (size * nmemb); @@ -138,7 +145,7 @@ static size_t header_callback(void *ptr, size_t size, size_t nmemb, void *data) return realsize; } -static http_data_t *do_lookup_url(switch_memory_pool_t *pool, const char *url, const char *method, const char *data, const char *content_type) +static http_data_t *do_lookup_url(switch_memory_pool_t *pool, const char *url, const char *method, const char *data, const char *content_type, curl_options_t *options) { switch_CURL *curl_handle = NULL; long httpRes = 0; @@ -156,9 +163,19 @@ static http_data_t *do_lookup_url(switch_memory_pool_t *pool, const char *url, c method = "get"; } - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "method: %s, url: %s, content-type: %s\n", method, url, content_type); curl_handle = switch_curl_easy_init(); + if (options) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "%ld %ld\n", options->connect_timeout, options->timeout); + if (options->connect_timeout) { + switch_curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT, options->connect_timeout); + } + + if (options->timeout) { + switch_curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, options->timeout); + } + } + if (!strncasecmp(url, "https", 5)) { switch_curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, 0); switch_curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYHOST, 0); @@ -710,6 +727,8 @@ SWITCH_STANDARD_APP(curl_app_function) switch_curl_slist_t *slist = NULL; switch_stream_handle_t stream = { 0 }; int i = 0; + curl_options_t options = { 0 }; + const char *curl_timeout; if (session) { pool = switch_core_session_get_pool(session); @@ -750,7 +769,16 @@ SWITCH_STANDARD_APP(curl_app_function) } } - http_data = do_lookup_url(pool, url, method, postdata, content_type); + curl_timeout = switch_channel_get_variable(channel, "curl_connect_timeout"); + + if (curl_timeout) options.connect_timeout = atoi(curl_timeout); + + curl_timeout = switch_channel_get_variable(channel, "curl_timeout"); + + if (curl_timeout) options.timeout = atoi(curl_timeout); + + + http_data = do_lookup_url(pool, url, method, postdata, content_type, &options); if (do_json) { switch_channel_set_variable(channel, "curl_response_data", print_json(pool, http_data)); } else { @@ -843,7 +871,7 @@ SWITCH_STANDARD_API(curl_function) } } - http_data = do_lookup_url(pool, url, method, postdata, content_type); + http_data = do_lookup_url(pool, url, method, postdata, content_type, NULL); if (do_json) { stream->write_function(stream, "%s", print_json(pool, http_data)); } else {