Added custom response stream and possibility to flush response to clients synchronously and asynchronously. Various speed ups, including reduced use of regex and preprocessing of regex objects. boost::asio::ip::tcp::no_delay is now turned on for both Client and Server. Note: Not backward compatible with earlier versions.
This commit is contained in:
parent
79903b6abb
commit
a6154c7c5b
8 changed files with 331 additions and 168 deletions
310
server_http.hpp
310
server_http.hpp
|
|
@ -2,15 +2,117 @@
|
|||
#define SERVER_HTTP_HPP
|
||||
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/asio/spawn.hpp>
|
||||
|
||||
#include <regex>
|
||||
#include <unordered_map>
|
||||
#include <thread>
|
||||
#include <functional>
|
||||
|
||||
namespace SimpleWeb {
|
||||
template <class socket_type>
|
||||
class ServerBase {
|
||||
public:
|
||||
class Response {
|
||||
friend class ServerBase<socket_type>;
|
||||
private:
|
||||
std::shared_ptr<boost::asio::strand> strand;
|
||||
|
||||
boost::asio::yield_context& yield;
|
||||
|
||||
boost::asio::streambuf streambuf;
|
||||
|
||||
std::shared_ptr<socket_type> socket;
|
||||
|
||||
std::shared_ptr<boost::asio::deadline_timer> async_timer;
|
||||
|
||||
std::shared_ptr<bool> async_writing;
|
||||
std::shared_ptr<bool> async_waiting;
|
||||
|
||||
Response(boost::asio::io_service& io_service, std::shared_ptr<socket_type> socket, std::shared_ptr<boost::asio::strand> strand,
|
||||
boost::asio::yield_context& yield):
|
||||
strand(strand), yield(yield), socket(socket), async_timer(new boost::asio::deadline_timer(io_service)),
|
||||
async_writing(new bool(false)), async_waiting(new bool(false)), stream(&streambuf) {}
|
||||
|
||||
void async_flush(std::function<void((const boost::system::error_code&))> callback=nullptr) {
|
||||
if(!callback && !socket->lowest_layer().is_open()) {
|
||||
if(*async_waiting)
|
||||
async_timer->cancel();
|
||||
throw std::runtime_error("Broken pipe.");
|
||||
}
|
||||
|
||||
std::shared_ptr<boost::asio::streambuf> write_buffer(new boost::asio::streambuf);
|
||||
std::ostream response(write_buffer.get());
|
||||
response << stream.rdbuf();
|
||||
|
||||
//Wait until previous async_flush is finished
|
||||
strand->dispatch([this](){
|
||||
if(*async_writing) {
|
||||
*async_waiting=true;
|
||||
try {
|
||||
async_timer->async_wait(yield);
|
||||
}
|
||||
catch(std::exception& e) {
|
||||
}
|
||||
*async_waiting=false;
|
||||
}
|
||||
});
|
||||
|
||||
*async_writing=true;
|
||||
|
||||
auto socket_=this->socket;
|
||||
auto async_writing_=this->async_writing;
|
||||
auto async_timer_=this->async_timer;
|
||||
auto async_waiting_=this->async_waiting;
|
||||
|
||||
boost::asio::async_write(*socket, *write_buffer,
|
||||
strand->wrap([socket_, write_buffer, callback, async_writing_, async_timer_, async_waiting_]
|
||||
(const boost::system::error_code& ec, size_t bytes_transferred) {
|
||||
*async_writing_=false;
|
||||
if(*async_waiting_)
|
||||
async_timer_->cancel();
|
||||
if(callback)
|
||||
callback(ec);
|
||||
}));
|
||||
}
|
||||
|
||||
void flush() {
|
||||
boost::asio::streambuf write_buffer;
|
||||
std::ostream response(&write_buffer);
|
||||
response << stream.rdbuf();
|
||||
|
||||
boost::asio::async_write(*socket, write_buffer, yield);
|
||||
}
|
||||
|
||||
public:
|
||||
std::ostream stream;
|
||||
|
||||
template <class T>
|
||||
Response& operator<<(const T& t) {
|
||||
stream << t;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Response& operator<<(std::ostream& (*manip)(std::ostream&)) {
|
||||
stream << manip;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Response& operator<<(Response& (*manip)(Response&)) {
|
||||
return manip(*this);
|
||||
}
|
||||
};
|
||||
|
||||
static Response& async_flush(Response& r) {
|
||||
r.async_flush();
|
||||
return r;
|
||||
}
|
||||
|
||||
static Response& flush(Response& r) {
|
||||
r.flush();
|
||||
return r;
|
||||
}
|
||||
|
||||
class Request {
|
||||
friend class ServerBase<socket_type>;
|
||||
public:
|
||||
|
|
@ -23,41 +125,45 @@ namespace SimpleWeb {
|
|||
std::smatch path_match;
|
||||
|
||||
private:
|
||||
Request(): content(&content_buffer) {}
|
||||
Request(): content(&streambuf) {}
|
||||
|
||||
boost::asio::streambuf content_buffer;
|
||||
boost::asio::streambuf streambuf;
|
||||
};
|
||||
|
||||
typedef std::map<std::string, std::unordered_map<std::string,
|
||||
std::function<void(std::ostream&, std::shared_ptr<ServerBase<socket_type>::Request>)> > > resource_type;
|
||||
std::unordered_map<std::string, std::unordered_map<std::string,
|
||||
std::function<void(ServerBase<socket_type>::Response&, std::shared_ptr<ServerBase<socket_type>::Request>)> > > resource;
|
||||
|
||||
resource_type resource;
|
||||
std::unordered_map<std::string,
|
||||
std::function<void(ServerBase<socket_type>::Response&, std::shared_ptr<ServerBase<socket_type>::Request>)> > default_resource;
|
||||
|
||||
resource_type default_resource;
|
||||
private:
|
||||
std::vector<std::pair<std::regex, std::unordered_map<std::string,
|
||||
std::function<void(ServerBase<socket_type>::Response&, std::shared_ptr<ServerBase<socket_type>::Request>)> > > > regex_resource;
|
||||
|
||||
public:
|
||||
void start() {
|
||||
//All resources with default_resource at the end of vector
|
||||
//Used in the respond-method
|
||||
all_resources.clear();
|
||||
for(auto it=resource.begin(); it!=resource.end();it++) {
|
||||
all_resources.push_back(it);
|
||||
}
|
||||
for(auto it=default_resource.begin(); it!=default_resource.end();it++) {
|
||||
all_resources.push_back(it);
|
||||
//Move the resources with regular expressions to regex_resource for more efficient request processing
|
||||
for(auto it=resource.begin();it!=resource.end();) {
|
||||
if(it->first.size()>0 && it->first[0]=='r') {
|
||||
regex_resource.emplace_back(std::regex(it->first.substr(1)), std::move(it->second));
|
||||
it=resource.erase(it);
|
||||
}
|
||||
else
|
||||
it++;
|
||||
}
|
||||
|
||||
accept();
|
||||
accept();
|
||||
|
||||
//If num_threads>1, start m_io_service.run() in (num_threads-1) threads for thread-pooling
|
||||
threads.clear();
|
||||
for(size_t c=1;c<num_threads;c++) {
|
||||
threads.emplace_back([this](){
|
||||
m_io_service.run();
|
||||
io_service.run();
|
||||
});
|
||||
}
|
||||
|
||||
//Main thread
|
||||
m_io_service.run();
|
||||
io_service.run();
|
||||
|
||||
//Wait for the rest of the threads, if any, to finish as well
|
||||
for(auto& t: threads) {
|
||||
|
|
@ -66,11 +172,11 @@ namespace SimpleWeb {
|
|||
}
|
||||
|
||||
void stop() {
|
||||
m_io_service.stop();
|
||||
io_service.stop();
|
||||
}
|
||||
|
||||
protected:
|
||||
boost::asio::io_service m_io_service;
|
||||
boost::asio::io_service io_service;
|
||||
boost::asio::ip::tcp::endpoint endpoint;
|
||||
boost::asio::ip::tcp::acceptor acceptor;
|
||||
size_t num_threads;
|
||||
|
|
@ -78,28 +184,38 @@ namespace SimpleWeb {
|
|||
|
||||
size_t timeout_request;
|
||||
size_t timeout_content;
|
||||
|
||||
//All resources with default_resource at the end of vector
|
||||
//Created in start()
|
||||
std::vector<typename resource_type::iterator> all_resources;
|
||||
|
||||
ServerBase(unsigned short port, size_t num_threads, size_t timeout_request, size_t timeout_send_or_receive) :
|
||||
endpoint(boost::asio::ip::tcp::v4(), port), acceptor(m_io_service, endpoint), num_threads(num_threads),
|
||||
endpoint(boost::asio::ip::tcp::v4(), port), acceptor(io_service, endpoint), num_threads(num_threads),
|
||||
timeout_request(timeout_request), timeout_content(timeout_send_or_receive) {}
|
||||
|
||||
virtual void accept()=0;
|
||||
|
||||
std::shared_ptr<boost::asio::deadline_timer> set_timeout_on_socket(std::shared_ptr<socket_type> socket, size_t seconds) {
|
||||
std::shared_ptr<boost::asio::deadline_timer> timer(new boost::asio::deadline_timer(m_io_service));
|
||||
std::shared_ptr<boost::asio::deadline_timer> timer(new boost::asio::deadline_timer(io_service));
|
||||
timer->expires_from_now(boost::posix_time::seconds(seconds));
|
||||
timer->async_wait([socket](const boost::system::error_code& ec){
|
||||
if(!ec) {
|
||||
socket->lowest_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_both);
|
||||
boost::system::error_code ec;
|
||||
socket->lowest_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
|
||||
socket->lowest_layer().close();
|
||||
}
|
||||
});
|
||||
return timer;
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<boost::asio::deadline_timer> set_timeout_on_socket(std::shared_ptr<socket_type> socket, std::shared_ptr<boost::asio::strand> strand, size_t seconds) {
|
||||
std::shared_ptr<boost::asio::deadline_timer> timer(new boost::asio::deadline_timer(io_service));
|
||||
timer->expires_from_now(boost::posix_time::seconds(seconds));
|
||||
timer->async_wait(strand->wrap([socket](const boost::system::error_code& ec){
|
||||
if(!ec) {
|
||||
boost::system::error_code ec;
|
||||
socket->lowest_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
|
||||
socket->lowest_layer().close();
|
||||
}
|
||||
}));
|
||||
return timer;
|
||||
}
|
||||
|
||||
void read_request_and_content(std::shared_ptr<socket_type> socket) {
|
||||
//Create new streambuf (Request::streambuf) for async_read_until()
|
||||
|
|
@ -110,8 +226,8 @@ namespace SimpleWeb {
|
|||
std::shared_ptr<boost::asio::deadline_timer> timer;
|
||||
if(timeout_request>0)
|
||||
timer=set_timeout_on_socket(socket, timeout_request);
|
||||
|
||||
boost::asio::async_read_until(*socket, request->content_buffer, "\r\n\r\n",
|
||||
|
||||
boost::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) {
|
||||
if(timeout_request>0)
|
||||
timer->cancel();
|
||||
|
|
@ -120,7 +236,7 @@ namespace SimpleWeb {
|
|||
//"After a successful async_read_until operation, the streambuf may contain additional data beyond the delimiter"
|
||||
//The chosen solution is to extract lines from the stream directly when parsing the header. What is left of the
|
||||
//streambuf (maybe some bytes of the content) is appended to in the async_read-function below (for retrieving content).
|
||||
size_t num_additional_bytes=request->content_buffer.size()-bytes_transferred;
|
||||
size_t num_additional_bytes=request->streambuf.size()-bytes_transferred;
|
||||
|
||||
parse_request(request, request->content);
|
||||
|
||||
|
|
@ -131,84 +247,101 @@ namespace SimpleWeb {
|
|||
if(timeout_content>0)
|
||||
timer=set_timeout_on_socket(socket, timeout_content);
|
||||
|
||||
boost::asio::async_read(*socket, request->content_buffer,
|
||||
boost::asio::async_read(*socket, request->streambuf,
|
||||
boost::asio::transfer_exactly(stoull(request->header["Content-Length"])-num_additional_bytes),
|
||||
[this, socket, request, timer]
|
||||
(const boost::system::error_code& ec, size_t bytes_transferred) {
|
||||
if(timeout_content>0)
|
||||
timer->cancel();
|
||||
if(!ec)
|
||||
write_response(socket, request);
|
||||
find_resource(socket, request);
|
||||
});
|
||||
}
|
||||
else {
|
||||
write_response(socket, request);
|
||||
else {
|
||||
find_resource(socket, request);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void parse_request(std::shared_ptr<Request> request, std::istream& stream) const {
|
||||
std::regex e("^([^ ]*) ([^ ]*) HTTP/([^ ]*)$");
|
||||
|
||||
std::smatch sm;
|
||||
|
||||
//First parse request method, path, and HTTP-version from the first line
|
||||
std::string line;
|
||||
getline(stream, line);
|
||||
line.pop_back();
|
||||
if(std::regex_match(line, sm, e)) {
|
||||
request->method=sm[1];
|
||||
request->path=sm[2];
|
||||
request->http_version=sm[3];
|
||||
size_t method_end=line.find(' ');
|
||||
size_t path_end=line.find(' ', method_end+1);
|
||||
if(method_end!=std::string::npos && path_end!=std::string::npos) {
|
||||
request->method=line.substr(0, method_end);
|
||||
request->path=line.substr(method_end+1, path_end-method_end-1);
|
||||
request->http_version=line.substr(path_end+6, line.size()-path_end-7);
|
||||
|
||||
getline(stream, line);
|
||||
size_t param_end=line.find(':');
|
||||
while(param_end!=std::string::npos) {
|
||||
size_t value_start=param_end+1;
|
||||
if(line[value_start]==' ')
|
||||
value_start++;
|
||||
|
||||
request->header[line.substr(0, param_end)]=line.substr(value_start, line.size()-value_start-1);
|
||||
|
||||
bool matched;
|
||||
e="^([^:]*): ?(.*)$";
|
||||
//Parse the rest of the header
|
||||
do {
|
||||
getline(stream, line);
|
||||
line.pop_back();
|
||||
matched=std::regex_match(line, sm, e);
|
||||
if(matched) {
|
||||
request->header[sm[1]]=sm[2];
|
||||
}
|
||||
|
||||
} while(matched==true);
|
||||
param_end=line.find(':');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void write_response(std::shared_ptr<socket_type> socket, std::shared_ptr<Request> request) {
|
||||
//Find path- and method-match, and generate response
|
||||
for(auto res_it: all_resources) {
|
||||
std::regex e(res_it->first);
|
||||
std::smatch sm_res;
|
||||
if(std::regex_match(request->path, sm_res, e)) {
|
||||
if(res_it->second.count(request->method)>0) {
|
||||
request->path_match=move(sm_res);
|
||||
|
||||
std::shared_ptr<boost::asio::streambuf> write_buffer(new boost::asio::streambuf);
|
||||
std::ostream response(write_buffer.get());
|
||||
res_it->second[request->method](response, request);
|
||||
|
||||
//Set timeout on the following boost::asio::async-read or write function
|
||||
std::shared_ptr<boost::asio::deadline_timer> timer;
|
||||
if(timeout_content>0)
|
||||
timer=set_timeout_on_socket(socket, timeout_content);
|
||||
|
||||
//Capture write_buffer in lambda so it is not destroyed before async_write is finished
|
||||
boost::asio::async_write(*socket, *write_buffer,
|
||||
[this, socket, request, write_buffer, timer]
|
||||
(const boost::system::error_code& ec, size_t bytes_transferred) {
|
||||
if(timeout_content>0)
|
||||
timer->cancel();
|
||||
//HTTP persistent connection (HTTP 1.1):
|
||||
if(!ec && stof(request->http_version)>1.05)
|
||||
read_request_and_content(socket);
|
||||
});
|
||||
void find_resource(std::shared_ptr<socket_type> socket, std::shared_ptr<Request> request) {
|
||||
//Find path- and method-match, and call write_response
|
||||
auto it_path=resource.find(request->path);
|
||||
if(it_path!=resource.end()) {
|
||||
auto it_method=it_path->second.find(request->method);
|
||||
if(it_method!=it_path->second.end()) {
|
||||
write_response(socket, request, it_method->second);
|
||||
return;
|
||||
}
|
||||
}
|
||||
for(auto& res: regex_resource) {
|
||||
auto it_method=res.second.find(request->method);
|
||||
if(it_method!=res.second.end()) {
|
||||
std::smatch sm_res;
|
||||
if(std::regex_match(request->path, sm_res, res.first)) {
|
||||
request->path_match=std::move(sm_res);
|
||||
write_response(socket, request, it_method->second);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
auto it_method=default_resource.find(request->method);
|
||||
if(it_method!=default_resource.end()) {
|
||||
write_response(socket, request, it_method->second);
|
||||
}
|
||||
}
|
||||
|
||||
void write_response(std::shared_ptr<socket_type> socket, std::shared_ptr<Request> request,
|
||||
std::function<void(ServerBase<socket_type>::Response&, std::shared_ptr<ServerBase<socket_type>::Request>)>& resource_function) {
|
||||
std::shared_ptr<boost::asio::strand> strand(new boost::asio::strand(io_service));
|
||||
|
||||
//Set timeout on the following boost::asio::async-read or write function
|
||||
std::shared_ptr<boost::asio::deadline_timer> timer;
|
||||
if(timeout_content>0)
|
||||
timer=set_timeout_on_socket(socket, strand, timeout_content);
|
||||
|
||||
boost::asio::spawn(*strand, [this, strand, &resource_function, socket, request, timer](boost::asio::yield_context yield) {
|
||||
Response response(io_service, socket, strand, yield);
|
||||
|
||||
try {
|
||||
resource_function(response, request);
|
||||
}
|
||||
catch(std::exception& e) {
|
||||
return;
|
||||
}
|
||||
|
||||
response.async_flush([this, socket, request, timer](const boost::system::error_code& ec) {
|
||||
if(timeout_content>0)
|
||||
timer->cancel();
|
||||
if(!ec && stof(request->http_version)>1.05)
|
||||
read_request_and_content(socket);
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -227,13 +360,16 @@ namespace SimpleWeb {
|
|||
void accept() {
|
||||
//Create new socket for this connection
|
||||
//Shared_ptr is used to pass temporary objects to the asynchronous functions
|
||||
std::shared_ptr<HTTP> socket(new HTTP(m_io_service));
|
||||
|
||||
acceptor.async_accept(*socket, [this, socket](const boost::system::error_code& ec) {
|
||||
std::shared_ptr<HTTP> socket(new HTTP(io_service));
|
||||
|
||||
acceptor.async_accept(*socket, [this, socket](const boost::system::error_code& ec){
|
||||
//Immediately start accepting a new connection
|
||||
accept();
|
||||
|
||||
|
||||
if(!ec) {
|
||||
boost::asio::ip::tcp::no_delay option(true);
|
||||
socket->set_option(option);
|
||||
|
||||
read_request_and_content(socket);
|
||||
}
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue