tizen 2.4 release
[external/nghttp2.git] / src / asio_server.cc
1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2014 Tatsuhiro Tsujikawa
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 // We wrote this code based on the original code which has the
26 // following license:
27 //
28 // server.cpp
29 // ~~~~~~~~~~
30 //
31 // Copyright (c) 2003-2013 Christopher M. Kohlhoff (chris at kohlhoff dot com)
32 //
33 // Distributed under the Boost Software License, Version 1.0. (See accompanying
34 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
35 //
36
37 #include "asio_server.h"
38
39 #include <boost/date_time/posix_time/posix_time.hpp>
40
41 namespace nghttp2 {
42 namespace asio_http2 {
43 namespace server {
44
45 server::server(const std::string &address, uint16_t port,
46                std::size_t io_service_pool_size, std::size_t thread_pool_size,
47                request_cb cb,
48                std::unique_ptr<boost::asio::ssl::context> ssl_ctx, int backlog)
49     : io_service_pool_(io_service_pool_size, thread_pool_size),
50       signals_(io_service_pool_.get_io_service()),
51       tick_timer_(io_service_pool_.get_io_service(),
52                   boost::posix_time::seconds(1)),
53       ssl_ctx_(std::move(ssl_ctx)), request_cb_(std::move(cb)) {
54   // Register to handle the signals that indicate when the server should exit.
55   // It is safe to register for the same signal multiple times in a program,
56   // provided all registration for the specified signal is made through Asio.
57   signals_.add(SIGINT);
58   signals_.add(SIGTERM);
59 #if defined(SIGQUIT)
60   signals_.add(SIGQUIT);
61 #endif // defined(SIGQUIT)
62   signals_.async_wait([this](const boost::system::error_code &error,
63                              int signal_number) { io_service_pool_.stop(); });
64
65   // Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR).
66   boost::asio::ip::tcp::resolver resolver(io_service_pool_.get_io_service());
67   boost::asio::ip::tcp::resolver::query query(address, std::to_string(port));
68
69   for (auto itr = resolver.resolve(query);
70        itr != boost::asio::ip::tcp::resolver::iterator(); ++itr) {
71     boost::asio::ip::tcp::endpoint endpoint = *itr;
72     auto acceptor =
73         boost::asio::ip::tcp::acceptor(io_service_pool_.get_io_service());
74
75     acceptor.open(endpoint.protocol());
76     acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
77     acceptor.bind(endpoint);
78     if (backlog == -1) {
79       acceptor.listen();
80     } else {
81       acceptor.listen(backlog);
82     }
83     acceptors_.push_back(std::move(acceptor));
84   }
85
86   start_accept();
87
88   start_timer();
89 }
90
91 void server::run() { io_service_pool_.run(); }
92
93 std::shared_ptr<std::string> cached_date;
94
95 namespace {
96 void update_date() {
97   cached_date = std::make_shared<std::string>(util::http_date(time(nullptr)));
98 }
99 } // namespace
100
101 void server::start_timer() {
102   update_date();
103
104   tick_timer_.async_wait([this](const boost::system::error_code &e) {
105     tick_timer_.expires_at(tick_timer_.expires_at() +
106                            boost::posix_time::seconds(1));
107     start_timer();
108   });
109 }
110
111 typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> ssl_socket;
112
113 void server::start_accept() {
114   if (ssl_ctx_) {
115     auto new_connection = std::make_shared<connection<ssl_socket>>(
116         request_cb_, io_service_pool_.get_task_io_service(),
117         io_service_pool_.get_io_service(), *ssl_ctx_);
118
119     for (auto &acceptor : acceptors_) {
120       acceptor.async_accept(
121           new_connection->socket().lowest_layer(),
122           [this, new_connection](const boost::system::error_code &e) {
123             if (!e) {
124               new_connection->socket().lowest_layer().set_option(
125                   boost::asio::ip::tcp::no_delay(true));
126               new_connection->socket().async_handshake(
127                   boost::asio::ssl::stream_base::server,
128                   [new_connection](const boost::system::error_code &e) {
129                     if (!e) {
130                       new_connection->start();
131                     }
132                   });
133             }
134
135             start_accept();
136           });
137     }
138   } else {
139     auto new_connection =
140         std::make_shared<connection<boost::asio::ip::tcp::socket>>(
141             request_cb_, io_service_pool_.get_task_io_service(),
142             io_service_pool_.get_io_service());
143
144     for (auto &acceptor : acceptors_) {
145       acceptor.async_accept(
146           new_connection->socket(),
147           [this, new_connection](const boost::system::error_code &e) {
148             if (!e) {
149               new_connection->socket().set_option(
150                   boost::asio::ip::tcp::no_delay(true));
151               new_connection->start();
152             }
153
154             start_accept();
155           });
156     }
157   }
158 }
159
160 } // namespace server
161 } // namespace asio_http2
162 } // namespace nghttp2