diff --git a/include/curlpp/Form.hpp b/include/curlpp/Form.hpp index c499aee..d7b5fa9 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,51 @@ namespace FormParts private: - const std::string mFilename; + const std::string mFilename; + const std::string mContentType; + const std::string mDisplayFilename; + + }; + + + /** + * 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; }; 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; diff --git a/src/curlpp/Form.cpp b/src/curlpp/Form.cpp index 41e1002..ea6a453 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,12 +188,68 @@ 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); } } +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)