From 06e50ae4f2392adc1b670f2c05b8f016f63fea7a Mon Sep 17 00:00:00 2001 From: Egor Pugin Date: Wed, 4 Jul 2018 21:07:44 +0300 Subject: Turn off keep-alive. Correctly escape post data. --- src/net/HttpClient.cpp | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'src/net/HttpClient.cpp') diff --git a/src/net/HttpClient.cpp b/src/net/HttpClient.cpp index 0f64e16..a72890f 100644 --- a/src/net/HttpClient.cpp +++ b/src/net/HttpClient.cpp @@ -119,13 +119,25 @@ string CurlHttpClient::makeRequest(const Url& url, const vector& arg auto u = url.protocol + "://" + url.host + url.path; curl_easy_setopt(curl, CURLOPT_URL, u.c_str()); + // disable keep-alive + struct curl_slist *headers = NULL; + headers = curl_slist_append(headers, "Connection: close"); + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + std::string data; + std::vector escaped; if (!args.empty()) { for (auto &a : args) - data += a.name + "=" + a.value + "&"; + { + escaped.push_back(curl_easy_escape(curl, a.name.c_str(), a.name.size())); + data += escaped.back() + std::string("="); + escaped.push_back(curl_easy_escape(curl, a.value.c_str(), a.value.size())); + data += escaped.back() + std::string("&"); + } data.resize(data.size() - 1); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str()); + curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)data.size()); } std::string response; @@ -135,8 +147,12 @@ string CurlHttpClient::makeRequest(const Url& url, const vector& arg auto res = curl_easy_perform(curl); long http_code; curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code); + curl_slist_free_all(headers); curl_easy_cleanup(curl); + for (auto &e : escaped) + curl_free(e); + if (res != CURLE_OK) throw std::runtime_error(std::string("curl error: ") + curl_easy_strerror(res)); if (http_code != 200) -- cgit v1.2.3