diff options
author | Oleg Morozenkov <omorozenkov@gmail.com> | 2015-07-09 15:03:59 +0300 |
---|---|---|
committer | Oleg Morozenkov <omorozenkov@gmail.com> | 2015-07-09 15:03:59 +0300 |
commit | 51d2176d1535c8c8176426909e1c7b70633d794b (patch) | |
tree | 4301e0bb6f93913c53d4f41bdd96ddf1fcff9221 /src/tgbot/net | |
parent | 28ef71aa3c6d9473d4127dd648161a0afa109895 (diff) |
Refactoring
Diffstat (limited to 'src/tgbot/net')
-rw-r--r-- | src/tgbot/net/HttpClient.cpp | 71 | ||||
-rw-r--r-- | src/tgbot/net/HttpClient.h | 49 | ||||
-rw-r--r-- | src/tgbot/net/HttpParser.cpp | 204 | ||||
-rw-r--r-- | src/tgbot/net/HttpParser.h | 68 | ||||
-rw-r--r-- | src/tgbot/net/HttpReqArg.h | 50 | ||||
-rw-r--r-- | src/tgbot/net/HttpServer.h | 93 | ||||
-rw-r--r-- | src/tgbot/net/TgLongPoll.cpp | 43 | ||||
-rw-r--r-- | src/tgbot/net/TgLongPoll.h | 48 | ||||
-rw-r--r-- | src/tgbot/net/TgWebhookLocalServer.h | 47 | ||||
-rw-r--r-- | src/tgbot/net/TgWebhookServer.h | 57 | ||||
-rw-r--r-- | src/tgbot/net/TgWebhookTcpServer.h | 46 | ||||
-rw-r--r-- | src/tgbot/net/Url.cpp | 78 | ||||
-rw-r--r-- | src/tgbot/net/Url.h | 44 |
13 files changed, 898 insertions, 0 deletions
diff --git a/src/tgbot/net/HttpClient.cpp b/src/tgbot/net/HttpClient.cpp new file mode 100644 index 0000000..fefeac5 --- /dev/null +++ b/src/tgbot/net/HttpClient.cpp @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2015 Oleg Morozenkov + * + * 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 "HttpClient.h" + +#include <boost/asio/ssl.hpp> + +using namespace std; +using namespace boost::asio; +using namespace boost::asio::ip; +using namespace boost::asio::local; + +namespace TgBot { + +HttpClient& HttpClient::getInstance() { + static HttpClient result; + return result; +} + +string HttpClient::makeRequest(const Url& url, const vector<HttpReqArg>& args) { + ssl::context context(ssl::context::sslv23); + context.set_default_verify_paths(); + + ssl::stream<tcp::socket> socket(_ioService, context); + tcp::resolver resolver(_ioService); + tcp::resolver::query query(url.host, url.protocol); + + 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); + + string requestText = HttpParser::getInstance().generateRequest(url, args, false); + write(socket, buffer(requestText.c_str(), requestText.length())); + + string response; + char buff[1024]; + boost::system::error_code error; + 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/HttpClient.h b/src/tgbot/net/HttpClient.h new file mode 100644 index 0000000..4b84dc3 --- /dev/null +++ b/src/tgbot/net/HttpClient.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2015 Oleg Morozenkov + * + * 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. + */ + +#ifndef TGBOT_HTTPCLIENT_H +#define TGBOT_HTTPCLIENT_H + +#include <string> + +#include <boost/asio.hpp> + +#include "tgbot/net/Url.h" +#include "tgbot/net/HttpReqArg.h" +#include "tgbot/net/HttpParser.h" + +namespace TgBot { + +class HttpClient { + +public: + static HttpClient& getInstance(); + + std::string makeRequest(const Url& url, const std::vector<HttpReqArg>& args); + +private: + boost::asio::io_service _ioService; +}; + +} + +#endif //TGBOT_HTTPCLIENT_H diff --git a/src/tgbot/net/HttpParser.cpp b/src/tgbot/net/HttpParser.cpp new file mode 100644 index 0000000..070c630 --- /dev/null +++ b/src/tgbot/net/HttpParser.cpp @@ -0,0 +1,204 @@ +/* + * Copyright (c) 2015 Oleg Morozenkov + * + * 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 "HttpParser.h" + +#include <boost/algorithm/string.hpp> + +#include "tgbot/tools/StringTools.h" + +using namespace std; +using namespace boost; + +namespace TgBot { + +HttpParser& HttpParser::getInstance() { + static HttpParser result; + return result; +} + +string HttpParser::generateRequest(const Url& url, const vector<HttpReqArg>& args, bool isKeepAlive) { + string result; + if (args.empty()) { + result += "GET "; + } else { + result += "POST "; + } + result += url.path; + result += url.query.empty() ? "" : "?" + url.query; + result += " HTTP/1.1\r\n"; + result += "Host: "; + result += url.host; + result += "\r\nConnection: "; + if (isKeepAlive) { + result += "keep-alive"; + } else { + result += "close"; + } + result += "\r\n"; + if (args.empty()) { + result += "\r\n"; + } else { + string requestData; + + string bondary = generateMultipartBoundary(args); + if (bondary.empty()) { + result += "Content-Type: application/x-www-form-urlencoded\r\n"; + requestData = generateWwwFormUrlencoded(args); + } else { + result += "Content-Type: multipart/form-data; boundary="; + result += bondary; + result += "\r\n"; + requestData = generateMultipartFormData(args, bondary); + } + + result += "Content-Length: "; + result += lexical_cast<string>(requestData.length()); + result += "\r\n\r\n"; + result += requestData; + } + return result; +} + +string HttpParser::generateMultipartFormData(const vector<HttpReqArg>& args, const string& bondary) { + string result; + for (const HttpReqArg& item : args) { + result += "--"; + result += bondary; + result += "\r\nContent-Disposition: form-data; name=\""; + result += item.name; + result += "\"\r\n"; + if (item.isFile) { + result += "Content-Type: "; + result += item.mimeType; + result += "\r\n"; + } + result += "\r\n"; + result += item.value; + result += "\r\n"; + } + return result; +} + +string HttpParser::generateMultipartBoundary(const vector<HttpReqArg>& args) { + string result; + srand((unsigned int) time(nullptr)); + for (const HttpReqArg& item : args) { + if (item.isFile) { + while (result.empty() || item.value.find(result) != item.value.npos) { + result += StringTools::generateRandomString(4); + } + } + } + return result; +} + +string HttpParser::generateWwwFormUrlencoded(const vector<HttpReqArg>& args) { + string result; + + bool firstRun = true; + for (const HttpReqArg& item : args) { + if (firstRun) { + firstRun = false; + } else { + result += '&'; + } + result += StringTools::urlEncode(item.name); + result += '='; + result += StringTools::urlEncode(item.value); + } + + return result; +} + +string HttpParser::generateResponse(const string& data, const string& mimeType, unsigned short statusCode, const string& statusStr, bool isKeepAlive) { + string result; + result += "HTTP/1.1 "; + result += lexical_cast<string>(statusCode); + result += ' '; + result += statusStr; + result += "\r\nContent-Type: "; + result += mimeType; + result += "\r\nContent-Length: "; + result += lexical_cast<string>(data.length()); + result += "\r\n\r\n"; + result += data; + return result; +} + +string HttpParser::parseHttp(bool isRequest, const string& data, map<string, string>& headers) { + bool onlyNewLineChar = false; + size_t headerEnd = data.find("\r\n\r\n"); + if (headerEnd == data.npos) { + headerEnd = data.find("\n\n"); + if (headerEnd != data.npos) { + onlyNewLineChar = true; + headerEnd += 2; + } + } else { + headerEnd += 4; + } + + size_t lineStart = 0; + size_t lineEnd = 0; + size_t lineSepPos = 0; + size_t lastLineEnd = data.npos; + while (lastLineEnd != lineEnd) { + lastLineEnd = lineEnd; + if (lineEnd == 0) { + if (isRequest) { + lineSepPos = data.find(' '); + lineEnd = data.find(onlyNewLineChar ? "\n" : "\r\n"); + headers["method"] = data.substr(0, lineSepPos); + headers["path"] = data.substr(lineSepPos + 1, data.find(' ', lineSepPos + 1) - lineSepPos - 1); + } else { + lineSepPos = data.find(' '); + lineEnd = data.find(onlyNewLineChar ? "\n" : "\r\n"); + headers["status"] = data.substr(lineSepPos + 1, data.find(' ', lineSepPos + 1) - lineSepPos - 1); + } + } else { + lineStart = lineEnd; + lineStart += onlyNewLineChar ? 1 : 2; + lineEnd = data.find(onlyNewLineChar ? "\n" : "\r\n", lineStart); + lineSepPos = data.find(':', lineStart); + if (lineEnd >= headerEnd || lastLineEnd == lineEnd || lineSepPos >= headerEnd) { + break; + } + headers[to_lower_copy(data.substr(lineStart, lineSepPos - lineStart))] = trim_copy(data.substr(lineSepPos + 1, lineEnd - lineSepPos - 1)); + } + } + + return headerEnd == data.npos ? "" : data.substr(headerEnd); +} + +string HttpParser::parseHttp(bool isRequest, const string& data) { + size_t headerEnd = data.find("\r\n\r\n"); + if (headerEnd == data.npos) { + headerEnd = data.find("\n\n"); + } + if (headerEnd == data.npos) { + headerEnd = 0; + } + return data.substr(headerEnd); +} + +} diff --git a/src/tgbot/net/HttpParser.h b/src/tgbot/net/HttpParser.h new file mode 100644 index 0000000..dcb857b --- /dev/null +++ b/src/tgbot/net/HttpParser.h @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2015 Oleg Morozenkov + * + * 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. + */ + +#ifndef TGBOT_HTTPPARSER_H +#define TGBOT_HTTPPARSER_H + +#include <string> +#include <map> + +#include "tgbot/net/Url.h" +#include "tgbot/net/HttpReqArg.h" + +namespace TgBot { + +class HttpParser { + +public: + static HttpParser& getInstance(); + + std::string generateRequest(const Url& url, const std::vector<HttpReqArg>& args, bool isKeepAlive = false); + std::string generateMultipartFormData(const std::vector<HttpReqArg>& args, const std::string& bondary); + std::string generateMultipartBoundary(const std::vector<HttpReqArg>& args); + std::string generateWwwFormUrlencoded(const std::vector<HttpReqArg>& args); + std::string generateResponse(const std::string& data, const std::string& mimeType = "text/plain", short unsigned statusCode = 200, const std::string& statusStr = "OK", bool isKeepAlive = false); + + inline std::string parseRequest(const std::string& data, std::map<std::string, std::string>& headers) { + return parseHttp(true, data, headers); + } + + inline std::string parseRequest(const std::string& data) { + return parseHttp(true, data); + } + + inline std::string parseResponse(const std::string& data, std::map<std::string, std::string>& headers) { + return parseHttp(false, data, headers); + } + + inline std::string parseResponse(const std::string& data) { + return parseHttp(false, data); + } + +private: + std::string parseHttp(bool isRequest, const std::string& data, std::map<std::string, std::string>& headers); + std::string parseHttp(bool isRequest, const std::string& data); +}; + +} + +#endif //TGBOT_HTTPPARSER_H diff --git a/src/tgbot/net/HttpReqArg.h b/src/tgbot/net/HttpReqArg.h new file mode 100644 index 0000000..e906e94 --- /dev/null +++ b/src/tgbot/net/HttpReqArg.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2015 Oleg Morozenkov + * + * 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. + */ + +#ifndef TGBOT_HTTPPARAMETER_H +#define TGBOT_HTTPPARAMETER_H + +#include <string> + +#include <boost/lexical_cast.hpp> + +namespace TgBot { + +class HttpReqArg { + +public: + template<typename T> + HttpReqArg(const std::string& name, const T& value, bool isFile = false, const std::string& mimeType = "text/plain") : + name(name), value(boost::lexical_cast<std::string>(value)), isFile(isFile), mimeType(mimeType) + { + } + + std::string name; + std::string value; + bool isFile = false; + std::string mimeType; +}; + +} + + +#endif //TGBOT_HTTPPARAMETER_H diff --git a/src/tgbot/net/HttpServer.h b/src/tgbot/net/HttpServer.h new file mode 100644 index 0000000..92a667a --- /dev/null +++ b/src/tgbot/net/HttpServer.h @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2015 Oleg Morozenkov + * + * 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. + */ + +#ifndef TGBOT_HTTPSERVER_H +#define TGBOT_HTTPSERVER_H + +#include <string> + +#include <boost/asio.hpp> + +#include "tgbot/net/HttpParser.h" + +namespace TgBot { + +template<typename Protocol> +class HttpServer { + +private: + class Connection; + +public: + typedef std::function<std::string (const std::string&, const std::map<std::string, std::string>)> ServerHandler; + + HttpServer(std::shared_ptr<boost::asio::basic_socket_acceptor<Protocol>>& acceptor, const ServerHandler& handler) : _acceptor(acceptor), _handler(handler) { + } + + void start() { + std::shared_ptr<boost::asio::basic_stream_socket<Protocol>> socket(new boost::asio::basic_stream_socket<Protocol>(acceptor->get_io_service())); + std::shared_ptr<Connection<Protocol>> connection(new Connection<Protocol>(socket, _handler)); + acceptor->async_accept(*connection->socket, [this, connection]() { + connection->start(); + start(); + }); + _ioService.run(); + } + + void stop() { + _ioService.stop(); + } + +private: + template<typename Protocol> + class Connection { + + public: + Connection(std::shared_ptr<boost::asio::basic_stream_socket<Protocol>>& socket, const ServerHandler& handler) : socket(socket), _handler(handler) { + boost::asio::socket_base::keep_alive option(true); + socket.set_option(option); + } + + void start() { + data.reserve(10240); + socket->async_receive(data, [this]() { + std::map<std::string, std::string> headers; + std::string body = HttpParser::parseResponse(data, headers); + socket->async_send(_handler(body, headers)); + }); + } + + std::shared_ptr<boost::asio::basic_stream_socket<Protocol>> socket; + std::string data; + + private: + const ServerHandler _handler; + }; + + boost::asio::io_service _ioService; + std::shared_ptr<boost::asio::basic_socket_acceptor<Protocol>> _acceptor; + const ServerHandler _handler; +}; + +} + +#endif //TGBOT_HTTPSERVER_H diff --git a/src/tgbot/net/TgLongPoll.cpp b/src/tgbot/net/TgLongPoll.cpp new file mode 100644 index 0000000..6359ec6 --- /dev/null +++ b/src/tgbot/net/TgLongPoll.cpp @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2015 Oleg Morozenkov + * + * 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 "TgLongPoll.h" + +namespace TgBot { + +TgLongPoll::TgLongPoll(const Api* api, const EventHandler* eventHandler) : _api(api), _eventHandler(eventHandler) { +} + +TgLongPoll::TgLongPoll(const Bot& bot) : TgLongPoll(&bot.getApi(), &bot.getEventHandler()) { +} + +void TgLongPoll::start() { + std::vector<Update::Ptr> updates = _api->getUpdates(_lastUpdateId, 100, 60); + for (Update::Ptr& item : updates) { + if (item->updateId >= _lastUpdateId) { + _lastUpdateId = item->updateId + 1; + } + _eventHandler->handleUpdate(item); + } +} + +} diff --git a/src/tgbot/net/TgLongPoll.h b/src/tgbot/net/TgLongPoll.h new file mode 100644 index 0000000..6c5175e --- /dev/null +++ b/src/tgbot/net/TgLongPoll.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2015 Oleg Morozenkov + * + * 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. + */ + +#ifndef TGBOT_TGLONGPOLL_H +#define TGBOT_TGLONGPOLL_H + +#include "tgbot/Bot.h" +#include "tgbot/Api.h" +#include "tgbot/EventHandler.h" + +namespace TgBot { + +class TgLongPoll { + +public: + TgLongPoll(const Api* api, const EventHandler* eventHandler); + TgLongPoll(const Bot& bot); + + void start(); + +private: + int32_t _lastUpdateId = 0; + const Api* _api; + const EventHandler* _eventHandler; +}; + +} + +#endif //TGBOT_TGLONGPOLL_H diff --git a/src/tgbot/net/TgWebhookLocalServer.h b/src/tgbot/net/TgWebhookLocalServer.h new file mode 100644 index 0000000..ec99ad7 --- /dev/null +++ b/src/tgbot/net/TgWebhookLocalServer.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2015 Oleg Morozenkov + * + * 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. + */ + +#ifndef TGBOT_TGWEBHOOKTCPSERVER_H +#define TGBOT_TGWEBHOOKTCPSERVER_H + +#include "tgbot/net/TgWebhookServer.h" + +namespace TgBot { + +class TgWebhookLocalServer : public TgWebhookServer<boost::asio::local::stream_protocol> { + +public: + TgWebhookLocalServer(std::shared_ptr<boost::asio::basic_socket_acceptor<boost::asio::local::stream_protocol>>& acceptor, const std::string& path, EventHandler* eventHandler) = delete; + + TgWebhookLocalServer(const std::string& path, const EventHandler* eventHandler) : + TgWebhookServer(std::shared_ptr<boost::asio::basic_socket_acceptor<boost::asio::local::stream_protocol>>(new boost::asio::local::stream_protocol::acceptor(_ioService, boost::asio::local::stream_protocol::endpoint(path))), path, eventHandler) + { + } + + TgWebhookLocalServer(const std::string& path, const Bot& bot) : TgWebhookLocalServer(path, &bot.getEventHandler()) { + + } +}; + +} + +#endif //TGBOT_TGWEBHOOKTCPSERVER_H diff --git a/src/tgbot/net/TgWebhookServer.h b/src/tgbot/net/TgWebhookServer.h new file mode 100644 index 0000000..a8155ff --- /dev/null +++ b/src/tgbot/net/TgWebhookServer.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2015 Oleg Morozenkov + * + * 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. + */ + +#ifndef TGBOT_TGHTTPSERVER_H +#define TGBOT_TGHTTPSERVER_H + +#include "tgbot/Bot.h" +#include "tgbot/EventHandler.h" +#include "tgbot/TgTypeParser.h" +#include "tgbot/net/HttpServer.h" + +namespace TgBot { + +template<typename Protocol> +class TgWebhookServer : public HttpServer<Protocol> { + +public: + TgWebhookServer(std::shared_ptr<boost::asio::basic_socket_acceptor<Protocol>>& acceptor, const ServerHandler& handler) = delete; + + TgWebhookServer(std::shared_ptr<boost::asio::basic_socket_acceptor<Protocol>>& acceptor, const std::string& path, const EventHandler* eventHandler) : + HttpServer(acceptor, [this, eventHandler, &path](const std::string& data, const std::map<std::string, std::string>& headers) -> std::string { + if (headers["method"] == "POST" && headers["path"] == path) { + eventHandler->handleUpdate(TgTypeParser::getInstance().parseUpdate(TgTypeParser::getInstance().parseJson(data))); + } + return HttpParser::generateResponse(""); + }) + { + } + + TgWebhookServer(std::shared_ptr<boost::asio::basic_socket_acceptor<Protocol>>& acceptor, const std::string& path, const Bot& bot) : + TgWebhookServer(acceptor, path, &bot.getEventHandler()) + { + } +}; + +} + +#endif //TGBOT_TGHTTPSERVER_H diff --git a/src/tgbot/net/TgWebhookTcpServer.h b/src/tgbot/net/TgWebhookTcpServer.h new file mode 100644 index 0000000..4dc58e1 --- /dev/null +++ b/src/tgbot/net/TgWebhookTcpServer.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2015 Oleg Morozenkov + * + * 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. + */ + +#ifndef TGBOT_TGWEBHOOKTCPSERVER_H +#define TGBOT_TGWEBHOOKTCPSERVER_H + +#include "tgbot/net/TgWebhookServer.h" + +namespace TgBot { + +class TgWebhookTcpServer : public TgWebhookServer<boost::asio::ip::tcp> { + +public: + TgWebhookTcpServer(std::shared_ptr<boost::asio::basic_socket_acceptor<boost::asio::ip::tcp>>& acceptor, const std::string& path, EventHandler* eventHandler) = delete; + + TgWebhookTcpServer(unsigned short port, const std::string& path, const EventHandler* eventHandler) : + TgWebhookServer(std::shared_ptr<boost::asio::basic_socket_acceptor<boost::asio::ip::tcp>>(new boost::asio::ip::tcp::acceptor(_ioService, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port))), path, eventHandler) + { + } + + TgWebhookTcpServer(const std::string& path, const Bot& bot) : TgWebhookTcpServer(path, &bot.getEventHandler()) { + } +}; + +} + +#endif //TGBOT_TGWEBHOOKTCPSERVER_H diff --git a/src/tgbot/net/Url.cpp b/src/tgbot/net/Url.cpp new file mode 100644 index 0000000..7424849 --- /dev/null +++ b/src/tgbot/net/Url.cpp @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2015 Oleg Morozenkov + * + * 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 "Url.h" + +using namespace std; + +namespace TgBot { + +Url::Url(const string& url) { + bool isProtocolParsed = false; + bool isHostParsed = false; + bool isPathParsed = false; + bool isQueryParsed = false; + + for (size_t i = 0, count = url.length(); i < count; ++i) { + char c = url[i]; + + if (!isProtocolParsed) { + if (c == ':') { + isProtocolParsed = true; + i += 2; + } else { + protocol += c; + } + } else if (!isHostParsed) { + if (c == '/') { + isHostParsed = true; + path += '/'; + } else if (c == '?') { + isHostParsed = isPathParsed = true; + path += '/'; + } else if (c == '#') { + isHostParsed = isPathParsed = isQueryParsed = true; + path += '/'; + } else { + host += c; + } + } else if (!isPathParsed) { + if (c == '?') { + isPathParsed = true; + } else if (c == '#') { + isPathParsed = isQueryParsed = true; + } else { + path += c; + } + } else if (!isQueryParsed) { + if (c == '#') { + isQueryParsed = true; + } else { + query += c; + } + } else { + fragment += c; + } + } +} + +} diff --git a/src/tgbot/net/Url.h b/src/tgbot/net/Url.h new file mode 100644 index 0000000..39ae348 --- /dev/null +++ b/src/tgbot/net/Url.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2015 Oleg Morozenkov + * + * 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. + */ + +#ifndef TGBOT_CPP_URL_H +#define TGBOT_CPP_URL_H + +#include <string> + +namespace TgBot { + +class Url { + +public: + Url(const std::string& url); + + std::string protocol; + std::string host; + std::string path; + std::string query; + std::string fragment; +}; + +} + +#endif //TGBOT_CPP_URL_H |