tgbot-cpp
HttpServer.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Oleg Morozenkov
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all
12  * copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22 
23 #ifndef TGBOT_HTTPSERVER_H
24 #define TGBOT_HTTPSERVER_H
25 
26 #include <string>
27 
28 #include <boost/asio.hpp>
29 
30 #include "tgbot/net/HttpParser.h"
31 
32 namespace TgBot {
33 
38 template<typename Protocol>
39 class HttpServer {
40 
41 protected:
42  class Connection;
43 
44 public:
45  typedef std::function<std::string (const std::string&, const std::map<std::string, std::string>)> ServerHandler;
46 
47  HttpServer(std::shared_ptr<boost::asio::basic_socket_acceptor<Protocol>> acceptor, const ServerHandler& handler) : _acceptor(acceptor), _handler(handler) {
48  }
49 
53  void start() {
54  std::shared_ptr<boost::asio::basic_stream_socket<Protocol>> socket(new boost::asio::basic_stream_socket<Protocol>(_acceptor->get_io_service()));
55  std::shared_ptr<Connection> connection(new Connection(socket, _handler));
56  _acceptor->async_accept(*connection->socket, [this, connection]() {
57  connection->start();
58  start();
59  });
60  _ioService.run();
61  }
62 
66  void stop() {
67  _ioService.stop();
68  }
69 
70 protected:
71  class Connection {
72 
73  public:
74  Connection(std::shared_ptr<boost::asio::basic_stream_socket<Protocol>>& socket, const ServerHandler& handler) : socket(socket), _handler(handler) {
75  boost::asio::socket_base::keep_alive option(true);
76  socket.set_option(option);
77  }
78 
79  void start() {
80  data.reserve(10240);
81  socket->async_receive(data, [this]() {
82  std::map<std::string, std::string> headers;
83  std::string body = HttpParser::getInstance().parseResponse(data, headers);
84  socket->async_send(_handler(body, headers));
85  });
86  }
87 
88  std::shared_ptr<boost::asio::basic_stream_socket<Protocol>> socket;
89  std::string data;
90 
91  protected:
93  };
94 
95  boost::asio::io_service _ioService;
96  std::shared_ptr<boost::asio::basic_socket_acceptor<Protocol>> _acceptor;
98 };
99 
100 }
101 
102 #endif //TGBOT_HTTPSERVER_H
std::shared_ptr< boost::asio::basic_socket_acceptor< Protocol > > _acceptor
Definition: HttpServer.h:96
const ServerHandler _handler
Definition: HttpServer.h:92
std::shared_ptr< boost::asio::basic_stream_socket< Protocol > > socket
Definition: HttpServer.h:88
Definition: Api.h:44
std::string parseResponse(const std::string &data, std::map< std::string, std::string > &headers)
Definition: HttpParser.h:54
Connection(std::shared_ptr< boost::asio::basic_stream_socket< Protocol >> &socket, const ServerHandler &handler)
Definition: HttpServer.h:74
const ServerHandler _handler
Definition: HttpServer.h:97
HttpServer(std::shared_ptr< boost::asio::basic_socket_acceptor< Protocol >> acceptor, const ServerHandler &handler)
Definition: HttpServer.h:47
static HttpParser & getInstance()
std::function< std::string(const std::string &, const std::map< std::string, std::string >)> ServerHandler
Definition: HttpServer.h:42
boost::asio::io_service _ioService
Definition: HttpServer.h:95