diff options
Diffstat (limited to 'include/tgbot/net')
-rw-r--r-- | include/tgbot/net/HttpClient.h | 16 | ||||
-rw-r--r-- | include/tgbot/net/HttpParser.h | 40 | ||||
-rw-r--r-- | include/tgbot/net/HttpReqArg.h | 42 | ||||
-rw-r--r-- | include/tgbot/net/HttpServer.h | 106 | ||||
-rw-r--r-- | include/tgbot/net/TgLongPoll.h | 18 | ||||
-rw-r--r-- | include/tgbot/net/TgWebhookLocalServer.h | 14 | ||||
-rw-r--r-- | include/tgbot/net/TgWebhookServer.h | 32 | ||||
-rw-r--r-- | include/tgbot/net/TgWebhookTcpServer.h | 14 | ||||
-rw-r--r-- | include/tgbot/net/Url.h | 12 |
9 files changed, 147 insertions, 147 deletions
diff --git a/include/tgbot/net/HttpClient.h b/include/tgbot/net/HttpClient.h index d39e108..e695398 100644 --- a/include/tgbot/net/HttpClient.h +++ b/include/tgbot/net/HttpClient.h @@ -43,17 +43,17 @@ public: /** * Returns instance which lives during all application lifetime. */ - static HttpClient& getInstance(); + static HttpClient& getInstance(); - /** - * Sends a request to the url. - * If there's no args specified, a GET request will be sent, otherwise a POST request will be sent. - * If at least 1 arg is marked as file, the content type of a request will be multipart/form-data, otherwise it will be application/x-www-form-urlencoded. - */ - std::string makeRequest(const Url& url, const std::vector<HttpReqArg>& args); + /** + * Sends a request to the url. + * If there's no args specified, a GET request will be sent, otherwise a POST request will be sent. + * If at least 1 arg is marked as file, the content type of a request will be multipart/form-data, otherwise it will be application/x-www-form-urlencoded. + */ + std::string makeRequest(const Url& url, const std::vector<HttpReqArg>& args); private: - boost::asio::io_service _ioService; + boost::asio::io_service _ioService; }; } diff --git a/include/tgbot/net/HttpParser.h b/include/tgbot/net/HttpParser.h index dcb857b..83068fb 100644 --- a/include/tgbot/net/HttpParser.h +++ b/include/tgbot/net/HttpParser.h @@ -34,33 +34,33 @@ namespace TgBot { class HttpParser { public: - static HttpParser& getInstance(); + 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); + 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, 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 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, std::map<std::string, std::string>& headers) { + return parseHttp(false, data, headers); + } - inline std::string parseResponse(const std::string& data) { - return parseHttp(false, data); - } + 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); + 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); }; } diff --git a/include/tgbot/net/HttpReqArg.h b/include/tgbot/net/HttpReqArg.h index d70e8b5..65f4f52 100644 --- a/include/tgbot/net/HttpReqArg.h +++ b/include/tgbot/net/HttpReqArg.h @@ -36,31 +36,31 @@ 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) - { - } + 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) + { + } - /** - * Name of an argument. - */ - std::string name; + /** + * Name of an argument. + */ + std::string name; - /** - * Value of an argument. - */ - std::string value; + /** + * Value of an argument. + */ + std::string value; - /** - * Should be true if an argument value hold some file contents - */ - bool isFile = false; + /** + * Should be true if an argument value hold some file contents + */ + bool isFile = false; - /** - * Mime type of an argument value. This field makes sense only if isFile is true. - */ - std::string mimeType = "text/plain"; + /** + * Mime type of an argument value. This field makes sense only if isFile is true. + */ + std::string mimeType = "text/plain"; }; } diff --git a/include/tgbot/net/HttpServer.h b/include/tgbot/net/HttpServer.h index 0a66900..bac7382 100644 --- a/include/tgbot/net/HttpServer.h +++ b/include/tgbot/net/HttpServer.h @@ -39,63 +39,63 @@ template<typename Protocol> class HttpServer { private: - class Connection; + 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) { - } - - /** - * Starts receiving new connections. - */ - 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(); - } - - /** - * Stops receiving new connections. - */ - void stop() { - _ioService.stop(); - } + 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) { + } + + /** + * Starts receiving new connections. + */ + 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(); + } + + /** + * Stops receiving new connections. + */ + 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; + 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; }; } diff --git a/include/tgbot/net/TgLongPoll.h b/include/tgbot/net/TgLongPoll.h index 932fc6d..949a2a4 100644 --- a/include/tgbot/net/TgLongPoll.h +++ b/include/tgbot/net/TgLongPoll.h @@ -36,18 +36,18 @@ namespace TgBot { class TgLongPoll { public: - TgLongPoll(const Api* api, const EventHandler* eventHandler); - TgLongPoll(const Bot& bot); + TgLongPoll(const Api* api, const EventHandler* eventHandler); + TgLongPoll(const Bot& bot); - /** - * Starts long poll. After new update will come, this method will parse it and send to EventHandler which invokes your listeners. Designed to be executed in a loop. - */ - void start(); + /** + * Starts long poll. After new update will come, this method will parse it and send to EventHandler which invokes your listeners. Designed to be executed in a loop. + */ + void start(); private: - int32_t _lastUpdateId = 0; - const Api* _api; - const EventHandler* _eventHandler; + int32_t _lastUpdateId = 0; + const Api* _api; + const EventHandler* _eventHandler; }; } diff --git a/include/tgbot/net/TgWebhookLocalServer.h b/include/tgbot/net/TgWebhookLocalServer.h index 7835f28..8faf61d 100644 --- a/include/tgbot/net/TgWebhookLocalServer.h +++ b/include/tgbot/net/TgWebhookLocalServer.h @@ -34,16 +34,16 @@ 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(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 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()) { + TgWebhookLocalServer(const std::string& path, const Bot& bot) : TgWebhookLocalServer(path, &bot.getEventHandler()) { - } + } }; } diff --git a/include/tgbot/net/TgWebhookServer.h b/include/tgbot/net/TgWebhookServer.h index a8155ff..f583093 100644 --- a/include/tgbot/net/TgWebhookServer.h +++ b/include/tgbot/net/TgWebhookServer.h @@ -34,22 +34,22 @@ 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()) - { - } + 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()) + { + } }; } diff --git a/include/tgbot/net/TgWebhookTcpServer.h b/include/tgbot/net/TgWebhookTcpServer.h index cac2638..417b162 100644 --- a/include/tgbot/net/TgWebhookTcpServer.h +++ b/include/tgbot/net/TgWebhookTcpServer.h @@ -34,15 +34,15 @@ 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(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(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()) { - } + TgWebhookTcpServer(const std::string& path, const Bot& bot) : TgWebhookTcpServer(path, &bot.getEventHandler()) { + } }; } diff --git a/include/tgbot/net/Url.h b/include/tgbot/net/Url.h index 818e934..56f502a 100644 --- a/include/tgbot/net/Url.h +++ b/include/tgbot/net/Url.h @@ -34,32 +34,32 @@ namespace TgBot { class Url { public: - Url(const std::string& url); + Url(const std::string& url); /** * Protocol part of an url. Example: https:// */ - std::string protocol; + std::string protocol; /** * Host part of an url. Example: www.example.com */ - std::string host; + std::string host; /** * Path part of an url including preceding '/' char. Example: /index.html */ - std::string path; + std::string path; /** * Query part of an url without '?' char. Example: a=1&b=2&c=3 */ - std::string query; + std::string query; /** * Fragment part of an url without '#' char. Example: section1 */ - std::string fragment; + std::string fragment; }; } |