Skip to content

Commit 5a1c841

Browse files
umenneldeanberris
authored andcommitted
Make http client connection buffer size configurable (#843)
Fixes #842
1 parent 90847f3 commit 5a1c841

File tree

8 files changed

+50
-28
lines changed

8 files changed

+50
-28
lines changed

boost/network/protocol/http/client.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include <string>
2222

2323
#include <boost/network/protocol/http/client/facade.hpp>
24-
#include <boost/network/protocol/http/client/macros.hpp>
2524
#include <boost/network/protocol/http/client/options.hpp>
2625

2726
namespace boost {

boost/network/protocol/http/client/async_impl.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <functional>
1414
#include <boost/asio/io_service.hpp>
1515
#include <boost/asio/strand.hpp>
16+
#include <boost/network/protocol/http/client/macros.hpp>
1617
#include <boost/network/protocol/http/traits/connection_policy.hpp>
1718

1819
namespace boost {
@@ -31,7 +32,10 @@ struct async_client
3132
typedef typename resolver<Tag>::type resolver_type;
3233
typedef typename string<Tag>::type string_type;
3334

34-
typedef typename std::array<typename char_<Tag>::type, 1024>::const_iterator const_iterator;
35+
typedef
36+
typename std::array<typename char_<Tag>::type,
37+
BOOST_NETWORK_HTTP_CLIENT_CONNECTION_BUFFER_SIZE>::
38+
const_iterator const_iterator;
3539
typedef iterator_range<const_iterator> char_const_range;
3640

3741
typedef std::function<void(char_const_range,

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ struct async_connection_base {
3131
typedef typename string<Tag>::type string_type;
3232
typedef basic_request<Tag> request;
3333
typedef basic_response<Tag> response;
34-
typedef typename std::array<typename char_<Tag>::type, 1024>::const_iterator const_iterator;
34+
typedef
35+
typename std::array<typename char_<Tag>::type,
36+
BOOST_NETWORK_HTTP_CLIENT_CONNECTION_BUFFER_SIZE>::
37+
const_iterator const_iterator;
3538
typedef iterator_range<const_iterator> char_const_range;
3639
typedef std::function<void(char_const_range const &, boost::system::error_code const &)>
3740
body_callback_function_type;

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

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,20 @@ namespace network {
3838
namespace http {
3939
namespace impl {
4040

41-
template <class Tag>
41+
template <class buffer_type>
4242
struct chunk_encoding_parser {
43+
typedef typename buffer_type::const_iterator const_iterator;
44+
typedef boost::iterator_range<const_iterator> char_const_range;
45+
4346
chunk_encoding_parser() : state(state_t::header), chunk_size(0) {}
4447

4548
enum class state_t { header, header_end, data, data_end };
4649

4750
state_t state;
4851
size_t chunk_size;
49-
std::array<typename char_<Tag>::type, 1024> buffer;
52+
buffer_type buffer;
5053

51-
void update_chunk_size(
52-
boost::iterator_range<typename std::array<
53-
typename char_<Tag>::type, 1024>::const_iterator> const &range) {
54+
void update_chunk_size(char_const_range const &range) {
5455
if (range.empty()) return;
5556
std::stringstream ss;
5657
ss << std::hex << range;
@@ -60,11 +61,7 @@ struct chunk_encoding_parser {
6061
chunk_size = (chunk_size << (range.size() * 4)) | size;
6162
}
6263

63-
boost::iterator_range<
64-
typename std::array<typename char_<Tag>::type, 1024>::const_iterator>
65-
operator()(
66-
boost::iterator_range<typename std::array<
67-
typename char_<Tag>::type, 1024>::const_iterator> const &range) {
64+
char_const_range operator()(char_const_range const &range) {
6865
auto iter = boost::begin(range);
6966
auto begin = iter;
7067
auto pos = boost::begin(buffer);
@@ -147,6 +144,7 @@ struct http_async_connection
147144
typedef typename delegate_factory<Tag>::type delegate_factory_type;
148145
typedef typename delegate_factory_type::connection_delegate_ptr
149146
connection_delegate_ptr;
147+
typedef chunk_encoding_parser<typename protocol_base::buffer_type> chunk_encoding_parser_type;
150148

151149
http_async_connection(resolver_type& resolver, resolve_function resolve,
152150
bool follow_redirect, int timeout,
@@ -484,13 +482,14 @@ struct http_async_connection
484482
} else {
485483
string_type body_string;
486484
if (this->is_chunk_encoding && remove_chunk_markers_) {
487-
for (size_t i = 0; i < this->partial_parsed.size(); i += 1024) {
485+
const auto parse_buffer_size = parse_chunk_encoding.buffer.size();
486+
for (size_t i = 0; i < this->partial_parsed.size(); i += parse_buffer_size) {
488487
auto range = parse_chunk_encoding(boost::make_iterator_range(
489-
static_cast<typename std::array<typename char_<Tag>::type, 1024>::const_iterator>(
490-
this->partial_parsed.data()) + i,
491-
static_cast<typename std::array<typename char_<Tag>::type, 1024>::const_iterator>(
492-
this->partial_parsed.data()) +
493-
std::min(i + 1024, this->partial_parsed.size())));
488+
static_cast<
489+
typename chunk_encoding_parser_type::const_iterator>(this->partial_parsed.data()) + i,
490+
static_cast<
491+
typename chunk_encoding_parser_type::const_iterator>(this->partial_parsed.data()) +
492+
std::min(i + parse_buffer_size, this->partial_parsed.size())));
494493
body_string.append(boost::begin(range), boost::end(range));
495494
}
496495
this->partial_parsed.clear();
@@ -602,7 +601,7 @@ struct http_async_connection
602601
connection_delegate_ptr delegate_;
603602
boost::asio::streambuf command_streambuf;
604603
string_type method;
605-
chunk_encoding_parser<Tag> parse_chunk_encoding;
604+
chunk_encoding_parser_type parse_chunk_encoding;
606605
};
607606

608607
} // namespace impl

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <array>
1414
#include <boost/logic/tribool.hpp>
1515
#include <boost/network/detail/debug.hpp>
16+
#include <boost/network/protocol/http/client/macros.hpp>
1617
#include <boost/network/protocol/http/algorithms/linearize.hpp>
1718
#include <boost/network/protocol/http/parser/incremental.hpp>
1819
#include <boost/network/protocol/http/request_parser.hpp>
@@ -400,8 +401,8 @@ struct http_async_protocol_handler {
400401
}
401402

402403
typedef response_parser<Tag> response_parser_type;
403-
// TODO(dberris): make 1024 go away and become a configurable value.
404-
typedef std::array<typename char_<Tag>::type, 1024> buffer_type;
404+
typedef std::array<typename char_<Tag>::type,
405+
BOOST_NETWORK_HTTP_CLIENT_CONNECTION_BUFFER_SIZE> buffer_type;
405406

406407
response_parser_type response_parser_;
407408
boost::promise<string_type> version_promise;

boost/network/protocol/http/client/facade.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ class basic_client_facade {
4040
/** The response type. This models the HTTP Response concept.*/
4141
typedef basic_response<Tag> response;
4242

43-
typedef typename std::array<typename char_<Tag>::type, 1024>::const_iterator const_iterator;
43+
typedef
44+
typename std::array<typename char_<Tag>::type,
45+
BOOST_NETWORK_HTTP_CLIENT_CONNECTION_BUFFER_SIZE>::
46+
const_iterator const_iterator;
4447
typedef iterator_range<const_iterator> char_const_range;
4548

4649
/**

boost/network/protocol/http/client/macros.hpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,21 @@
1010
#include <array>
1111
#include <system_error>
1212

13+
#ifndef BOOST_NETWORK_HTTP_CLIENT_CONNECTION_BUFFER_SIZE
14+
/**
15+
* We define the buffer size for each connection that we will use on the client
16+
* side.
17+
*/
18+
#define BOOST_NETWORK_HTTP_CLIENT_CONNECTION_BUFFER_SIZE 4096uL
19+
#endif
20+
1321
#ifndef BOOST_NETWORK_HTTP_BODY_CALLBACK
14-
#define BOOST_NETWORK_HTTP_BODY_CALLBACK(function_name, range_name, \
15-
error_name) \
16-
void function_name(boost::iterator_range<std::array<char, 1024>::const_iterator> (range_name), \
17-
boost::system::error_code const& (error_name))
22+
#define BOOST_NETWORK_HTTP_BODY_CALLBACK(function_name, range_name, error_name)\
23+
void function_name( \
24+
boost::iterator_range< \
25+
std::array<char, BOOST_NETWORK_HTTP_CLIENT_CONNECTION_BUFFER_SIZE>:: \
26+
const_iterator>(range_name), \
27+
boost::system::error_code const&(error_name))
1828
#endif
1929

2030
#endif /* BOOST_NETWORK_PROTOCOL_HTTP_CLIENT_MACROS_HPP_20110430 */

boost/network/protocol/http/policies/async_connection.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ struct async_connection_policy : resolver_policy<Tag>::type {
3030
typedef typename resolver_base::resolve_function resolve_function;
3131
typedef typename resolver_base::resolve_completion_function
3232
resolve_completion_function;
33-
typedef typename std::array<typename char_<Tag>::type, 1024>::const_iterator const_iterator;
33+
typedef
34+
typename std::array<typename char_<Tag>::type,
35+
BOOST_NETWORK_HTTP_CLIENT_CONNECTION_BUFFER_SIZE>::
36+
const_iterator const_iterator;
3437
typedef iterator_range<const_iterator> char_const_range;
3538
typedef std::function<void(char_const_range,
3639
boost::system::error_code const&)>

0 commit comments

Comments
 (0)