Skip to content

Commit 7134584

Browse files
authored
Merge pull request #233 from rnijveld/curl_header_memory_leaks
Fixed some memory leaks of curl headers
2 parents 4dfd2c6 + 090565a commit 7134584

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

lib/cache_rest.c

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,9 @@ size_t buffer_write_callback(void *ptr, size_t size, size_t nmemb, void *data)
187187
return mapcache_buffer_append(buffer, realsize, ptr);
188188
}
189189

190-
static void _set_headers(mapcache_context *ctx, CURL *curl, apr_table_t *headers) {
190+
static struct curl_slist* _set_headers(mapcache_context *ctx, CURL *curl, apr_table_t *headers) {
191191
if(!headers) {
192-
return;
192+
return NULL;
193193
} else {
194194
struct curl_slist *curl_headers=NULL;
195195
const apr_array_header_t *array = apr_table_elts(headers);
@@ -203,13 +203,15 @@ static void _set_headers(mapcache_context *ctx, CURL *curl, apr_table_t *headers
203203
}
204204
}
205205
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, curl_headers);
206+
return curl_headers;
206207
}
207208
}
208209

209210
static void _put_request(mapcache_context *ctx, CURL *curl, mapcache_buffer *buffer, char *url, apr_table_t *headers) {
210211
CURLcode res;
211212
buffer_struct data;
212213
mapcache_buffer *response;
214+
struct curl_slist *curl_header_data;
213215

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

239241
/* don't use an Expect: 100 Continue header */
240242
apr_table_set(headers, "Expect", "");
241-
_set_headers(ctx, curl, headers);
243+
curl_header_data = _set_headers(ctx, curl, headers);
242244

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

277+
curl_slist_free_all(curl_header_data);
275278
}
276279

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

279282
CURLcode res;
280283
long http_code;
281-
282-
_set_headers(ctx, curl, headers);
284+
struct curl_slist *curl_header_data;
285+
286+
curl_header_data = _set_headers(ctx, curl, headers);
283287

284288
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
285289

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

306+
curl_slist_free_all(curl_header_data);
307+
302308
return (int)http_code;
303309
}
304310

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

307313
CURLcode res;
308314
long http_code;
315+
struct curl_slist *curl_header_data;
309316

310-
_set_headers(ctx, curl, headers);
317+
curl_header_data = _set_headers(ctx, curl, headers);
311318

312319
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
313320

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

338+
curl_slist_free_all(curl_header_data);
339+
331340
return (int)http_code;
332341
}
333342

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

340-
_set_headers(ctx, curl, headers);
350+
curl_header_data = _set_headers(ctx, curl, headers);
341351

342352
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
343353

@@ -384,6 +394,8 @@ static mapcache_buffer* _get_request(mapcache_context *ctx, CURL *curl, char *ur
384394
}
385395
}
386396

397+
curl_slist_free_all(curl_header_data);
398+
387399
return data;
388400
}
389401

@@ -1019,7 +1031,7 @@ static int _mapcache_cache_rest_has_tile(mapcache_context *ctx, mapcache_cache *
10191031
int status;
10201032
mapcache_pooled_connection *pc;
10211033
CURL *curl;
1022-
1034+
10231035
_mapcache_cache_rest_tile_url(ctx, tile, &rcache->rest, &rcache->rest.has_tile, &url);
10241036
headers = _mapcache_cache_rest_headers(ctx, tile, &rcache->rest, &rcache->rest.has_tile);
10251037

@@ -1122,7 +1134,7 @@ static int _mapcache_cache_rest_get(mapcache_context *ctx, mapcache_cache *pcach
11221134
if(rcache->rest.get_tile.add_headers) {
11231135
rcache->rest.get_tile.add_headers(ctx,rcache,tile,url,headers);
11241136
}
1125-
1137+
11261138
pc = _rest_get_connection(ctx, rcache, tile);
11271139
if(GC_HAS_ERROR(ctx))
11281140
return MAPCACHE_FAILURE;
@@ -1250,7 +1262,7 @@ static void _mapcache_cache_rest_configuration_parse_xml(mapcache_context *ctx,
12501262
} else {
12511263
dcache->connection_timeout = 30;
12521264
}
1253-
1265+
12541266
if ((cur_node = ezxml_child(node,"timeout")) != NULL) {
12551267
char *endptr;
12561268
dcache->timeout = (int)strtol(cur_node->txt,&endptr,10);

lib/http.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ void mapcache_http_do_request(mapcache_context *ctx, mapcache_http *req, mapcach
169169
ctx->set_error(ctx, 502, "curl failed to request url %s : %s", req->url, error_msg);
170170
}
171171
/* cleanup curl stuff */
172+
curl_slist_free_all(curl_headers);
172173
curl_easy_cleanup(curl_handle);
173174
}
174175

@@ -195,11 +196,11 @@ char to_hex(char code) {
195196
char *url_encode(apr_pool_t *p, const char *str) {
196197
char *buf = apr_pcalloc(p, strlen(str) * 3 + 1), *pbuf = buf;
197198
while (*str) {
198-
if (isalnum(*str) || *str == '-' || *str == '_' || *str == '.' || *str == '~')
199+
if (isalnum(*str) || *str == '-' || *str == '_' || *str == '.' || *str == '~')
199200
*pbuf++ = *str;
200-
else if (*str == ' ')
201+
else if (*str == ' ')
201202
*pbuf++ = '+';
202-
else
203+
else
203204
*pbuf++ = '%', *pbuf++ = to_hex(*str >> 4), *pbuf++ = to_hex(*str & 15);
204205
str++;
205206
}
@@ -388,7 +389,7 @@ mapcache_http* mapcache_http_configuration_parse_xml(mapcache_context *ctx, ezxm
388389
} else {
389390
req->connection_timeout = 30;
390391
}
391-
392+
392393
if ((http_node = ezxml_child(node,"timeout")) != NULL) {
393394
char *endptr;
394395
req->timeout = (int)strtol(http_node->txt,&endptr,10);

0 commit comments

Comments
 (0)