Skip to content

Commit 72d6d02

Browse files
committed
Refactoring the request linearization to use the linearize algorithm in asynchronous client connections.
1 parent 85eb3dd commit 72d6d02

File tree

4 files changed

+17
-81
lines changed

4 files changed

+17
-81
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <boost/network/protocol/http/parser/incremental.hpp>
2121
#include <boost/network/protocol/http/message/wrappers/uri.hpp>
2222
#include <boost/network/protocol/http/client/connection/async_protocol_handler.hpp>
23+
#include <boost/network/protocol/http/algorithms/linearize.hpp>
2324
#include <boost/array.hpp>
2425
#include <boost/assert.hpp>
2526
#include <iterator>
@@ -60,7 +61,8 @@ namespace boost { namespace network { namespace http { namespace impl {
6061
virtual response start(request const & request, string_type const & method, bool get_body) {
6162
response response_;
6263
this->init_response(response_, get_body);
63-
command_string_ = this->init_command_stream(request, method);
64+
linearize(request, method, version_major, version_minor,
65+
std::ostreambuf_iterator<typename char_<Tag>::type>(&command_streambuf));
6466
this->method = method;
6567
boost::uint16_t port_ = port(request);
6668
resolve_(resolver_, host(request),
@@ -107,8 +109,10 @@ namespace boost { namespace network { namespace http { namespace impl {
107109

108110
void handle_connected(boost::uint16_t port, bool get_body, resolver_iterator_pair endpoint_range, boost::system::error_code const & ec) {
109111
if (!ec) {
110-
boost::asio::async_write(*socket_, boost::asio::buffer(command_string_.data(), command_string_.size()),
111-
request_strand_.wrap(
112+
boost::asio::async_write(
113+
*socket_
114+
, command_streambuf
115+
, request_strand_.wrap(
112116
boost::bind(
113117
&http_async_connection<Tag,version_major,version_minor>::handle_sent_request,
114118
http_async_connection<Tag,version_major,version_minor>::shared_from_this(),
@@ -308,7 +312,7 @@ namespace boost { namespace network { namespace http { namespace impl {
308312
boost::shared_ptr<boost::asio::ip::tcp::socket> socket_;
309313
resolve_function resolve_;
310314
boost::asio::io_service::strand request_strand_;
311-
string_type command_string_;
315+
boost::asio::streambuf command_streambuf;
312316
string_type method;
313317
};
314318

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

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
// (See accompanying file LICENSE_1_0.txt or copy at
77
// http://www.boost.org/LICENSE_1_0.txt)
88

9+
#include <boost/network/protocol/http/algorithms/linearize.hpp>
10+
911
namespace boost { namespace network { namespace http { namespace impl {
1012

1113
template <class Tag, unsigned version_major, unsigned version_minor>
@@ -48,52 +50,6 @@ namespace boost { namespace network { namespace http { namespace impl {
4850
}
4951
};
5052

51-
template <class RequestType>
52-
string_type init_command_stream(RequestType const & request, string_type const & method) {
53-
typename ostringstream<Tag>::type command_stream;
54-
string_type path_str;
55-
path_str = path(request);
56-
typedef constants<Tag> constants;
57-
command_stream
58-
<< method << constants::space()
59-
<< path_str << constants::space()
60-
<< constants::http_slash() << version_major
61-
<< constants::dot() << version_minor
62-
<< constants::crlf();
63-
64-
typedef typename headers_range<RequestType>::type headers_range_type;
65-
headers_range_type headers_ = headers(request);
66-
boost::range::transform(
67-
headers_,
68-
typename ostream_iterator<Tag, string_type>::type (command_stream),
69-
to_http_headers());
70-
71-
if (boost::empty(headers(request)[constants::host()])) {
72-
string_type host_str = host(request);
73-
command_stream
74-
<< constants::host() << constants::colon() << constants::space() << host_str << constants::crlf();
75-
}
76-
77-
if (boost::empty(headers(request)[constants::accept()])) {
78-
command_stream
79-
<< constants::accept() << constants::colon() << constants::space() << constants::default_accept_mime() << constants::crlf();
80-
}
81-
82-
if (version_major == 1u && version_minor == 1u && boost::empty(headers(request)[constants::accept_encoding()])) {
83-
command_stream
84-
<< constants::accept_encoding() << constants::colon() << constants::space() << constants::default_accept_encoding() << constants::crlf();
85-
}
86-
87-
if (boost::empty(headers(request)[constants::user_agent()])) {
88-
command_stream
89-
<< constants::user_agent() << constants::colon() << constants::space() << constants::cpp_netlib_slash() << BOOST_NETLIB_VERSION << constants::crlf();
90-
}
91-
92-
command_stream << constants::crlf();
93-
94-
return command_stream.str();
95-
}
96-
9753
template <class Socket, class Callback>
9854
logic::tribool parse_version(Socket & socket_, Callback callback) {
9955
logic::tribool parsed_ok;

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ namespace boost { namespace network { namespace http { namespace impl {
5050
virtual response start(request const & request, string_type const & method, bool get_body) {
5151
response response_;
5252
this->init_response(response_, get_body);
53-
command_string_ = this->init_command_stream(request, method);
53+
linearize(request, method, version_major, version_minor,
54+
std::ostreambuf_iterator<typename char_<Tag>::type>(&command_streambuf));
5455
this->method = method;
5556
boost::uint16_t port_ = port(request);
5657
resolve_(resolver_, host(request),
@@ -159,8 +160,10 @@ namespace boost { namespace network { namespace http { namespace impl {
159160

160161
void handle_handshake(boost::uint16_t port, bool get_body, boost::system::error_code const & ec) {
161162
if (!ec) {
162-
boost::asio::async_write(*socket_, boost::asio::buffer(command_string_.data(), command_string_.size()),
163-
request_strand_.wrap(
163+
boost::asio::async_write(
164+
*socket_
165+
, command_streambuf
166+
, request_strand_.wrap(
164167
boost::bind(
165168
&https_async_connection<Tag,version_major,version_minor>::handle_sent_request,
166169
https_async_connection<Tag,version_major,version_minor>::shared_from_this(),
@@ -344,7 +347,7 @@ namespace boost { namespace network { namespace http { namespace impl {
344347
boost::shared_ptr<boost::asio::ssl::context> context_;
345348
boost::shared_ptr<boost::asio::ssl::stream<boost::asio::ip::tcp::socket> > socket_;
346349
boost::asio::io_service::strand request_strand_;
347-
string_type command_string_;
350+
boost::asio::streambuf command_streambuf;
348351
string_type method;
349352
};
350353

libs/network/test/https_localhost_tests.cpp

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -55,33 +55,6 @@ namespace {
5555
return buffer.size();
5656
}
5757

58-
std::map<std::string, std::string> parse_headers(std::string const& body) {
59-
std::map<std::string, std::string> headers;
60-
61-
std::istringstream stream(body);
62-
while (stream.good())
63-
{
64-
std::string line;
65-
std::getline(stream, line);
66-
if (!stream.eof())
67-
{
68-
std::size_t colon = line.find(':');
69-
if (colon != std::string::npos)
70-
{
71-
std::string header = line.substr(0, colon);
72-
std::string value = line.substr(colon + 2);
73-
headers[header] = value;
74-
}
75-
}
76-
}
77-
78-
return headers;
79-
}
80-
81-
std::string get_content_length(std::string const& content) {
82-
return boost::lexical_cast<std::string>(content.size());
83-
}
84-
8558
}
8659

8760
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)

0 commit comments

Comments
 (0)