Skip to content
This repository was archived by the owner on Oct 18, 2023. It is now read-only.

Commit ec5996d

Browse files
committed
Refactoring to move request/response types into both client and server types sharing the same base template.
1 parent b8bb431 commit ec5996d

17 files changed

+221
-235
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ CMakeFiles
66
Makefile
77
Testing
88
build
9+
bin
910

+40-35
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1+
#ifndef BOOST_NETWORK_PROTOCOL_HTTP_CLIENT_20091215
2+
#define BOOST_NETWORK_PROTOCOL_HTTP_CLIENT_20091215
13

24
// Copyright Dean Michael Berris 2007-2009.
35
// Distributed under the Boost Software License, Version 1.0.
46
// (See accompanying file LICENSE_1_0.txt or copy at
57
// http://www.boost.org/LICENSE_1_0.txt)
68

7-
#ifndef __NETWORK_PROTOCOL_HTTP_CLIENT_20070908_1_HPP__
8-
#define __NETWORK_PROTOCOL_HTTP_CLIENT_20070908_1_HPP__
9-
109
#include <boost/network/version.hpp>
1110
#include <boost/network/traits/ostringstream.hpp>
1211
#include <boost/network/protocol/http/message.hpp>
@@ -27,24 +26,13 @@
2726
namespace boost { namespace network { namespace http {
2827

2928
template <class Tag, unsigned version_major, unsigned version_minor>
30-
class basic_client : connection_policy<Tag, version_major, version_minor>::type {
31-
32-
private:
33-
typedef typename connection_policy<Tag, version_major, version_minor>::type connection_base;
34-
boost::asio::io_service service_;
35-
typename connection_base::resolver_type resolver_;
36-
29+
struct basic_client : connection_policy<Tag, version_major, version_minor>::type {
30+
private:
3731
typedef typename string<Tag>::type string_type;
3832

39-
basic_response<Tag> const sync_request_skeleton(basic_request<Tag> const & request_, string_type method, bool get_body) {
40-
using boost::asio::ip::tcp;
41-
42-
typename connection_base::connection_ptr connection_;
43-
connection_ = connection_base::get_connection(resolver_, request_);
44-
return connection_->send_request(method, request_, get_body);
45-
}
46-
47-
public:
33+
public:
34+
typedef basic_request<Tag> request;
35+
typedef basic_response<Tag> response;
4836

4937
struct cache_resolved_type { };
5038

@@ -62,70 +50,87 @@ namespace boost { namespace network { namespace http {
6250
return follow_redirect_type();
6351
};
6452

53+
// Constructors
54+
// =================================================================
6555
basic_client()
6656
: connection_base(false, false), service_(), resolver_(service_)
67-
{};
57+
{}
6858

6959
explicit basic_client(cache_resolved_type (*)())
7060
: connection_base(true, false), service_(), resolver_(service_)
71-
{};
61+
{}
7262

7363
explicit basic_client(follow_redirect_type (*)())
7464
: connection_base(false, true), service_(), resolver_(service_)
75-
{};
65+
{}
7666

7767
basic_client(cache_resolved_type (*)(), follow_redirect_type (*)())
7868
: connection_base(true, true), service_(), resolver_(service_)
79-
{};
69+
{}
70+
71+
//
72+
// =================================================================
8073

8174
void clear_resolved_cache() {
8275
connection_base::endpoint_cache_.clear();
8376
}
8477

85-
basic_response<Tag> const head (basic_request<Tag> const & request_) {
78+
response const head (request const & request_) {
8679
return sync_request_skeleton(request_, "HEAD", false);
8780
};
8881

89-
basic_response<Tag> const get (basic_request<Tag> const & request_) {
82+
response const get (request const & request_) {
9083
return sync_request_skeleton(request_, "GET", true);
9184
};
9285

93-
basic_response<Tag> const post (basic_request<Tag> const & request_) {
86+
response const post (request const & request_) {
9487
return sync_request_skeleton(request_, "POST", true);
9588
};
9689

97-
basic_response<Tag> const post (basic_request<Tag> const & request_, string_type const & content_type, string_type const & body_) {
98-
basic_request<Tag> request_copy = request_;
90+
response const post (request const & request_, string_type const & content_type, string_type const & body_) {
91+
request request_copy = request_;
9992
request_copy << body(body_)
10093
<< header("Content-Type", content_type)
10194
<< header("Content-Length", boost::lexical_cast<string_type>(body_.size()));
10295
return post(request_copy);
10396
};
10497

105-
basic_response<Tag> const post (basic_request<Tag> const & request_, string_type const & body_) {
98+
response const post (request const & request_, string_type const & body_) {
10699
return post(request_, "x-application/octet-stream", body_);
107100
};
108101

109-
basic_response<Tag> const put (basic_request<Tag> const & request_) {
102+
response const put (request const & request_) {
110103
return sync_request_skeleton(request_, "PUT", true);
111104
};
112105

113-
basic_response<Tag> const put (basic_request<Tag> const & request_, string_type const & body_) {
106+
response const put (request const & request_, string_type const & body_) {
114107
return put(request_, "x-application/octet-stream", body_);
115108
};
116109

117-
basic_response<Tag> const put (basic_request<Tag> const & request_, string_type const & content_type, string_type const & body_) {
118-
basic_request<Tag> request_copy = request_;
110+
response const put (request const & request_, string_type const & content_type, string_type const & body_) {
111+
request request_copy = request_;
119112
request_copy << body(body_)
120113
<< header("Content-Type", content_type)
121114
<< header("Content-Length", boost::lexical_cast<string_type>(body_.size()));
122115
return put(request_copy);
123116
};
124117

125-
basic_response<Tag> const delete_ (basic_request<Tag> const & request_) {
118+
response const delete_ (request const & request_) {
126119
return sync_request_skeleton(request_, "DELETE", true);
127120
};
128121

122+
private:
123+
124+
typedef typename connection_policy<Tag, version_major, version_minor>::type connection_base;
125+
boost::asio::io_service service_;
126+
typename connection_base::resolver_type resolver_;
127+
128+
basic_response<Tag> const sync_request_skeleton(basic_request<Tag> const & request_, string_type method, bool get_body) {
129+
typename connection_base::connection_ptr connection_;
130+
connection_ = connection_base::get_connection(resolver_, request_);
131+
return connection_->send_request(method, request_, get_body);
132+
}
133+
129134
};
130135

131136
typedef basic_client<tags::http_default_8bit_tcp_resolve, 1, 0> client;
@@ -136,4 +141,4 @@ namespace boost { namespace network { namespace http {
136141

137142
} // namespace boost
138143

139-
#endif // __NETWORK_PROTOCOL_HTTP_CLIENT_20070908_1_HPP__
144+
#endif // BOOST_NETWORK_PROTOCOL_HTTP_CLIENT_20091215

boost/network/protocol/http/connection.hpp

+42-51
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,19 @@
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), [email protected]
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 {
4536
using boost::bind;
4637
using 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

5142
connection(io_service & service, Handler & handler)
5243
: service_(service)
@@ -75,8 +66,8 @@ namespace boost { namespace network { namespace http {
7566
boost::asio::buffer(buffer_),
7667
wrapper_.wrap(
7768
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(),
8071
boost::asio::placeholders::error,
8172
boost::asio::placeholders::bytes_transferred
8273
)
@@ -107,14 +98,14 @@ namespace boost { namespace network { namespace http {
10798
is_content_length()
10899
);
109100
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);
111102
boost::asio::async_write(
112103
socket_,
113-
reply_.to_buffers(),
104+
response_.to_buffers(),
114105
wrapper_.wrap(
115106
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(),
118109
boost::asio::placeholders::error
119110
)
120111
)
@@ -127,14 +118,14 @@ namespace boost { namespace network { namespace http {
127118
try {
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);
131122
boost::asio::async_write(
132123
socket_,
133-
reply_.to_buffers(),
124+
response_.to_buffers(),
134125
wrapper_.wrap(
135126
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(),
138129
boost::asio::placeholders::error
139130
)
140131
)
@@ -149,8 +140,8 @@ namespace boost { namespace network { namespace http {
149140
boost::asio::transfer_at_least(content_length),
150141
wrapper_.wrap(
151142
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(),
154145
boost::asio::placeholders::error,
155146
content_length,
156147
boost::asio::placeholders::bytes_transferred
@@ -160,41 +151,41 @@ namespace boost { namespace network { namespace http {
160151
return;
161152
}
162153

163-
handler_(request_, reply_);
154+
handler_(request_, response_);
164155
boost::asio::async_write(
165156
socket_,
166-
reply_.to_buffers(),
157+
response_.to_buffers(),
167158
wrapper_.wrap(
168159
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(),
171162
boost::asio::placeholders::error
172163
)
173164
)
174165
);
175166
} else {
176-
handler_(request_, reply_);
167+
handler_(request_, response_);
177168
boost::asio::async_write(
178169
socket_,
179-
reply_.to_buffers(),
170+
response_.to_buffers(),
180171
wrapper_.wrap(
181172
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(),
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);
191182
boost::asio::async_write(
192183
socket_,
193-
reply_.to_buffers(),
184+
response_.to_buffers(),
194185
wrapper_.wrap(
195186
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(),
198189
boost::asio::placeholders::error
199190
)
200191
)
@@ -204,8 +195,8 @@ namespace boost { namespace network { namespace http {
204195
boost::asio::buffer(buffer_),
205196
wrapper_.wrap(
206197
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(),
209200
boost::asio::placeholders::error,
210201
boost::asio::placeholders::bytes_transferred
211202
)
@@ -221,14 +212,14 @@ namespace boost { namespace network { namespace http {
221212
size_t difference = bytes_to_read - bytes_transferred;
222213
request_.body.append(buffer_.begin(), buffer_.end());
223214
if (difference == 0) {
224-
handler_(request_, reply_);
215+
handler_(request_, response_);
225216
boost::asio::async_write(
226217
socket_,
227-
reply_.to_buffers(),
218+
response_.to_buffers(),
228219
wrapper_.wrap(
229220
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(),
232223
boost::asio::placeholders::error
233224
)
234225
)
@@ -238,8 +229,8 @@ namespace boost { namespace network { namespace http {
238229
boost::asio::buffer(buffer_),
239230
wrapper_.wrap(
240231
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(),
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

0 commit comments

Comments
 (0)