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 
39 template<typename Protocol>
40 class HttpServer {
41 
42 protected:
43  class Connection;
44 
45 public:
46  typedef std::function<std::string (const std::string&, const std::map<std::string, std::string>)> ServerHandler;
47 
48  HttpServer(std::shared_ptr<boost::asio::basic_socket_acceptor<Protocol>> acceptor, const ServerHandler& handler) : _acceptor(acceptor), _handler(handler) {
49  }
50 
54  void start() {
55  auto socket(std::make_shared<boost::asio::basic_stream_socket<Protocol>>(_acceptor->get_io_service()));
56  auto connection(std::make_shared<Connection>(socket, _handler));
57  _acceptor->async_accept(*connection->socket, [this, connection]() {
58  connection->start();
59  start();
60  });
61  _ioService.run();
62  }
63 
67  void stop() {
68  _ioService.stop();
69  }
70 
71 protected:
72  class Connection {
73 
74  public:
75  Connection(std::shared_ptr<boost::asio::basic_stream_socket<Protocol>>& socket, const ServerHandler& handler) : socket(socket), _handler(handler) {
76  boost::asio::socket_base::keep_alive option(true);
77  socket.set_option(option);
78  }
79 
80  void start() {
81  data.reserve(10240);
82  socket->async_receive(data, [this]() {
83  std::map<std::string, std::string> headers;
84  std::string body = HttpParser::getInstance().parseResponse(data, headers);
85  socket->async_send(_handler(body, headers));
86  });
87  }
88 
89  std::shared_ptr<boost::asio::basic_stream_socket<Protocol>> socket;
90  std::string data;
91 
92  protected:
94  };
95 
96  boost::asio::io_service _ioService;
97  std::shared_ptr<boost::asio::basic_socket_acceptor<Protocol>> _acceptor;
99 };
100 
101 }
102 
103 #endif //TGBOT_HTTPSERVER_H
std::shared_ptr< boost::asio::basic_socket_acceptor< Protocol > > _acceptor
Definition: HttpServer.h:97
const ServerHandler _handler
Definition: HttpServer.h:93
std::shared_ptr< boost::asio::basic_stream_socket< Protocol > > socket
Definition: HttpServer.h:89
This class handles HTTP requests from the Internet.
Definition: HttpServer.h:40
void start()
Starts receiving new connections.
Definition: HttpServer.h:54
Definition: Api.h:47
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:75
const ServerHandler _handler
Definition: HttpServer.h:98
void stop()
Stops receiving new connections.
Definition: HttpServer.h:67
HttpServer(std::shared_ptr< boost::asio::basic_socket_acceptor< Protocol >> acceptor, const ServerHandler &handler)
Definition: HttpServer.h:48
static HttpParser & getInstance()
std::function< std::string(const std::string &, const std::map< std::string, std::string >)> ServerHandler
Definition: HttpServer.h:43
boost::asio::io_service _ioService
Definition: HttpServer.h:96