Imported Upstream version 1.51.0
[platform/upstream/boost.git] / libs / asio / example / http / server4 / server.hpp
1 //
2 // server.hpp
3 // ~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2012 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10
11 #ifndef HTTP_SERVER4_SERVER_HPP
12 #define HTTP_SERVER4_SERVER_HPP
13
14 #include <boost/asio.hpp>
15 #include <string>
16 #include <boost/array.hpp>
17 #include <boost/function.hpp>
18 #include <boost/shared_ptr.hpp>
19 #include "coroutine.hpp"
20 #include "request_parser.hpp"
21
22 namespace http {
23 namespace server4 {
24
25 struct request;
26 struct reply;
27
28 /// The top-level coroutine of the HTTP server.
29 class server : coroutine
30 {
31 public:
32   /// Construct the server to listen on the specified TCP address and port, and
33   /// serve up files from the given directory.
34   explicit server(boost::asio::io_service& io_service,
35       const std::string& address, const std::string& port,
36       boost::function<void(const request&, reply&)> request_handler);
37
38   /// Perform work associated with the server.
39   void operator()(
40       boost::system::error_code ec = boost::system::error_code(),
41       std::size_t length = 0);
42
43 private:
44   typedef boost::asio::ip::tcp tcp;
45
46   /// The user-supplied handler for all incoming requests.
47   boost::function<void(const request&, reply&)> request_handler_;
48
49   /// Acceptor used to listen for incoming connections.
50   boost::shared_ptr<tcp::acceptor> acceptor_;
51
52   /// The current connection from a client.
53   boost::shared_ptr<tcp::socket> socket_;
54
55   /// Buffer for incoming data.
56   boost::shared_ptr<boost::array<char, 8192> > buffer_;
57
58   /// The incoming request.
59   boost::shared_ptr<request> request_;
60
61   /// Whether the request is valid or not.
62   boost::tribool valid_request_;
63
64   /// The parser for the incoming request.
65   request_parser request_parser_;
66
67   /// The reply to be sent back to the client.
68   boost::shared_ptr<reply> reply_;
69 };
70
71 } // namespace server4
72 } // namespace http
73
74 #endif // HTTP_SERVER4_SERVER_HPP