added Support for NO_BOOST
This commit is contained in:
parent
3f8fcc0c31
commit
d50bcabf8e
6 changed files with 256 additions and 157 deletions
166
client_http.hpp
166
client_http.hpp
|
|
@ -1,19 +1,65 @@
|
|||
#ifndef CLIENT_HTTP_HPP
|
||||
#define CLIENT_HTTP_HPP
|
||||
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/utility/string_ref.hpp>
|
||||
#include <boost/algorithm/string/predicate.hpp>
|
||||
#include <boost/functional/hash.hpp>
|
||||
|
||||
#include <unordered_map>
|
||||
#include <map>
|
||||
#include <random>
|
||||
#include <mutex>
|
||||
#include <type_traits>
|
||||
|
||||
#ifndef CASE_INSENSITIVE_EQUALS_AND_HASH
|
||||
#define CASE_INSENSITIVE_EQUALS_AND_HASH
|
||||
#ifdef NO_BOOST
|
||||
#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);
|
||||
});
|
||||
}
|
||||
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;
|
||||
|
||||
# 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:
|
||||
|
|
@ -30,6 +76,8 @@ public:
|
|||
return seed;
|
||||
}
|
||||
};
|
||||
# endif
|
||||
|
||||
#endif
|
||||
|
||||
namespace SimpleWeb {
|
||||
|
|
@ -52,7 +100,7 @@ namespace SimpleWeb {
|
|||
std::unordered_multimap<std::string, std::string, case_insensitive_hash, case_insensitive_equals> header;
|
||||
|
||||
private:
|
||||
boost::asio::streambuf content_buffer;
|
||||
asio_ns::streambuf content_buffer;
|
||||
|
||||
Response(): content(&content_buffer) {}
|
||||
};
|
||||
|
|
@ -78,10 +126,10 @@ namespace SimpleWeb {
|
|||
auto corrected_path=path;
|
||||
if(corrected_path=="")
|
||||
corrected_path="/";
|
||||
if(!config.proxy_server.empty() && std::is_same<socket_type, boost::asio::ip::tcp::socket>::value)
|
||||
if(!config.proxy_server.empty() && std::is_same<socket_type, asio_ns::ip::tcp::socket>::value)
|
||||
corrected_path="http://"+host+':'+std::to_string(port)+corrected_path;
|
||||
|
||||
boost::asio::streambuf write_buffer;
|
||||
asio_ns::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";
|
||||
|
|
@ -95,21 +143,21 @@ namespace SimpleWeb {
|
|||
connect();
|
||||
|
||||
auto timer=get_timeout_timer();
|
||||
boost::asio::async_write(*socket, write_buffer,
|
||||
[this, &content, timer](const boost::system::error_code &ec, size_t /*bytes_transferred*/) {
|
||||
asio_ns::async_write(*socket, write_buffer,
|
||||
[this, &content, timer](const error_ns::error_code &ec, size_t /*bytes_transferred*/) {
|
||||
if(timer)
|
||||
timer->cancel();
|
||||
if(!ec) {
|
||||
if(!content.empty()) {
|
||||
auto timer=get_timeout_timer();
|
||||
boost::asio::async_write(*socket, boost::asio::buffer(content.data(), content.size()),
|
||||
[this, timer](const boost::system::error_code &ec, size_t /*bytes_transferred*/) {
|
||||
asio_ns::async_write(*socket, asio_ns::buffer(content.data(), content.size()),
|
||||
[this, timer](const error_ns::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 boost::system::system_error(ec);
|
||||
throw error_ns::system_error(ec);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -117,7 +165,7 @@ namespace SimpleWeb {
|
|||
else {
|
||||
std::lock_guard<std::mutex> lock(socket_mutex);
|
||||
socket=nullptr;
|
||||
throw boost::system::system_error(ec);
|
||||
throw error_ns::system_error(ec);
|
||||
}
|
||||
});
|
||||
io_service.reset();
|
||||
|
|
@ -131,14 +179,14 @@ namespace SimpleWeb {
|
|||
auto corrected_path=path;
|
||||
if(corrected_path=="")
|
||||
corrected_path="/";
|
||||
if(!config.proxy_server.empty() && std::is_same<socket_type, boost::asio::ip::tcp::socket>::value)
|
||||
if(!config.proxy_server.empty() && std::is_same<socket_type, asio_ns::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);
|
||||
|
||||
boost::asio::streambuf write_buffer;
|
||||
asio_ns::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";
|
||||
|
|
@ -154,14 +202,14 @@ namespace SimpleWeb {
|
|||
connect();
|
||||
|
||||
auto timer=get_timeout_timer();
|
||||
boost::asio::async_write(*socket, write_buffer,
|
||||
[this, timer](const boost::system::error_code &ec, size_t /*bytes_transferred*/) {
|
||||
asio_ns::async_write(*socket, write_buffer,
|
||||
[this, timer](const error_ns::error_code &ec, size_t /*bytes_transferred*/) {
|
||||
if(timer)
|
||||
timer->cancel();
|
||||
if(ec) {
|
||||
std::lock_guard<std::mutex> lock(socket_mutex);
|
||||
socket=nullptr;
|
||||
throw boost::system::system_error(ec);
|
||||
throw error_ns::system_error(ec);
|
||||
}
|
||||
});
|
||||
io_service.reset();
|
||||
|
|
@ -173,15 +221,15 @@ namespace SimpleWeb {
|
|||
void close() {
|
||||
std::lock_guard<std::mutex> lock(socket_mutex);
|
||||
if(socket) {
|
||||
boost::system::error_code ec;
|
||||
socket->lowest_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
|
||||
error_ns::error_code ec;
|
||||
socket->lowest_layer().shutdown(asio_ns::ip::tcp::socket::shutdown_both, ec);
|
||||
socket->lowest_layer().close();
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
boost::asio::io_service io_service;
|
||||
boost::asio::ip::tcp::resolver resolver;
|
||||
asio_ns::io_service io_service;
|
||||
asio_ns::ip::tcp::resolver resolver;
|
||||
|
||||
std::unique_ptr<socket_type> socket;
|
||||
std::mutex socket_mutex;
|
||||
|
|
@ -211,15 +259,15 @@ namespace SimpleWeb {
|
|||
|
||||
virtual void connect()=0;
|
||||
|
||||
std::shared_ptr<boost::asio::deadline_timer> get_timeout_timer(size_t timeout=0) {
|
||||
std::shared_ptr<asio_ns::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<boost::asio::deadline_timer>(io_service);
|
||||
auto timer=std::make_shared<asio_ns::deadline_timer>(io_service);
|
||||
timer->expires_from_now(boost::posix_time::seconds(timeout));
|
||||
timer->async_wait([this](const boost::system::error_code& ec) {
|
||||
timer->async_wait([this](const error_ns::error_code& ec) {
|
||||
if(!ec) {
|
||||
close();
|
||||
}
|
||||
|
|
@ -256,11 +304,11 @@ namespace SimpleWeb {
|
|||
std::shared_ptr<Response> request_read() {
|
||||
std::shared_ptr<Response> response(new Response());
|
||||
|
||||
boost::asio::streambuf chunked_streambuf;
|
||||
asio_ns::streambuf chunked_streambuf;
|
||||
|
||||
auto timer=get_timeout_timer();
|
||||
boost::asio::async_read_until(*socket, response->content_buffer, "\r\n\r\n",
|
||||
[this, &response, &chunked_streambuf, timer](const boost::system::error_code& ec, size_t bytes_transferred) {
|
||||
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) {
|
||||
if(timer)
|
||||
timer->cancel();
|
||||
if(!ec) {
|
||||
|
|
@ -273,15 +321,15 @@ namespace SimpleWeb {
|
|||
auto content_length=stoull(header_it->second);
|
||||
if(content_length>num_additional_bytes) {
|
||||
auto timer=get_timeout_timer();
|
||||
boost::asio::async_read(*socket, response->content_buffer,
|
||||
boost::asio::transfer_exactly(content_length-num_additional_bytes),
|
||||
[this, timer](const boost::system::error_code& ec, size_t /*bytes_transferred*/) {
|
||||
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*/) {
|
||||
if(timer)
|
||||
timer->cancel();
|
||||
if(ec) {
|
||||
std::lock_guard<std::mutex> lock(socket_mutex);
|
||||
this->socket=nullptr;
|
||||
throw boost::system::system_error(ec);
|
||||
throw error_ns::system_error(ec);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -291,15 +339,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();
|
||||
boost::asio::async_read(*socket, response->content_buffer,
|
||||
[this, timer](const boost::system::error_code& ec, size_t /*bytes_transferred*/) {
|
||||
asio_ns::async_read(*socket, response->content_buffer,
|
||||
[this, timer](const error_ns::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!=boost::asio::error::eof)
|
||||
throw boost::system::system_error(ec);
|
||||
if(ec!=asio_ns::error::eof)
|
||||
throw error_ns::system_error(ec);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -307,7 +355,7 @@ namespace SimpleWeb {
|
|||
else {
|
||||
std::lock_guard<std::mutex> lock(socket_mutex);
|
||||
socket=nullptr;
|
||||
throw boost::system::system_error(ec);
|
||||
throw error_ns::system_error(ec);
|
||||
}
|
||||
});
|
||||
io_service.reset();
|
||||
|
|
@ -316,10 +364,10 @@ namespace SimpleWeb {
|
|||
return response;
|
||||
}
|
||||
|
||||
void request_read_chunked(const std::shared_ptr<Response> &response, boost::asio::streambuf &streambuf) {
|
||||
void request_read_chunked(const std::shared_ptr<Response> &response, asio_ns::streambuf &streambuf) {
|
||||
auto timer=get_timeout_timer();
|
||||
boost::asio::async_read_until(*socket, response->content_buffer, "\r\n",
|
||||
[this, &response, &streambuf, timer](const boost::system::error_code& ec, size_t bytes_transferred) {
|
||||
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) {
|
||||
if(timer)
|
||||
timer->cancel();
|
||||
if(!ec) {
|
||||
|
|
@ -353,9 +401,9 @@ namespace SimpleWeb {
|
|||
|
||||
if((2+length)>num_additional_bytes) {
|
||||
auto timer=get_timeout_timer();
|
||||
boost::asio::async_read(*socket, response->content_buffer,
|
||||
boost::asio::transfer_exactly(2+length-num_additional_bytes),
|
||||
[this, post_process, timer](const boost::system::error_code& ec, size_t /*bytes_transferred*/) {
|
||||
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*/) {
|
||||
if(timer)
|
||||
timer->cancel();
|
||||
if(!ec) {
|
||||
|
|
@ -364,7 +412,7 @@ namespace SimpleWeb {
|
|||
else {
|
||||
std::lock_guard<std::mutex> lock(socket_mutex);
|
||||
this->socket=nullptr;
|
||||
throw boost::system::system_error(ec);
|
||||
throw error_ns::system_error(ec);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -374,7 +422,7 @@ namespace SimpleWeb {
|
|||
else {
|
||||
std::lock_guard<std::mutex> lock(socket_mutex);
|
||||
socket=nullptr;
|
||||
throw boost::system::system_error(ec);
|
||||
throw error_ns::system_error(ec);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -383,7 +431,7 @@ namespace SimpleWeb {
|
|||
template<class socket_type>
|
||||
class Client : public ClientBase<socket_type> {};
|
||||
|
||||
typedef boost::asio::ip::tcp::socket HTTP;
|
||||
typedef asio_ns::ip::tcp::socket HTTP;
|
||||
|
||||
template<>
|
||||
class Client<HTTP> : public ClientBase<HTTP> {
|
||||
|
|
@ -393,15 +441,15 @@ namespace SimpleWeb {
|
|||
protected:
|
||||
void connect() {
|
||||
if(!socket || !socket->is_open()) {
|
||||
std::unique_ptr<boost::asio::ip::tcp::resolver::query> query;
|
||||
std::unique_ptr<asio_ns::ip::tcp::resolver::query> query;
|
||||
if(config.proxy_server.empty())
|
||||
query=std::unique_ptr<boost::asio::ip::tcp::resolver::query>(new boost::asio::ip::tcp::resolver::query(host, std::to_string(port)));
|
||||
query=std::unique_ptr<asio_ns::ip::tcp::resolver::query>(new asio_ns::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<boost::asio::ip::tcp::resolver::query>(new boost::asio::ip::tcp::resolver::query(proxy_host_port.first, std::to_string(proxy_host_port.second)));
|
||||
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)));
|
||||
}
|
||||
resolver.async_resolve(*query, [this](const boost::system::error_code &ec,
|
||||
boost::asio::ip::tcp::resolver::iterator it){
|
||||
resolver.async_resolve(*query, [this](const error_ns::error_code &ec,
|
||||
asio_ns::ip::tcp::resolver::iterator it){
|
||||
if(!ec) {
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(socket_mutex);
|
||||
|
|
@ -409,25 +457,25 @@ namespace SimpleWeb {
|
|||
}
|
||||
|
||||
auto timer=get_timeout_timer(config.timeout_connect);
|
||||
boost::asio::async_connect(*socket, it, [this, timer]
|
||||
(const boost::system::error_code &ec, boost::asio::ip::tcp::resolver::iterator /*it*/){
|
||||
asio_ns::async_connect(*socket, it, [this, timer]
|
||||
(const error_ns::error_code &ec, asio_ns::ip::tcp::resolver::iterator /*it*/){
|
||||
if(timer)
|
||||
timer->cancel();
|
||||
if(!ec) {
|
||||
boost::asio::ip::tcp::no_delay option(true);
|
||||
asio_ns::ip::tcp::no_delay option(true);
|
||||
this->socket->set_option(option);
|
||||
}
|
||||
else {
|
||||
std::lock_guard<std::mutex> lock(socket_mutex);
|
||||
this->socket=nullptr;
|
||||
throw boost::system::system_error(ec);
|
||||
throw error_ns::system_error(ec);
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
std::lock_guard<std::mutex> lock(socket_mutex);
|
||||
socket=nullptr;
|
||||
throw boost::system::system_error(ec);
|
||||
throw error_ns::system_error(ec);
|
||||
}
|
||||
});
|
||||
io_service.reset();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue