From 95f86f4ce18e65d45894d5a130c4bdaf8ecbaac5 Mon Sep 17 00:00:00 2001 From: Egor Pugin Date: Wed, 4 Jul 2018 19:11:03 +0300 Subject: Implement CurlHttpClient. --- src/net/HttpClient.cpp | 66 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 63 insertions(+), 3 deletions(-) (limited to 'src/net/HttpClient.cpp') diff --git a/src/net/HttpClient.cpp b/src/net/HttpClient.cpp index 340eb1e..479b72a 100644 --- a/src/net/HttpClient.cpp +++ b/src/net/HttpClient.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2015 Oleg Morozenkov + * Copyright (c) 2018 Egor Pugin * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -30,12 +31,12 @@ using namespace boost::asio::ip; namespace TgBot { -HttpClient& HttpClient::getInstance() { - static HttpClient result; +BoostHttpClient& BoostHttpClient::getInstance() { + static BoostHttpClient result; return result; } -string HttpClient::makeRequest(const Url& url, const vector& args) { +string BoostHttpClient::makeRequest(const Url& url, const vector& args) const { ssl::context context(ssl::context::sslv23); context.set_default_verify_paths(); @@ -87,4 +88,63 @@ string HttpClient::makeRequest(const Url& url, const vector& args) { return HttpParser::getInstance().parseResponse(response); } +#ifdef HAVE_CURL + +CurlHttpClient::CurlHttpClient() { + curlSettings = curl_easy_init(); +} + +CurlHttpClient::~CurlHttpClient() { + curl_easy_cleanup(curlSettings); +} + +CurlHttpClient& CurlHttpClient::getInstance() { + static CurlHttpClient result; + return result; +} + +static size_t curl_write_string(char *ptr, size_t size, size_t nmemb, void *userdata) +{ + std::string &s = *(std::string *)userdata; + auto read = size * nmemb; + s.append(ptr, ptr + read); + return read; +}; + +string CurlHttpClient::makeRequest(const Url& url, const vector& args) const { + // Copy settings for each call because we change CURLOPT_URL and other stuff. + // This also protects multithreaded case. + auto curl = curl_easy_duphandle(curlSettings); + + auto u = url.protocol + "://" + url.host + url.path; + curl_easy_setopt(curl, CURLOPT_URL, u.c_str()); + + if (!args.empty()) + { + std::string data; + for (auto &a : args) + data += a.name + "=" + a.value + "&"; + data.resize(data.size() - 1); + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str()); + } + + std::string response; + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_string); + + auto res = curl_easy_perform(curl); + long http_code; + curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code); + curl_easy_cleanup(curl); + + if (res != CURLE_OK) + throw std::runtime_error("curl error: "s + curl_easy_strerror(res)); + if (http_code != 200) + throw std::runtime_error("curl request returned with code = " + std::to_string(http_code)); + + return HttpParser::getInstance().parseResponse(response); +} + +#endif + } -- cgit v1.2.3 From ac3e87822afb6065a82dfe547ecbc0c307a04147 Mon Sep 17 00:00:00 2001 From: Egor Pugin Date: Wed, 4 Jul 2018 19:27:20 +0300 Subject: Remove string literal. --- src/net/HttpClient.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/net/HttpClient.cpp') diff --git a/src/net/HttpClient.cpp b/src/net/HttpClient.cpp index 479b72a..b61285c 100644 --- a/src/net/HttpClient.cpp +++ b/src/net/HttpClient.cpp @@ -32,7 +32,7 @@ using namespace boost::asio::ip; namespace TgBot { BoostHttpClient& BoostHttpClient::getInstance() { - static BoostHttpClient result; + static BoostHttpClient result; return result; } @@ -78,7 +78,7 @@ string BoostHttpClient::makeRequest(const Url& url, const vector& ar #else char buff[1024]; #endif //TGBOT_CHANGE_READ_BUFFER_SIZE - + boost::system::error_code error; while (!error) { size_t bytes = read(socket, buffer(buff), error); @@ -138,7 +138,7 @@ string CurlHttpClient::makeRequest(const Url& url, const vector& arg curl_easy_cleanup(curl); if (res != CURLE_OK) - throw std::runtime_error("curl error: "s + curl_easy_strerror(res)); + throw std::runtime_error(std::string("curl error: ") + curl_easy_strerror(res)); if (http_code != 200) throw std::runtime_error("curl request returned with code = " + std::to_string(http_code)); -- cgit v1.2.3 From d7ea724622bb2fc91d977eea5ad063acf26ae104 Mon Sep 17 00:00:00 2001 From: Egor Pugin Date: Wed, 4 Jul 2018 19:43:57 +0300 Subject: Fix error message. --- src/net/HttpClient.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/net/HttpClient.cpp') diff --git a/src/net/HttpClient.cpp b/src/net/HttpClient.cpp index b61285c..90dc4b2 100644 --- a/src/net/HttpClient.cpp +++ b/src/net/HttpClient.cpp @@ -140,7 +140,7 @@ string CurlHttpClient::makeRequest(const Url& url, const vector& arg if (res != CURLE_OK) throw std::runtime_error(std::string("curl error: ") + curl_easy_strerror(res)); if (http_code != 200) - throw std::runtime_error("curl request returned with code = " + std::to_string(http_code)); + throw std::runtime_error("http request returned with code = " + std::to_string(http_code)); return HttpParser::getInstance().parseResponse(response); } -- cgit v1.2.3 From c2d4bdeedd61bbbbf18f2d0f653a669248dcd06f Mon Sep 17 00:00:00 2001 From: Egor Pugin Date: Wed, 4 Jul 2018 20:22:21 +0300 Subject: Don't destroy post data early. --- src/net/HttpClient.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/net/HttpClient.cpp') diff --git a/src/net/HttpClient.cpp b/src/net/HttpClient.cpp index 90dc4b2..0f64e16 100644 --- a/src/net/HttpClient.cpp +++ b/src/net/HttpClient.cpp @@ -119,9 +119,9 @@ 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()); + std::string data; if (!args.empty()) { - std::string data; for (auto &a : args) data += a.name + "=" + a.value + "&"; data.resize(data.size() - 1); -- cgit v1.2.3 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 From cea20d4078f2088dea0dd589f1cc9dd7ee22461b Mon Sep 17 00:00:00 2001 From: Egor Pugin Date: Wed, 4 Jul 2018 22:43:01 +0300 Subject: Remove error on bad http request. It is handled by Api. --- src/net/HttpClient.cpp | 4 ---- 1 file changed, 4 deletions(-) (limited to 'src/net/HttpClient.cpp') diff --git a/src/net/HttpClient.cpp b/src/net/HttpClient.cpp index a72890f..528d988 100644 --- a/src/net/HttpClient.cpp +++ b/src/net/HttpClient.cpp @@ -145,8 +145,6 @@ string CurlHttpClient::makeRequest(const Url& url, const vector& arg curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_string); 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); @@ -155,8 +153,6 @@ string CurlHttpClient::makeRequest(const Url& url, const vector& arg if (res != CURLE_OK) throw std::runtime_error(std::string("curl error: ") + curl_easy_strerror(res)); - if (http_code != 200) - throw std::runtime_error("http request returned with code = " + std::to_string(http_code)); return HttpParser::getInstance().parseResponse(response); } -- cgit v1.2.3 From d47ee877be5d1175bdc36f2d87881ddaf875a8e9 Mon Sep 17 00:00:00 2001 From: Oleg Morozenkov Date: Mon, 23 Jul 2018 01:56:42 +0300 Subject: Refactor http clients, fix webhook server, add more samples, change tabs to 4 spaces --- src/net/HttpClient.cpp | 162 ------------------------------------------------- 1 file changed, 162 deletions(-) delete mode 100644 src/net/HttpClient.cpp (limited to 'src/net/HttpClient.cpp') diff --git a/src/net/HttpClient.cpp b/src/net/HttpClient.cpp deleted file mode 100644 index 528d988..0000000 --- a/src/net/HttpClient.cpp +++ /dev/null @@ -1,162 +0,0 @@ -/* - * Copyright (c) 2015 Oleg Morozenkov - * Copyright (c) 2018 Egor Pugin - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -#include "tgbot/net/HttpClient.h" - -#include - -using namespace std; -using namespace boost::asio; -using namespace boost::asio::ip; - -namespace TgBot { - -BoostHttpClient& BoostHttpClient::getInstance() { - static BoostHttpClient result; - return result; -} - -string BoostHttpClient::makeRequest(const Url& url, const vector& args) const { - ssl::context context(ssl::context::sslv23); - context.set_default_verify_paths(); - - ssl::stream socket(_ioService, context); - tcp::resolver resolver(_ioService); - tcp::resolver::query query(url.host, url.protocol); - - connect(socket.lowest_layer(), resolver.resolve(query)); - - #ifdef TGBOT_DISABLE_NAGLES_ALGORITHM - socket.lowest_layer().set_option(tcp::no_delay(true)); - #endif //TGBOT_DISABLE_NAGLES_ALGORITHM - - #ifdef TGBOT_CHANGE_SOCKET_BUFFER_SIZE - #if _WIN64 || __amd64__ || __x86_64__ || __MINGW64__ || __aarch64__ || __powerpc64__ - socket.lowest_layer().set_option(socket_base::send_buffer_size(65536)); - socket.lowest_layer().set_option(socket_base::receive_buffer_size(65536)); - #else //for 32-bit - socket.lowest_layer().set_option(socket_base::send_buffer_size(32768)); - socket.lowest_layer().set_option(socket_base::receive_buffer_size(32768)); - #endif //Processor architecture - #endif //TGBOT_CHANGE_SOCKET_BUFFER_SIZE - - socket.set_verify_mode(ssl::verify_none); - socket.set_verify_callback(ssl::rfc2818_verification(url.host)); - socket.handshake(ssl::stream::client); - - string requestText = HttpParser::getInstance().generateRequest(url, args, false); - write(socket, buffer(requestText.c_str(), requestText.length())); - - string response; - - #ifdef TGBOT_CHANGE_READ_BUFFER_SIZE - #if _WIN64 || __amd64__ || __x86_64__ || __MINGW64__ || __aarch64__ || __powerpc64__ - char buff[65536]; - #else //for 32-bit - char buff[32768]; - #endif //Processor architecture - #else - char buff[1024]; - #endif //TGBOT_CHANGE_READ_BUFFER_SIZE - - boost::system::error_code error; - while (!error) { - size_t bytes = read(socket, buffer(buff), error); - response += string(buff, bytes); - } - - return HttpParser::getInstance().parseResponse(response); -} - -#ifdef HAVE_CURL - -CurlHttpClient::CurlHttpClient() { - curlSettings = curl_easy_init(); -} - -CurlHttpClient::~CurlHttpClient() { - curl_easy_cleanup(curlSettings); -} - -CurlHttpClient& CurlHttpClient::getInstance() { - static CurlHttpClient result; - return result; -} - -static size_t curl_write_string(char *ptr, size_t size, size_t nmemb, void *userdata) -{ - std::string &s = *(std::string *)userdata; - auto read = size * nmemb; - s.append(ptr, ptr + read); - return read; -}; - -string CurlHttpClient::makeRequest(const Url& url, const vector& args) const { - // Copy settings for each call because we change CURLOPT_URL and other stuff. - // This also protects multithreaded case. - auto curl = curl_easy_duphandle(curlSettings); - - 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) - { - 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; - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_string); - - auto res = curl_easy_perform(curl); - 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)); - - return HttpParser::getInstance().parseResponse(response); -} - -#endif - -} -- cgit v1.2.3