Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions boost/network/protocol/http/client/async_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ struct async_client
typedef function<bool(string_type&)> body_generator_function_type;

async_client(bool cache_resolved, bool follow_redirect,
bool always_verify_peer,
bool always_verify_peer, int timeout,
boost::shared_ptr<boost::asio::io_service> service,
optional<string_type> const& certificate_filename,
optional<string_type> const& verify_path,
optional<string_type> const& certificate_file,
optional<string_type> const& private_key_file)
: connection_base(cache_resolved, follow_redirect),
: connection_base(cache_resolved, follow_redirect, timeout),
service_ptr(service.get()
? service
: boost::make_shared<boost::asio::io_service>()),
Expand Down
2 changes: 2 additions & 0 deletions boost/network/protocol/http/client/connection/async_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ namespace boost { namespace network { namespace http { namespace impl {
bool follow_redirect,
bool always_verify_peer,
bool https,
int timeout,
optional<string_type> certificate_filename=optional<string_type>(),
optional<string_type> const & verify_path=optional<string_type>(),
optional<string_type> certificate_file=optional<string_type>(),
Expand All @@ -52,6 +53,7 @@ namespace boost { namespace network { namespace http { namespace impl {
resolver,
resolve,
follow_redirect,
timeout,
delegate_factory_type::new_connection_delegate(
resolver.get_io_service(),
https,
Expand Down
22 changes: 20 additions & 2 deletions boost/network/protocol/http/client/connection/async_normal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,11 @@ struct http_async_connection
connection_delegate_ptr;

http_async_connection(resolver_type& resolver, resolve_function resolve,
bool follow_redirect, connection_delegate_ptr delegate)
: follow_redirect_(follow_redirect),
bool follow_redirect, int timeout,
connection_delegate_ptr delegate)
: timeout_(timeout),
timer_(resolver.get_io_service()),
follow_redirect_(follow_redirect),
resolver_(resolver),
resolve_(resolve),
request_strand_(resolver.get_io_service()),
Expand All @@ -92,6 +95,13 @@ struct http_async_connection
request_strand_.wrap(boost::bind(
&this_type::handle_resolved, this_type::shared_from_this(),
port_, get_body, callback, generator, _1, _2)));
if (timeout_ > 0) {
timer_.expires_from_now(boost::posix_time::seconds(timeout_));
timer_.async_wait(request_strand_.wrap(
boost::bind(&this_type::handle_timeout,
this_type::shared_from_this(),
_1)));
}
return response_;
}

Expand All @@ -107,6 +117,11 @@ struct http_async_connection
this->source_promise.set_exception(boost::copy_exception(error));
this->destination_promise.set_exception(boost::copy_exception(error));
this->body_promise.set_exception(boost::copy_exception(error));
this->timer_.cancel();
}

void handle_timeout(boost::system::error_code const &ec) {
if (!ec) delegate_->disconnect();
}

void handle_resolved(boost::uint16_t port, bool get_body,
Expand Down Expand Up @@ -348,6 +363,7 @@ struct http_async_connection
this->source_promise.set_value("");
this->part.assign('\0');
this->response_parser_.reset();
this->timer_.cancel();
} else {
// This means the connection has not been closed yet and we want
// to get more
Expand Down Expand Up @@ -436,6 +452,8 @@ struct http_async_connection
return body;
}

int timeout_;
boost::asio::deadline_timer timer_;
bool follow_redirect_;
resolver_type& resolver_;
resolve_function resolve_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ struct connection_delegate {
function<void(system::error_code const &, size_t)> handler) = 0;
virtual void read_some(asio::mutable_buffers_1 const & read_buffer,
function<void(system::error_code const &, size_t)> handler) = 0;
virtual void disconnect() = 0;
virtual ~connection_delegate() {}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ struct normal_delegate : connection_delegate {
function<void(system::error_code const &, size_t)> handler);
virtual void read_some(asio::mutable_buffers_1 const & read_buffer,
function<void(system::error_code const &, size_t)> handler);
virtual void disconnect();
~normal_delegate();

private:
Expand Down
10 changes: 10 additions & 0 deletions boost/network/protocol/http/client/connection/normal_delegate.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ void boost::network::http::impl::normal_delegate::read_some(
socket_->async_read_some(read_buffer, handler);
}

void boost::network::http::impl::normal_delegate::disconnect() {
if (socket_.get() && socket_->is_open()) {
boost::system::error_code ignored;
socket_->shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored);
if (!ignored) {
socket_->close(ignored);
}
}
}

boost::network::http::impl::normal_delegate::~normal_delegate() {}

#endif /* BOOST_NETWORK_PROTOCOL_HTTP_CLIENT_CONNECTION_NORMAL_DELEGATE_IPP_20110819 */
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ struct ssl_delegate : connection_delegate,
virtual void read_some(
asio::mutable_buffers_1 const &read_buffer,
function<void(system::error_code const &, size_t)> handler);
virtual void disconnect();
~ssl_delegate();

private:
Expand Down
10 changes: 10 additions & 0 deletions boost/network/protocol/http/client/connection/ssl_delegate.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ void boost::network::http::impl::ssl_delegate::read_some(
socket_->async_read_some(read_buffer, handler);
}

void boost::network::http::impl::ssl_delegate::disconnect() {
if (socket_.get() && socket_->lowest_layer().is_open()) {
boost::system::error_code ignored;
socket_->lowest_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored);
if (!ignored) {
socket_->lowest_layer().close(ignored);
}
}
}

boost::network::http::impl::ssl_delegate::~ssl_delegate() {}

#endif /* BOOST_NETWORK_PROTOCOL_HTTP_CLIENT_CONNECTION_SSL_DELEGATE_IPP_20110819 \
Expand Down
7 changes: 4 additions & 3 deletions boost/network/protocol/http/client/connection/sync_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ struct sync_connection_base {
// ranges
static sync_connection_base<Tag, version_major, version_minor>*
new_connection(resolver_type& resolver, resolver_function_type resolve,
bool https, bool always_verify_peer,
bool https, bool always_verify_peer, int timeout,
optional<string_type> const& certificate_filename =
optional<string_type>(),
optional<string_type> const& verify_path =
Expand All @@ -253,7 +253,7 @@ struct sync_connection_base {
return dynamic_cast<
sync_connection_base<Tag, version_major, version_minor>*>(
new https_sync_connection<Tag, version_major, version_minor>(
resolver, resolve,
resolver, resolve, always_verify_peer, timeout,
certificate_filename, verify_path,
certificate_file, private_key_file));
#else
Expand All @@ -263,7 +263,8 @@ struct sync_connection_base {
return dynamic_cast<
sync_connection_base<Tag, version_major, version_minor>*>(
new http_sync_connection<Tag, version_major, version_minor>(resolver,
resolve));
resolve,
timeout));
}

virtual void init_socket(string_type const& hostname,
Expand Down
22 changes: 20 additions & 2 deletions boost/network/protocol/http/client/connection/sync_normal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,26 @@ struct sync_connection_base;
template <class Tag, unsigned version_major, unsigned version_minor>
struct http_sync_connection
: public virtual sync_connection_base<Tag, version_major, version_minor>,
sync_connection_base_impl<Tag, version_major, version_minor> {
sync_connection_base_impl<Tag, version_major, version_minor>,
boost::enable_shared_from_this<http_sync_connection<Tag, version_major, version_minor> > {
typedef typename resolver_policy<Tag>::type resolver_base;
typedef typename resolver_base::resolver_type resolver_type;
typedef typename string<Tag>::type string_type;
typedef function<typename resolver_base::resolver_iterator_pair(
resolver_type&,
string_type const&,
string_type const&)> resolver_function_type;
typedef http_sync_connection<Tag, version_major, version_minor> this_type;
typedef sync_connection_base_impl<Tag, version_major, version_minor>
connection_base;
typedef function<bool(string_type&)> body_generator_function_type;

http_sync_connection(resolver_type& resolver, resolver_function_type resolve)
http_sync_connection(resolver_type& resolver,
resolver_function_type resolve,
int timeout)
: connection_base(),
timeout_(timeout),
timer_(resolver.get_io_service()),
resolver_(resolver),
resolve_(resolve),
socket_(resolver.get_io_service()) {}
Expand Down Expand Up @@ -69,6 +75,12 @@ struct http_sync_connection
connection_base::send_request_impl(socket_, method, request_buffer);
}
}
if (timeout_ > 0) {
timer_.expires_from_now(boost::posix_time::seconds(timeout_));
timer_.async_wait(boost::bind(&this_type::handle_timeout,
this_type::shared_from_this(),
_1));
}
}

void read_status(basic_response<Tag>& response_,
Expand Down Expand Up @@ -97,6 +109,7 @@ struct http_sync_connection
bool is_open() { return socket_.is_open(); }

void close_socket() {
timer_.cancel();
if (!is_open())
return;
boost::system::error_code ignored;
Expand All @@ -107,7 +120,12 @@ struct http_sync_connection
}

private:
void handle_timeout(boost::system::error_code const &ec) {
if (!ec) close_socket();
}

int timeout_;
boost::asio::deadline_timer timer_;
resolver_type& resolver_;
resolver_function_type resolve_;
boost::asio::ip::tcp::socket socket_;
Expand Down
23 changes: 21 additions & 2 deletions boost/network/protocol/http/client/connection/sync_ssl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,25 @@ struct sync_connection_base;
template <class Tag, unsigned version_major, unsigned version_minor>
struct https_sync_connection
: public virtual sync_connection_base<Tag, version_major, version_minor>,
sync_connection_base_impl<Tag, version_major, version_minor> {
sync_connection_base_impl<Tag, version_major, version_minor>,
boost::enable_shared_from_this<https_sync_connection<Tag, version_major, version_minor> > {
typedef typename resolver_policy<Tag>::type resolver_base;
typedef typename resolver_base::resolver_type resolver_type;
typedef typename string<Tag>::type string_type;
typedef function<typename resolver_base::resolver_iterator_pair(
resolver_type&, string_type const&, string_type const&)>
resolver_function_type;
typedef https_sync_connection<Tag, version_major, version_minor> this_type;
typedef sync_connection_base_impl<Tag, version_major, version_minor>
connection_base;
typedef function<bool(string_type&)> body_generator_function_type;

// FIXME make the certificate filename and verify path parameters be
// optional ranges
https_sync_connection(resolver_type& resolver, resolver_function_type resolve,
https_sync_connection(resolver_type& resolver,
resolver_function_type resolve,
bool always_verify_peer,
int timeout,
optional<string_type> const& certificate_filename =
optional<string_type>(),
optional<string_type> const& verify_path =
Expand All @@ -57,6 +61,8 @@ struct https_sync_connection
optional<string_type> const& private_key_file =
optional<string_type>())
: connection_base(),
timeout_(timeout),
timer_(resolver.get_io_service()),
resolver_(resolver),
resolve_(resolve),
context_(resolver.get_io_service(),
Expand Down Expand Up @@ -107,6 +113,12 @@ struct https_sync_connection
connection_base::send_request_impl(socket_, method, request_buffer);
}
}
if (timeout_ > 0) {
timer_.expires_from_now(boost::posix_time::seconds(timeout_));
timer_.async_wait(boost::bind(&this_type::handle_timeout,
this_type::shared_from_this(),
_1));
}
}

void read_status(basic_response<Tag>& response_,
Expand Down Expand Up @@ -135,6 +147,7 @@ struct https_sync_connection
bool is_open() { return socket_.lowest_layer().is_open(); }

void close_socket() {
timer_.cancel();
boost::system::error_code ignored;
socket_.lowest_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_both,
ignored);
Expand All @@ -145,6 +158,12 @@ struct https_sync_connection
~https_sync_connection() { close_socket(); }

private:
void handle_timeout(boost::system::error_code const &ec) {
if (!ec) close_socket();
}

int timeout_;
boost::asio::deadline_timer timer_;
resolver_type& resolver_;
resolver_function_type resolve_;
boost::asio::ssl::context context_;
Expand Down
3 changes: 2 additions & 1 deletion boost/network/protocol/http/client/facade.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ struct basic_client_facade {
options.openssl_verify_path(),
options.openssl_certificate_file(),
options.openssl_private_key_file(),
options.io_service()));
options.io_service(),
options.timeout()));
}
};

Expand Down
15 changes: 13 additions & 2 deletions boost/network/protocol/http/client/options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ struct client_options {
openssl_certificate_file_(),
openssl_private_key_file_(),
io_service_(),
always_verify_peer_(false) {}
always_verify_peer_(false),
timeout_(0) {}

client_options(client_options const& other)
: cache_resolved_(other.cache_resolved_),
Expand All @@ -38,7 +39,8 @@ struct client_options {
openssl_certificate_file_(other.openssl_certificate_file_),
openssl_private_key_file_(other.openssl_private_key_file_),
io_service_(other.io_service_),
always_verify_peer_(other.always_verify_peer_) {}
always_verify_peer_(other.always_verify_peer_),
timeout_(other.timeout_) {}

client_options& operator=(client_options other) {
other.swap(*this);
Expand All @@ -55,6 +57,7 @@ struct client_options {
swap(openssl_private_key_file_, other.openssl_private_key_file_);
swap(io_service_, other.io_service_);
swap(always_verify_peer_, other.always_verify_peer_);
swap(timeout_, other.timeout_);
}

client_options& cache_resolved(bool v) {
Expand Down Expand Up @@ -92,6 +95,11 @@ struct client_options {
return *this;
}

client_options& timeout(int v) {
timeout_ = v;
return *this;
}

bool cache_resolved() const { return cache_resolved_; }

bool follow_redirects() const { return follow_redirects_; }
Expand All @@ -118,6 +126,8 @@ struct client_options {

bool always_verify_peer() const { return always_verify_peer_; }

int timeout() const { return timeout_; }

private:
bool cache_resolved_;
bool follow_redirects_;
Expand All @@ -127,6 +137,7 @@ struct client_options {
boost::optional<string_type> openssl_private_key_file_;
boost::shared_ptr<boost::asio::io_service> io_service_;
bool always_verify_peer_;
int timeout_;
};

template <class Tag>
Expand Down
6 changes: 4 additions & 2 deletions boost/network/protocol/http/client/pimpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ struct basic_client_impl
optional<string_type> const& verify_path,
optional<string_type> const& certificate_file,
optional<string_type> const& private_key_file,
boost::shared_ptr<boost::asio::io_service> service)
: base_type(cache_resolved, follow_redirect, always_verify_peer, service,
boost::shared_ptr<boost::asio::io_service> service,
int timeout)
: base_type(cache_resolved, follow_redirect,
always_verify_peer, timeout, service,
certificate_filename, verify_path,
certificate_file, private_key_file) {}

Expand Down
Loading