Skip to content

Commit

Permalink
Fixed some memory leaks of curl headers
Browse files Browse the repository at this point in the history
  • Loading branch information
rnijveld committed Feb 16, 2020
1 parent 4dfd2c6 commit 090565a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
32 changes: 22 additions & 10 deletions lib/cache_rest.c
Expand Up @@ -187,9 +187,9 @@ size_t buffer_write_callback(void *ptr, size_t size, size_t nmemb, void *data)
return mapcache_buffer_append(buffer, realsize, ptr); return mapcache_buffer_append(buffer, realsize, ptr);
} }


static void _set_headers(mapcache_context *ctx, CURL *curl, apr_table_t *headers) { static struct curl_slist* _set_headers(mapcache_context *ctx, CURL *curl, apr_table_t *headers) {
if(!headers) { if(!headers) {
return; return NULL;
} else { } else {
struct curl_slist *curl_headers=NULL; struct curl_slist *curl_headers=NULL;
const apr_array_header_t *array = apr_table_elts(headers); const apr_array_header_t *array = apr_table_elts(headers);
Expand All @@ -203,13 +203,15 @@ static void _set_headers(mapcache_context *ctx, CURL *curl, apr_table_t *headers
} }
} }
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, curl_headers); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, curl_headers);
return curl_headers;
} }
} }


static void _put_request(mapcache_context *ctx, CURL *curl, mapcache_buffer *buffer, char *url, apr_table_t *headers) { static void _put_request(mapcache_context *ctx, CURL *curl, mapcache_buffer *buffer, char *url, apr_table_t *headers) {
CURLcode res; CURLcode res;
buffer_struct data; buffer_struct data;
mapcache_buffer *response; mapcache_buffer *response;
struct curl_slist *curl_header_data;


data.buffer = buffer; data.buffer = buffer;
data.offset = 0; data.offset = 0;
Expand Down Expand Up @@ -238,7 +240,7 @@ static void _put_request(mapcache_context *ctx, CURL *curl, mapcache_buffer *buf


/* don't use an Expect: 100 Continue header */ /* don't use an Expect: 100 Continue header */
apr_table_set(headers, "Expect", ""); apr_table_set(headers, "Expect", "");
_set_headers(ctx, curl, headers); curl_header_data = _set_headers(ctx, curl, headers);


/* specify target URL, and note that this URL should include a file /* specify target URL, and note that this URL should include a file
* name, not only a directory */ * name, not only a directory */
Expand Down Expand Up @@ -272,14 +274,16 @@ static void _put_request(mapcache_context *ctx, CURL *curl, mapcache_buffer *buf
} }
} }


curl_slist_free_all(curl_header_data);
} }


static int _head_request(mapcache_context *ctx, CURL *curl, char *url, apr_table_t *headers) { static int _head_request(mapcache_context *ctx, CURL *curl, char *url, apr_table_t *headers) {


CURLcode res; CURLcode res;
long http_code; long http_code;

struct curl_slist *curl_header_data;
_set_headers(ctx, curl, headers);
curl_header_data = _set_headers(ctx, curl, headers);


curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);


Expand All @@ -299,15 +303,18 @@ static int _head_request(mapcache_context *ctx, CURL *curl, char *url, apr_table
curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code); curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
} }


curl_slist_free_all(curl_header_data);

return (int)http_code; return (int)http_code;
} }


static int _delete_request(mapcache_context *ctx, CURL *curl, char *url, apr_table_t *headers) { static int _delete_request(mapcache_context *ctx, CURL *curl, char *url, apr_table_t *headers) {


CURLcode res; CURLcode res;
long http_code; long http_code;
struct curl_slist *curl_header_data;


_set_headers(ctx, curl, headers); curl_header_data = _set_headers(ctx, curl, headers);


curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);


Expand All @@ -328,6 +335,8 @@ static int _delete_request(mapcache_context *ctx, CURL *curl, char *url, apr_tab
curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code); curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
} }


curl_slist_free_all(curl_header_data);

return (int)http_code; return (int)http_code;
} }


Expand All @@ -336,8 +345,9 @@ static mapcache_buffer* _get_request(mapcache_context *ctx, CURL *curl, char *ur
CURLcode res; CURLcode res;
mapcache_buffer *data = NULL; mapcache_buffer *data = NULL;
long http_code; long http_code;
struct curl_slist *curl_header_data;


_set_headers(ctx, curl, headers); curl_header_data = _set_headers(ctx, curl, headers);


curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);


Expand Down Expand Up @@ -384,6 +394,8 @@ static mapcache_buffer* _get_request(mapcache_context *ctx, CURL *curl, char *ur
} }
} }


curl_slist_free_all(curl_header_data);

return data; return data;
} }


Expand Down Expand Up @@ -1019,7 +1031,7 @@ static int _mapcache_cache_rest_has_tile(mapcache_context *ctx, mapcache_cache *
int status; int status;
mapcache_pooled_connection *pc; mapcache_pooled_connection *pc;
CURL *curl; CURL *curl;

_mapcache_cache_rest_tile_url(ctx, tile, &rcache->rest, &rcache->rest.has_tile, &url); _mapcache_cache_rest_tile_url(ctx, tile, &rcache->rest, &rcache->rest.has_tile, &url);
headers = _mapcache_cache_rest_headers(ctx, tile, &rcache->rest, &rcache->rest.has_tile); headers = _mapcache_cache_rest_headers(ctx, tile, &rcache->rest, &rcache->rest.has_tile);


Expand Down Expand Up @@ -1122,7 +1134,7 @@ static int _mapcache_cache_rest_get(mapcache_context *ctx, mapcache_cache *pcach
if(rcache->rest.get_tile.add_headers) { if(rcache->rest.get_tile.add_headers) {
rcache->rest.get_tile.add_headers(ctx,rcache,tile,url,headers); rcache->rest.get_tile.add_headers(ctx,rcache,tile,url,headers);
} }

pc = _rest_get_connection(ctx, rcache, tile); pc = _rest_get_connection(ctx, rcache, tile);
if(GC_HAS_ERROR(ctx)) if(GC_HAS_ERROR(ctx))
return MAPCACHE_FAILURE; return MAPCACHE_FAILURE;
Expand Down Expand Up @@ -1250,7 +1262,7 @@ static void _mapcache_cache_rest_configuration_parse_xml(mapcache_context *ctx,
} else { } else {
dcache->connection_timeout = 30; dcache->connection_timeout = 30;
} }

if ((cur_node = ezxml_child(node,"timeout")) != NULL) { if ((cur_node = ezxml_child(node,"timeout")) != NULL) {
char *endptr; char *endptr;
dcache->timeout = (int)strtol(cur_node->txt,&endptr,10); dcache->timeout = (int)strtol(cur_node->txt,&endptr,10);
Expand Down
9 changes: 5 additions & 4 deletions lib/http.c
Expand Up @@ -169,6 +169,7 @@ void mapcache_http_do_request(mapcache_context *ctx, mapcache_http *req, mapcach
ctx->set_error(ctx, 502, "curl failed to request url %s : %s", req->url, error_msg); ctx->set_error(ctx, 502, "curl failed to request url %s : %s", req->url, error_msg);
} }
/* cleanup curl stuff */ /* cleanup curl stuff */
curl_slist_free_all(curl_headers);
curl_easy_cleanup(curl_handle); curl_easy_cleanup(curl_handle);
} }


Expand All @@ -195,11 +196,11 @@ char to_hex(char code) {
char *url_encode(apr_pool_t *p, const char *str) { char *url_encode(apr_pool_t *p, const char *str) {
char *buf = apr_pcalloc(p, strlen(str) * 3 + 1), *pbuf = buf; char *buf = apr_pcalloc(p, strlen(str) * 3 + 1), *pbuf = buf;
while (*str) { while (*str) {
if (isalnum(*str) || *str == '-' || *str == '_' || *str == '.' || *str == '~') if (isalnum(*str) || *str == '-' || *str == '_' || *str == '.' || *str == '~')
*pbuf++ = *str; *pbuf++ = *str;
else if (*str == ' ') else if (*str == ' ')
*pbuf++ = '+'; *pbuf++ = '+';
else else
*pbuf++ = '%', *pbuf++ = to_hex(*str >> 4), *pbuf++ = to_hex(*str & 15); *pbuf++ = '%', *pbuf++ = to_hex(*str >> 4), *pbuf++ = to_hex(*str & 15);
str++; str++;
} }
Expand Down Expand Up @@ -388,7 +389,7 @@ mapcache_http* mapcache_http_configuration_parse_xml(mapcache_context *ctx, ezxm
} else { } else {
req->connection_timeout = 30; req->connection_timeout = 30;
} }

if ((http_node = ezxml_child(node,"timeout")) != NULL) { if ((http_node = ezxml_child(node,"timeout")) != NULL) {
char *endptr; char *endptr;
req->timeout = (int)strtol(http_node->txt,&endptr,10); req->timeout = (int)strtol(http_node->txt,&endptr,10);
Expand Down

0 comments on commit 090565a

Please sign in to comment.