Fixes #46: library is now fully asynchronous

This commit is contained in:
eidheim 2016-06-28 12:49:12 +02:00
commit dc466e7d1d
4 changed files with 128 additions and 128 deletions

View file

@ -20,7 +20,7 @@ include_directories(.)
find_package(Threads REQUIRED) find_package(Threads REQUIRED)
find_package(Boost 1.54.0 COMPONENTS regex system thread coroutine context filesystem date_time REQUIRED) find_package(Boost 1.54.0 COMPONENTS regex system thread filesystem date_time REQUIRED)
include_directories(SYSTEM ${Boost_INCLUDE_DIR}) include_directories(SYSTEM ${Boost_INCLUDE_DIR})
if(APPLE) if(APPLE)

View file

@ -20,12 +20,14 @@ typedef SimpleWeb::Server<SimpleWeb::HTTP> HttpServer;
typedef SimpleWeb::Client<SimpleWeb::HTTP> HttpClient; typedef SimpleWeb::Client<SimpleWeb::HTTP> HttpClient;
int main() { int main() {
//HTTP-server at port 8080 using 4 threads //HTTP-server at port 8080 using 1 thread
HttpServer server(8080, 4); //Unless you do more heavy non-threaded processing in the resources,
//1 thread is usually faster than several threads
HttpServer server(8080, 1);
//Add resources using path-regex and method-string, and an anonymous function //Add resources using path-regex and method-string, and an anonymous function
//POST-example for the path /string, responds the posted string //POST-example for the path /string, responds the posted string
server.resource["^/string$"]["POST"]=[](HttpServer::Response& response, shared_ptr<HttpServer::Request> request) { server.resource["^/string$"]["POST"]=[](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) {
//Retrieve string: //Retrieve string:
auto content=request->content.string(); auto content=request->content.string();
//request->content.string() is a convenience function for: //request->content.string() is a convenience function for:
@ -33,7 +35,7 @@ int main() {
//ss << request->content.rdbuf(); //ss << request->content.rdbuf();
//string content=ss.str(); //string content=ss.str();
response << "HTTP/1.1 200 OK\r\nContent-Length: " << content.length() << "\r\n\r\n" << content; *response << "HTTP/1.1 200 OK\r\nContent-Length: " << content.length() << "\r\n\r\n" << content;
}; };
//POST-example for the path /json, responds firstName+" "+lastName from the posted json //POST-example for the path /json, responds firstName+" "+lastName from the posted json
@ -44,23 +46,23 @@ int main() {
// "lastName": "Smith", // "lastName": "Smith",
// "age": 25 // "age": 25
//} //}
server.resource["^/json$"]["POST"]=[](HttpServer::Response& response, shared_ptr<HttpServer::Request> request) { server.resource["^/json$"]["POST"]=[](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) {
try { try {
ptree pt; ptree pt;
read_json(request->content, pt); read_json(request->content, pt);
string name=pt.get<string>("firstName")+" "+pt.get<string>("lastName"); string name=pt.get<string>("firstName")+" "+pt.get<string>("lastName");
response << "HTTP/1.1 200 OK\r\nContent-Length: " << name.length() << "\r\n\r\n" << name; *response << "HTTP/1.1 200 OK\r\nContent-Length: " << name.length() << "\r\n\r\n" << name;
} }
catch(exception& e) { catch(exception& e) {
response << "HTTP/1.1 400 Bad Request\r\nContent-Length: " << strlen(e.what()) << "\r\n\r\n" << e.what(); *response << "HTTP/1.1 400 Bad Request\r\nContent-Length: " << strlen(e.what()) << "\r\n\r\n" << e.what();
} }
}; };
//GET-example for the path /info //GET-example for the path /info
//Responds with request-information //Responds with request-information
server.resource["^/info$"]["GET"]=[](HttpServer::Response& response, shared_ptr<HttpServer::Request> request) { server.resource["^/info$"]["GET"]=[](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) {
stringstream content_stream; stringstream content_stream;
content_stream << "<h1>Request from " << request->remote_endpoint_address << " (" << request->remote_endpoint_port << ")</h1>"; content_stream << "<h1>Request from " << request->remote_endpoint_address << " (" << request->remote_endpoint_port << ")</h1>";
content_stream << request->method << " " << request->path << " HTTP/" << request->http_version << "<br>"; content_stream << request->method << " " << request->path << " HTTP/" << request->http_version << "<br>";
@ -71,21 +73,21 @@ int main() {
//find length of content_stream (length received using content_stream.tellp()) //find length of content_stream (length received using content_stream.tellp())
content_stream.seekp(0, ios::end); content_stream.seekp(0, ios::end);
response << "HTTP/1.1 200 OK\r\nContent-Length: " << content_stream.tellp() << "\r\n\r\n" << content_stream.rdbuf(); *response << "HTTP/1.1 200 OK\r\nContent-Length: " << content_stream.tellp() << "\r\n\r\n" << content_stream.rdbuf();
}; };
//GET-example for the path /match/[number], responds with the matched string in path (number) //GET-example for the path /match/[number], responds with the matched string in path (number)
//For instance a request GET /match/123 will receive: 123 //For instance a request GET /match/123 will receive: 123
server.resource["^/match/([0-9]+)$"]["GET"]=[](HttpServer::Response& response, shared_ptr<HttpServer::Request> request) { server.resource["^/match/([0-9]+)$"]["GET"]=[&server](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) {
string number=request->path_match[1]; string number=request->path_match[1];
response << "HTTP/1.1 200 OK\r\nContent-Length: " << number.length() << "\r\n\r\n" << number; *response << "HTTP/1.1 200 OK\r\nContent-Length: " << number.length() << "\r\n\r\n" << number;
}; };
//Default GET-example. If no other matches, this anonymous function will be called. //Default GET-example. If no other matches, this anonymous function will be called.
//Will respond with content in the web/-directory, and its subdirectories. //Will respond with content in the web/-directory, and its subdirectories.
//Default file: index.html //Default file: index.html
//Can for instance be used to retrieve an HTML 5 client that uses REST-resources on this server //Can for instance be used to retrieve an HTML 5 client that uses REST-resources on this server
server.default_resource["GET"]=[](HttpServer::Response& response, shared_ptr<HttpServer::Request> request) { server.default_resource["GET"]=[&server](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) {
const auto web_root_path=boost::filesystem::canonical("web"); const auto web_root_path=boost::filesystem::canonical("web");
boost::filesystem::path path=web_root_path; boost::filesystem::path path=web_root_path;
path/=request->path; path/=request->path;
@ -97,39 +99,41 @@ int main() {
if(boost::filesystem::is_directory(path)) if(boost::filesystem::is_directory(path))
path/="index.html"; path/="index.html";
if(boost::filesystem::exists(path) && boost::filesystem::is_regular_file(path)) { if(boost::filesystem::exists(path) && boost::filesystem::is_regular_file(path)) {
ifstream ifs; auto ifs=make_shared<ifstream>();
ifs.open(path.string(), ifstream::in | ios::binary); ifs->open(path.string(), ifstream::in | ios::binary);
if(ifs) { if(ifs) {
ifs.seekg(0, ios::end);
auto length=ifs.tellg();
ifs.seekg(0, ios::beg);
response << "HTTP/1.1 200 OK\r\nContent-Length: " << length << "\r\n\r\n";
//read and send 128 KB at a time //read and send 128 KB at a time
const size_t buffer_size=131072; size_t buffer_size=131072;
vector<char> buffer(buffer_size); auto buffer=make_shared<vector<char>>(buffer_size);
streamsize read_length;
try {
while((read_length=ifs.read(&buffer[0], buffer_size).gcount())>0) {
response.write(&buffer[0], read_length);
response.flush();
}
}
catch(const exception &) {
cerr << "Connection interrupted, closing file" << endl;
}
ifs.close(); auto send_callback=make_shared<std::function<void(const boost::system::error_code&)> >(nullptr);
*send_callback=[&server, response, ifs, buffer, buffer_size, send_callback](const boost::system::error_code &ec) {
if(!ec) {
streamsize read_length;
if((read_length=ifs->read(&(*buffer)[0], buffer_size).gcount())>0) {
response->write(&(*buffer)[0], read_length);
server.send(response, *send_callback);
}
}
else
cerr << "Connection interrupted" << endl;
};
ifs->seekg(0, ios::end);
auto length=ifs->tellg();
ifs->seekg(0, ios::beg);
*response << "HTTP/1.1 200 OK\r\nContent-Length: " << length << "\r\n\r\n";
server.send(response, *send_callback);
return; return;
} }
} }
} }
} }
string content="Could not open path "+request->path; string content="Could not open path "+request->path;
response << "HTTP/1.1 400 Bad Request\r\nContent-Length: " << content.length() << "\r\n\r\n" << content; *response << "HTTP/1.1 400 Bad Request\r\nContent-Length: " << content.length() << "\r\n\r\n" << content;
}; };
thread server_thread([&server](){ thread server_thread([&server](){

View file

@ -20,12 +20,14 @@ typedef SimpleWeb::Server<SimpleWeb::HTTPS> HttpsServer;
typedef SimpleWeb::Client<SimpleWeb::HTTPS> HttpsClient; typedef SimpleWeb::Client<SimpleWeb::HTTPS> HttpsClient;
int main() { int main() {
//HTTPS-server at port 8080 using 4 threads //HTTPS-server at port 8080 using 1 thread
HttpsServer server(8080, 4, "server.crt", "server.key"); //Unless you do more heavy non-threaded processing in the resources,
//1 thread is usually faster than several threads
HttpsServer server(8080, 1, "server.crt", "server.key");
//Add resources using path-regex and method-string, and an anonymous function //Add resources using path-regex and method-string, and an anonymous function
//POST-example for the path /string, responds the posted string //POST-example for the path /string, responds the posted string
server.resource["^/string$"]["POST"]=[](HttpsServer::Response& response, shared_ptr<HttpsServer::Request> request) { server.resource["^/string$"]["POST"]=[](shared_ptr<HttpsServer::Response> response, shared_ptr<HttpsServer::Request> request) {
//Retrieve string: //Retrieve string:
auto content=request->content.string(); auto content=request->content.string();
//request->content.string() is a convenience function for: //request->content.string() is a convenience function for:
@ -33,7 +35,7 @@ int main() {
//ss << request->content.rdbuf(); //ss << request->content.rdbuf();
//string content=ss.str(); //string content=ss.str();
response << "HTTP/1.1 200 OK\r\nContent-Length: " << content.length() << "\r\n\r\n" << content; *response << "HTTP/1.1 200 OK\r\nContent-Length: " << content.length() << "\r\n\r\n" << content;
}; };
//POST-example for the path /json, responds firstName+" "+lastName from the posted json //POST-example for the path /json, responds firstName+" "+lastName from the posted json
@ -44,23 +46,23 @@ int main() {
// "lastName": "Smith", // "lastName": "Smith",
// "age": 25 // "age": 25
//} //}
server.resource["^/json$"]["POST"]=[](HttpsServer::Response& response, shared_ptr<HttpsServer::Request> request) { server.resource["^/json$"]["POST"]=[](shared_ptr<HttpsServer::Response> response, shared_ptr<HttpsServer::Request> request) {
try { try {
ptree pt; ptree pt;
read_json(request->content, pt); read_json(request->content, pt);
string name=pt.get<string>("firstName")+" "+pt.get<string>("lastName"); string name=pt.get<string>("firstName")+" "+pt.get<string>("lastName");
response << "HTTP/1.1 200 OK\r\nContent-Length: " << name.length() << "\r\n\r\n" << name; *response << "HTTP/1.1 200 OK\r\nContent-Length: " << name.length() << "\r\n\r\n" << name;
} }
catch(exception& e) { catch(exception& e) {
response << "HTTP/1.1 400 Bad Request\r\nContent-Length: " << strlen(e.what()) << "\r\n\r\n" << e.what(); *response << "HTTP/1.1 400 Bad Request\r\nContent-Length: " << strlen(e.what()) << "\r\n\r\n" << e.what();
} }
}; };
//GET-example for the path /info //GET-example for the path /info
//Responds with request-information //Responds with request-information
server.resource["^/info$"]["GET"]=[](HttpsServer::Response& response, shared_ptr<HttpsServer::Request> request) { server.resource["^/info$"]["GET"]=[](shared_ptr<HttpsServer::Response> response, shared_ptr<HttpsServer::Request> request) {
stringstream content_stream; stringstream content_stream;
content_stream << "<h1>Request from " << request->remote_endpoint_address << " (" << request->remote_endpoint_port << ")</h1>"; content_stream << "<h1>Request from " << request->remote_endpoint_address << " (" << request->remote_endpoint_port << ")</h1>";
content_stream << request->method << " " << request->path << " HTTP/" << request->http_version << "<br>"; content_stream << request->method << " " << request->path << " HTTP/" << request->http_version << "<br>";
@ -71,21 +73,21 @@ int main() {
//find length of content_stream (length received using content_stream.tellp()) //find length of content_stream (length received using content_stream.tellp())
content_stream.seekp(0, ios::end); content_stream.seekp(0, ios::end);
response << "HTTP/1.1 200 OK\r\nContent-Length: " << content_stream.tellp() << "\r\n\r\n" << content_stream.rdbuf(); *response << "HTTP/1.1 200 OK\r\nContent-Length: " << content_stream.tellp() << "\r\n\r\n" << content_stream.rdbuf();
}; };
//GET-example for the path /match/[number], responds with the matched string in path (number) //GET-example for the path /match/[number], responds with the matched string in path (number)
//For instance a request GET /match/123 will receive: 123 //For instance a request GET /match/123 will receive: 123
server.resource["^/match/([0-9]+)$"]["GET"]=[](HttpsServer::Response& response, shared_ptr<HttpsServer::Request> request) { server.resource["^/match/([0-9]+)$"]["GET"]=[&server](shared_ptr<HttpsServer::Response> response, shared_ptr<HttpsServer::Request> request) {
string number=request->path_match[1]; string number=request->path_match[1];
response << "HTTP/1.1 200 OK\r\nContent-Length: " << number.length() << "\r\n\r\n" << number; *response << "HTTP/1.1 200 OK\r\nContent-Length: " << number.length() << "\r\n\r\n" << number;
}; };
//Default GET-example. If no other matches, this anonymous function will be called. //Default GET-example. If no other matches, this anonymous function will be called.
//Will respond with content in the web/-directory, and its subdirectories. //Will respond with content in the web/-directory, and its subdirectories.
//Default file: index.html //Default file: index.html
//Can for instance be used to retrieve an HTML 5 client that uses REST-resources on this server //Can for instance be used to retrieve an HTML 5 client that uses REST-resources on this server
server.default_resource["GET"]=[](HttpsServer::Response& response, shared_ptr<HttpsServer::Request> request) { server.default_resource["GET"]=[&server](shared_ptr<HttpsServer::Response> response, shared_ptr<HttpsServer::Request> request) {
const auto web_root_path=boost::filesystem::canonical("web"); const auto web_root_path=boost::filesystem::canonical("web");
boost::filesystem::path path=web_root_path; boost::filesystem::path path=web_root_path;
path/=request->path; path/=request->path;
@ -97,39 +99,41 @@ int main() {
if(boost::filesystem::is_directory(path)) if(boost::filesystem::is_directory(path))
path/="index.html"; path/="index.html";
if(boost::filesystem::exists(path) && boost::filesystem::is_regular_file(path)) { if(boost::filesystem::exists(path) && boost::filesystem::is_regular_file(path)) {
ifstream ifs; auto ifs=make_shared<ifstream>();
ifs.open(path.string(), ifstream::in | ios::binary); ifs->open(path.string(), ifstream::in | ios::binary);
if(ifs) { if(ifs) {
ifs.seekg(0, ios::end);
auto length=ifs.tellg();
ifs.seekg(0, ios::beg);
response << "HTTP/1.1 200 OK\r\nContent-Length: " << length << "\r\n\r\n";
//read and send 128 KB at a time //read and send 128 KB at a time
const size_t buffer_size=131072; size_t buffer_size=131072;
vector<char> buffer(buffer_size); auto buffer=make_shared<vector<char>>(buffer_size);
streamsize read_length;
try {
while((read_length=ifs.read(&buffer[0], buffer_size).gcount())>0) {
response.write(&buffer[0], read_length);
response.flush();
}
}
catch(const exception &) {
cerr << "Connection interrupted, closing file" << endl;
}
ifs.close(); auto send_callback=make_shared<std::function<void(const boost::system::error_code&)> >(nullptr);
*send_callback=[&server, response, ifs, buffer, buffer_size, send_callback](const boost::system::error_code &ec) {
if(!ec) {
streamsize read_length;
if((read_length=ifs->read(&(*buffer)[0], buffer_size).gcount())>0) {
response->write(&(*buffer)[0], read_length);
server.send(response, *send_callback);
}
}
else
cerr << "Connection interrupted" << endl;
};
ifs->seekg(0, ios::end);
auto length=ifs->tellg();
ifs->seekg(0, ios::beg);
*response << "HTTP/1.1 200 OK\r\nContent-Length: " << length << "\r\n\r\n";
server.send(response, *send_callback);
return; return;
} }
} }
} }
} }
string content="Could not open path "+request->path; string content="Could not open path "+request->path;
response << "HTTP/1.1 400 Bad Request\r\nContent-Length: " << content.length() << "\r\n\r\n" << content; *response << "HTTP/1.1 400 Bad Request\r\nContent-Length: " << content.length() << "\r\n\r\n" << content;
}; };
thread server_thread([&server](){ thread server_thread([&server](){

View file

@ -2,7 +2,6 @@
#define SERVER_HTTP_HPP #define SERVER_HTTP_HPP
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/regex.hpp> #include <boost/regex.hpp>
#include <boost/algorithm/string/predicate.hpp> #include <boost/algorithm/string/predicate.hpp>
#include <boost/functional/hash.hpp> #include <boost/functional/hash.hpp>
@ -21,27 +20,17 @@ namespace SimpleWeb {
class Response : public std::ostream { class Response : public std::ostream {
friend class ServerBase<socket_type>; friend class ServerBase<socket_type>;
private:
boost::asio::yield_context& yield;
boost::asio::streambuf streambuf; boost::asio::streambuf streambuf;
socket_type &socket; std::shared_ptr<socket_type> socket;
Response(socket_type &socket, boost::asio::yield_context& yield): Response(std::shared_ptr<socket_type> socket): std::ostream(&streambuf), socket(socket) {}
std::ostream(&streambuf), yield(yield), socket(socket) {}
public: public:
size_t size() { size_t size() {
return streambuf.size(); return streambuf.size();
} }
void flush() {
boost::system::error_code ec;
boost::asio::async_write(socket, streambuf, yield[ec]);
if(ec)
throw std::runtime_error(ec.message());
}
}; };
class Content : public std::istream { class Content : public std::istream {
@ -109,11 +98,11 @@ namespace SimpleWeb {
class Config { class Config {
friend class ServerBase<socket_type>; friend class ServerBase<socket_type>;
private:
Config(unsigned short port, size_t num_threads): port(port), num_threads(num_threads), reuse_address(true) {} Config(unsigned short port, size_t num_threads): num_threads(num_threads), port(port), reuse_address(true) {}
unsigned short port;
size_t num_threads; size_t num_threads;
public: public:
unsigned short port;
///IPv4 address in dotted decimal form or IPv6 address in hexadecimal notation. ///IPv4 address in dotted decimal form or IPv6 address in hexadecimal notation.
///If empty, the address will be any address. ///If empty, the address will be any address.
std::string address; std::string address;
@ -124,14 +113,14 @@ namespace SimpleWeb {
Config config; Config config;
std::unordered_map<std::string, std::unordered_map<std::string, std::unordered_map<std::string, std::unordered_map<std::string,
std::function<void(typename ServerBase<socket_type>::Response&, std::shared_ptr<typename ServerBase<socket_type>::Request>)> > > resource; std::function<void(std::shared_ptr<typename ServerBase<socket_type>::Response>, std::shared_ptr<typename ServerBase<socket_type>::Request>)> > > resource;
std::unordered_map<std::string, std::unordered_map<std::string,
std::function<void(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;
private: private:
std::vector<std::pair<std::string, std::vector<std::pair<boost::regex, std::vector<std::pair<std::string, std::vector<std::pair<boost::regex,
std::function<void(typename ServerBase<socket_type>::Response&, std::shared_ptr<typename ServerBase<socket_type>::Request>)> > > > > opt_resource; std::function<void(std::shared_ptr<typename ServerBase<socket_type>::Response>, std::shared_ptr<typename ServerBase<socket_type>::Request>)> > > > > opt_resource;
public: public:
void start() { void start() {
@ -192,6 +181,13 @@ namespace SimpleWeb {
io_service.stop(); io_service.stop();
} }
void send(std::shared_ptr<Response> response, const std::function<void(const boost::system::error_code&)>& callback=nullptr) {
boost::asio::async_write(*response->socket, response->streambuf, [this, response, callback](const boost::system::error_code& ec, size_t /*bytes_transferred*/) {
if(callback)
callback(ec);
});
}
protected: protected:
boost::asio::io_service io_service; boost::asio::io_service io_service;
boost::asio::ip::tcp::acceptor acceptor; boost::asio::ip::tcp::acceptor acceptor;
@ -357,48 +353,44 @@ namespace SimpleWeb {
} }
void write_response(std::shared_ptr<socket_type> socket, std::shared_ptr<Request> request, void write_response(std::shared_ptr<socket_type> socket, std::shared_ptr<Request> request,
std::function<void(typename ServerBase<socket_type>::Response&, std::shared_ptr<typename ServerBase<socket_type>::Request>)>& resource_function) { std::function<void(std::shared_ptr<typename ServerBase<socket_type>::Response>,
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 boost::asio::async-read or write function
std::shared_ptr<boost::asio::deadline_timer> timer; std::shared_ptr<boost::asio::deadline_timer> timer;
if(timeout_content>0) if(timeout_content>0)
timer=set_timeout_on_socket(socket, request, timeout_content); timer=set_timeout_on_socket(socket, request, timeout_content);
boost::asio::spawn(request->strand, [this, &resource_function, socket, request, timer](boost::asio::yield_context yield) { auto response=std::shared_ptr<Response>(new Response(socket), [this, request, timer](Response *response_ptr) {
Response response(*socket, yield); auto response=std::shared_ptr<Response>(response_ptr);
send(response, [this, response, request, timer](const boost::system::error_code& ec) {
if(!ec) {
if(timeout_content>0)
timer->cancel();
float http_version;
try {
http_version=stof(request->http_version);
}
catch(const std::exception &) {
return;
}
try { auto range=request->header.equal_range("Connection");
resource_function(response, request); for(auto it=range.first;it!=range.second;it++) {
} if(boost::iequals(it->second, "close"))
catch(const std::exception&) { return;
return; }
} if(http_version>1.05)
read_request_and_content(response->socket);
if(response.size()>0) {
try {
response.flush();
} }
catch(const std::exception &) { });
return;
}
}
if(timeout_content>0)
timer->cancel();
float http_version;
try {
http_version=stof(request->http_version);
}
catch(const std::exception &) {
return;
}
auto range=request->header.equal_range("Connection");
for(auto it=range.first;it!=range.second;it++) {
if(boost::iequals(it->second, "close"))
return;
}
if(http_version>1.05)
read_request_and_content(socket);
}); });
try {
resource_function(response, request);
}
catch(const std::exception&) {
return;
}
} }
}; };