Skip to content
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
12 changes: 7 additions & 5 deletions tinycurl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

TinyCurl::TinyCurl(const std::string &url)
{
curl_global_init(CURL_GLOBAL_ALL);
m_handle = curl_easy_init();
if ( m_handle == NULL )
throw Exception("Unable to initialize curl handler");
Expand All @@ -17,23 +18,24 @@ TinyCurl::TinyCurl(const std::string &url)
m_url = url;
}

TinyCurl::~TinyCurl()
TinyCurl::~TinyCurl() noexcept
{
curl_easy_cleanup(m_handle);
curl_slist_free_all(opt_list);
curl_global_cleanup();
}

void TinyCurl::setHTTPHeaders(const headers_t &hdrs) const
void TinyCurl::setHTTPHeaders(const headers_t &hdrs)
{
if (! hdrs.empty())
{
struct curl_slist *chunk = NULL;
for (headers_t::const_iterator i = hdrs.begin() ; i != hdrs.end() ; ++i) {
std::string h(i->first);
h += ": ";
h += i->second;
chunk = curl_slist_append(chunk, h.c_str());
opt_list = curl_slist_append(opt_list, h.c_str());
}
CURLcode res = curl_easy_setopt(m_handle, CURLOPT_HTTPHEADER, chunk);
CURLcode res = curl_easy_setopt(m_handle, CURLOPT_HTTPHEADER, opt_list);
if ( res != CURLE_OK)
throw Exception(res);
}
Expand Down
5 changes: 3 additions & 2 deletions tinycurl.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class TinyCurl
};

TinyCurl(const std::string &url);
~TinyCurl();
~TinyCurl() noexcept;

typedef std::map<std::string,std::string> headers_t;

Expand All @@ -58,12 +58,13 @@ class TinyCurl
TinyCurl(const TinyCurl &);

void setOptions();
void setHTTPHeaders(const headers_t &hdrs) const;
void setHTTPHeaders(const headers_t &hdrs);

void curlPerform();

CURL *m_handle;
std::string m_url;
std::string m_data;
std::string m_upload_data;
struct curl_slist *opt_list = NULL;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this will work with C++ 98

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no C++98 compiler at hand, but since this is libcurl interface,which is plain old C, maybe its worth a try...

};