6
6
// (See accompanying file LICENSE_1_0.txt or copy at
7
7
// http://www.boost.org/LICENSE_1_0.txt)
8
8
9
+ #include < boost/throw_exception.hpp>
10
+ #include < boost/scope_exit.hpp>
11
+ #include < boost/network/protocol/http/algorithms/linearize.hpp>
12
+ #include < boost/network/utils/thread_pool.hpp>
13
+ #include < boost/range/algorithm/transform.hpp>
14
+ #include < boost/asio/ip/tcp.hpp>
15
+ #include < boost/asio/streambuf.hpp>
16
+ #include < iterator>
17
+
9
18
namespace boost { namespace network { namespace http {
10
19
11
20
template <class Tag , class Handler >
12
- struct async_connection {
21
+ struct async_connection : boost::enable_shared_from_this<async_connection<Tag,Handler> > {
13
22
enum status_t {
14
23
ok = 200
15
- , method_not_supported = 306
24
+ , created = 201
25
+ , accepted = 202
26
+ , no_content = 204
27
+ , multiple_choices = 300
28
+ , moved_permanently = 301
29
+ , moved_temporarily = 302
30
+ , not_modified = 304
31
+ , bad_request = 400
32
+ , unauthorized = 401
33
+ , forbidden = 403
34
+ , not_found = 404
35
+ , not_supported = 405
36
+ , not_acceptable = 406
37
+ , internal_server_error = 500
38
+ , not_implemented = 501
39
+ , bad_gateway = 502
40
+ , service_unavailable = 503
16
41
};
42
+
43
+ async_connection (
44
+ asio::io_service & io_service
45
+ , Handler & handler
46
+ , utils::thread_pool & thread_pool
47
+ )
48
+ : socket_(io_service)
49
+ , handler(handler)
50
+ , thread_pool_(thread_pool)
51
+ , headers_already_set(false )
52
+ {}
53
+
54
+ /* * Function: template <class Range> set_headers(Range headers)
55
+ * Precondition: headers have not been set yet
56
+ * Postcondition: headers have been set, and cannot be modified anymore.
57
+ * Throws: std::logic_error in case the headers have already been set
58
+ * and the precondition is not satisfied.
59
+ *
60
+ * A call to set_headers takes a Range where each element models the
61
+ * Header concept. This Range will be linearized onto a buffer, which is
62
+ * then sent as soon as the first call to `write` or `flush` commences.
63
+ */
17
64
template <class Range >
18
- void set_headers (Range);
19
- void set_status (status_t );
65
+ void set_headers (Range headers) {
66
+ if (headers_already_set)
67
+ boost::throw_exception (std::logic_error (" Headers have already been set." ));
68
+
69
+ bool commit = false ;
70
+ BOOST_SCOPE_EXIT_TPL ((&commit)(&headers_already_set))
71
+ {
72
+ if (!commit) {
73
+ headers_already_set = false ;
74
+ }
75
+ } BOOST_SCOPE_EXIT_END
76
+
77
+ typedef constants<Tag> consts;
78
+
79
+ std::ostream stream (&headers_buffer);
80
+ if (!boost::empty (headers)) {
81
+ typedef typename Range::const_iterator iterator;
82
+ typedef typename Range::value_type value_type;
83
+ typedef typename string<Tag>::type string_type;
84
+ boost::transform (headers,
85
+ std::ostream_iterator<string_type>(stream),
86
+ linearize<Tag, value_type>());
87
+ } else {
88
+ stream << consts::crlf ();
89
+ }
90
+ stream << consts::crlf ();
91
+ commit = headers_already_set = true ;
92
+ }
93
+
94
+ void set_status (status_t new_status) {
95
+ status = new_status;
96
+ }
97
+
20
98
template <class Range >
21
- void write (Range);
22
- void flush ();
23
- void close ();
99
+ void write (Range) {
100
+ // linearize the range into a shared array
101
+ // schedule a stranded asynchronous write
102
+ }
103
+
104
+ void flush () {
105
+ // use this as a synchronization point to ensure
106
+ // that data has been written; use a unique_future
107
+ }
108
+
109
+ void close () {
110
+ flush ();
111
+ socket_.shutdown (asio::ip::tcp::socket::shutdown_both);
112
+ socket_.close ();
113
+ }
114
+
115
+ asio::ip::tcp::socket & socket () { return socket_; }
116
+ utils::thread_pool & thread_pool () { return thread_pool_; }
117
+
118
+ private:
119
+ asio::ip::tcp::socket socket_;
120
+ Handler & handler;
121
+ utils::thread_pool & thread_pool_;
122
+ bool headers_already_set;
123
+ asio::streambuf headers_buffer;
124
+ boost::uint16_t status;
125
+
126
+ template <class , class > friend struct async_server_base ;
127
+
128
+ void start () {
129
+ // FIXME do something!
130
+ }
131
+
24
132
};
25
133
26
134
} /* http */
@@ -30,3 +138,4 @@ namespace boost { namespace network { namespace http {
30
138
} /* boost */
31
139
32
140
#endif /* BOOST_NETWORK_PROTOCOL_HTTP_SERVER_CONNECTION_HPP_20101027 */
141
+
0 commit comments