diff --git a/a8/curl.cc b/a8/curl.cc index b2667e6..7806e60 100644 --- a/a8/curl.cc +++ b/a8/curl.cc @@ -15,7 +15,8 @@ namespace a8 return size * nmemb; } - bool Post(const char* url, const std::string& content, std::string& response, const char* headers) + bool Post(const char* url, const std::string& content, std::string& response, a8::XObject* headers, + int timeout) { CURL *curl = curl_easy_init(); curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10); @@ -23,30 +24,42 @@ namespace a8 curl_easy_setopt(curl, CURLOPT_HEADER, 0); curl_easy_setopt(curl, CURLOPT_HTTPPOST, 1); curl_slist* list = nullptr; - if (headers) { - list = curl_slist_append(list, headers); + if (headers && headers->GetType() == a8::XOT_ARRAY) { + for (int i = 0; i < headers->Size(); ++i) { + list = curl_slist_append(list, headers->At(i)->AsXValue().GetString().c_str()); + } curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list); } curl_easy_setopt(curl, CURLOPT_POSTFIELDS, content.c_str()); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &_ReceiveResponse); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); bool ok = curl_easy_perform(curl) == CURLE_OK; - curl_easy_cleanup(curl); if (list) { curl_slist_free_all(list); } + curl_easy_cleanup(curl); return ok; } - bool Get(const std::string& url, std::string& response, int timeout=10) + bool Get(const std::string& url, std::string& response, a8::XObject* headers, int timeout) { CURL *curl = curl_easy_init(); curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout); curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1); + curl_slist* list = nullptr; + if (headers && headers->GetType() == a8::XOT_ARRAY) { + for (int i = 0; i < headers->Size(); ++i) { + list = curl_slist_append(list, headers->At(i)->AsXValue().GetString().c_str()); + } + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list); + } curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &_ReceiveResponse); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); bool ok = curl_easy_perform(curl) == CURLE_OK; + if (list) { + curl_slist_free_all(list); + } curl_easy_cleanup(curl); return ok; } diff --git a/a8/curl.h b/a8/curl.h index 925da2a..9605968 100644 --- a/a8/curl.h +++ b/a8/curl.h @@ -6,8 +6,8 @@ namespace a8 namespace http { bool Post(const char* url, const std::string& content, std::string& response, - const char* headers = nullptr); - bool Get(const std::string& url, std::string& response, int timeout=10); + a8::XObject* headers = nullptr, int timeout = 10); + bool Get(const std::string& url, std::string& response, a8::XObject* headers = nullptr, int timeout=10); } }