From 1a65267627b95776450af57755e27155680c8228 Mon Sep 17 00:00:00 2001 From: David Guillen Fandos Date: Sun, 8 Jul 2018 19:56:12 +0200 Subject: [PATCH 1/3] Add display name to File() as optional 4th argument. This enables showing a different name file that the one read from disk. --- include/curlpp/Form.hpp | 25 ++++++++++++++++++++++++- src/curlpp/Form.cpp | 32 +++++++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/include/curlpp/Form.hpp b/include/curlpp/Form.hpp index c499aee..96b8d18 100644 --- a/include/curlpp/Form.hpp +++ b/include/curlpp/Form.hpp @@ -163,6 +163,17 @@ namespace FormParts const char * filename, const char * contentType); + /** + * initialize a File part. "name" is the name of the field. + * "filename" is the string that holds the filename. + * "contentType" is the MIME type of the file. + * "displayName" is the file name to use in the form field. + */ + File(const char * name, + const char * filename, + const char * contentType, + const char * displayName); + /** * initialize a File part. "name" is the name of the field. * "filename" is the string that holds the filename. @@ -179,6 +190,17 @@ namespace FormParts const std::string & filename, const std::string & contentType); + /** + * initialize a File part. "name" is the name of the field. + * "filename" is the string that holds the filename. + * "contentType" is the MIME type of the file. + * "displayName" is the file name to use in the form field. + */ + File(const std::string & name, + const std::string & filename, + const std::string & contentType, + const std::string & displayName); + virtual ~File(); /** @@ -193,8 +215,9 @@ namespace FormParts private: - const std::string mFilename; + const std::string mFilename; const std::string mContentType; + const std::string mDisplayFilename; }; diff --git a/src/curlpp/Form.cpp b/src/curlpp/Form.cpp index 41e1002..637822b 100644 --- a/src/curlpp/Form.cpp +++ b/src/curlpp/Form.cpp @@ -107,21 +107,36 @@ curlpp::FormPart::FormPart(const std::string & name) curlpp::FormParts::File::File(const char * name, const char * filename) : FormPart(name) , mFilename(filename) + , mDisplayFilename(filename) {} curlpp::FormParts::File::~File() {} -curlpp::FormParts::File::File(const char * name, const char * filename, const char * contentType) +curlpp::FormParts::File::File(const char * name, + const char * filename, + const char * contentType) : FormPart(name) , mFilename(filename) , mContentType(contentType) + , mDisplayFilename(filename) +{} + +curlpp::FormParts::File::File(const char * name, + const char * filename, + const char * contentType, + const char * displayName) + : FormPart(name) + , mFilename(filename) + , mContentType(contentType) + , mDisplayFilename(displayName) {} curlpp::FormParts::File::File(const std::string & name, const std::string & filename) : FormPart(name) , mFilename(filename) + , mDisplayFilename(filename) {} curlpp::FormParts::File::File(const std::string & name, @@ -130,6 +145,17 @@ curlpp::FormParts::File::File(const std::string & name, : FormPart(name) , mFilename(filename) , mContentType(contentType) + , mDisplayFilename(filename) +{} + +curlpp::FormParts::File::File(const std::string & name, + const std::string & filename, + const std::string & contentType, + const std::string & displayName) + : FormPart(name) + , mFilename(filename) + , mContentType(contentType) + , mDisplayFilename(displayName) {} curlpp::FormParts::File * @@ -151,6 +177,8 @@ curlpp::FormParts::File::add(::curl_httppost ** first, mName.c_str(), CURLFORM_FILE, mFilename.c_str(), + CURLFORM_FILENAME, + mDisplayFilename.c_str(), CURLFORM_END ); } else { @@ -160,6 +188,8 @@ curlpp::FormParts::File::add(::curl_httppost ** first, mName.c_str(), CURLFORM_FILE, mFilename.c_str(), + CURLFORM_FILENAME, + mDisplayFilename.c_str(), CURLFORM_CONTENTTYPE, mContentType.c_str(), CURLFORM_END); From 36020bedd3f1387c20ff3800304e5847219fd6ae Mon Sep 17 00:00:00 2001 From: root Date: Sun, 8 Jul 2018 23:42:18 +0200 Subject: [PATCH 2/3] Add inmemory file upload --- include/curlpp/Form.hpp | 42 ++++++++++++++++++++++++++++++++ src/curlpp/Form.cpp | 54 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) diff --git a/include/curlpp/Form.hpp b/include/curlpp/Form.hpp index 96b8d18..d7b5fa9 100644 --- a/include/curlpp/Form.hpp +++ b/include/curlpp/Form.hpp @@ -222,6 +222,48 @@ namespace FormParts }; + /** + * This class is a file post in memory. It will send a file in the + * HTTP post whose contents are stored in memory. + */ + + class MemFile : public FormPart + { + + public: + + /** + * initialize a File part. "name" is the name of the field. + * "content" is the string that holds the contents of the file. + * "contentType" is the MIME type of the file. + * "displayName" is the file name to use in the form field. + */ + MemFile(const std::string & name, + const std::string & content, + const std::string & contentType, + const std::string & displayName); + + virtual ~MemFile(); + + /** + * This function will return a copy of the instance. + */ + virtual MemFile * clone() const; + + private: + + void add(::curl_httppost ** first, + ::curl_httppost ** last); + + private: + + const std::string mContent; + const std::string mContentType; + const std::string mDisplayFilename; + + }; + + /** * This class is a file post. It will send a file in the * HTTP post. diff --git a/src/curlpp/Form.cpp b/src/curlpp/Form.cpp index 637822b..ea6a453 100644 --- a/src/curlpp/Form.cpp +++ b/src/curlpp/Form.cpp @@ -196,6 +196,60 @@ curlpp::FormParts::File::add(::curl_httppost ** first, } } +curlpp::FormParts::MemFile::MemFile(const std::string & name, + const std::string & content, + const std::string & contentType, + const std::string & displayName) + : FormPart(name) + , mContent(content) + , mContentType(contentType) + , mDisplayFilename(displayName) +{} + +curlpp::FormParts::MemFile::~MemFile() +{} + +curlpp::FormParts::MemFile * +curlpp::FormParts::MemFile::clone() const +{ + return new curlpp::FormParts::MemFile(* this); +} + +void +curlpp::FormParts::MemFile::add(::curl_httppost ** first, + ::curl_httppost ** last) +{ + // One instance = One curl_httppost, so we don't + // need to duplicate the memory. + if(mContentType.empty()) { + curl_formadd(first, + last, + CURLFORM_PTRNAME, + mName.c_str(), + CURLFORM_BUFFERPTR, + mContent.c_str(), + CURLFORM_BUFFERLENGTH, + mContent.size(), + CURLFORM_FILENAME, + mDisplayFilename.c_str(), + CURLFORM_END ); + } + else { + curl_formadd(first, + last, + CURLFORM_PTRNAME, + mName.c_str(), + CURLFORM_BUFFERPTR, + mContent.c_str(), + CURLFORM_BUFFERLENGTH, + mContent.size(), + CURLFORM_FILENAME, + mDisplayFilename.c_str(), + CURLFORM_CONTENTTYPE, + mContentType.c_str(), + CURLFORM_END); + } +} curlpp::FormParts::Content::Content(const char * name, const char * content) From b58ce45a2ba07bb25460a1619e6794fb3c6f08ab Mon Sep 17 00:00:00 2001 From: David Guillen Fandos Date: Tue, 10 Jul 2018 22:31:40 +0200 Subject: [PATCH 3/3] Add Username/Password options --- include/curlpp/Options.hpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/curlpp/Options.hpp b/include/curlpp/Options.hpp index c705c9d..b9199a8 100644 --- a/include/curlpp/Options.hpp +++ b/include/curlpp/Options.hpp @@ -198,7 +198,9 @@ namespace options typedef curlpp::OptionTrait Netrc; typedef curlpp::OptionTrait NetrcFile; + typedef curlpp::OptionTrait Password; typedef curlpp::OptionTrait UserPwd; + typedef curlpp::OptionTrait Username; typedef curlpp::OptionTrait ProxyUserPwd; typedef curlpp::OptionTrait HttpAuth; typedef curlpp::OptionTrait ProxyAuth;