Skip to content

Commit 6a6875a

Browse files
committed
Mine Tripped: http::request and http::response.
The thing I've been delaying for a long time already is something I now have to face. The good part about this is that there are concept requirements that I can use to keep the interface and semantics from changing. Unfortunately this also means that I need to come up with a working http::request and http::response implementation that *does the right thing* now that it's necessary. The http::http_async_normal_connection is undergoing a lot of changes especially now that the details of how the message stores the data has been abstracted out of the client. It used to be that the wiring of the promise objects and the future objects were evident in the http_async_connection implementation and the http::response<async> objects. That was bad design and now we're going all good OOP with this implementation, which means we're going to have to hide these in the http::response implementation. This is yet another black hole I'm going to disappear in as I wire in an efficient non-std::string storage mechanism for efficient small and large payload processing in the http::response objects. The same goes for supporting streams in the http::request objects. Hang on tight, from here on out there will be a lot of changes that will pertain to the http::request and http::response implementations that may not be for the faint at heart. Cheers
1 parent 7d932bf commit 6a6875a

File tree

4 files changed

+58
-39
lines changed

4 files changed

+58
-39
lines changed

boost/network/protocol/http/client/connection/async_normal.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <boost/asio/io_service.hpp>
1313
#include <boost/network/protocol/http/request.hpp>
1414
#include <boost/network/protocol/http/response.hpp>
15+
#include <boost/network/protocol/http/client/client_connection.hpp>
1516
#include <boost/network/protocol/http/client/connection/resolver_delegate.hpp>
1617
#include <boost/network/protocol/http/client/connection/connection_delegate.hpp>
1718

boost/network/protocol/http/client/connection/async_normal.ipp

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,40 @@
99

1010
#include <boost/asio/placeholders.hpp>
1111
#include <boost/network/protocol/http/client/connection/async_normal.hpp>
12+
#include <boost/network/protocol/http/algorithms/linearize.hpp>
13+
#include <boost/asio/strand.hpp>
1214

