1
Fork 0
mirror of https://github.com/SapphireServer/Sapphire.git synced 2025-04-25 22:17:45 +00:00

remove more boost from api

This commit is contained in:
NotAdam 2018-10-25 14:16:47 +11:00
parent 0684149310
commit ae0af25ea8
4 changed files with 138 additions and 137 deletions

View file

@ -1,7 +1,7 @@
#ifndef CLIENT_HTTP_HPP #ifndef CLIENT_HTTP_HPP
#define CLIENT_HTTP_HPP #define CLIENT_HTTP_HPP
#include <boost/asio.hpp> #include <asio.hpp>
#include <boost/utility/string_ref.hpp> #include <boost/utility/string_ref.hpp>
#include <boost/algorithm/string/predicate.hpp> #include <boost/algorithm/string/predicate.hpp>
#include <boost/functional/hash.hpp> #include <boost/functional/hash.hpp>
@ -33,7 +33,7 @@ namespace SimpleWeb {
std::unordered_multimap<std::string, std::string, case_insensitive_hash, case_insensitive_equals> header; std::unordered_multimap<std::string, std::string, case_insensitive_hash, case_insensitive_equals> header;
private: private:
boost::asio::streambuf content_buffer; asio::streambuf content_buffer;
Response(): content(&content_buffer) {} Response(): content(&content_buffer) {}
}; };
@ -57,10 +57,10 @@ namespace SimpleWeb {
auto corrected_path=path; auto corrected_path=path;
if(corrected_path=="") if(corrected_path=="")
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::ip::tcp::socket>::value)
corrected_path="http://"+host+':'+std::to_string(port)+corrected_path; corrected_path="http://"+host+':'+std::to_string(port)+corrected_path;
boost::asio::streambuf write_buffer; asio::streambuf write_buffer;
std::ostream write_stream(&write_buffer); std::ostream write_stream(&write_buffer);
write_stream << request_type << " " << corrected_path << " HTTP/1.1\r\n"; write_stream << request_type << " " << corrected_path << " HTTP/1.1\r\n";
write_stream << "Host: " << host << "\r\n"; write_stream << "Host: " << host << "\r\n";
@ -74,21 +74,21 @@ namespace SimpleWeb {
connect(); connect();
auto timer=get_timeout_timer(); auto timer=get_timeout_timer();
boost::asio::async_write(*socket, write_buffer, asio::async_write(*socket, write_buffer,
[this, &content, timer](const boost::system::error_code &ec, size_t /*bytes_transferred*/) { [this, &content, timer](const std::error_code &ec, size_t /*bytes_transferred*/) {
if(timer) if(timer)
timer->cancel(); timer->cancel();
if(!ec) { if(!ec) {
if(!content.empty()) { if(!content.empty()) {
auto timer=get_timeout_timer(); auto timer=get_timeout_timer();
boost::asio::async_write(*socket, boost::asio::buffer(content.data(), content.size()), asio::async_write(*socket, asio::buffer(content.data(), content.size()),
[this, timer](const boost::system::error_code &ec, size_t /*bytes_transferred*/) { [this, timer](const std::error_code &ec, size_t /*bytes_transferred*/) {
if(timer) if(timer)
timer->cancel(); timer->cancel();
if(ec) { if(ec) {
std::lock_guard<std::mutex> lock(socket_mutex); std::lock_guard<std::mutex> lock(socket_mutex);
this->socket=nullptr; this->socket=nullptr;
throw boost::system::system_error(ec); throw std::system_error(ec);
} }
}); });
} }
@ -96,7 +96,7 @@ namespace SimpleWeb {
else { else {
std::lock_guard<std::mutex> lock(socket_mutex); std::lock_guard<std::mutex> lock(socket_mutex);
socket=nullptr; socket=nullptr;
throw boost::system::system_error(ec); throw std::system_error(ec);
} }
}); });
io_service.reset(); io_service.reset();
@ -110,14 +110,14 @@ namespace SimpleWeb {
auto corrected_path=path; auto corrected_path=path;
if(corrected_path=="") if(corrected_path=="")
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::ip::tcp::socket>::value)
corrected_path="http://"+host+':'+std::to_string(port)+corrected_path; corrected_path="http://"+host+':'+std::to_string(port)+corrected_path;
content.seekp(0, std::ios::end); content.seekp(0, std::ios::end);
auto content_length=content.tellp(); auto content_length=content.tellp();
content.seekp(0, std::ios::beg); content.seekp(0, std::ios::beg);
boost::asio::streambuf write_buffer; asio::streambuf write_buffer;
std::ostream write_stream(&write_buffer); std::ostream write_stream(&write_buffer);
write_stream << request_type << " " << corrected_path << " HTTP/1.1\r\n"; write_stream << request_type << " " << corrected_path << " HTTP/1.1\r\n";
write_stream << "Host: " << host << "\r\n"; write_stream << "Host: " << host << "\r\n";
@ -133,14 +133,14 @@ namespace SimpleWeb {
connect(); connect();
auto timer=get_timeout_timer(); auto timer=get_timeout_timer();
boost::asio::async_write(*socket, write_buffer, asio::async_write(*socket, write_buffer,
[this, timer](const boost::system::error_code &ec, size_t /*bytes_transferred*/) { [this, timer](const std::error_code &ec, size_t /*bytes_transferred*/) {
if(timer) if(timer)
timer->cancel(); timer->cancel();
if(ec) { if(ec) {
std::lock_guard<std::mutex> lock(socket_mutex); std::lock_guard<std::mutex> lock(socket_mutex);
socket=nullptr; socket=nullptr;
throw boost::system::system_error(ec); throw std::system_error(ec);
} }
}); });
io_service.reset(); io_service.reset();
@ -152,15 +152,15 @@ namespace SimpleWeb {
void close() { void close() {
std::lock_guard<std::mutex> lock(socket_mutex); std::lock_guard<std::mutex> lock(socket_mutex);
if(socket) { if(socket) {
boost::system::error_code ec; std::error_code ec;
socket->lowest_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec); socket->lowest_layer().shutdown(asio::ip::tcp::socket::shutdown_both, ec);
socket->lowest_layer().close(); socket->lowest_layer().close();
} }
} }
protected: protected:
boost::asio::io_service io_service; asio::io_service io_service;
boost::asio::ip::tcp::resolver resolver; asio::ip::tcp::resolver resolver;
std::unique_ptr<socket_type> socket; std::unique_ptr<socket_type> socket;
std::mutex socket_mutex; std::mutex socket_mutex;
@ -190,13 +190,13 @@ namespace SimpleWeb {
virtual void connect()=0; virtual void connect()=0;
std::shared_ptr<boost::asio::deadline_timer> get_timeout_timer() { std::shared_ptr<asio::deadline_timer> get_timeout_timer() {
if(config.timeout==0) if(config.timeout==0)
return nullptr; return nullptr;
auto timer=std::make_shared<boost::asio::deadline_timer>(io_service); auto timer=std::make_shared<asio::deadline_timer>(io_service);
timer->expires_from_now(boost::posix_time::seconds(config.timeout)); timer->expires_from_now(boost::posix_time::seconds(config.timeout));
timer->async_wait([this](const boost::system::error_code& ec) { timer->async_wait([this](const std::error_code& ec) {
if(!ec) { if(!ec) {
close(); close();
} }
@ -233,11 +233,11 @@ namespace SimpleWeb {
std::shared_ptr<Response> request_read() { std::shared_ptr<Response> request_read() {
std::shared_ptr<Response> response(new Response()); std::shared_ptr<Response> response(new Response());
boost::asio::streambuf chunked_streambuf; asio::streambuf chunked_streambuf;
auto timer=get_timeout_timer(); auto timer=get_timeout_timer();
boost::asio::async_read_until(*socket, response->content_buffer, "\r\n\r\n", 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) { [this, &response, &chunked_streambuf, timer](const std::error_code& ec, size_t bytes_transferred) {
if(timer) if(timer)
timer->cancel(); timer->cancel();
if(!ec) { if(!ec) {
@ -250,15 +250,15 @@ namespace SimpleWeb {
auto content_length=stoull(header_it->second); auto content_length=stoull(header_it->second);
if(content_length>num_additional_bytes) { if(content_length>num_additional_bytes) {
auto timer=get_timeout_timer(); auto timer=get_timeout_timer();
boost::asio::async_read(*socket, response->content_buffer, asio::async_read(*socket, response->content_buffer,
boost::asio::transfer_exactly(content_length-num_additional_bytes), asio::transfer_exactly(content_length-num_additional_bytes),
[this, timer](const boost::system::error_code& ec, size_t /*bytes_transferred*/) { [this, timer](const std::error_code& ec, size_t /*bytes_transferred*/) {
if(timer) if(timer)
timer->cancel(); timer->cancel();
if(ec) { if(ec) {
std::lock_guard<std::mutex> lock(socket_mutex); std::lock_guard<std::mutex> lock(socket_mutex);
this->socket=nullptr; this->socket=nullptr;
throw boost::system::system_error(ec); throw std::system_error(ec);
} }
}); });
} }
@ -270,7 +270,7 @@ namespace SimpleWeb {
else { else {
std::lock_guard<std::mutex> lock(socket_mutex); std::lock_guard<std::mutex> lock(socket_mutex);
socket=nullptr; socket=nullptr;
throw boost::system::system_error(ec); throw std::system_error(ec);
} }
}); });
io_service.reset(); io_service.reset();
@ -279,10 +279,10 @@ namespace SimpleWeb {
return response; 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::streambuf &streambuf) {
auto timer=get_timeout_timer(); auto timer=get_timeout_timer();
boost::asio::async_read_until(*socket, response->content_buffer, "\r\n", asio::async_read_until(*socket, response->content_buffer, "\r\n",
[this, &response, &streambuf, timer](const boost::system::error_code& ec, size_t bytes_transferred) { [this, &response, &streambuf, timer](const std::error_code& ec, size_t bytes_transferred) {
if(timer) if(timer)
timer->cancel(); timer->cancel();
if(!ec) { if(!ec) {
@ -316,9 +316,9 @@ namespace SimpleWeb {
if((2+length)>num_additional_bytes) { if((2+length)>num_additional_bytes) {
auto timer=get_timeout_timer(); auto timer=get_timeout_timer();
boost::asio::async_read(*socket, response->content_buffer, asio::async_read(*socket, response->content_buffer,
boost::asio::transfer_exactly(2+length-num_additional_bytes), asio::transfer_exactly(2+length-num_additional_bytes),
[this, post_process, timer](const boost::system::error_code& ec, size_t /*bytes_transferred*/) { [this, post_process, timer](const std::error_code& ec, size_t /*bytes_transferred*/) {
if(timer) if(timer)
timer->cancel(); timer->cancel();
if(!ec) { if(!ec) {
@ -327,7 +327,7 @@ namespace SimpleWeb {
else { else {
std::lock_guard<std::mutex> lock(socket_mutex); std::lock_guard<std::mutex> lock(socket_mutex);
this->socket=nullptr; this->socket=nullptr;
throw boost::system::system_error(ec); throw std::system_error(ec);
} }
}); });
} }
@ -337,7 +337,7 @@ namespace SimpleWeb {
else { else {
std::lock_guard<std::mutex> lock(socket_mutex); std::lock_guard<std::mutex> lock(socket_mutex);
socket=nullptr; socket=nullptr;
throw boost::system::system_error(ec); throw std::system_error(ec);
} }
}); });
} }
@ -346,7 +346,7 @@ namespace SimpleWeb {
template<class socket_type> template<class socket_type>
class Client : public ClientBase<socket_type> {}; class Client : public ClientBase<socket_type> {};
typedef boost::asio::ip::tcp::socket HTTP; typedef asio::ip::tcp::socket HTTP;
template<> template<>
class Client<HTTP> : public ClientBase<HTTP> { class Client<HTTP> : public ClientBase<HTTP> {
@ -356,15 +356,15 @@ namespace SimpleWeb {
protected: protected:
void connect() { void connect() {
if(!socket || !socket->is_open()) { if(!socket || !socket->is_open()) {
std::unique_ptr<boost::asio::ip::tcp::resolver::query> query; std::unique_ptr<asio::ip::tcp::resolver::query> query;
if(config.proxy_server.empty()) 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::ip::tcp::resolver::query>(new asio::ip::tcp::resolver::query(host, std::to_string(port)));
else { else {
auto proxy_host_port=parse_host_port(config.proxy_server, 8080); 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::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 boost::system::error_code &ec, resolver.async_resolve(*query, [this](const std::error_code &ec,
boost::asio::ip::tcp::resolver::iterator it){ asio::ip::tcp::resolver::iterator it){
if(!ec) { if(!ec) {
{ {
std::lock_guard<std::mutex> lock(socket_mutex); std::lock_guard<std::mutex> lock(socket_mutex);
@ -372,25 +372,25 @@ namespace SimpleWeb {
} }
auto timer=get_timeout_timer(); auto timer=get_timeout_timer();
boost::asio::async_connect(*socket, it, [this, timer] asio::async_connect(*socket, it, [this, timer]
(const boost::system::error_code &ec, boost::asio::ip::tcp::resolver::iterator /*it*/){ (const std::error_code &ec, asio::ip::tcp::resolver::iterator /*it*/){
if(timer) if(timer)
timer->cancel(); timer->cancel();
if(!ec) { if(!ec) {
boost::asio::ip::tcp::no_delay option(true); asio::ip::tcp::no_delay option(true);
this->socket->set_option(option); this->socket->set_option(option);
} }
else { else {
std::lock_guard<std::mutex> lock(socket_mutex); std::lock_guard<std::mutex> lock(socket_mutex);
this->socket=nullptr; this->socket=nullptr;
throw boost::system::system_error(ec); throw std::system_error(ec);
} }
}); });
} }
else { else {
std::lock_guard<std::mutex> lock(socket_mutex); std::lock_guard<std::mutex> lock(socket_mutex);
socket=nullptr; socket=nullptr;
throw boost::system::system_error(ec); throw std::system_error(ec);
} }
}); });
io_service.reset(); io_service.reset();

View file

@ -804,7 +804,7 @@ void default_resource_send( const HttpServer& server, const shared_ptr< HttpServ
response->write( &buffer[ 0 ], read_length ); response->write( &buffer[ 0 ], read_length );
if( read_length == static_cast< streamsize >( buffer.size() ) ) if( read_length == static_cast< streamsize >( buffer.size() ) )
{ {
server.send( response, [ &server, response, ifs ]( const boost::system::error_code& ec ) server.send( response, [ &server, response, ifs ]( const std::error_code& ec )
{ {
if( !ec ) if( !ec )
default_resource_send( server, response, ifs ); default_resource_send( server, response, ifs );

View file

@ -1,7 +1,7 @@
#ifndef SERVER_HTTP_HPP #ifndef SERVER_HTTP_HPP
#define SERVER_HTTP_HPP #define SERVER_HTTP_HPP
#include <boost/asio.hpp> #include <asio.hpp>
#include <boost/algorithm/string/predicate.hpp> #include <boost/algorithm/string/predicate.hpp>
#include <boost/functional/hash.hpp> #include <boost/functional/hash.hpp>
@ -64,7 +64,7 @@ namespace SimpleWeb {
class Response : public std::ostream { class Response : public std::ostream {
friend class ServerBase<socket_type>; friend class ServerBase<socket_type>;
boost::asio::streambuf streambuf; asio::streambuf streambuf;
std::shared_ptr<socket_type> socket; std::shared_ptr<socket_type> socket;
@ -88,8 +88,8 @@ namespace SimpleWeb {
return ss.str(); return ss.str();
} }
private: private:
boost::asio::streambuf &streambuf; asio::streambuf &streambuf;
Content(boost::asio::streambuf &streambuf): std::istream(&streambuf), streambuf(streambuf) {} Content(asio::streambuf &streambuf): std::istream(&streambuf), streambuf(streambuf) {}
}; };
class Request { class Request {
@ -116,7 +116,7 @@ namespace SimpleWeb {
catch(...) {} catch(...) {}
} }
boost::asio::streambuf streambuf; asio::streambuf streambuf;
}; };
class Config { class Config {
@ -159,27 +159,27 @@ namespace SimpleWeb {
std::map<std::string, std::map<std::string,
std::function<void(std::shared_ptr<typename ServerBase<socket_type>::Response>, std::shared_ptr<typename ServerBase<socket_type>::Request>)> > default_resource; std::function<void(std::shared_ptr<typename ServerBase<socket_type>::Response>, std::shared_ptr<typename ServerBase<socket_type>::Request>)> > default_resource;
std::function<void(std::shared_ptr<typename ServerBase<socket_type>::Request>, const boost::system::error_code&)> on_error; std::function<void(std::shared_ptr<typename ServerBase<socket_type>::Request>, const std::error_code&)> on_error;
std::function<void(std::shared_ptr<socket_type> socket, std::shared_ptr<typename ServerBase<socket_type>::Request>)> on_upgrade; std::function<void(std::shared_ptr<socket_type> socket, std::shared_ptr<typename ServerBase<socket_type>::Request>)> on_upgrade;
virtual void start() { virtual void start() {
if(!io_service) if(!io_service)
io_service=std::make_shared<boost::asio::io_service>(); io_service=std::make_shared<asio::io_service>();
if(io_service->stopped()) if(io_service->stopped())
io_service->reset(); io_service->reset();
boost::asio::ip::tcp::endpoint endpoint; asio::ip::tcp::endpoint endpoint;
if(config.address.size()>0) if(config.address.size()>0)
endpoint=boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string(config.address), config.port); endpoint=asio::ip::tcp::endpoint(asio::ip::address::from_string(config.address), config.port);
else else
endpoint=boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), config.port); endpoint=asio::ip::tcp::endpoint(asio::ip::tcp::v4(), config.port);
if(!acceptor) if(!acceptor)
acceptor=std::unique_ptr<boost::asio::ip::tcp::acceptor>(new boost::asio::ip::tcp::acceptor(*io_service)); acceptor=std::unique_ptr<asio::ip::tcp::acceptor>(new asio::ip::tcp::acceptor(*io_service));
acceptor->open(endpoint.protocol()); acceptor->open(endpoint.protocol());
acceptor->set_option(boost::asio::socket_base::reuse_address(config.reuse_address)); acceptor->set_option(asio::socket_base::reuse_address(config.reuse_address));
acceptor->bind(endpoint); acceptor->bind(endpoint);
acceptor->listen(); acceptor->listen();
@ -210,34 +210,34 @@ namespace SimpleWeb {
} }
///Use this function if you need to recursively send parts of a longer message ///Use this function if you need to recursively send parts of a longer message
void send(const std::shared_ptr<Response> &response, const std::function<void(const boost::system::error_code&)>& callback=nullptr) const { void send(const std::shared_ptr<Response> &response, const std::function<void(const std::error_code&)>& callback=nullptr) const {
boost::asio::async_write(*response->socket, response->streambuf, [this, response, callback](const boost::system::error_code& ec, size_t /*bytes_transferred*/) { asio::async_write(*response->socket, response->streambuf, [this, response, callback](const std::error_code& ec, size_t /*bytes_transferred*/) {
if(callback) if(callback)
callback(ec); callback(ec);
}); });
} }
/// If you have your own boost::asio::io_service, store its pointer here before running start(). /// If you have your own asio::io_service, store its pointer here before running start().
/// You might also want to set config.thread_pool_size to 0. /// You might also want to set config.thread_pool_size to 0.
std::shared_ptr<boost::asio::io_service> io_service; std::shared_ptr<asio::io_service> io_service;
protected: protected:
std::unique_ptr<boost::asio::ip::tcp::acceptor> acceptor; std::unique_ptr<asio::ip::tcp::acceptor> acceptor;
std::vector<std::thread> threads; std::vector<std::thread> threads;
ServerBase(unsigned short port) : config(port) {} ServerBase(unsigned short port) : config(port) {}
virtual void accept()=0; virtual void accept()=0;
std::shared_ptr<boost::asio::deadline_timer> get_timeout_timer(const std::shared_ptr<socket_type> &socket, long seconds) { std::shared_ptr<asio::deadline_timer> get_timeout_timer(const std::shared_ptr<socket_type> &socket, long seconds) {
if(seconds==0) if(seconds==0)
return nullptr; return nullptr;
auto timer=std::make_shared<boost::asio::deadline_timer>(*io_service); auto timer=std::make_shared<asio::deadline_timer>(*io_service);
timer->expires_from_now(boost::posix_time::seconds(seconds)); timer->expires_from_now(boost::posix_time::seconds(seconds));
timer->async_wait([socket](const boost::system::error_code& ec){ timer->async_wait([socket](const std::error_code& ec){
if(!ec) { if(!ec) {
boost::system::error_code ec; std::error_code ec;
socket->lowest_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec); socket->lowest_layer().shutdown(asio::ip::tcp::socket::shutdown_both, ec);
socket->lowest_layer().close(); socket->lowest_layer().close();
} }
}); });
@ -249,11 +249,11 @@ namespace SimpleWeb {
//shared_ptr is used to pass temporary objects to the asynchronous functions //shared_ptr is used to pass temporary objects to the asynchronous functions
std::shared_ptr<Request> request(new Request(*socket)); std::shared_ptr<Request> request(new Request(*socket));
//Set timeout on the following boost::asio::async-read or write function //Set timeout on the following asio::async-read or write function
auto timer=this->get_timeout_timer(socket, config.timeout_request); auto timer=this->get_timeout_timer(socket, config.timeout_request);
boost::asio::async_read_until(*socket, request->streambuf, "\r\n\r\n", asio::async_read_until(*socket, request->streambuf, "\r\n\r\n",
[this, socket, request, timer](const boost::system::error_code& ec, size_t bytes_transferred) { [this, socket, request, timer](const std::error_code& ec, size_t bytes_transferred) {
if(timer) if(timer)
timer->cancel(); timer->cancel();
if(!ec) { if(!ec) {
@ -275,16 +275,16 @@ namespace SimpleWeb {
} }
catch( const std::exception & ) { catch( const std::exception & ) {
if(on_error) if(on_error)
on_error(request, boost::system::error_code(boost::system::errc::protocol_error, boost::system::generic_category())); on_error( request, std::error_code( std::errc::protocol_error, std::generic_category() ) );
return; return;
} }
if(content_length>num_additional_bytes) { if(content_length>num_additional_bytes) {
//Set timeout on the following boost::asio::async-read or write function //Set timeout on the following asio::async-read or write function
auto timer=this->get_timeout_timer(socket, config.timeout_content); auto timer=this->get_timeout_timer(socket, config.timeout_content);
boost::asio::async_read(*socket, request->streambuf, asio::async_read(*socket, request->streambuf,
boost::asio::transfer_exactly(static_cast< size_t >(content_length-num_additional_bytes)), asio::transfer_exactly(static_cast< size_t >(content_length-num_additional_bytes)),
[this, socket, request, timer] [this, socket, request, timer]
(const boost::system::error_code& ec, size_t /*bytes_transferred*/) { (const std::error_code& ec, size_t /*bytes_transferred*/) {
if(timer) if(timer)
timer->cancel(); timer->cancel();
if(!ec) if(!ec)
@ -375,12 +375,12 @@ namespace SimpleWeb {
void write_response(const std::shared_ptr<socket_type> &socket, const std::shared_ptr<Request> &request, void write_response(const std::shared_ptr<socket_type> &socket, const std::shared_ptr<Request> &request,
std::function<void(std::shared_ptr<typename ServerBase<socket_type>::Response>, std::function<void(std::shared_ptr<typename ServerBase<socket_type>::Response>,
std::shared_ptr<typename ServerBase<socket_type>::Request>)>& resource_function) { std::shared_ptr<typename ServerBase<socket_type>::Request>)>& resource_function) {
//Set timeout on the following boost::asio::async-read or write function //Set timeout on the following asio::async-read or write function
auto timer=this->get_timeout_timer(socket, config.timeout_content); auto timer=this->get_timeout_timer(socket, config.timeout_content);
auto response=std::shared_ptr<Response>(new Response(socket), [this, request, timer](Response *response_ptr) { auto response=std::shared_ptr<Response>(new Response(socket), [this, request, timer](Response *response_ptr) {
auto response=std::shared_ptr<Response>(response_ptr); auto response=std::shared_ptr<Response>(response_ptr);
this->send(response, [this, response, request, timer](const boost::system::error_code& ec) { this->send(response, [this, response, request, timer](const std::error_code& ec) {
if(timer) if(timer)
timer->cancel(); timer->cancel();
if(!ec) { if(!ec) {
@ -390,7 +390,7 @@ namespace SimpleWeb {
} }
catch( const std::exception & ){ catch( const std::exception & ){
if(on_error) if(on_error)
on_error(request, boost::system::error_code(boost::system::errc::protocol_error, boost::system::generic_category())); on_error( request, std::error_code( std::errc::protocol_error, std::generic_category() ) );
return; return;
} }
@ -412,7 +412,7 @@ namespace SimpleWeb {
} }
catch( const std::exception & ) { catch( const std::exception & ) {
if(on_error) if(on_error)
on_error(request, boost::system::error_code(boost::system::errc::operation_canceled, boost::system::generic_category())); on_error( request, std::error_code( std::errc::protocol_error, std::generic_category() ) );
return; return;
} }
} }
@ -421,7 +421,7 @@ namespace SimpleWeb {
template<class socket_type> template<class socket_type>
class Server : public ServerBase<socket_type> {}; class Server : public ServerBase<socket_type> {};
typedef boost::asio::ip::tcp::socket HTTP; typedef asio::ip::tcp::socket HTTP;
template<> template<>
class Server<HTTP> : public ServerBase<HTTP> { class Server<HTTP> : public ServerBase<HTTP> {
@ -442,13 +442,13 @@ namespace SimpleWeb {
//Shared_ptr is used to pass temporary objects to the asynchronous functions //Shared_ptr is used to pass temporary objects to the asynchronous functions
auto socket=std::make_shared<HTTP>(*io_service); auto socket=std::make_shared<HTTP>(*io_service);
acceptor->async_accept(*socket, [this, socket](const boost::system::error_code& ec){ acceptor->async_accept(*socket, [this, socket](const std::error_code& ec){
//Immediately start accepting a new connection (if io_service hasn't been stopped) //Immediately start accepting a new connection (if io_service hasn't been stopped)
if (ec != boost::asio::error::operation_aborted) if (ec != asio::error::operation_aborted)
accept(); accept();
if(!ec) { if(!ec) {
boost::asio::ip::tcp::no_delay option(true); asio::ip::tcp::no_delay option(true);
socket->set_option(option); socket->set_option(option);
this->read_request_and_content(socket); this->read_request_and_content(socket);

View file

@ -1,11 +1,12 @@
#ifndef CLIENT_HTTP_HPP #ifndef CLIENT_HTTP_HPP
#define CLIENT_HTTP_HPP #define CLIENT_HTTP_HPP
#include <boost/asio.hpp>
#include <boost/utility/string_ref.hpp> #include <boost/utility/string_ref.hpp>
#include <boost/algorithm/string/predicate.hpp> #include <boost/algorithm/string/predicate.hpp>
#include <boost/functional/hash.hpp> #include <boost/functional/hash.hpp>
#include <asio.hpp>
#include <unordered_map> #include <unordered_map>
#include <map> #include <map>
#include <random> #include <random>
@ -48,7 +49,7 @@ namespace SimpleWeb {
std::unordered_multimap<std::string, std::string, case_insensitive_hash, case_insensitive_equals> header; std::unordered_multimap<std::string, std::string, case_insensitive_hash, case_insensitive_equals> header;
private: private:
boost::asio::streambuf content_buffer; asio::streambuf content_buffer;
Response() : content( &content_buffer ) {} Response() : content( &content_buffer ) {}
}; };
@ -72,10 +73,10 @@ namespace SimpleWeb {
auto corrected_path = path; auto corrected_path = path;
if( corrected_path == "" ) if( corrected_path == "" )
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::ip::tcp::socket>::value )
corrected_path = "http://" + host + ':' + std::to_string( port ) + corrected_path; corrected_path = "http://" + host + ':' + std::to_string( port ) + corrected_path;
boost::asio::streambuf write_buffer; asio::streambuf write_buffer;
std::ostream write_stream( &write_buffer ); std::ostream write_stream( &write_buffer );
write_stream << request_type << " " << corrected_path << " HTTP/1.1\r\n"; write_stream << request_type << " " << corrected_path << " HTTP/1.1\r\n";
write_stream << "Host: " << host << "\r\n"; write_stream << "Host: " << host << "\r\n";
@ -89,21 +90,21 @@ namespace SimpleWeb {
connect(); connect();
auto timer = get_timeout_timer(); auto timer = get_timeout_timer();
boost::asio::async_write( *socket, write_buffer, asio::async_write( *socket, write_buffer,
[this, &content, timer]( const boost::system::error_code &ec, size_t /*bytes_transferred*/ ) { [this, &content, timer]( const std::error_code &ec, size_t /*bytes_transferred*/ ) {
if( timer ) if( timer )
timer->cancel(); timer->cancel();
if( !ec ) { if( !ec ) {
if( !content.empty() ) { if( !content.empty() ) {
auto timer = get_timeout_timer(); auto timer = get_timeout_timer();
boost::asio::async_write( *socket, boost::asio::buffer( content.data(), content.size() ), asio::async_write( *socket, asio::buffer( content.data(), content.size() ),
[this, timer]( const boost::system::error_code &ec, size_t /*bytes_transferred*/ ) { [this, timer]( const std::error_code &ec, size_t /*bytes_transferred*/ ) {
if( timer ) if( timer )
timer->cancel(); timer->cancel();
if( ec ) { if( ec ) {
std::lock_guard<std::mutex> lock( socket_mutex ); std::lock_guard<std::mutex> lock( socket_mutex );
this->socket = nullptr; this->socket = nullptr;
throw boost::system::system_error( ec ); throw std::system_error( ec );
} }
} ); } );
} }
@ -111,7 +112,7 @@ namespace SimpleWeb {
else { else {
std::lock_guard<std::mutex> lock( socket_mutex ); std::lock_guard<std::mutex> lock( socket_mutex );
socket = nullptr; socket = nullptr;
throw boost::system::system_error( ec ); throw std::system_error( ec );
} }
} ); } );
io_service.reset(); io_service.reset();
@ -125,14 +126,14 @@ namespace SimpleWeb {
auto corrected_path = path; auto corrected_path = path;
if( corrected_path == "" ) if( corrected_path == "" )
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::ip::tcp::socket>::value )
corrected_path = "http://" + host + ':' + std::to_string( port ) + corrected_path; corrected_path = "http://" + host + ':' + std::to_string( port ) + corrected_path;
content.seekp( 0, std::ios::end ); content.seekp( 0, std::ios::end );
auto content_length = content.tellp(); auto content_length = content.tellp();
content.seekp( 0, std::ios::beg ); content.seekp( 0, std::ios::beg );
boost::asio::streambuf write_buffer; asio::streambuf write_buffer;
std::ostream write_stream( &write_buffer ); std::ostream write_stream( &write_buffer );
write_stream << request_type << " " << corrected_path << " HTTP/1.1\r\n"; write_stream << request_type << " " << corrected_path << " HTTP/1.1\r\n";
write_stream << "Host: " << host << "\r\n"; write_stream << "Host: " << host << "\r\n";
@ -148,14 +149,14 @@ namespace SimpleWeb {
connect(); connect();
auto timer = get_timeout_timer(); auto timer = get_timeout_timer();
boost::asio::async_write( *socket, write_buffer, asio::async_write( *socket, write_buffer,
[this, timer]( const boost::system::error_code &ec, size_t /*bytes_transferred*/ ) { [this, timer]( const std::error_code &ec, size_t /*bytes_transferred*/ ) {
if( timer ) if( timer )
timer->cancel(); timer->cancel();
if( ec ) { if( ec ) {
std::lock_guard<std::mutex> lock( socket_mutex ); std::lock_guard<std::mutex> lock( socket_mutex );
socket = nullptr; socket = nullptr;
throw boost::system::system_error( ec ); throw std::system_error( ec );
} }
} ); } );
io_service.reset(); io_service.reset();
@ -167,15 +168,15 @@ namespace SimpleWeb {
void close() { void close() {
std::lock_guard<std::mutex> lock( socket_mutex ); std::lock_guard<std::mutex> lock( socket_mutex );
if( socket ) { if( socket ) {
boost::system::error_code ec; std::error_code ec;
socket->lowest_layer().shutdown( boost::asio::ip::tcp::socket::shutdown_both, ec ); socket->lowest_layer().shutdown( asio::ip::tcp::socket::shutdown_both, ec );
socket->lowest_layer().close(); socket->lowest_layer().close();
} }
} }
protected: protected:
boost::asio::io_service io_service; asio::io_service io_service;
boost::asio::ip::tcp::resolver resolver; asio::ip::tcp::resolver resolver;
std::unique_ptr<socket_type> socket; std::unique_ptr<socket_type> socket;
std::mutex socket_mutex; std::mutex socket_mutex;
@ -205,13 +206,13 @@ namespace SimpleWeb {
virtual void connect() = 0; virtual void connect() = 0;
std::shared_ptr<boost::asio::deadline_timer> get_timeout_timer() { std::shared_ptr< asio::deadline_timer > get_timeout_timer() {
if( config.timeout == 0 ) if( config.timeout == 0 )
return nullptr; return nullptr;
auto timer = std::make_shared<boost::asio::deadline_timer>( io_service ); auto timer = std::make_shared< asio::deadline_timer >( io_service );
timer->expires_from_now( boost::posix_time::seconds( config.timeout ) ); timer->expires_from_now( boost::posix_time::seconds( config.timeout ) );
timer->async_wait( [this]( const boost::system::error_code& ec ) { timer->async_wait( [this]( const std::error_code& ec ) {
if( !ec ) { if( !ec ) {
close(); close();
} }
@ -248,11 +249,11 @@ namespace SimpleWeb {
std::shared_ptr<Response> request_read() { std::shared_ptr<Response> request_read() {
std::shared_ptr<Response> response( new Response() ); std::shared_ptr<Response> response( new Response() );
boost::asio::streambuf chunked_streambuf; asio::streambuf chunked_streambuf;
auto timer = get_timeout_timer(); auto timer = get_timeout_timer();
boost::asio::async_read_until( *socket, response->content_buffer, "\r\n\r\n", 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 ) { [this, &response, &chunked_streambuf, timer]( const std::error_code& ec, size_t bytes_transferred ) {
if( timer ) if( timer )
timer->cancel(); timer->cancel();
if( !ec ) { if( !ec ) {
@ -265,15 +266,15 @@ namespace SimpleWeb {
auto content_length = stoull( header_it->second ); auto content_length = stoull( header_it->second );
if( content_length>num_additional_bytes ) { if( content_length>num_additional_bytes ) {
auto timer = get_timeout_timer(); auto timer = get_timeout_timer();
boost::asio::async_read( *socket, response->content_buffer, asio::async_read( *socket, response->content_buffer,
boost::asio::transfer_exactly( static_cast< size_t >( content_length - num_additional_bytes ) ), asio::transfer_exactly( static_cast< size_t >( content_length - num_additional_bytes ) ),
[this, timer]( const boost::system::error_code& ec, size_t /*bytes_transferred*/ ) { [this, timer]( const std::error_code& ec, size_t /*bytes_transferred*/ ) {
if( timer ) if( timer )
timer->cancel(); timer->cancel();
if( ec ) { if( ec ) {
std::lock_guard<std::mutex> lock( socket_mutex ); std::lock_guard<std::mutex> lock( socket_mutex );
this->socket = nullptr; this->socket = nullptr;
throw boost::system::system_error( ec ); throw std::system_error( ec );
} }
} ); } );
} }
@ -285,7 +286,7 @@ namespace SimpleWeb {
else { else {
std::lock_guard<std::mutex> lock( socket_mutex ); std::lock_guard<std::mutex> lock( socket_mutex );
socket = nullptr; socket = nullptr;
throw boost::system::system_error( ec ); throw std::system_error( ec );
} }
} ); } );
io_service.reset(); io_service.reset();
@ -294,10 +295,10 @@ namespace SimpleWeb {
return response; 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::streambuf &streambuf ) {
auto timer = get_timeout_timer(); auto timer = get_timeout_timer();
boost::asio::async_read_until( *socket, response->content_buffer, "\r\n", asio::async_read_until( *socket, response->content_buffer, "\r\n",
[this, &response, &streambuf, timer]( const boost::system::error_code& ec, size_t bytes_transferred ) { [this, &response, &streambuf, timer]( const std::error_code& ec, size_t bytes_transferred ) {
if( timer ) if( timer )
timer->cancel(); timer->cancel();
if( !ec ) { if( !ec ) {
@ -331,9 +332,9 @@ namespace SimpleWeb {
if( ( 2 + length )>num_additional_bytes ) { if( ( 2 + length )>num_additional_bytes ) {
auto timer = get_timeout_timer(); auto timer = get_timeout_timer();
boost::asio::async_read( *socket, response->content_buffer, asio::async_read( *socket, response->content_buffer,
boost::asio::transfer_exactly( static_cast< size_t >( 2 + length - num_additional_bytes ) ), asio::transfer_exactly( static_cast< size_t >( 2 + length - num_additional_bytes ) ),
[this, post_process, timer]( const boost::system::error_code& ec, size_t /*bytes_transferred*/ ) { [this, post_process, timer]( const std::error_code& ec, size_t /*bytes_transferred*/ ) {
if( timer ) if( timer )
timer->cancel(); timer->cancel();
if( !ec ) { if( !ec ) {
@ -342,7 +343,7 @@ namespace SimpleWeb {
else { else {
std::lock_guard<std::mutex> lock( socket_mutex ); std::lock_guard<std::mutex> lock( socket_mutex );
this->socket = nullptr; this->socket = nullptr;
throw boost::system::system_error( ec ); throw std::system_error( ec );
} }
} ); } );
} }
@ -352,7 +353,7 @@ namespace SimpleWeb {
else { else {
std::lock_guard<std::mutex> lock( socket_mutex ); std::lock_guard<std::mutex> lock( socket_mutex );
socket = nullptr; socket = nullptr;
throw boost::system::system_error( ec ); throw std::system_error( ec );
} }
} ); } );
} }
@ -361,7 +362,7 @@ namespace SimpleWeb {
template<class socket_type> template<class socket_type>
class Client : public ClientBase<socket_type> {}; class Client : public ClientBase<socket_type> {};
typedef boost::asio::ip::tcp::socket HTTP; typedef asio::ip::tcp::socket HTTP;
template<> template<>
class Client<HTTP> : public ClientBase<HTTP> { class Client<HTTP> : public ClientBase<HTTP> {
@ -371,15 +372,15 @@ namespace SimpleWeb {
protected: protected:
void connect() { void connect() {
if( !socket || !socket->is_open() ) { if( !socket || !socket->is_open() ) {
std::unique_ptr<boost::asio::ip::tcp::resolver::query> query; std::unique_ptr<asio::ip::tcp::resolver::query> query;
if( config.proxy_server.empty() ) 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::ip::tcp::resolver::query>( new asio::ip::tcp::resolver::query( host, std::to_string( port ) ) );
else { else {
auto proxy_host_port = parse_host_port( config.proxy_server, 8080 ); 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::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 boost::system::error_code &ec, resolver.async_resolve( *query, [this]( const std::error_code &ec,
boost::asio::ip::tcp::resolver::iterator it ) { asio::ip::tcp::resolver::iterator it ) {
if( !ec ) { if( !ec ) {
{ {
std::lock_guard<std::mutex> lock( socket_mutex ); std::lock_guard<std::mutex> lock( socket_mutex );
@ -387,25 +388,25 @@ namespace SimpleWeb {
} }
auto timer = get_timeout_timer(); auto timer = get_timeout_timer();
boost::asio::async_connect( *socket, it, [this, timer] asio::async_connect( *socket, it, [this, timer]
( const boost::system::error_code &ec, boost::asio::ip::tcp::resolver::iterator /*it*/ ) { ( const std::error_code &ec, asio::ip::tcp::resolver::iterator /*it*/ ) {
if( timer ) if( timer )
timer->cancel(); timer->cancel();
if( !ec ) { if( !ec ) {
boost::asio::ip::tcp::no_delay option( true ); asio::ip::tcp::no_delay option( true );
this->socket->set_option( option ); this->socket->set_option( option );
} }
else { else {
std::lock_guard<std::mutex> lock( socket_mutex ); std::lock_guard<std::mutex> lock( socket_mutex );
this->socket = nullptr; this->socket = nullptr;
throw boost::system::system_error( ec ); throw std::system_error( ec );
} }
} ); } );
} }
else { else {
std::lock_guard<std::mutex> lock( socket_mutex ); std::lock_guard<std::mutex> lock( socket_mutex );
socket = nullptr; socket = nullptr;
throw boost::system::system_error( ec ); throw std::system_error( ec );
} }
} ); } );
io_service.reset(); io_service.reset();