diff options
author | Oleg Morozenkov <m@oleg.rocks> | 2021-04-06 00:58:34 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-06 00:58:34 +0300 |
commit | a0d31e277f0b62d24b5ee1777c07c7c942b92e1b (patch) | |
tree | 882ea522bf30ca36749a0456176dc16aa372ae51 | |
parent | f2d7dc110d3ba37914298bd073500396fcf93967 (diff) | |
parent | a7c75fdc8177d2fa6ed13293945a7d80d32cdbad (diff) |
Merge pull request #165 from MPSMPS/master
Fixes https://github.com/reo7sp/tgbot-cpp/issues/162
-rw-r--r-- | src/net/BoostHttpOnlySslClient.cpp | 26 | ||||
-rw-r--r-- | src/net/CurlHttpClient.cpp | 3 | ||||
-rw-r--r-- | src/tools/StringTools.cpp | 3 |
3 files changed, 31 insertions, 1 deletions
diff --git a/src/net/BoostHttpOnlySslClient.cpp b/src/net/BoostHttpOnlySslClient.cpp index 301d0c8..6e59649 100644 --- a/src/net/BoostHttpOnlySslClient.cpp +++ b/src/net/BoostHttpOnlySslClient.cpp @@ -48,6 +48,32 @@ string BoostHttpOnlySslClient::makeRequest(const Url& url, const vector<HttpReqA string requestText = _httpParser.generateRequest(url, args, false); write(socket, buffer(requestText.c_str(), requestText.length())); + fd_set fileDescriptorSet; + struct timeval timeStruct; + + // set the timeout to 20 seconds + timeStruct.tv_sec = 20; + timeStruct.tv_usec = 0; + FD_ZERO(&fileDescriptorSet); + + // We'll need to get the underlying native socket for this select call, in order + // to add a simple timeout on the read: + + int nativeSocket = socket.lowest_layer().native_handle(); + + FD_SET(nativeSocket,&fileDescriptorSet); + select(nativeSocket+1,&fileDescriptorSet,NULL,NULL,&timeStruct); + + if(!FD_ISSET(nativeSocket,&fileDescriptorSet)){ // timeout + + std::string sMsg("TIMEOUT on read client data. Client IP: "); + + sMsg.append(socket.next_layer().remote_endpoint().address().to_string()); + _ioService.reset(); + + throw std::exception(); + } + string response; #ifdef TGBOT_CHANGE_READ_BUFFER_SIZE diff --git a/src/net/CurlHttpClient.cpp b/src/net/CurlHttpClient.cpp index 6dc43f8..13973cc 100644 --- a/src/net/CurlHttpClient.cpp +++ b/src/net/CurlHttpClient.cpp @@ -11,6 +11,9 @@ namespace TgBot { CurlHttpClient::CurlHttpClient() : _httpParser() { curlSettings = curl_easy_init(); + + curl_easy_setopt(curlSettings, CURLOPT_CONNECTTIMEOUT, 20); + curl_easy_setopt(curlSettings, CURLOPT_TIMEOUT, 25); } CurlHttpClient::~CurlHttpClient() { diff --git a/src/tools/StringTools.cpp b/src/tools/StringTools.cpp index 8b8f4da..653047c 100644 --- a/src/tools/StringTools.cpp +++ b/src/tools/StringTools.cpp @@ -56,7 +56,8 @@ void split(const string& str, char delimiter, vector<string>& dest) { } string generateRandomString(std::size_t length) { - static const string chars("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890-=[]\\;',./!@#$%^&*()_+{}|:\"<>?`~"); + static const string chars("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890-=[]\\',./!@#$%^&*()_+{}|:\"<>?`~"); + static const std::size_t charsLen = chars.length(); string result; |