1 #ifndef BOOST_NETWORK_PROTOCOL_HTTP_SERVER_OPTIONS_20130128
2 #define BOOST_NETWORK_PROTOCOL_HTTP_SERVER_OPTIONS_20130128
3 
4 // Copyright 2013 Google, Inc.
5 // Copyright 2013 Dean Michael Berris <dberris@google.com>
6 // Copyright 2014 Jelle Van den Driessche
7 // Distributed under the Boost Software License, Version 1.0.
8 // (See accompanying file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
10 
11 #include <memory>
12 #include <boost/asio/io_service.hpp>
13 #include <boost/asio/socket_base.hpp>
14 #include <boost/network/protocol/stream_handler.hpp>
15 #include <boost/network/traits/string.hpp>
16 #include <boost/network/utils/thread_pool.hpp>
17 #include <boost/optional.hpp>
18 
19 namespace boost {
20 namespace network {
21 namespace http {
22 
23 /**
24  * The options supported by an HTTP Server's constructor.
25  */
26 template <class Tag, class Handler>
27 struct server_options {
28   typedef typename string<Tag>::type string_type;
29 
30   /// A single-argument constructor that takes a Handler, and sets all options
31   /// to defaults.
server_optionsboost::network::http::server_options32   explicit server_options(Handler &handler)
33       : io_service_(),
34         handler_(handler),
35         address_("localhost"),
36         port_("80"),
37         protocol_family_(undefined),
38         reuse_address_(false),
39         report_aborted_(false),
40         non_blocking_io_(true),
41         linger_(true),
42         linger_timeout_(0),
43         receive_buffer_size_(),
44         send_buffer_size_(),
45         receive_low_watermark_(),
46         send_low_watermark_(),
47         thread_pool_(),
48         context_() {}
49 
50   /// Disabled default constructor for the options class.
51   server_options() = delete;
52 
53   /// Copy constructor for the options class.
54   server_options(const server_options &) = default;
55 
56   /// Copy assignment for the options class.
57   server_options &operator=(const server_options &) = default;
58 
59   /// Move constructor for the options class.
60   server_options(server_options &&) = default;
61 
62   /// Move assignment for the options class.
63   server_options &operator=(server_options &&) = default;
64 
65   /// Destructor for the options class.
66   ~server_options() = default;
67 
68   /// Sets the SSL context for the server. Default is nullptr.
contextboost::network::http::server_options69   server_options &context(std::shared_ptr<ssl_context> v) {
70     context_ = v;
71     return *this;
72   }
73 
74   /// Provides an Asio io_service for the server. Default is nullptr.
io_serviceboost::network::http::server_options75   server_options &io_service(std::shared_ptr<boost::asio::io_service> v) {
76     io_service_ = v;
77     return *this;
78   }
79 
80   /// Sets the address to listen to for the server. Default is localhost.
addressboost::network::http::server_options81   server_options &address(string_type v) {
82     address_ = std::move(v);
83     return *this;
84   }
85 
86   /// Set the port to listen to for the server. Default is 80.
portboost::network::http::server_options87   server_options &port(string_type const &v) {
88     port_ = v;
89     return *this;
90   }
91 
92   enum protocol_family_t { ipv4, ipv6, undefined };
93 
94   /// Set the protocol family for address resolving. Default is AF_UNSPEC.
protocol_familyboost::network::http::server_options95   server_options &protocol_family(protocol_family_t v) {
96     protocol_family_ = v;
97     return *this;
98   }
99 
100   /// Set whether to reuse the address (SO_REUSE_ADDR). Default is false.
reuse_addressboost::network::http::server_options101   server_options &reuse_address(bool v) {
102     reuse_address_ = v;
103     return *this;
104   }
105 
106   /// Set whether to report aborted connections. Default is false.
report_abortedboost::network::http::server_options107   server_options &report_aborted(bool v) {
108     report_aborted_ = v;
109     return *this;
110   }
111 
112   /// Set whether to use non-blocking IO. Default is true.
non_blocking_ioboost::network::http::server_options113   server_options &non_blocking_io(bool v) {
114     non_blocking_io_ = v;
115     return *this;
116   }
117 
118   /// Set whether sockets linger (SO_LINGER). Default is true.
lingerboost::network::http::server_options119   server_options &linger(bool v) {
120     linger_ = v;
121     return *this;
122   }
123 
124   /// Set the linger timeout. Default is 0.
linger_timeoutboost::network::http::server_options125   server_options &linger_timeout(size_t v) {
126     linger_timeout_ = v;
127     return *this;
128   }
129 
130   /// Set the socket receive buffer size. Unset by default.
receive_buffer_sizeboost::network::http::server_options131   server_options &receive_buffer_size(
132       boost::asio::socket_base::receive_buffer_size v) {
133     receive_buffer_size_ = v;
134     return *this;
135   }
136 
137   /// Set the send buffer size. Unset by default.
send_buffer_sizeboost::network::http::server_options138   server_options &send_buffer_size(boost::asio::socket_base::send_buffer_size v) {
139     send_buffer_size_ = v;
140     return *this;
141   }
142 
143   /// Set the socket receive low watermark. Unset by default.
receive_low_watermarkboost::network::http::server_options144   server_options &receive_low_watermark(
145       boost::asio::socket_base::receive_low_watermark v) {
146     receive_low_watermark_ = v;
147     return *this;
148   }
149 
150   /// Set the socket send low watermark. Unset by default.
send_low_watermarkboost::network::http::server_options151   server_options &send_low_watermark(boost::asio::socket_base::send_low_watermark v) {
152     send_low_watermark_ = v;
153     return *this;
154   }
155 
156   /// Set the thread-pool to use. Default is nullptr.
thread_poolboost::network::http::server_options157   server_options &thread_pool(std::shared_ptr<utils::thread_pool> v) {
158     thread_pool_ = v;
159     return *this;
160   }
161 
162   /// Returns the provided Asio io_service.
io_serviceboost::network::http::server_options163   std::shared_ptr<boost::asio::io_service> io_service() const { return io_service_; }
164 
165   /// Returns the address to listen on.
addressboost::network::http::server_options166   string_type address() const { return address_; }
167 
168   /// Returns the port to listen on.
portboost::network::http::server_options169   string_type port() const { return port_; }
170 
171   /// Returns the protocol family used for address resolving.
protocol_familyboost::network::http::server_options172   protocol_family_t protocol_family() const { return protocol_family_; }
173 
174   /// Returns a reference to the provided handler.
handlerboost::network::http::server_options175   Handler &handler() const { return handler_; }
176 
177   /// Returns whether to reuse the address.
reuse_addressboost::network::http::server_options178   bool reuse_address() const { return reuse_address_; }
179 
180   /// Returns whether to report aborted connections.
report_abortedboost::network::http::server_options181   bool report_aborted() const { return report_aborted_; }
182 
183   /// Returns whether to perform non-blocking IO.
non_blocking_ioboost::network::http::server_options184   bool non_blocking_io() const { return non_blocking_io_; }
185 
186   /// Returns whether to linger.
lingerboost::network::http::server_options187   bool linger() const { return linger_; }
188 
189   /// Returns the linger timeout.
linger_timeoutboost::network::http::server_options190   size_t linger_timeout() const { return linger_timeout_; }
191 
192   /// Returns the optional receive buffer size.
receive_buffer_sizeboost::network::http::server_options193   boost::optional<boost::asio::socket_base::receive_buffer_size> receive_buffer_size()
194       const {
195     return receive_buffer_size_;
196   }
197 
198   /// Returns the optional send buffer size.
send_buffer_sizeboost::network::http::server_options199   boost::optional<boost::asio::socket_base::send_buffer_size> send_buffer_size()
200       const {
201     return send_buffer_size_;
202   }
203 
204   /// Returns the optional receive low watermark.
205   boost::optional<boost::asio::socket_base::receive_low_watermark>
receive_low_watermarkboost::network::http::server_options206       receive_low_watermark() const {
207     return receive_low_watermark_;
208   }
209 
210   /// Returns the optional send low watermark.
send_low_watermarkboost::network::http::server_options211   boost::optional<boost::asio::socket_base::send_low_watermark> send_low_watermark()
212       const {
213     return send_low_watermark_;
214   }
215 
216   /// Returns a pointer to the provided thread pool.
thread_poolboost::network::http::server_options217   std::shared_ptr<utils::thread_pool> thread_pool() const {
218     return thread_pool_;
219   }
220 
221   /// Returns a pointer to the provided context.
contextboost::network::http::server_options222   std::shared_ptr<ssl_context> context() const { return context_; }
223 
224   /// Swap implementation for the server options.
swapboost::network::http::server_options225   void swap(server_options &other) {
226     using std::swap;
227     swap(io_service_, other.io_service_);
228     swap(address_, other.address_);
229     swap(port_, other.port_);
230     swap(protocol_family_, other.protocol_family_);
231     swap(reuse_address_, other.reuse_address_);
232     swap(report_aborted_, other.report_aborted_);
233     swap(non_blocking_io_, other.non_blocking_io_);
234     swap(linger_, other.linger_);
235     swap(linger_timeout_, other.linger_timeout_);
236     swap(receive_buffer_size_, other.receive_buffer_size_);
237     swap(send_buffer_size_, other.send_buffer_size_);
238     swap(receive_low_watermark_, other.receive_low_watermark_);
239     swap(send_low_watermark_, other.send_low_watermark_);
240     swap(thread_pool_, other.thread_pool_);
241     swap(context_, other.context_);
242   }
243 
244  private:
245   std::shared_ptr<boost::asio::io_service> io_service_;
246   Handler &handler_;
247   string_type address_;
248   string_type port_;
249   protocol_family_t protocol_family_;
250   bool reuse_address_;
251   bool report_aborted_;
252   bool non_blocking_io_;
253   bool linger_;
254   size_t linger_timeout_;
255   boost::optional<boost::asio::socket_base::receive_buffer_size> receive_buffer_size_;
256   boost::optional<boost::asio::socket_base::send_buffer_size> send_buffer_size_;
257   boost::optional<boost::asio::socket_base::receive_low_watermark>
258       receive_low_watermark_;
259   boost::optional<boost::asio::socket_base::send_low_watermark> send_low_watermark_;
260   std::shared_ptr<utils::thread_pool> thread_pool_;
261   std::shared_ptr<ssl_context> context_;
262 };
263 
264 template <class Tag, class Handler>
swap(server_options<Tag,Handler> & a,server_options<Tag,Handler> & b)265 inline void swap(server_options<Tag, Handler> &a,
266                  server_options<Tag, Handler> &b) {
267   a.swap(b);
268 }
269 
270 } /* http */
271 } /* network */
272 } /* boost */
273 
274 #endif /* BOOST_NETWORK_PROTOCOL_HTTP_SERVER_OPTIONS_20130128 */
275