Minor updates.

This commit is contained in:
eidheim 2014-07-31 22:27:57 +02:00
commit 00dbe0bdd5
3 changed files with 24 additions and 21 deletions

View file

@ -17,7 +17,7 @@ int main() {
//Add resources using regular expression for path, a method-string, and an anonymous function //Add resources using regular expression for path, a 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.resources["^/string/?$"]["POST"]=[](ostream& response, const Request& request, const smatch& path_match) { server.resources["^/string/?$"]["POST"]=[](ostream& response, Request& request) {
//Retrieve string from istream (*request.content) //Retrieve string from istream (*request.content)
stringstream ss; stringstream ss;
*request.content >> ss.rdbuf(); *request.content >> ss.rdbuf();
@ -34,7 +34,7 @@ int main() {
// "lastName": "Smith", // "lastName": "Smith",
// "age": 25 // "age": 25
//} //}
server.resources["^/json/?$"]["POST"]=[](ostream& response, const Request& request, const smatch& path_match) { server.resources["^/json/?$"]["POST"]=[](ostream& response, Request& request) {
try { try {
ptree pt; ptree pt;
read_json(*request.content, pt); read_json(*request.content, pt);
@ -50,7 +50,7 @@ int main() {
//GET-example for the path /info //GET-example for the path /info
//Responds with request-information //Responds with request-information
server.resources["^/info/?$"]["GET"]=[](ostream& response, const Request& request, const smatch& path_match) { server.resources["^/info/?$"]["GET"]=[](ostream& response, Request& request) {
stringstream content_stream; stringstream content_stream;
content_stream << "<h1>Request:</h1>"; content_stream << "<h1>Request:</h1>";
content_stream << request.method << " " << request.path << " HTTP/" << request.http_version << "<br>"; content_stream << request.method << " " << request.path << " HTTP/" << request.http_version << "<br>";
@ -66,8 +66,8 @@ int main() {
//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.resources["^/match/([0-9]+)/?$"]["GET"]=[](ostream& response, const Request& request, const smatch& path_match) { server.resources["^/match/([0-9]+)/?$"]["GET"]=[](ostream& response, Request& request) {
string number=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;
}; };
@ -75,10 +75,10 @@ int main() {
//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"]=[](ostream& response, const Request& request, const smatch& path_match) { server.default_resource["^/?(.*)$"]["GET"]=[](ostream& response, Request& request) {
string filename="web/"; string filename="web/";
string path=path_match[1]; string path=request.path_match[1];
//Remove all but the last '.' (so we can't leave the web-directory) //Remove all but the last '.' (so we can't leave the web-directory)
size_t last_pos=path.rfind("."); size_t last_pos=path.rfind(".");

View file

@ -17,7 +17,7 @@ int main() {
//Add resources using regular expression for path, a method-string, and an anonymous function //Add resources using regular expression for path, a 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.resources["^/string/?$"]["POST"]=[](ostream& response, const Request& request, const smatch& path_match) { server.resources["^/string/?$"]["POST"]=[](ostream& response, Request& request) {
//Retrieve string from istream (*request.content) //Retrieve string from istream (*request.content)
stringstream ss; stringstream ss;
*request.content >> ss.rdbuf(); *request.content >> ss.rdbuf();
@ -34,7 +34,7 @@ int main() {
// "lastName": "Smith", // "lastName": "Smith",
// "age": 25 // "age": 25
//} //}
server.resources["^/json/?$"]["POST"]=[](ostream& response, const Request& request, const smatch& path_match) { server.resources["^/json/?$"]["POST"]=[](ostream& response, Request& request) {
try { try {
ptree pt; ptree pt;
read_json(*request.content, pt); read_json(*request.content, pt);
@ -50,7 +50,7 @@ int main() {
//GET-example for the path /info //GET-example for the path /info
//Responds with request-information //Responds with request-information
server.resources["^/info/?$"]["GET"]=[](ostream& response, const Request& request, const smatch& path_match) { server.resources["^/info/?$"]["GET"]=[](ostream& response, Request& request) {
stringstream content_stream; stringstream content_stream;
content_stream << "<h1>Request:</h1>"; content_stream << "<h1>Request:</h1>";
content_stream << request.method << " " << request.path << " HTTP/" << request.http_version << "<br>"; content_stream << request.method << " " << request.path << " HTTP/" << request.http_version << "<br>";
@ -66,8 +66,8 @@ int main() {
//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.resources["^/match/([0-9]+)/?$"]["GET"]=[](ostream& response, const Request& request, const smatch& path_match) { server.resources["^/match/([0-9]+)/?$"]["GET"]=[](ostream& response, Request& request) {
string number=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;
}; };
@ -75,10 +75,10 @@ int main() {
//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"]=[](ostream& response, const Request& request, const smatch& path_match) { server.default_resource["^/?(.*)$"]["GET"]=[](ostream& response, Request& request) {
string filename="web/"; string filename="web/";
string path=path_match[1]; string path=request.path_match[1];
//Remove all but the last '.' (so we can't leave the web-directory) //Remove all but the last '.' (so we can't leave the web-directory)
size_t last_pos=path.rfind("."); size_t last_pos=path.rfind(".");
@ -117,7 +117,7 @@ int main() {
} }
}; };
//Start HTTP-server //Start HTTPS-server
server.start(); server.start();
return 0; return 0;

View file

@ -14,10 +14,12 @@ namespace SimpleWeb {
std::shared_ptr<std::istream> content; std::shared_ptr<std::istream> content;
std::unordered_map<std::string, std::string> header; std::unordered_map<std::string, std::string> header;
std::smatch path_match;
}; };
typedef std::map<std::string, std::unordered_map<std::string, typedef std::map<std::string, std::unordered_map<std::string,
std::function<void(std::ostream&, const Request&, const std::smatch&)> > > resource_type; std::function<void(std::ostream&, Request&)> > > resource_type;
template <class socket_type> template <class socket_type>
class ServerBase { class ServerBase {
@ -61,7 +63,6 @@ namespace SimpleWeb {
boost::asio::io_service m_io_service; boost::asio::io_service m_io_service;
boost::asio::ip::tcp::endpoint endpoint; boost::asio::ip::tcp::endpoint endpoint;
boost::asio::ip::tcp::acceptor acceptor; boost::asio::ip::tcp::acceptor acceptor;
//shared_ptr<ssl::context> context;
size_t num_threads; size_t num_threads;
std::vector<std::thread> threads; std::vector<std::thread> threads;
@ -71,7 +72,7 @@ namespace SimpleWeb {
virtual void accept() {} virtual void accept() {}
void process_request_and_respond(std::shared_ptr<socket_type> socket) { void process_request_and_respond(std::shared_ptr<socket_type> socket) const {
//Create new read_buffer for async_read_until() //Create new read_buffer for async_read_until()
//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<boost::asio::streambuf> read_buffer(new boost::asio::streambuf); std::shared_ptr<boost::asio::streambuf> read_buffer(new boost::asio::streambuf);
@ -113,7 +114,7 @@ namespace SimpleWeb {
}); });
} }
Request parse_request(std::istream& stream) { Request parse_request(std::istream& stream) const {
Request request; Request request;
std::regex e("^([^ ]*) ([^ ]*) HTTP/([^ ]*)$"); std::regex e("^([^ ]*) ([^ ]*) HTTP/([^ ]*)$");
@ -146,16 +147,18 @@ namespace SimpleWeb {
return request; return request;
} }
void respond(std::shared_ptr<socket_type> socket, std::shared_ptr<Request> request) { void respond(std::shared_ptr<socket_type> socket, std::shared_ptr<Request> request) const {
//Find path- and method-match, and generate response //Find path- and method-match, and generate response
for(auto res_it: all_resources) { for(auto res_it: all_resources) {
std::regex e(res_it->first); std::regex e(res_it->first);
std::smatch sm_res; std::smatch sm_res;
if(std::regex_match(request->path, sm_res, e)) { if(std::regex_match(request->path, sm_res, e)) {
if(res_it->second.count(request->method)>0) { 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::shared_ptr<boost::asio::streambuf> write_buffer(new boost::asio::streambuf);
std::ostream response(write_buffer.get()); std::ostream response(write_buffer.get());
res_it->second[request->method](response, *request, sm_res); res_it->second[request->method](response, *request);
//Capture write_buffer in lambda so it is not destroyed before async_write is finished //Capture write_buffer in lambda so it is not destroyed before async_write is finished
boost::asio::async_write(*socket, *write_buffer, boost::asio::async_write(*socket, *write_buffer,