diff options
author | Oleg Morozenkov <omorozenkov@gmail.com> | 2015-07-29 14:45:45 +0300 |
---|---|---|
committer | Oleg Morozenkov <omorozenkov@gmail.com> | 2015-07-29 14:45:45 +0300 |
commit | 99072def67e54d664edd96b9c0f124c4f09cedee (patch) | |
tree | b34ce972e87686b27f6d66ffaa31b079aa0b52d7 /src | |
parent | f69b2ac4ff123e0fb8b335fe28f6de4242c4f396 (diff) |
Fix includes + fix some minor bugs
Diffstat (limited to 'src')
-rw-r--r-- | src/Api.cpp (renamed from src/tgbot/Api.cpp) | 32 | ||||
-rw-r--r-- | src/TgException.cpp (renamed from src/tgbot/TgException.cpp) | 2 | ||||
-rw-r--r-- | src/TgTypeParser.cpp (renamed from src/tgbot/TgTypeParser.cpp) | 2 | ||||
-rw-r--r-- | src/net/HttpClient.cpp (renamed from src/tgbot/net/HttpClient.cpp) | 6 | ||||
-rw-r--r-- | src/net/HttpParser.cpp (renamed from src/tgbot/net/HttpParser.cpp) | 2 | ||||
-rw-r--r-- | src/net/TgLongPoll.cpp (renamed from src/tgbot/net/TgLongPoll.cpp) | 2 | ||||
-rw-r--r-- | src/net/Url.cpp (renamed from src/tgbot/net/Url.cpp) | 9 | ||||
-rw-r--r-- | src/tools/StringTools.cpp (renamed from src/tgbot/tools/StringTools.cpp) | 6 |
8 files changed, 34 insertions, 27 deletions
diff --git a/src/tgbot/Api.cpp b/src/Api.cpp index 913401b..0c04081 100644 --- a/src/tgbot/Api.cpp +++ b/src/Api.cpp @@ -20,7 +20,7 @@ * SOFTWARE. */ -#include "Api.h" +#include "tgbot/Api.h" #include "tgbot/TgTypeParser.h" #include "tgbot/TgException.h" @@ -94,7 +94,7 @@ Message::Ptr Api::sendPhoto(int32_t chatId, const string& photo, const string& c return TgTypeParser::getInstance().parseMessage(sendRequest("sendPhoto", args).find("result")->second); } -Message::Ptr Api::sendAudio(int32_t chatId, const InputFile::Ptr& audio, int32_t duration = 0, int32_t replyToMessageId, const GenericReply::Ptr& replyMarkup) const { +Message::Ptr Api::sendAudio(int32_t chatId, const InputFile::Ptr& audio, int32_t duration, int32_t replyToMessageId, const GenericReply::Ptr& replyMarkup) const { vector<HttpReqArg> args; args.push_back(HttpReqArg("chat_id", chatId)); args.push_back(HttpReqArg("audio", audio->data, true, audio->mimeType)); @@ -110,7 +110,7 @@ Message::Ptr Api::sendAudio(int32_t chatId, const InputFile::Ptr& audio, int32_t return TgTypeParser::getInstance().parseMessage(sendRequest("sendAudio", args).find("result")->second); } -Message::Ptr Api::sendAudio(int32_t chatId, const string& audio, int32_t duration = 0, int32_t replyToMessageId, const GenericReply::Ptr& replyMarkup) const { +Message::Ptr Api::sendAudio(int32_t chatId, const string& audio, int32_t duration, int32_t replyToMessageId, const GenericReply::Ptr& replyMarkup) const { vector<HttpReqArg> args; args.push_back(HttpReqArg("chat_id", chatId)); args.push_back(HttpReqArg("audio", audio)); @@ -259,17 +259,21 @@ boost::property_tree::ptree Api::sendRequest(const std::string& method, const st std::string url = "https://api.telegram.org/bot"; url += _token; url += "/"; - url += method; - try { - ptree result = TgTypeParser::getInstance().parseJson(HttpClient::getInstance().makeRequest(url, args)); - if (result.get<bool>("ok")) { - return result; - } else { - throw TgException(result.get("description", "")); - } - } catch (boost::property_tree::ptree_error& e) { - return ptree(); - } + url += method; + string serverResponse = HttpClient::getInstance().makeRequest(url, args); + if (serverResponse.find("<html>") != serverResponse.npos) { + throw TgException("Bad request"); + } + ptree result = TgTypeParser::getInstance().parseJson(serverResponse); + try { + if (result.get<bool>("ok")) { + return result; + } else { + throw TgException(result.get("description", "")); + } + } catch (boost::property_tree::ptree_error& e) { + throw TgException(""); + } } } diff --git a/src/tgbot/TgException.cpp b/src/TgException.cpp index 2c8263e..0c118ec 100644 --- a/src/tgbot/TgException.cpp +++ b/src/TgException.cpp @@ -20,7 +20,7 @@ * SOFTWARE. */ -#include "TgException.h" +#include "tgbot/TgException.h" namespace TgBot { diff --git a/src/tgbot/TgTypeParser.cpp b/src/TgTypeParser.cpp index 70588da..9a1f93a 100644 --- a/src/tgbot/TgTypeParser.cpp +++ b/src/TgTypeParser.cpp @@ -20,7 +20,7 @@ * SOFTWARE. */ -#include "TgTypeParser.h" +#include "tgbot/TgTypeParser.h" using namespace std; using namespace boost::property_tree; diff --git a/src/tgbot/net/HttpClient.cpp b/src/net/HttpClient.cpp index fefeac5..5027b09 100644 --- a/src/tgbot/net/HttpClient.cpp +++ b/src/net/HttpClient.cpp @@ -20,7 +20,7 @@ * SOFTWARE. */ -#include "HttpClient.h" +#include "tgbot/net/HttpClient.h" #include <boost/asio/ssl.hpp> @@ -46,9 +46,6 @@ string HttpClient::makeRequest(const Url& url, const vector<HttpReqArg>& args) { connect(socket.lowest_layer(), resolver.resolve(query)); -// boost::asio::socket_base::keep_alive keepAliveOption(true); -// socket.lowest_layer().set_option(keepAliveOption); - socket.set_verify_mode(ssl::verify_none); socket.set_verify_callback(ssl::rfc2818_verification(url.host)); socket.handshake(ssl::stream<tcp::socket>::client); @@ -62,7 +59,6 @@ string HttpClient::makeRequest(const Url& url, const vector<HttpReqArg>& args) { while (!error) { size_t bytes = read(socket, buffer(buff), error); response += string(buff, bytes); - printf("%s", string(buff, bytes).c_str()); } return HttpParser::getInstance().parseResponse(response); diff --git a/src/tgbot/net/HttpParser.cpp b/src/net/HttpParser.cpp index 070c630..ae9de62 100644 --- a/src/tgbot/net/HttpParser.cpp +++ b/src/net/HttpParser.cpp @@ -20,7 +20,7 @@ * SOFTWARE. */ -#include "HttpParser.h" +#include "tgbot/net/HttpParser.h" #include <boost/algorithm/string.hpp> diff --git a/src/tgbot/net/TgLongPoll.cpp b/src/net/TgLongPoll.cpp index 6359ec6..f420e92 100644 --- a/src/tgbot/net/TgLongPoll.cpp +++ b/src/net/TgLongPoll.cpp @@ -20,7 +20,7 @@ * SOFTWARE. */ -#include "TgLongPoll.h" +#include "tgbot/net/TgLongPoll.h" namespace TgBot { diff --git a/src/tgbot/net/Url.cpp b/src/net/Url.cpp index 7424849..c773f3b 100644 --- a/src/tgbot/net/Url.cpp +++ b/src/net/Url.cpp @@ -20,7 +20,9 @@ * SOFTWARE. */ -#include "Url.h" +#include "tgbot/net/Url.h" + +#include "tgbot/tools/StringTools.h" using namespace std; @@ -73,6 +75,11 @@ Url::Url(const string& url) { fragment += c; } } + + host = StringTools::urlEncode(host, "."); + path = StringTools::urlEncode(path, "/"); + query = StringTools::urlEncode(query, "&"); + fragment = StringTools::urlEncode(fragment); } } diff --git a/src/tgbot/tools/StringTools.cpp b/src/tools/StringTools.cpp index de3bbb8..188e71c 100644 --- a/src/tgbot/tools/StringTools.cpp +++ b/src/tools/StringTools.cpp @@ -20,7 +20,7 @@ * SOFTWARE. */ -#include "StringTools.h" +#include "tgbot/tools/StringTools.h" #include <stdlib.h> #include <iomanip> @@ -78,13 +78,13 @@ string generateRandomString(size_t length) { return result; } -string urlEncode(const string& value) { +string urlEncode(const string& value, const std::string additionalLegitChars) { static const string legitPunctuation = "-_.~"; ostringstream result; result.fill('0'); result << hex; for (const char& c : value) { - if (isalnum(c) || legitPunctuation.find(c) != legitPunctuation.npos) { + if (isalnum(c) || legitPunctuation.find(c) != legitPunctuation.npos || additionalLegitChars.find(c) != additionalLegitChars.npos) { result << c; } else { result << '%' << setw(2) << int((unsigned char) c); |