From 00dbe0bdd500e0f9a40af59f6cd98c8452ca0a37 Mon Sep 17 00:00:00 2001 From: eidheim Date: Thu, 31 Jul 2014 22:27:57 +0200 Subject: [PATCH] Minor updates. --- main_http.cpp | 14 +++++++------- main_https.cpp | 16 ++++++++-------- server_http.hpp | 15 +++++++++------ 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/main_http.cpp b/main_http.cpp index 627e7a0..120ac71 100644 --- a/main_http.cpp +++ b/main_http.cpp @@ -17,7 +17,7 @@ int main() { //Add resources using regular expression for path, a method-string, and an anonymous function //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) stringstream ss; *request.content >> ss.rdbuf(); @@ -34,7 +34,7 @@ int main() { // "lastName": "Smith", // "age": 25 //} - server.resources["^/json/?$"]["POST"]=[](ostream& response, const Request& request, const smatch& path_match) { + server.resources["^/json/?$"]["POST"]=[](ostream& response, Request& request) { try { ptree pt; read_json(*request.content, pt); @@ -50,7 +50,7 @@ int main() { //GET-example for the path /info //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; content_stream << "

Request:

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

Request:

"; content_stream << request.method << " " << request.path << " HTTP/" << request.http_version << "
"; @@ -66,8 +66,8 @@ int main() { //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 - server.resources["^/match/([0-9]+)/?$"]["GET"]=[](ostream& response, const Request& request, const smatch& path_match) { - string number=path_match[1]; + server.resources["^/match/([0-9]+)/?$"]["GET"]=[](ostream& response, Request& request) { + string number=request.path_match[1]; 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. //Default file: index.html //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 path=path_match[1]; + string path=request.path_match[1]; //Remove all but the last '.' (so we can't leave the web-directory) size_t last_pos=path.rfind("."); @@ -117,7 +117,7 @@ int main() { } }; - //Start HTTP-server + //Start HTTPS-server server.start(); return 0; diff --git a/server_http.hpp b/server_http.hpp index 79dcbbb..1264f05 100644 --- a/server_http.hpp +++ b/server_http.hpp @@ -14,10 +14,12 @@ namespace SimpleWeb { std::shared_ptr content; std::unordered_map header; + + std::smatch path_match; }; typedef std::map > > resource_type; + std::function > > resource_type; template class ServerBase { @@ -61,7 +63,6 @@ namespace SimpleWeb { boost::asio::io_service m_io_service; boost::asio::ip::tcp::endpoint endpoint; boost::asio::ip::tcp::acceptor acceptor; - //shared_ptr context; size_t num_threads; std::vector threads; @@ -71,7 +72,7 @@ namespace SimpleWeb { virtual void accept() {} - void process_request_and_respond(std::shared_ptr socket) { + void process_request_and_respond(std::shared_ptr socket) const { //Create new read_buffer for async_read_until() //Shared_ptr is used to pass temporary objects to the asynchronous functions std::shared_ptr 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; std::regex e("^([^ ]*) ([^ ]*) HTTP/([^ ]*)$"); @@ -146,16 +147,18 @@ namespace SimpleWeb { return request; } - void respond(std::shared_ptr socket, std::shared_ptr request) { + void respond(std::shared_ptr socket, std::shared_ptr request) const { //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 write_buffer(new boost::asio::streambuf); 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 boost::asio::async_write(*socket, *write_buffer,