17
17
#include < boost/asio/buffer.hpp>
18
18
#include < iterator>
19
19
20
+ #ifndef BOOST_NETWORK_HTTP_SERVER_CONNECTION_HEADER_BUFFER_MAX_SIZE
21
+ /* * Here we define a page's worth of header connection buffer data.
22
+ * This can be tuned to reduce the memory cost of connections, but this
23
+ * default size is set to be friendly to typical service applications.
24
+ * This is the maximum size though and Boost.Asio's internal representation
25
+ * of a streambuf would make appropriate decisions on how big a buffer
26
+ * is to begin with.
27
+ */
28
+ #define BOOST_NETWORK_HTTP_SERVER_CONNECTION_HEADER_BUFFER_MAX_SIZE 4096
29
+ #endif /* BOOST_NETWORK_HTTP_SERVER_CONNECTION_HEADER_BUFFER_MAX_SIZE */
30
+
20
31
namespace boost { namespace network { namespace http {
21
32
22
33
template <class Tag , class Handler >
@@ -51,31 +62,31 @@ namespace boost { namespace network { namespace http {
51
62
, strand(io_service)
52
63
, handler(handler)
53
64
, thread_pool_(thread_pool)
54
- , headers_already_set(false )
65
+ , headers_already_sent(false )
66
+ , headers_buffer(BOOST_NETWORK_HTTP_SERVER_CONNECTION_HEADER_BUFFER_MAX_SIZE)
55
67
{}
56
68
57
69
/* * Function: template <class Range> set_headers(Range headers)
58
- * Precondition: headers have not been set yet
59
- * Postcondition: headers have been set, and cannot be modified anymore.
60
- * Throws: std::logic_error in case the headers have already been set
61
- * and the precondition is not satisfied.
70
+ * Precondition: headers have not been sent yet
71
+ * Postcondition: headers have been linearized to a buffer.
72
+ * Throws: std::logic_error in case the headers have already been sent.
62
73
*
63
74
* A call to set_headers takes a Range where each element models the
64
75
* Header concept. This Range will be linearized onto a buffer, which is
65
76
* then sent as soon as the first call to `write` or `flush` commences.
66
77
*/
67
78
template <class Range >
68
79
void set_headers (Range headers) {
69
- if (headers_already_set )
70
- boost::throw_exception (std::logic_error (" Headers have already been set ." ));
80
+ if (headers_already_sent )
81
+ boost::throw_exception (std::logic_error (" Headers have already been sent ." ));
71
82
72
83
bool commit = false ;
73
- BOOST_SCOPE_EXIT_TPL ((&commit)(&headers_already_set )) {
74
- if (!commit) headers_already_set = false ;
84
+ BOOST_SCOPE_EXIT_TPL ((&commit)(&headers_already_sent )) {
85
+ if (!commit) headers_already_sent = false ;
75
86
} BOOST_SCOPE_EXIT_END
76
87
77
88
typedef constants<Tag> consts;
78
-
89
+ headers_buffer. consume (headers_buffer. size ());
79
90
std::ostream stream (&headers_buffer);
80
91
if (!boost::empty (headers)) {
81
92
typedef typename Range::const_iterator iterator;
@@ -88,7 +99,7 @@ namespace boost { namespace network { namespace http {
88
99
stream << consts::crlf ();
89
100
}
90
101
stream << consts::crlf ();
91
- commit = headers_already_set = true ;
102
+ commit = true ;
92
103
}
93
104
94
105
void set_status (status_t new_status) {
@@ -97,21 +108,14 @@ namespace boost { namespace network { namespace http {
97
108
98
109
template <class Range >
99
110
void write (Range) {
111
+ if (!headers_already_sent) {
112
+ // TODO write out the headers that are already
113
+ // linearized to the headers_buffer.
114
+ }
100
115
// linearize the range into a shared array
101
116
// schedule a stranded asynchronous write
102
117
}
103
118
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
119
asio::ip::tcp::socket & socket () { return socket_; }
116
120
utils::thread_pool & thread_pool () { return thread_pool_; }
117
121
@@ -120,7 +124,7 @@ namespace boost { namespace network { namespace http {
120
124
asio::io_service::strand strand;
121
125
Handler & handler;
122
126
utils::thread_pool & thread_pool_;
123
- bool headers_already_set ;
127
+ bool headers_already_sent ;
124
128
asio::streambuf headers_buffer;
125
129
126
130
typedef boost::array<char , BOOST_NETWORK_HTTP_SERVER_CONNECTION_BUFFER_SIZE>
0 commit comments