diff options
author | Egor Pugin <egor.pugin@gmail.com> | 2018-07-04 21:07:44 +0300 |
---|---|---|
committer | Egor Pugin <egor.pugin@gmail.com> | 2018-07-04 21:07:44 +0300 |
commit | 06e50ae4f2392adc1b670f2c05b8f016f63fea7a (patch) | |
tree | e43cf26a92c030d8a2db7fe3696f14a3f9142e7d | |
parent | c2d4bdeedd61bbbbf18f2d0f653a669248dcd06f (diff) |
Turn off keep-alive. Correctly escape post data.
-rw-r--r-- | src/net/HttpClient.cpp | 18 |
1 files changed, 17 insertions, 1 deletions
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<HttpReqArg>& 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<char *> 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<HttpReqArg>& 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) |