From 8d04f7e9ea8896f0e2e5b85cc8b86ef15a4d74aa Mon Sep 17 00:00:00 2001 From: tastytea Date: Sat, 11 Jan 2020 14:27:10 +0100 Subject: [PATCH] Add support for sending files in HTTP forms. --- include/curl_wrapper.hpp | 6 +++--- include/mastodonpp.hpp | 5 ++++- src/curl_wrapper.cpp | 10 +++++++++- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/include/curl_wrapper.hpp b/include/curl_wrapper.hpp index 8523057..44f3b0d 100644 --- a/include/curl_wrapper.hpp +++ b/include/curl_wrapper.hpp @@ -297,9 +297,9 @@ private: /*! * @brief Add `*curl_mimepart` to `*curl_mime`. * - * @param mime Initialized `*curl_mime`. - * @param name Name of the field. - * @param data Data of the field, or \@filename. + * @param mime Initialized `*curl_mime`. @param name Name of the field. + * @param data Data of the field. If it begins with `\@file:, the + * rest of the ergument is treated as a filename. * * @since 0.1.1 */ diff --git a/include/mastodonpp.hpp b/include/mastodonpp.hpp index 9d00369..a696eac 100644 --- a/include/mastodonpp.hpp +++ b/include/mastodonpp.hpp @@ -69,7 +69,10 @@ * * @subsection input Input * - * All text input is expected to be UTF-8. + * * All text input is expected to be UTF-8. + * + * * To send a file, use “\@file:” followed by the file name as value + * in the @link mastodonpp::parametermap parametermap@endlink. * * @section exceptions Exceptions * diff --git a/src/curl_wrapper.cpp b/src/curl_wrapper.cpp index 4853628..047b799 100644 --- a/src/curl_wrapper.cpp +++ b/src/curl_wrapper.cpp @@ -354,7 +354,15 @@ void CURLWrapper::add_mime_part(curl_mime *mime, throw CURLException{code, "Could not build HTTP form."}; } - code = curl_mime_data(part, data.data(), CURL_ZERO_TERMINATED); + if (data.substr(0, 6) == "@file:") + { + const string_view filename{data.substr(6)}; + code = curl_mime_filedata(part, filename.data()); + } + else + { + code = curl_mime_data(part, data.data(), CURL_ZERO_TERMINATED); + } if (code != CURLE_OK) { throw CURLException{code, "Could not build HTTP form."};