rename Http:put() to Http:post() and add a separate Http:put()

This commit is contained in:
Tomasz Sowa 2022-11-17 02:47:00 +01:00
parent 1d0956abab
commit 204688464e
2 changed files with 47 additions and 5 deletions

View File

@ -73,6 +73,7 @@ Http & Http::begin()
debug_info = nullptr;
follow_location = true;
verify_ssl_cert = true;
use_post_method = true;
return *this;
}
@ -281,7 +282,7 @@ bool Http::get(const pt::WTextStream & url, pt::WTextStream & out, bool clear_st
}
bool Http::put(const wchar_t * url, const std::string & in, pt::WTextStream & out, bool clear_stream)
bool Http::post_put(const wchar_t * url, const std::string & in, pt::WTextStream & out, bool clear_stream)
{
std::string url_ascii;
pt::TextStream out_stream;
@ -300,9 +301,40 @@ bool Http::put(const wchar_t * url, const std::string & in, pt::WTextStream & ou
}
bool Http::post(const wchar_t * url, const std::string & in, pt::WTextStream & out, bool clear_stream)
{
use_post_method = true;
return post_put(url, in, out, clear_stream);
}
bool Http::post(const std::wstring & url, const std::string & in, pt::WTextStream & out, bool clear_stream)
{
use_post_method = true;
return post_put(url.c_str(), in, out, clear_stream);
}
bool Http::post(const wchar_t * url, pt::WTextStream & in, pt::WTextStream & out, bool clear_stream)
{
std::string in_ascii;
in.to_str(in_ascii);
use_post_method = true;
return post_put(url, in_ascii, out, clear_stream);
}
bool Http::put(const wchar_t * url, const std::string & in, pt::WTextStream & out, bool clear_stream)
{
use_post_method = false;
return post_put(url, in, out, clear_stream);
}
bool Http::put(const std::wstring & url, const std::string & in, pt::WTextStream & out, bool clear_stream)
{
return put(url.c_str(), in, out, clear_stream);
use_post_method = false;
return post_put(url.c_str(), in, out, clear_stream);
}
@ -310,8 +342,8 @@ bool Http::put(const wchar_t * url, pt::WTextStream & in, pt::WTextStream & out,
{
std::string in_ascii;
in.to_str(in_ascii);
return put(url, in_ascii, out, clear_stream);
use_post_method = false;
return post_put(url, in_ascii, out, clear_stream);
}
@ -488,7 +520,11 @@ bool Http::fetch_internal(const char * url, const std::string * in, pt::TextStre
curl_easy_setopt(curl, CURLOPT_READDATA, this);
curl_easy_setopt(curl, CURLOPT_SEEKFUNCTION, fetch_seek_function);
curl_easy_setopt(curl, CURLOPT_SEEKDATA, this);
curl_easy_setopt(curl, CURLOPT_POST, 1);
if( use_post_method )
curl_easy_setopt(curl, CURLOPT_POST, 1);
else
curl_easy_setopt(curl, CURLOPT_PUT, 1);
curl_off_t size = read_function_input->size();
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE, size);

View File

@ -165,6 +165,10 @@ public:
bool get(const std::wstring & url, pt::WTextStream & out, bool clear_stream = true);
bool get(const pt::WTextStream & url, pt::WTextStream & out, bool clear_stream = true);
bool post(const wchar_t * url, const std::string & in, pt::WTextStream & out, bool clear_stream = true);
bool post(const std::wstring & url, const std::string & in, pt::WTextStream & out, bool clear_stream = true);
bool post(const wchar_t * url, pt::WTextStream & in, pt::WTextStream & out, bool clear_stream = true);
bool put(const wchar_t * url, const std::string & in, pt::WTextStream & out, bool clear_stream = true);
bool put(const std::wstring & url, const std::string & in, pt::WTextStream & out, bool clear_stream = true);
bool put(const wchar_t * url, pt::WTextStream & in, pt::WTextStream & out, bool clear_stream = true);
@ -196,6 +200,7 @@ private:
pt::Space * debug_info;
bool follow_location;
bool verify_ssl_cert;
bool use_post_method; /* if false then use put method - used only when calling post() or put() methods */
std::wstring temp_header;
std::string temp_header_ascii;
@ -203,6 +208,7 @@ private:
std::wstring temp_header_value;
std::string temp_header_value_ascii;
bool post_put(const wchar_t * url, const std::string & in, pt::WTextStream & out, bool clear_stream);
bool fetch_internal(const char * url, const std::string * in, pt::TextStream & out);
static size_t fetch_read_function(char * ptr, size_t size, size_t nmemb, void * userdata);
static int fetch_seek_set(Http * http, curl_off_t offset);