Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add display name to File() as optional 4th argument. #70

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 66 additions & 1 deletion include/curlpp/Form.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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();

/**
Expand All @@ -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;

};

Expand Down
2 changes: 2 additions & 0 deletions include/curlpp/Options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,9 @@ namespace options

typedef curlpp::OptionTrait<long, CURLOPT_NETRC> Netrc;
typedef curlpp::OptionTrait<std::string, CURLOPT_NETRC_FILE> NetrcFile;
typedef curlpp::OptionTrait<std::string, CURLOPT_PASSWORD> Password;
typedef curlpp::OptionTrait<std::string, CURLOPT_USERPWD> UserPwd;
typedef curlpp::OptionTrait<std::string, CURLOPT_USERNAME> Username;
typedef curlpp::OptionTrait<std::string, CURLOPT_PROXYUSERPWD> ProxyUserPwd;
typedef curlpp::OptionTrait<long, CURLOPT_HTTPAUTH> HttpAuth;
typedef curlpp::OptionTrait<long, CURLOPT_PROXYAUTH> ProxyAuth;
Expand Down
86 changes: 85 additions & 1 deletion src/curlpp/Form.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 *
Expand All @@ -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 {
Expand All @@ -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)
Expand Down