diff --git a/kamailio/modules/http_client/functions.c b/kamailio/modules/http_client/functions.c index 52861a7..2d04d1b 100644 --- a/kamailio/modules/http_client/functions.c +++ b/kamailio/modules/http_client/functions.c @@ -34,6 +34,7 @@ #include +#include #include "../../mod_fix.h" #include "../../pvar.h" @@ -100,13 +101,13 @@ size_t write_function( void *ptr, size_t size, size_t nmemb, void *stream_ptr) return size * nmemb; } +static CURL *curl = NULL; /*! Send query to server, optionally post data. */ static int curL_query_url(struct sip_msg* _m, const char* _url, str* _dst, const curl_query_t * const params) { - CURL *curl; - CURLcode res; + CURLcode res; char *at = NULL; curl_res_stream_t stream; long stat; @@ -118,11 +119,15 @@ static int curL_query_url(struct sip_msg* _m, const char* _url, str* _dst, const memset(&stream, 0, sizeof(curl_res_stream_t)); stream.max_size = (size_t) params->maxdatasize; - curl = curl_easy_init(); - if (curl == NULL) { - LM_ERR("failed to initialize curl\n"); - return -1; - } + if (curl == NULL) { + curl = curl_easy_init(); + if (curl == NULL) { + LM_ERR("failed to initialize curl\n"); + return -1; + } + } else { + curl_easy_reset(curl); + } LM_DBG("****** ##### CURL URL [%s] \n", _url); res = curl_easy_setopt(curl, CURLOPT_URL, _url); @@ -202,7 +207,18 @@ static int curL_query_url(struct sip_msg* _m, const char* _url, str* _dst, const /* PANIC */ LM_ERR("Could not set CURL options. Library error \n"); } else { - res = curl_easy_perform(curl); + struct timespec timeBefore; + struct timespec timeAfter; + + clock_gettime(CLOCK_MONOTONIC, &timeBefore); + + res = curl_easy_perform(curl); + + clock_gettime(CLOCK_MONOTONIC, &timeAfter); + + int timeMillis = (timeAfter.tv_sec-timeBefore.tv_sec)*1000 + (timeAfter.tv_nsec-timeBefore.tv_nsec)/1000000; + + LM_INFO("HTTP Call performed in %d ms \n", timeMillis); } if (headerlist) { curl_slist_free_all(headerlist); @@ -218,7 +234,6 @@ static int curL_query_url(struct sip_msg* _m, const char* _url, str* _dst, const LM_ERR("failed to perform curl (%d)\n", res); } - curl_easy_cleanup(curl); if(stream.buf) { pkg_free(stream.buf); } @@ -285,7 +300,6 @@ static int curL_query_url(struct sip_msg* _m, const char* _url, str* _dst, const } /* CURLcode curl_easy_getinfo(CURL *curl, CURLINFO info, ... ); */ - curl_easy_cleanup(curl); if (stream.buf != NULL) pkg_free(stream.buf); return stat;