From 9ffcd708b63ec015534fd90d6791770bfc61a0ee Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sat, 24 Dec 2022 17:47:15 +0200 Subject: [PATCH 12/12] Use libcurl mime API when available See osdn #46345 Signed-off-by: Marko Lindqvist --- configure.ac | 12 ++++++++---- utility/netfile.c | 38 +++++++++++++++++++++++++++++++++++--- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/configure.ac b/configure.ac index 31429e33a0..bc580797db 100644 --- a/configure.ac +++ b/configure.ac @@ -594,10 +594,14 @@ PKG_PROG_PKG_CONFIG if test "x$PKG_CONFIG" = "x" ; then AC_MSG_ERROR([pkg-config not found]) fi -PKG_CHECK_MODULES([CURL], [libcurl >= 7.15.4],, - [AC_MSG_ERROR([libcurl development files required])]) -UTILITY_CFLAGS="${UTILITY_CFLAGS} ${CURL_CFLAGS}" -UTILITY_LIBS="${UTILITY_LIBS} ${CURL_LIBS}" +PKG_CHECK_MODULES([CURL_MIME_API], [libcurl > 7.56.0], + [UTILITY_CFLAGS="${UTILITY_CFLAGS} ${CURL_MIME_API_CFLAGS}" + UTILITY_LIBS="${UTILITY_LIBS} ${CURL_MIME_API_LIBS}" + AC_DEFINE([HAVE_CURL_MIME_API], [1], [libcurl has the mime api])], + [PKG_CHECK_MODULES([CURL], [libcurl >= 7.15.4], + [UTILITY_CFLAGS="${UTILITY_CFLAGS} ${CURL_CFLAGS}" + UTILITY_LIBS="${UTILITY_LIBS} ${CURL_LIBS}"], + [AC_MSG_ERROR([libcurl development files required])])]) AC_ARG_ENABLE([xml-registry], AS_HELP_STRING([--enable-xml-registry], [build xml-backend for registry (WIP)]), diff --git a/utility/netfile.c b/utility/netfile.c index d4cfcc048b..33ac2afeec 100644 --- a/utility/netfile.c +++ b/utility/netfile.c @@ -31,8 +31,12 @@ #include "netfile.h" struct netfile_post { +#ifdef HAVE_CURL_MIME_API + curl_mime *mime; +#else /* HAVE_CURL_MIME_API */ struct curl_httppost *first; struct curl_httppost *last; +#endif /* HAVE_CURL_MIME_API */ }; typedef size_t (*netfile_write_cb)(char *ptr, size_t size, size_t nmemb, void *userdata); @@ -186,7 +190,13 @@ bool netfile_download_file(const char *URL, const char *filename, ***********************************************************************/ struct netfile_post *netfile_start_post(void) { - return fc_calloc(1, sizeof(struct netfile_post)); + struct netfile_post *post = fc_calloc(1, sizeof(struct netfile_post)); + +#ifdef HAVE_CURL_MIME_API + post->mime = curl_mime_init(chandle); +#endif + + return post; } /********************************************************************** @@ -195,10 +205,19 @@ struct netfile_post *netfile_start_post(void) void netfile_add_form_str(struct netfile_post *post, const char *name, const char *val) { +#ifdef HAVE_CURL_MIME_API + curl_mimepart *part; + + part = curl_mime_addpart(post->mime); + + curl_mime_data(part, val, CURL_ZERO_TERMINATED); + curl_mime_name(part, name); +#else /* HAVE_CURL_MIME_API */ curl_formadd(&post->first, &post->last, CURLFORM_COPYNAME, name, CURLFORM_COPYCONTENTS, val, CURLFORM_END); +#endif /* HAVE_CURL_MIME_API */ } /********************************************************************** @@ -218,7 +237,12 @@ void netfile_add_form_int(struct netfile_post *post, ***********************************************************************/ void netfile_close_post(struct netfile_post *post) { +#ifdef HAVE_CURL_MIME_API + curl_mime_free(post->mime); +#else /* HAVE_CURL_MIME_API */ curl_formfree(post->first); +#endif /* HAVE_CURL_MIME_API */ + FC_FREE(post); } @@ -226,7 +250,8 @@ void netfile_close_post(struct netfile_post *post) Dummy write callback used only to make sure curl's default write function does not get used as we don't want reply to stdout ***********************************************************************/ -static size_t dummy_write(void *buffer, size_t size, size_t nmemb, void *userp) +static size_t dummy_write(void *buffer, size_t size, size_t nmemb, + void *userp) { return size * nmemb; } @@ -235,7 +260,8 @@ static size_t dummy_write(void *buffer, size_t size, size_t nmemb, void *userp) Send HTTP POST ***********************************************************************/ bool netfile_send_post(const char *URL, struct netfile_post *post, - FILE *reply_fp, struct netfile_write_cb_data *mem_data, + FILE *reply_fp, + struct netfile_write_cb_data *mem_data, const char *addr) { CURLcode curlret; @@ -248,7 +274,13 @@ bool netfile_send_post(const char *URL, struct netfile_post *post, headers = curl_slist_append(headers,"User-Agent: Freeciv/" VERSION_STRING); curl_easy_setopt(handle, CURLOPT_URL, URL); + +#ifdef HAVE_CURL_MIME_API + curl_easy_setopt(handle, CURLOPT_MIMEPOST, post->mime); +#else /* HAVE_CURL_MIME_API */ curl_easy_setopt(handle, CURLOPT_HTTPPOST, post->first); +#endif /* HAVE_CURL_MIME_API */ + if (mem_data != NULL) { mem_data->mem = NULL; mem_data->size = 0; -- 2.35.1