From 729992d5ae6c593e17afb17410ad3db392a61260 Mon Sep 17 00:00:00 2001 From: Andrey Volk Date: Fri, 21 May 2021 01:00:35 +0300 Subject: [PATCH] [mod_httapi] Avoid using CURLOPT_WRITEFUNCTION for headers --- src/mod/applications/mod_httapi/mod_httapi.c | 22 +++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/mod/applications/mod_httapi/mod_httapi.c b/src/mod/applications/mod_httapi/mod_httapi.c index 86a05bb258..0b89fd35a7 100644 --- a/src/mod/applications/mod_httapi/mod_httapi.c +++ b/src/mod/applications/mod_httapi/mod_httapi.c @@ -2430,6 +2430,13 @@ static char *load_cache_data(http_file_context_t *context, const char *url) return context->cache_file; } +static size_t dummy_save_file_callback(void* ptr, size_t size, size_t nmemb, void* data) +{ + (void)ptr; + (void)data; + return (size * nmemb); +} + static size_t save_file_callback(void *ptr, size_t size, size_t nmemb, void *data) { register unsigned int realsize = (unsigned int) (size * nmemb); @@ -2616,19 +2623,20 @@ static switch_status_t fetch_cache_data(http_file_context_t *context, const char } else { switch_curl_easy_setopt(curl_handle, CURLOPT_HEADER, 1); switch_curl_easy_setopt(curl_handle, CURLOPT_NOBODY, 1); + + /* Prevent writing the data (headers in this case) to stdout */ + switch_curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, dummy_save_file_callback); + switch_curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, 0); } if (headers) { if (!client->headers) { switch_event_create(&client->headers, SWITCH_EVENT_CLONE); } - if (save_path) { - switch_curl_easy_setopt(curl_handle, CURLOPT_HEADERFUNCTION, get_header_callback); - switch_curl_easy_setopt(curl_handle, CURLOPT_WRITEHEADER, (void *) client); - } else { - switch_curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, get_header_callback); - switch_curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *) client); - } + + /* CURLOPT_HEADERFUNCTION guarantees to call the callback for each complete header line, CURLOPT_WRITEFUNCTION does not! */ + switch_curl_easy_setopt(curl_handle, CURLOPT_HEADERFUNCTION, get_header_callback); + switch_curl_easy_setopt(curl_handle, CURLOPT_WRITEHEADER, (void *) client); } if (!zstr(dup_creds)) {