Cleanup of #127: using standalone asio.

This commit is contained in:
eidheim 2017-06-07 18:36:53 +02:00
commit ec63c4345e
8 changed files with 238 additions and 265 deletions

View file

@ -7,79 +7,58 @@
#include <mutex>
#include <type_traits>
#ifdef NO_BOOST
#ifdef USE_STANDALONE_ASIO
#include <boost/utility/string_ref.hpp>
#include <asio.hpp>
#include <type_traits>
#include <system_error>
namespace asio_ns = asio;
namespace error_ns = std;
namespace merror_ns = std;
# ifndef CASE_INSENSITIVE_EQUALS_AND_HASH
# define CASE_INSENSITIVE_EQUALS_AND_HASH
class case_insensitive_equals {
public:
bool operator()(const std::string &Left, const std::string &Right) const {
return Left.size() == Right.size()
&& std::equal ( Left.begin() , Left.end() , Right.begin() ,
[]( char a , char b ) {
return tolower(a) == tolower(b);
});
}
};
bool IEQUALS(const std::string& Left, const std::string& Right)
{
return Left.size() == Right.size()
&& std::equal ( Left.begin() , Left.end() , Right.begin() , []( char a , char b ) {
return tolower(a) == tolower(b);
});
namespace SimpleWeb {
using error_code = std::error_code;
using errc = std::errc;
using system_error = std::system_error;
}
class case_insensitive_hash {
public:
size_t operator()(const std::string& Keyval) const
{
//You might need a better hash function than this
size_t h = 0;
std::for_each( Keyval.begin() , Keyval.end() , [&](char c ) {
h += tolower(c);
});
return h;
}
};
# endif
#else
#include <boost/asio.hpp>
#include <boost/utility/string_ref.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/functional/hash.hpp>
namespace asio_ns = boost::asio;
namespace error_ns = boost::system;
namespace merror_ns = boost::system::errc;
namespace SimpleWeb {
namespace asio = boost::asio;
using error_code = boost::system::error_code;
namespace errc = boost::system::errc;
using system_error = boost::system::system_error;
}
#endif
# ifndef CASE_INSENSITIVE_EQUALS_AND_HASH
# define CASE_INSENSITIVE_EQUALS_AND_HASH
# define IEQUALS boost::iequals
//Based on http://www.boost.org/doc/libs/1_60_0/doc/html/unordered/hash_equality.html
class case_insensitive_equals {
public:
bool operator()(const std::string &key1, const std::string &key2) const {
return boost::algorithm::iequals(key1, key2);
}
};
class case_insensitive_hash {
public:
size_t operator()(const std::string &key) const {
std::size_t seed=0;
for(auto &c: key)
boost::hash_combine(seed, std::tolower(c));
return seed;
}
};
namespace SimpleWeb {
bool iequals(const std::string &str1, const std::string &str2) {
return str1.size() == str2.size() &&
std::equal(str1.begin(), str1.end(), str2.begin(), [](char a, char b) {
return tolower(a) == tolower(b);
});
}
class case_insensitive_equals {
public:
bool operator()(const std::string &str1, const std::string &str2) const {
return iequals(str1, str2);
}
};
// Based on https://stackoverflow.com/questions/2590677/how-do-i-combine-hash-values-in-c0x/2595226#2595226
class case_insensitive_hash {
public:
size_t operator()(const std::string &str) const {
size_t h = 0;
std::hash<int> hash;
for (auto c : str)
h ^= hash(tolower(c)) + 0x9e3779b9 + (h << 6) + (h >> 2);
return h;
}
};
}
# endif
#endif
namespace SimpleWeb {
template <class socket_type>
class Client;
@ -100,7 +79,7 @@ namespace SimpleWeb {
std::unordered_multimap<std::string, std::string, case_insensitive_hash, case_insensitive_equals> header;
private:
asio_ns::streambuf content_buffer;
asio::streambuf content_buffer;
Response(): content(&content_buffer) {}
};
@ -126,10 +105,10 @@ namespace SimpleWeb {
auto corrected_path=path;
if(corrected_path=="")
corrected_path="/";
if(!config.proxy_server.empty() && std::is_same<socket_type, asio_ns::ip::tcp::socket>::value)
if(!config.proxy_server.empty() && std::is_same<socket_type, asio::ip::tcp::socket>::value)
corrected_path="http://"+host+':'+std::to_string(port)+corrected_path;
asio_ns::streambuf write_buffer;
asio::streambuf write_buffer;
std::ostream write_stream(&write_buffer);
write_stream << request_type << " " << corrected_path << " HTTP/1.1\r\n";
write_stream << "Host: " << host << "\r\n";
@ -143,21 +122,21 @@ namespace SimpleWeb {
connect();
auto timer=get_timeout_timer();
asio_ns::async_write(*socket, write_buffer,
[this, &content, timer](const error_ns::error_code &ec, size_t /*bytes_transferred*/) {
asio::async_write(*socket, write_buffer,
[this, &content, timer](const error_code &ec, size_t /*bytes_transferred*/) {
if(timer)
timer->cancel();
if(!ec) {
if(!content.empty()) {
auto timer=get_timeout_timer();
asio_ns::async_write(*socket, asio_ns::buffer(content.data(), content.size()),
[this, timer](const error_ns::error_code &ec, size_t /*bytes_transferred*/) {
asio::async_write(*socket, asio::buffer(content.data(), content.size()),
[this, timer](const error_code &ec, size_t /*bytes_transferred*/) {
if(timer)
timer->cancel();
if(ec) {
std::lock_guard<std::mutex> lock(socket_mutex);
this->socket=nullptr;
throw error_ns::system_error(ec);
throw system_error(ec);
}
});
}
@ -165,7 +144,7 @@ namespace SimpleWeb {
else {
std::lock_guard<std::mutex> lock(socket_mutex);
socket=nullptr;
throw error_ns::system_error(ec);
throw system_error(ec);
}
});
io_service.reset();
@ -179,14 +158,14 @@ namespace SimpleWeb {
auto corrected_path=path;
if(corrected_path=="")
corrected_path="/";
if(!config.proxy_server.empty() && std::is_same<socket_type, asio_ns::ip::tcp::socket>::value)
if(!config.proxy_server.empty() && std::is_same<socket_type, asio::ip::tcp::socket>::value)
corrected_path="http://"+host+':'+std::to_string(port)+corrected_path;
content.seekp(0, std::ios::end);
auto content_length=content.tellp();
content.seekp(0, std::ios::beg);
asio_ns::streambuf write_buffer;
asio::streambuf write_buffer;
std::ostream write_stream(&write_buffer);
write_stream << request_type << " " << corrected_path << " HTTP/1.1\r\n";
write_stream << "Host: " << host << "\r\n";
@ -202,14 +181,14 @@ namespace SimpleWeb {
connect();
auto timer=get_timeout_timer();
asio_ns::async_write(*socket, write_buffer,
[this, timer](const error_ns::error_code &ec, size_t /*bytes_transferred*/) {
asio::async_write(*socket, write_buffer,
[this, timer](const error_code &ec, size_t /*bytes_transferred*/) {
if(timer)
timer->cancel();
if(ec) {
std::lock_guard<std::mutex> lock(socket_mutex);
socket=nullptr;
throw error_ns::system_error(ec);
throw system_error(ec);
}
});
io_service.reset();
@ -221,15 +200,15 @@ namespace SimpleWeb {
void close() {
std::lock_guard<std::mutex> lock(socket_mutex);
if(socket) {
error_ns::error_code ec;
socket->lowest_layer().shutdown(asio_ns::ip::tcp::socket::shutdown_both, ec);
error_code ec;
socket->lowest_layer().shutdown(asio::ip::tcp::socket::shutdown_both, ec);
socket->lowest_layer().close();
}
}
protected:
asio_ns::io_service io_service;
asio_ns::ip::tcp::resolver resolver;
asio::io_service io_service;
asio::ip::tcp::resolver resolver;
std::unique_ptr<socket_type> socket;
std::mutex socket_mutex;
@ -259,15 +238,15 @@ namespace SimpleWeb {
virtual void connect()=0;
std::shared_ptr<asio_ns::deadline_timer> get_timeout_timer(size_t timeout=0) {
std::shared_ptr<asio::deadline_timer> get_timeout_timer(size_t timeout=0) {
if(timeout==0)
timeout=config.timeout;
if(timeout==0)
return nullptr;
auto timer=std::make_shared<asio_ns::deadline_timer>(io_service);
auto timer=std::make_shared<asio::deadline_timer>(io_service);
timer->expires_from_now(boost::posix_time::seconds(timeout));
timer->async_wait([this](const error_ns::error_code& ec) {
timer->async_wait([this](const error_code& ec) {
if(!ec) {
close();
}
@ -304,11 +283,11 @@ namespace SimpleWeb {
std::shared_ptr<Response> request_read() {
std::shared_ptr<Response> response(new Response());
asio_ns::streambuf chunked_streambuf;
asio::streambuf chunked_streambuf;
auto timer=get_timeout_timer();
asio_ns::async_read_until(*socket, response->content_buffer, "\r\n\r\n",
[this, &response, &chunked_streambuf, timer](const error_ns::error_code& ec, size_t bytes_transferred) {
asio::async_read_until(*socket, response->content_buffer, "\r\n\r\n",
[this, &response, &chunked_streambuf, timer](const error_code& ec, size_t bytes_transferred) {
if(timer)
timer->cancel();
if(!ec) {
@ -321,15 +300,15 @@ namespace SimpleWeb {
auto content_length=stoull(header_it->second);
if(content_length>num_additional_bytes) {
auto timer=get_timeout_timer();
asio_ns::async_read(*socket, response->content_buffer,
asio_ns::transfer_exactly(content_length-num_additional_bytes),
[this, timer](const error_ns::error_code& ec, size_t /*bytes_transferred*/) {
asio::async_read(*socket, response->content_buffer,
asio::transfer_exactly(content_length-num_additional_bytes),
[this, timer](const error_code& ec, size_t /*bytes_transferred*/) {
if(timer)
timer->cancel();
if(ec) {
std::lock_guard<std::mutex> lock(socket_mutex);
this->socket=nullptr;
throw error_ns::system_error(ec);
throw system_error(ec);
}
});
}
@ -339,15 +318,15 @@ namespace SimpleWeb {
}
else if(response->http_version<"1.1" || ((header_it=response->header.find("Connection"))!=response->header.end() && header_it->second=="close")) {
auto timer=get_timeout_timer();
asio_ns::async_read(*socket, response->content_buffer,
[this, timer](const error_ns::error_code& ec, size_t /*bytes_transferred*/) {
asio::async_read(*socket, response->content_buffer,
[this, timer](const error_code& ec, size_t /*bytes_transferred*/) {
if(timer)
timer->cancel();
if(ec) {
std::lock_guard<std::mutex> lock(socket_mutex);
this->socket=nullptr;
if(ec!=asio_ns::error::eof)
throw error_ns::system_error(ec);
if(ec!=asio::error::eof)
throw system_error(ec);
}
});
}
@ -355,7 +334,7 @@ namespace SimpleWeb {
else {
std::lock_guard<std::mutex> lock(socket_mutex);
socket=nullptr;
throw error_ns::system_error(ec);
throw system_error(ec);
}
});
io_service.reset();
@ -364,10 +343,10 @@ namespace SimpleWeb {
return response;
}
void request_read_chunked(const std::shared_ptr<Response> &response, asio_ns::streambuf &streambuf) {
void request_read_chunked(const std::shared_ptr<Response> &response, asio::streambuf &streambuf) {
auto timer=get_timeout_timer();
asio_ns::async_read_until(*socket, response->content_buffer, "\r\n",
[this, &response, &streambuf, timer](const error_ns::error_code& ec, size_t bytes_transferred) {
asio::async_read_until(*socket, response->content_buffer, "\r\n",
[this, &response, &streambuf, timer](const error_code& ec, size_t bytes_transferred) {
if(timer)
timer->cancel();
if(!ec) {
@ -401,9 +380,9 @@ namespace SimpleWeb {
if((2+length)>num_additional_bytes) {
auto timer=get_timeout_timer();
asio_ns::async_read(*socket, response->content_buffer,
asio_ns::transfer_exactly(2+length-num_additional_bytes),
[this, post_process, timer](const error_ns::error_code& ec, size_t /*bytes_transferred*/) {
asio::async_read(*socket, response->content_buffer,
asio::transfer_exactly(2+length-num_additional_bytes),
[this, post_process, timer](const error_code& ec, size_t /*bytes_transferred*/) {
if(timer)
timer->cancel();
if(!ec) {
@ -412,7 +391,7 @@ namespace SimpleWeb {
else {
std::lock_guard<std::mutex> lock(socket_mutex);
this->socket=nullptr;
throw error_ns::system_error(ec);
throw system_error(ec);
}
});
}
@ -422,7 +401,7 @@ namespace SimpleWeb {
else {
std::lock_guard<std::mutex> lock(socket_mutex);
socket=nullptr;
throw error_ns::system_error(ec);
throw system_error(ec);
}
});
}
@ -431,7 +410,7 @@ namespace SimpleWeb {
template<class socket_type>
class Client : public ClientBase<socket_type> {};
typedef asio_ns::ip::tcp::socket HTTP;
typedef asio::ip::tcp::socket HTTP;
template<>
class Client<HTTP> : public ClientBase<HTTP> {
@ -441,15 +420,15 @@ namespace SimpleWeb {
protected:
void connect() {
if(!socket || !socket->is_open()) {
std::unique_ptr<asio_ns::ip::tcp::resolver::query> query;
std::unique_ptr<asio::ip::tcp::resolver::query> query;
if(config.proxy_server.empty())
query=std::unique_ptr<asio_ns::ip::tcp::resolver::query>(new asio_ns::ip::tcp::resolver::query(host, std::to_string(port)));
query=std::unique_ptr<asio::ip::tcp::resolver::query>(new asio::ip::tcp::resolver::query(host, std::to_string(port)));
else {
auto proxy_host_port=parse_host_port(config.proxy_server, 8080);
query=std::unique_ptr<asio_ns::ip::tcp::resolver::query>(new asio_ns::ip::tcp::resolver::query(proxy_host_port.first, std::to_string(proxy_host_port.second)));
query=std::unique_ptr<asio::ip::tcp::resolver::query>(new asio::ip::tcp::resolver::query(proxy_host_port.first, std::to_string(proxy_host_port.second)));
}
resolver.async_resolve(*query, [this](const error_ns::error_code &ec,
asio_ns::ip::tcp::resolver::iterator it){
resolver.async_resolve(*query, [this](const error_code &ec,
asio::ip::tcp::resolver::iterator it){
if(!ec) {
{
std::lock_guard<std::mutex> lock(socket_mutex);
@ -457,25 +436,25 @@ namespace SimpleWeb {
}
auto timer=get_timeout_timer(config.timeout_connect);
asio_ns::async_connect(*socket, it, [this, timer]
(const error_ns::error_code &ec, asio_ns::ip::tcp::resolver::iterator /*it*/){
asio::async_connect(*socket, it, [this, timer]
(const error_code &ec, asio::ip::tcp::resolver::iterator /*it*/){
if(timer)
timer->cancel();
if(!ec) {
asio_ns::ip::tcp::no_delay option(true);
asio::ip::tcp::no_delay option(true);
this->socket->set_option(option);
}
else {
std::lock_guard<std::mutex> lock(socket_mutex);
this->socket=nullptr;
throw error_ns::system_error(ec);
throw system_error(ec);
}
});
}
else {
std::lock_guard<std::mutex> lock(socket_mutex);
socket=nullptr;
throw error_ns::system_error(ec);
throw system_error(ec);
}
});
io_service.reset();