44// the Boost Software License, Version 1.0. (See acccompanying file LICENSE_1_0.txt
55// or copy at http://www.boost.org/LICENSE_1_0.txt)
66//
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), mikhailberis@gmail.com
17- //
18- // =====================================================================================
19- //
207
218#ifndef BOOST_NETWORK_HTTP_CONNECTION_HPP_
229#define BOOST_NETWORK_HTTP_CONNECTION_HPP_
2310
11+ #ifndef BOOST_HTTP_SERVER_BUFFER_SIZE
12+ #define BOOST_HTTP_SERVER_BUFFER_SIZE 1024
13+ #endif
14+
2415#include < boost/enable_shared_from_this.hpp>
2516#include < boost/network/protocol/http/request_parser.hpp>
2617#include < boost/network/protocol/http/request.hpp>
2718#include < boost/network/protocol/http/header.hpp>
28- #include < boost/network/protocol/http/reply .hpp>
19+ #include < boost/network/protocol/http/response .hpp>
2920#include < boost/asio.hpp>
3021#include < boost/array.hpp>
3122#include < boost/lexical_cast.hpp>
@@ -45,8 +36,8 @@ namespace boost { namespace network { namespace http {
4536using boost::bind;
4637using boost::to_lower_copy;
4738
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> > {
5041
5142connection (io_service & service, Handler & handler)
5243 : service_(service)
@@ -75,8 +66,8 @@ namespace boost { namespace network { namespace http {
7566boost::asio::buffer (buffer_),
7667 wrapper_.wrap (
7768bind (
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 (),
8071 boost::asio::placeholders::error,
8172 boost::asio::placeholders::bytes_transferred
8273 )
@@ -107,14 +98,14 @@ namespace boost { namespace network { namespace http {
10798is_content_length ()
10899 );
109100if (it == request_.headers .end ()) {
110- reply_= reply ::stock_reply (reply ::bad_request);
101+ response_= basic_response<Tag> ::stock_reply (basic_response<Tag> ::bad_request);
111102boost::asio::async_write (
112103 socket_,
113- reply_ .to_buffers (),
104+ response_ .to_buffers (),
114105 wrapper_.wrap (
115106bind (
116- &connection<Handler>::handle_write,
117- connection<Handler>::shared_from_this (),
107+ &connection<Tag, Handler>::handle_write,
108+ connection<Tag, Handler>::shared_from_this (),
118109 boost::asio::placeholders::error
119110 )
120111 )
@@ -127,14 +118,14 @@ namespace boost { namespace network { namespace http {
127118try {
128119 content_length = boost::lexical_cast<size_t >(it->value );
129120 }catch (...) {
130- reply_= reply ::stock_reply (reply ::bad_request);
121+ response_= basic_response<Tag> ::stock_reply (basic_response<Tag> ::bad_request);
131122boost::asio::async_write (
132123 socket_,
133- reply_ .to_buffers (),
124+ response_ .to_buffers (),
134125 wrapper_.wrap (
135126bind (
136- &connection<Handler>::handle_write,
137- connection<Handler>::shared_from_this (),
127+ &connection<Tag, Handler>::handle_write,
128+ connection<Tag, Handler>::shared_from_this (),
138129 boost::asio::placeholders::error
139130 )
140131 )
@@ -149,8 +140,8 @@ namespace boost { namespace network { namespace http {
149140boost::asio::transfer_at_least (content_length),
150141 wrapper_.wrap (
151142bind (
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 (),
154145 boost::asio::placeholders::error,
155146 content_length,
156147 boost::asio::placeholders::bytes_transferred
@@ -160,41 +151,41 @@ namespace boost { namespace network { namespace http {
160151return ;
161152 }
162153
163- handler_ (request_,reply_ );
154+ handler_ (request_,response_ );
164155boost::asio::async_write (
165156 socket_,
166- reply_ .to_buffers (),
157+ response_ .to_buffers (),
167158 wrapper_.wrap (
168159bind (
169- &connection<Handler>::handle_write,
170- connection<Handler>::shared_from_this (),
160+ &connection<Tag, Handler>::handle_write,
161+ connection<Tag, Handler>::shared_from_this (),
171162 boost::asio::placeholders::error
172163 )
173164 )
174165 );
175166 }else {
176- handler_ (request_,reply_ );
167+ handler_ (request_,response_ );
177168boost::asio::async_write (
178169 socket_,
179- reply_ .to_buffers (),
170+ response_ .to_buffers (),
180171 wrapper_.wrap (
181172bind (
182- &connection<Handler>::handle_write,
183- connection<Handler>::shared_from_this (),
173+ &connection<Tag, Handler>::handle_write,
174+ connection<Tag, Handler>::shared_from_this (),
184175 boost::asio::placeholders::error
185176 )
186177 )
187178 );
188179 }
189180 }else if (!done) {
190- reply_= reply ::stock_reply (reply ::bad_request);
181+ response_= basic_response<Tag> ::stock_reply (basic_response<Tag> ::bad_request);
191182boost::asio::async_write (
192183 socket_,
193- reply_ .to_buffers (),
184+ response_ .to_buffers (),
194185 wrapper_.wrap (
195186bind (
196- &connection<Handler>::handle_write,
197- connection<Handler>::shared_from_this (),
187+ &connection<Tag, Handler>::handle_write,
188+ connection<Tag, Handler>::shared_from_this (),
198189 boost::asio::placeholders::error
199190 )
200191 )
@@ -204,8 +195,8 @@ namespace boost { namespace network { namespace http {
204195boost::asio::buffer (buffer_),
205196 wrapper_.wrap (
206197bind (
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 (),
209200 boost::asio::placeholders::error,
210201 boost::asio::placeholders::bytes_transferred
211202 )
@@ -221,14 +212,14 @@ namespace boost { namespace network { namespace http {
221212size_t difference = bytes_to_read - bytes_transferred;
222213 request_.body .append (buffer_.begin (), buffer_.end ());
223214if (difference ==0 ) {
224- handler_ (request_,reply_ );
215+ handler_ (request_,response_ );
225216boost::asio::async_write (
226217 socket_,
227- reply_ .to_buffers (),
218+ response_ .to_buffers (),
228219 wrapper_.wrap (
229220bind (
230- &connection<Handler>::handle_write,
231- connection<Handler>::shared_from_this (),
221+ &connection<Tag, Handler>::handle_write,
222+ connection<Tag, Handler>::shared_from_this (),
232223 boost::asio::placeholders::error
233224 )
234225 )
@@ -238,8 +229,8 @@ namespace boost { namespace network { namespace http {
238229boost::asio::buffer (buffer_),
239230 wrapper_.wrap (
240231bind (
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 (),
243234 boost::asio::placeholders::error,
244235 difference,
245236 boost::asio::placeholders::bytes_transferred
@@ -262,10 +253,10 @@ namespace boost { namespace network { namespace http {
262253 Handler & handler_;
263254 tcp::socket socket_;
264255 io_service::strand wrapper_;
265- array<char ,4096 > buffer_;
256+ array<char ,BOOST_HTTP_SERVER_BUFFER_SIZE > buffer_;
266257 request_parser parser_;
267- request_pod request_;
268- reply reply_ ;
258+ basic_request<Tag> request_;
259+ basic_response<Tag> response_ ;
269260 };
270261
271262