4
4
// the Boost Software License, Version 1.0. (See acccompanying file LICENSE_1_0.txt
5
5
// or copy at http://www.boost.org/LICENSE_1_0.txt)
6
6
//
7
- // =====================================================================================
8
- //
9
- // Filename: connection.hpp
10
- //
11
- // Description: Connection handler for the HTTP requests.
12
- //
13
- // Version: 1.1
14
- // Created: Sunday, 15 November, 2009 07:46:40 PHT
15
- //
16
- // Author: Dean Michael Berris (dmb), [email protected]
17
- //
18
- // =====================================================================================
19
- //
20
7
21
8
#ifndef BOOST_NETWORK_HTTP_CONNECTION_HPP_
22
9
#define BOOST_NETWORK_HTTP_CONNECTION_HPP_
23
10
11
+ #ifndef BOOST_HTTP_SERVER_BUFFER_SIZE
12
+ #define BOOST_HTTP_SERVER_BUFFER_SIZE 1024
13
+ #endif
14
+
24
15
#include < boost/enable_shared_from_this.hpp>
25
16
#include < boost/network/protocol/http/request_parser.hpp>
26
17
#include < boost/network/protocol/http/request.hpp>
27
18
#include < boost/network/protocol/http/header.hpp>
28
- #include < boost/network/protocol/http/reply .hpp>
19
+ #include < boost/network/protocol/http/response .hpp>
29
20
#include < boost/asio.hpp>
30
21
#include < boost/array.hpp>
31
22
#include < boost/lexical_cast.hpp>
@@ -45,8 +36,8 @@ namespace boost { namespace network { namespace http {
45
36
using boost::bind;
46
37
using boost::to_lower_copy;
47
38
48
- template <class Handler >
49
- struct connection : boost::enable_shared_from_this<connection<Handler> > {
39
+ template <class Tag , class Handler >
40
+ struct connection : boost::enable_shared_from_this<connection<Tag, Handler> > {
50
41
51
42
connection (io_service & service, Handler & handler)
52
43
: service_(service)
@@ -75,8 +66,8 @@ namespace boost { namespace network { namespace http {
75
66
boost::asio::buffer (buffer_),
76
67
wrapper_.wrap (
77
68
bind (
78
- &connection<Handler>::handle_read_headers,
79
- connection<Handler>::shared_from_this (),
69
+ &connection<Tag, Handler>::handle_read_headers,
70
+ connection<Tag, Handler>::shared_from_this (),
80
71
boost::asio::placeholders::error,
81
72
boost::asio::placeholders::bytes_transferred
82
73
)
@@ -107,14 +98,14 @@ namespace boost { namespace network { namespace http {
107
98
is_content_length ()
108
99
);
109
100
if (it == request_.headers .end ()) {
110
- reply_= reply ::stock_reply (reply ::bad_request);
101
+ response_= basic_response<Tag> ::stock_reply (basic_response<Tag> ::bad_request);
111
102
boost::asio::async_write (
112
103
socket_,
113
- reply_ .to_buffers (),
104
+ response_ .to_buffers (),
114
105
wrapper_.wrap (
115
106
bind (
116
- &connection<Handler>::handle_write,
117
- connection<Handler>::shared_from_this (),
107
+ &connection<Tag, Handler>::handle_write,
108
+ connection<Tag, Handler>::shared_from_this (),
118
109
boost::asio::placeholders::error
119
110
)
120
111
)
@@ -127,14 +118,14 @@ namespace boost { namespace network { namespace http {
127
118
try {
128
119
content_length = boost::lexical_cast<size_t >(it->value );
129
120
} catch (...) {
130
- reply_= reply ::stock_reply (reply ::bad_request);
121
+ response_= basic_response<Tag> ::stock_reply (basic_response<Tag> ::bad_request);
131
122
boost::asio::async_write (
132
123
socket_,
133
- reply_ .to_buffers (),
124
+ response_ .to_buffers (),
134
125
wrapper_.wrap (
135
126
bind (
136
- &connection<Handler>::handle_write,
137
- connection<Handler>::shared_from_this (),
127
+ &connection<Tag, Handler>::handle_write,
128
+ connection<Tag, Handler>::shared_from_this (),
138
129
boost::asio::placeholders::error
139
130
)
140
131
)
@@ -149,8 +140,8 @@ namespace boost { namespace network { namespace http {
149
140
boost::asio::transfer_at_least (content_length),
150
141
wrapper_.wrap (
151
142
bind (
152
- &connection<Handler>::handle_read_body_contents,
153
- connection<Handler>::shared_from_this (),
143
+ &connection<Tag, Handler>::handle_read_body_contents,
144
+ connection<Tag, Handler>::shared_from_this (),
154
145
boost::asio::placeholders::error,
155
146
content_length,
156
147
boost::asio::placeholders::bytes_transferred
@@ -160,41 +151,41 @@ namespace boost { namespace network { namespace http {
160
151
return ;
161
152
}
162
153
163
- handler_ (request_, reply_ );
154
+ handler_ (request_, response_ );
164
155
boost::asio::async_write (
165
156
socket_,
166
- reply_ .to_buffers (),
157
+ response_ .to_buffers (),
167
158
wrapper_.wrap (
168
159
bind (
169
- &connection<Handler>::handle_write,
170
- connection<Handler>::shared_from_this (),
160
+ &connection<Tag, Handler>::handle_write,
161
+ connection<Tag, Handler>::shared_from_this (),
171
162
boost::asio::placeholders::error
172
163
)
173
164
)
174
165
);
175
166
} else {
176
- handler_ (request_, reply_ );
167
+ handler_ (request_, response_ );
177
168
boost::asio::async_write (
178
169
socket_,
179
- reply_ .to_buffers (),
170
+ response_ .to_buffers (),
180
171
wrapper_.wrap (
181
172
bind (
182
- &connection<Handler>::handle_write,
183
- connection<Handler>::shared_from_this (),
173
+ &connection<Tag, Handler>::handle_write,
174
+ connection<Tag, Handler>::shared_from_this (),
184
175
boost::asio::placeholders::error
185
176
)
186
177
)
187
178
);
188
179
}
189
180
} else if (!done) {
190
- reply_= reply ::stock_reply (reply ::bad_request);
181
+ response_= basic_response<Tag> ::stock_reply (basic_response<Tag> ::bad_request);
191
182
boost::asio::async_write (
192
183
socket_,
193
- reply_ .to_buffers (),
184
+ response_ .to_buffers (),
194
185
wrapper_.wrap (
195
186
bind (
196
- &connection<Handler>::handle_write,
197
- connection<Handler>::shared_from_this (),
187
+ &connection<Tag, Handler>::handle_write,
188
+ connection<Tag, Handler>::shared_from_this (),
198
189
boost::asio::placeholders::error
199
190
)
200
191
)
@@ -204,8 +195,8 @@ namespace boost { namespace network { namespace http {
204
195
boost::asio::buffer (buffer_),
205
196
wrapper_.wrap (
206
197
bind (
207
- &connection<Handler>::handle_read_headers,
208
- connection<Handler>::shared_from_this (),
198
+ &connection<Tag, Handler>::handle_read_headers,
199
+ connection<Tag, Handler>::shared_from_this (),
209
200
boost::asio::placeholders::error,
210
201
boost::asio::placeholders::bytes_transferred
211
202
)
@@ -221,14 +212,14 @@ namespace boost { namespace network { namespace http {
221
212
size_t difference = bytes_to_read - bytes_transferred;
222
213
request_.body .append (buffer_.begin (), buffer_.end ());
223
214
if (difference == 0 ) {
224
- handler_ (request_, reply_ );
215
+ handler_ (request_, response_ );
225
216
boost::asio::async_write (
226
217
socket_,
227
- reply_ .to_buffers (),
218
+ response_ .to_buffers (),
228
219
wrapper_.wrap (
229
220
bind (
230
- &connection<Handler>::handle_write,
231
- connection<Handler>::shared_from_this (),
221
+ &connection<Tag, Handler>::handle_write,
222
+ connection<Tag, Handler>::shared_from_this (),
232
223
boost::asio::placeholders::error
233
224
)
234
225
)
@@ -238,8 +229,8 @@ namespace boost { namespace network { namespace http {
238
229
boost::asio::buffer (buffer_),
239
230
wrapper_.wrap (
240
231
bind (
241
- &connection<Handler>::handle_read_body_contents,
242
- connection<Handler>::shared_from_this (),
232
+ &connection<Tag, Handler>::handle_read_body_contents,
233
+ connection<Tag, Handler>::shared_from_this (),
243
234
boost::asio::placeholders::error,
244
235
difference,
245
236
boost::asio::placeholders::bytes_transferred
@@ -262,10 +253,10 @@ namespace boost { namespace network { namespace http {
262
253
Handler & handler_;
263
254
tcp::socket socket_;
264
255
io_service::strand wrapper_;
265
- array<char ,4096 > buffer_;
256
+ array<char ,BOOST_HTTP_SERVER_BUFFER_SIZE > buffer_;
266
257
request_parser parser_;
267
- request_pod request_;
268
- reply reply_ ;
258
+ basic_request<Tag> request_;
259
+ basic_response<Tag> response_ ;
269
260
};
270
261
271
262
0 commit comments