From f12e03e1c171a723097eb14f1074d705ed1404e4 Mon Sep 17 00:00:00 2001 From: "Olle E. Johansson" Date: Mon, 2 Nov 2015 11:44:49 +0100 Subject: [PATCH] curl Enforce max data size when downloading data. Stop re-allocating memory for data we do not bother with. --- modules/curl/curl.h | 5 +++-- modules/curl/functions.c | 39 ++++++++++++++++++++------------------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/modules/curl/curl.h b/modules/curl/curl.h index b83438c1386..8f5aba9eaf2 100644 --- a/modules/curl/curl.h +++ b/modules/curl/curl.h @@ -55,7 +55,8 @@ typedef struct { char *buf; size_t curr_size; size_t pos; -} http_res_stream_t; + size_t max_size; +} curl_res_stream_t; /*! Predefined connection objects */ @@ -76,7 +77,7 @@ typedef struct _curl_con unsigned int port; /*!< The port to connect to */ int timeout; /*!< Timeout for this connection */ long maxdatasize; /*!< Maximum data download on GET or POST */ - http_res_stream_t *stream; /*!< Curl stream */ + curl_res_stream_t *stream; /*!< Curl stream */ struct _curl_con *next; /*!< next connection */ char redirecturl[512]; /*!< Last redirect URL - to use for $curlredirect(curlcon) pv */ } curl_con_t; diff --git a/modules/curl/functions.c b/modules/curl/functions.c index 19908c64520..6925c71f0b8 100644 --- a/modules/curl/functions.c +++ b/modules/curl/functions.c @@ -60,26 +60,26 @@ static int curL_query_url(struct sip_msg* _m, char* _url, char* _dst, const char */ size_t write_function( void *ptr, size_t size, size_t nmemb, void *stream_ptr) { - /* A question here is if we can somehow signal maxdatasize and stop filling - buffers at maxdatasize - we don't need any more. Or just ignore and stop - allocating pkg memory at that point. A good todo. - */ - http_res_stream_t *stream = (http_res_stream_t *) stream_ptr; - - stream->buf = (char *) pkg_realloc(stream->buf, stream->curr_size + - (size * nmemb) + 1); - - if (stream->buf == NULL) { - LM_ERR("cannot allocate memory for stream\n"); - return CURLE_WRITE_ERROR; - } + curl_res_stream_t *stream = (curl_res_stream_t *) stream_ptr; + + + if (stream->max_size == 0 || stream->curr_size < stream->max_size) { + stream->buf = (char *) pkg_realloc(stream->buf, stream->curr_size + (size * nmemb) + 1); - memcpy(&stream->buf[stream->pos], (char *) ptr, (size * nmemb)); + if (stream->buf == NULL) { + LM_ERR("cannot allocate memory for stream\n"); + return CURLE_WRITE_ERROR; + } - stream->curr_size += ((size * nmemb) + 1); - stream->pos += (size * nmemb); + memcpy(&stream->buf[stream->pos], (char *) ptr, (size * nmemb)); - stream->buf[stream->pos + 1] = '\0'; + stream->curr_size += ((size * nmemb) + 1); + stream->pos += (size * nmemb); + + stream->buf[stream->pos + 1] = '\0'; + } else { + LM_DBG("****** ##### CURL Max datasize exceeded: max %u current %u\n", (unsigned int) stream->max_size, (unsigned int)stream->curr_size); + } return size * nmemb; } @@ -93,7 +93,7 @@ static int curL_query_url(struct sip_msg* _m, char* _url, char* _dst, const char CURLcode res; str value; char *url, *at = NULL; - http_res_stream_t stream; + curl_res_stream_t stream; long stat; pv_spec_t *dst; pv_value_t val; @@ -101,7 +101,8 @@ static int curL_query_url(struct sip_msg* _m, char* _url, char* _dst, const char double total_time; struct curl_slist *headerlist = NULL; - memset(&stream, 0, sizeof(http_res_stream_t)); + memset(&stream, 0, sizeof(curl_res_stream_t)); + stream.max_size = (size_t) maxdatasize; value.s = _url; value.len = strlen(_url);