1315
namespace boost { namespace network { namespace http {
1416

1517
namespace placeholders = boost::asio::placeholders;
1618

1719
struct http_async_connection_pimpl : boost::enable_shared_from_this<http_async_connection_pimpl>
1820
{
21+
typedef http_async_connection::callback_type body_callback_function_type;
22+
typedef resolver_delegate::resolver_iterator resolver_iterator;
23+
typedef resolver_delegate::iterator_pair resolver_iterator_pair;
24+
1925
http_async_connection_pimpl(
20-
resolver_delegate_ptr resolver_delegate,
26+
shared_ptr<resolver_delegate> resolver_delegate,
27+
shared_ptr<connection_delegate> connection_delegate,
2128
asio::io_service & io_service,
22-
bool follow_redirect,
23-
connection_delegate_ptr delegate)
29+
bool follow_redirect)
2430
:
2531
follow_redirect_(follow_redirect),
2632
request_strand_(io_service),
2733
resolver_delegate_(resolver_delegate),
28-
delegate_(delegate) {}
34+
connection_delegate_(connection_delegate) {}
2935

3036
// This is the main entry point for the connection/request pipeline. We're
3137
// overriding async_connection_base<...>::start(...) here which is called
3238
// by the client.
3339
response start(request const & request,
34-
string_type const & method,
40+
std::string const & method,
3541
bool get_body,
3642
body_callback_function_type callback) {
3743
response response_;
38-
this->init_response(response_, get_body);
39-
linearize(request, method, version_major, version_minor,
40-
std::ostreambuf_iterator<typename char_<Tag>::type>(&command_streambuf));
44+
linearize(request, method, 1, 1,
45+
std::ostreambuf_iterator<char>(&command_streambuf));
4146
this->method = method;
4247
boost::uint16_t port_ = port(request);
4348
resolver_delegate_->resolve(
@@ -61,13 +66,14 @@ struct http_async_connection_pimpl : boost::enable_shared_from_this<http_async_c
6166

6267
void set_errors(boost::system::error_code const & ec) {
6368
boost::system::system_error error(ec);
64-
this->version_promise.set_exception(boost::copy_exception(error));
65-
this->status_promise.set_exception(boost::copy_exception(error));
66-
this->status_message_promise.set_exception(boost::copy_exception(error));
67-
this->headers_promise.set_exception(boost::copy_exception(error));
68-
this->source_promise.set_exception(boost::copy_exception(error));
69-
this->destination_promise.set_exception(boost::copy_exception(error));
70-
this->body_promise.set_exception(boost::copy_exception(error));
69+
// FIXME: Wire up the response object for errors.
70+
// this->version_promise.set_exception(boost::copy_exception(error));
71+
// this->status_promise.set_exception(boost::copy_exception(error));
72+
// this->status_message_promise.set_exception(boost::copy_exception(error));
73+
// this->headers_promise.set_exception(boost::copy_exception(error));
74+
// this->source_promise.set_exception(boost::copy_exception(error));
75+
// this->destination_promise.set_exception(boost::copy_exception(error));
76+
// this->body_promise.set_exception(boost::copy_exception(error));
7177
}
7278

7379
void handle_resolved(boost::uint16_t port,
@@ -80,7 +86,7 @@ struct http_async_connection_pimpl : boost::enable_shared_from_this<http_async_c
8086
// that there's still more endpoints to try connecting to.
8187
resolver_iterator iter = boost::begin(endpoint_range);
8288
asio::ip::tcp::endpoint endpoint(iter->endpoint().address(), port);
83-
delegate_->connect(endpoint,
89+
connection_delegate_->connect(endpoint,
8490
request_strand_.wrap(
8591
boost::bind(
8692
&this_type::handle_connected,
@@ -102,8 +108,8 @@ struct http_async_connection_pimpl : boost::enable_shared_from_this<http_async_c
102108
resolver_iterator_pair endpoint_range,
103109
boost::system::error_code const & ec) {
104110
if (!ec) {
105-
BOOST_ASSERT(delegate_.get() != 0);
106-
delegate_->write(command_streambuf,
111+
BOOST_ASSERT(connection_delegate_.get() != 0);
112+
connection_delegate_->write(command_streambuf,
107113
request_strand_.wrap(
108114
boost::bind(
109115
&this_type::handle_sent_request,
@@ -116,7 +122,7 @@ struct http_async_connection_pimpl : boost::enable_shared_from_this<http_async_c
116122
if (!boost::empty(endpoint_range)) {
117123
resolver_iterator iter = boost::begin(endpoint_range);
118124
asio::ip::tcp::endpoint endpoint(iter->endpoint().address(), port);
119-
delegate_->connect(endpoint,
125+
connection_delegate_->connect(endpoint,
120126
request_strand_.wrap(
121127
boost::bind(
122128
&this_type::handle_connected,
@@ -142,7 +148,7 @@ struct http_async_connection_pimpl : boost::enable_shared_from_this<http_async_c
142148
boost::system::error_code const & ec,
143149
std::size_t bytes_transferred) {
144150
if (!ec) {
145-
delegate_->read_some(
151+
connection_delegate_->read_some(
146152
boost::asio::mutable_buffers_1(this->part.c_array(),
147153
this->part.size()),
148154
request_strand_.wrap(
@@ -163,7 +169,7 @@ struct http_async_connection_pimpl : boost::enable_shared_from_this<http_async_c
163169
switch(state) {
164170
case version:
165171
parsed_ok =
166-
this->parse_version(delegate_,
172+
this->parse_version(connection_delegate_,
167173
request_strand_.wrap(
168174
boost::bind(
169175
&this_type::handle_received_data,
@@ -175,7 +181,7 @@ struct http_async_connection_pimpl : boost::enable_shared_from_this<http_async_c
175181
if (!parsed_ok || indeterminate(parsed_ok)) return;
176182
case status:
177183
parsed_ok =
178-
this->parse_status(delegate_,
184+
this->parse_status(connection_delegate_,
179185
request_strand_.wrap(
180186
boost::bind(
181187
&this_type::handle_received_data,
@@ -187,7 +193,7 @@ struct http_async_connection_pimpl : boost::enable_shared_from_this<http_async_c
187193
if (!parsed_ok || indeterminate(parsed_ok)) return;
188194
case status_message:
189195
parsed_ok =
190-
this->parse_status_message(delegate_,
196+
this->parse_status_message(connection_delegate_,
191197
request_strand_.wrap(
192198
boost::bind(
193199
&this_type::handle_received_data,
@@ -205,7 +211,7 @@ struct http_async_connection_pimpl : boost::enable_shared_from_this<http_async_c
205211
// that the data remaining in the buffer is dealt with before
206212
// another call to get more data for the body is scheduled.
207213
fusion::tie(parsed_ok, remainder) =
208-
this->parse_headers(delegate_,
214+
this->parse_headers(connection_delegate_,
209215
request_strand_.wrap(
210216
boost::bind(
211217
&this_type::handle_received_data,
@@ -251,7 +257,7 @@ struct http_async_connection_pimpl : boost::enable_shared_from_this<http_async_c
251257
// wait before scheduling another read.
252258
callback(make_iterator_range(begin, end), ec);
253259

254-
delegate_->read_some(
260+
connection_delegate_->read_some(
255261
boost::asio::mutable_buffers_1(this->part.c_array(),
256262
this->part.size()),
257263
request_strand_.wrap(
@@ -266,7 +272,7 @@ struct http_async_connection_pimpl : boost::enable_shared_from_this<http_async_c
266272
// Here we handle the body data ourself and append to an
267273
// ever-growing string buffer.
268274
this->parse_body(
269-
delegate_,
275+
connection_delegate_,
270276
request_strand_.wrap(
271277
boost::bind(
272278
&this_type::handle_received_data,
@@ -295,7 +301,7 @@ struct http_async_connection_pimpl : boost::enable_shared_from_this<http_async_c
295301
// it appropriately.
296302
callback(make_iterator_range(begin, end), ec);
297303
} else {
298-
string_type body_string;
304+
std::string body_string;
299305
std::swap(body_string, this->partial_parsed);
300306
body_string.append(
301307
this->part.begin()
@@ -319,7 +325,7 @@ struct http_async_connection_pimpl : boost::enable_shared_from_this<http_async_c
319325
typename protocol_base::buffer_type::const_iterator end = begin;
320326
std::advance(end, bytes_transferred);
321327
callback(make_iterator_range(begin, end), ec);
322-
delegate_->read_some(
328+
connection_delegate_->read_some(
323329
boost::asio::mutable_buffers_1(
324330
this->part.c_array(),
325331
this->part.size()),
@@ -337,7 +343,7 @@ struct http_async_connection_pimpl : boost::enable_shared_from_this<http_async_c
337343
// make sure that we deal with the remainder
338344
// from the headers part in case we do have data
339345
// that's still in the buffer.
340-
this->parse_body(delegate_,
346+
this->parse_body(connection_delegate_,
341347
request_strand_.wrap(
342348
boost::bind(
343349
&this_type::handle_received_data,
@@ -360,16 +366,16 @@ struct http_async_connection_pimpl : boost::enable_shared_from_this<http_async_c
360366
this->destination_promise.set_exception(boost::copy_exception(error));
361367
switch (state) {
362368
case version:
363-
this->version_promise.set_exception(boost::copy_exception(error));
369+
// this->version_promise.set_exception(boost::copy_exception(error));
364370
case status:
365-
this->status_promise.set_exception(boost::copy_exception(error));
371+
// this->status_promise.set_exception(boost::copy_exception(error));
366372
case status_message:
367-
this->status_message_promise.set_exception(boost::copy_exception(error));
373+
// this->status_message_promise.set_exception(boost::copy_exception(error));
368374
case headers:
369-
this->headers_promise.set_exception(boost::copy_exception(error));
375+
// this->headers_promise.set_exception(boost::copy_exception(error));
370376
case body:
371-
this->body_promise.set_exception(boost::copy_exception(error));
372-
break;
377+
// this->body_promise.set_exception(boost::copy_exception(error));
378+
// break;
373379
default:
374380
BOOST_ASSERT(false && "Bug, report this to the developers!");
375381
}
@@ -378,10 +384,10 @@ struct http_async_connection_pimpl : boost::enable_shared_from_this<http_async_c
378384

379385
bool follow_redirect_;
380386
boost::asio::io_service::strand request_strand_;
381-
resolver_delegate_ptr resolver_delegate_;
382-
connection_delegate_ptr delegate_;
387+
shared_ptr<resolver_delegate> resolver_delegate_;
388+
shared_ptr<connection_delegate> connection_delegate_;
383389
boost::asio::streambuf command_streambuf;
384-
string_type method;
390+
std::string method;
385391
};
386392

387393
// END OF PIMPL DEFINITION

libs/network/src/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ set(CPP-NETLIB_HTTP_CLIENT_CONNECTIONS_SRCS
3737
http/client_resolver_delegate_factory.cpp
3838
http/client_connection_delegates.cpp
3939
http/client_connection_factory.cpp
40-
http/client_async_resolver.cpp)
40+
http/client_async_resolver.cpp
41+
http/client_connection_normal.cpp)
4142
add_library(cppnetlib-http-client-connections ${CPP-NETLIB_HTTP_CLIENT_CONNECTIONS_SRCS})
4243

4344
set(CPP-NETLIB_HTTP_CLIENT_SRCS
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2011 Dean Michael Berris <dberris@google.com>.
2+
// Copyright 2011 Google, Inc.
3+
// Distributed under the Boost Software License, Version 1.0.
4+
// (See accompanying file LICENSE_1_0.txt or copy at
5+
// http://www.boost.org/LICENSE_1_0.txt)
6+
7+
#ifdef BOOST_NETWORK_NO_LIB
8+
#undef BOOST_NETWORK_NO_LIB
9+
#endif
10+
11+
#include <boost/network/protocol/http/client/connection/async_normal.ipp>

0 commit comments

Comments
 (0)