From 039823848906b11fd6183ba81f037b7211a29497 Mon Sep 17 00:00:00 2001 From: Clarence Date: Fri, 28 Apr 2023 04:43:32 +0800 Subject: [PATCH] [Core, mod_curl, mod_httapi, mod_http_cache] Compatible with libcurl>=7.87.0 * [core] fix deprecated with libcurl>=7.56.0 * [mod_httpapi] fix deprecated with libcurl>=7.56.0 * [mod_curl] fix deprecated with libcurl>=7.56.0 * [mod_http_cache] fix deprecated with libcurl>=7.12.1 --- src/include/switch_curl.h | 4 +++ src/mod/applications/mod_curl/mod_curl.c | 36 +++++++++++++++++++ src/mod/applications/mod_httapi/mod_httapi.c | 12 +++++++ src/mod/applications/mod_http_cache/azure.c | 4 +++ .../mod_http_cache/mod_http_cache.c | 2 ++ src/switch_curl.c | 31 ++++++++++++++-- 6 files changed, 87 insertions(+), 2 deletions(-) diff --git a/src/include/switch_curl.h b/src/include/switch_curl.h index 5872527865..389548791a 100644 --- a/src/include/switch_curl.h +++ b/src/include/switch_curl.h @@ -50,7 +50,11 @@ SWITCH_DECLARE(switch_CURLcode) switch_curl_easy_setopt(CURL *handle, switch_CUR SWITCH_DECLARE(const char *) switch_curl_easy_strerror(switch_CURLcode errornum ); SWITCH_DECLARE(void) switch_curl_init(void); SWITCH_DECLARE(void) switch_curl_destroy(void); +#if defined(LIBCURL_VERSION_NUM) && (LIBCURL_VERSION_NUM >= 0x073800) +SWITCH_DECLARE(switch_status_t) switch_curl_process_form_post_params(switch_event_t *event, switch_CURL *curl_handle, curl_mime **mimep); +#else SWITCH_DECLARE(switch_status_t) switch_curl_process_form_post_params(switch_event_t *event, switch_CURL *curl_handle, struct curl_httppost **formpostp); +#endif #define switch_curl_easy_setopt curl_easy_setopt SWITCH_END_EXTERN_C diff --git a/src/mod/applications/mod_curl/mod_curl.c b/src/mod/applications/mod_curl/mod_curl.c index 02079d6767..c780e6947e 100644 --- a/src/mod/applications/mod_curl/mod_curl.c +++ b/src/mod/applications/mod_curl/mod_curl.c @@ -104,8 +104,13 @@ struct http_sendfile_data_obj { char *extrapost_elements; switch_CURL *curl_handle; char *cacert; +#if defined(LIBCURL_VERSION_NUM) && (LIBCURL_VERSION_NUM >= 0x073800) + curl_mime *mime; + curl_mimepart *part; +#else struct curl_httppost *formpost; struct curl_httppost *lastptr; +#endif uint8_t flags; /* This is for where to send output of the curl_sendfile commands */ switch_stream_handle_t *stream; char *sendfile_response; @@ -456,8 +461,19 @@ static void http_sendfile_initialize_curl(http_sendfile_data_t *http_data) curl_easy_setopt(http_data->curl_handle, CURLOPT_WRITEFUNCTION, http_sendfile_response_callback); curl_easy_setopt(http_data->curl_handle, CURLOPT_WRITEDATA, (void *) http_data); + /* Initial http_data->mime */ +#if defined(LIBCURL_VERSION_NUM) && (LIBCURL_VERSION_NUM >= 0x073800) + http_data->mime = curl_mime_init(http_data->curl_handle); +#endif + /* Add the file to upload as a POST form field */ +#if defined(LIBCURL_VERSION_NUM) && (LIBCURL_VERSION_NUM >= 0x073800) + http_data->part = curl_mime_addpart(http_data->mime); + curl_mime_name(http_data->part, http_data->filename_element_name); + curl_mime_filedata(http_data->part, http_data->filename_element); +#else curl_formadd(&http_data->formpost, &http_data->lastptr, CURLFORM_COPYNAME, http_data->filename_element_name, CURLFORM_FILE, http_data->filename_element, CURLFORM_END); +#endif if(!zstr(http_data->extrapost_elements)) { @@ -476,16 +492,32 @@ static void http_sendfile_initialize_curl(http_sendfile_data_t *http_data) if(argc2 == 2) { switch_url_decode(argv2[0]); switch_url_decode(argv2[1]); +#if defined(LIBCURL_VERSION_NUM) && (LIBCURL_VERSION_NUM >= 0x073800) + http_data->part = curl_mime_addpart(http_data->mime); + curl_mime_name(http_data->part, argv2[0]); + curl_mime_data(http_data->part, argv2[1], CURL_ZERO_TERMINATED); +#else curl_formadd(&http_data->formpost, &http_data->lastptr, CURLFORM_COPYNAME, argv2[0], CURLFORM_COPYCONTENTS, argv2[1], CURLFORM_END); +#endif } } } /* Fill in the submit field too, even if this isn't really needed */ +#if defined(LIBCURL_VERSION_NUM) && (LIBCURL_VERSION_NUM >= 0x073800) + http_data->part = curl_mime_addpart(http_data->mime); + curl_mime_name(http_data->part, "submit"); + curl_mime_data(http_data->part, "or_die", CURL_ZERO_TERMINATED); +#else curl_formadd(&http_data->formpost, &http_data->lastptr, CURLFORM_COPYNAME, "submit", CURLFORM_COPYCONTENTS, "or_die", CURLFORM_END); +#endif /* what URL that receives this POST */ +#if defined(LIBCURL_VERSION_NUM) && (LIBCURL_VERSION_NUM >= 0x073800) + curl_easy_setopt(http_data->curl_handle, CURLOPT_MIMEPOST, http_data->mime); +#else curl_easy_setopt(http_data->curl_handle, CURLOPT_HTTPPOST, http_data->formpost); +#endif // This part actually fires off the curl, captures the HTTP response code, and then frees up the handle. curl_easy_perform(http_data->curl_handle); @@ -494,7 +526,11 @@ static void http_sendfile_initialize_curl(http_sendfile_data_t *http_data) curl_easy_cleanup(http_data->curl_handle); // Clean up the form data from POST +#if defined(LIBCURL_VERSION_NUM) && (LIBCURL_VERSION_NUM >= 0x073800) + curl_mime_free(http_data->mime); +#else curl_formfree(http_data->formpost); +#endif } static switch_status_t http_sendfile_test_file_open(http_sendfile_data_t *http_data, switch_event_t *event) diff --git a/src/mod/applications/mod_httapi/mod_httapi.c b/src/mod/applications/mod_httapi/mod_httapi.c index 4b32d87c6d..4da0deeb49 100644 --- a/src/mod/applications/mod_httapi/mod_httapi.c +++ b/src/mod/applications/mod_httapi/mod_httapi.c @@ -1419,7 +1419,11 @@ static switch_status_t httapi_sync(client_t *client) switch_status_t status = SWITCH_STATUS_FALSE; int get_style_method = 0; char *method = NULL; +#if defined(LIBCURL_VERSION_NUM) && (LIBCURL_VERSION_NUM >= 0x073800) + curl_mime *formpost = NULL; +#else struct curl_httppost *formpost=NULL; +#endif switch_event_t *save_params = NULL; const char *put_file; FILE *fd = NULL; @@ -1588,7 +1592,11 @@ static switch_status_t httapi_sync(client_t *client) curl_easy_setopt(curl_handle, CURLOPT_READFUNCTION, put_file_read); } else if (formpost) { +#if defined(LIBCURL_VERSION_NUM) && (LIBCURL_VERSION_NUM >= 0x073800) + curl_easy_setopt(curl_handle, CURLOPT_MIMEPOST, formpost); +#else curl_easy_setopt(curl_handle, CURLOPT_HTTPPOST, formpost); +#endif } else { switch_curl_easy_setopt(curl_handle, CURLOPT_POST, !get_style_method); } @@ -1671,7 +1679,11 @@ static switch_status_t httapi_sync(client_t *client) switch_curl_slist_free_all(headers); if (formpost) { +#if defined(LIBCURL_VERSION_NUM) && (LIBCURL_VERSION_NUM >= 0x073800) + curl_mime_free(formpost); +#else curl_formfree(formpost); +#endif } if (client->err) { diff --git a/src/mod/applications/mod_http_cache/azure.c b/src/mod/applications/mod_http_cache/azure.c index f1e4cf8925..a16d2e9f94 100644 --- a/src/mod/applications/mod_http_cache/azure.c +++ b/src/mod/applications/mod_http_cache/azure.c @@ -279,7 +279,11 @@ switch_status_t azure_blob_finalise_put(http_profile_t *profile, const char *url goto done; } +#if defined(LIBCURL_VERSION_NUM) && (LIBCURL_VERSION_NUM >= 0x070c01) + switch_curl_easy_setopt(curl_handle, CURLOPT_UPLOAD, 1); +#else switch_curl_easy_setopt(curl_handle, CURLOPT_PUT, 1); +#endif switch_curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 1); switch_curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, headers); switch_curl_easy_setopt(curl_handle, CURLOPT_URL, full_url); diff --git a/src/mod/applications/mod_http_cache/mod_http_cache.c b/src/mod/applications/mod_http_cache/mod_http_cache.c index 9d19099e56..365ba27425 100644 --- a/src/mod/applications/mod_http_cache/mod_http_cache.c +++ b/src/mod/applications/mod_http_cache/mod_http_cache.c @@ -393,7 +393,9 @@ static switch_status_t http_put(url_cache_t *cache, http_profile_t *profile, swi goto done; } switch_curl_easy_setopt(curl_handle, CURLOPT_UPLOAD, 1); +#if !defined(LIBCURL_VERSION_NUM) || (LIBCURL_VERSION_NUM < 0x070c01) switch_curl_easy_setopt(curl_handle, CURLOPT_PUT, 1); +#endif switch_curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 1); switch_curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, headers); switch_curl_easy_setopt(curl_handle, CURLOPT_URL, full_url); diff --git a/src/switch_curl.c b/src/switch_curl.c index c99a5f61de..2612faa0da 100644 --- a/src/switch_curl.c +++ b/src/switch_curl.c @@ -58,11 +58,19 @@ SWITCH_DECLARE(void) switch_curl_destroy(void) curl_global_cleanup(); } +#if defined(LIBCURL_VERSION_NUM) && (LIBCURL_VERSION_NUM >= 0x073800) +SWITCH_DECLARE(switch_status_t) switch_curl_process_form_post_params(switch_event_t *event, switch_CURL *curl_handle, curl_mime **mimep) +#else SWITCH_DECLARE(switch_status_t) switch_curl_process_form_post_params(switch_event_t *event, switch_CURL *curl_handle, struct curl_httppost **formpostp) +#endif { - +#if defined(LIBCURL_VERSION_NUM) && (LIBCURL_VERSION_NUM >= 0x073800) + curl_mime *mime = NULL; + curl_mimepart *part = NULL; +#else struct curl_httppost *formpost=NULL; struct curl_httppost *lastptr=NULL; +#endif switch_event_header_t *hp; int go = 0; @@ -77,6 +85,9 @@ SWITCH_DECLARE(switch_status_t) switch_curl_process_form_post_params(switch_even return SWITCH_STATUS_FALSE; } +#if defined(LIBCURL_VERSION_NUM) && (LIBCURL_VERSION_NUM >= 0x073800) + mime = curl_mime_init(curl_handle); +#endif for (hp = event->headers; hp; hp = hp->next) { if (!strncasecmp(hp->name, "attach_file:", 12)) { @@ -87,26 +98,42 @@ SWITCH_DECLARE(switch_status_t) switch_curl_process_form_post_params(switch_even if (fname) { *fname++ = '\0'; +#if defined(LIBCURL_VERSION_NUM) && (LIBCURL_VERSION_NUM >= 0x073800) + part = curl_mime_addpart(mime); + curl_mime_name(part, pname); + curl_mime_filename(part, fname); + curl_mime_filedata(part, hp->value); +#else curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, pname, CURLFORM_FILENAME, fname, CURLFORM_FILE, hp->value, CURLFORM_END); +#endif } free(pname); } } else { +#if defined(LIBCURL_VERSION_NUM) && (LIBCURL_VERSION_NUM >= 0x073800) + part = curl_mime_addpart(mime); + curl_mime_name(part, hp->name); + curl_mime_data(part, hp->value, CURL_ZERO_TERMINATED); +#else curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, hp->name, CURLFORM_COPYCONTENTS, hp->value, CURLFORM_END); - +#endif } } +#if defined(LIBCURL_VERSION_NUM) && (LIBCURL_VERSION_NUM >= 0x073800) + *mimep = mime; +#else *formpostp = formpost; +#endif return SWITCH_STATUS_SUCCESS;