diff --git a/http_examples.cpp b/http_examples.cpp index 1767f64..96650ab 100644 --- a/http_examples.cpp +++ b/http_examples.cpp @@ -20,9 +20,9 @@ int main() { //HTTP-server at port 8080 using 4 threads HttpServer server(8080, 4); - //Add resources using path- 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 - server.resource["/string"]["POST"]=[](HttpServer::Response& response, shared_ptr request) { + server.resource["^/string$"]["POST"]=[](HttpServer::Response& response, shared_ptr request) { //Retrieve string from istream (request->content) stringstream ss; request->content >> ss.rdbuf(); @@ -39,7 +39,7 @@ int main() { // "lastName": "Smith", // "age": 25 //} - server.resource["/json"]["POST"]=[](HttpServer::Response& response, shared_ptr request) { + server.resource["^/json$"]["POST"]=[](HttpServer::Response& response, shared_ptr request) { try { ptree pt; read_json(request->content, pt); @@ -55,7 +55,7 @@ int main() { //GET-example for the path /info //Responds with request-information - server.resource["/info"]["GET"]=[](HttpServer::Response& response, shared_ptr request) { + server.resource["^/info$"]["GET"]=[](HttpServer::Response& response, shared_ptr request) { stringstream content_stream; content_stream << "

Request:

"; content_stream << request->method << " " << request->path << " HTTP/" << request->http_version << "
"; @@ -69,10 +69,9 @@ int main() { 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] using regex, responds with the matched string in path (number) - //The 'r' at the beginning of the path-string indicates that the following string is a regular expression + //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.resource["r^/match/([0-9]+)/?$"]["GET"]=[](HttpServer::Response& response, shared_ptr request) { + server.resource["^/match/([0-9]+)/?$"]["GET"]=[](HttpServer::Response& response, shared_ptr request) { string number=request->path_match[1]; response << "HTTP/1.1 200 OK\r\nContent-Length: " << number.length() << "\r\n\r\n" << number; }; diff --git a/https_examples.cpp b/https_examples.cpp index a3a7c7a..f04118a 100644 --- a/https_examples.cpp +++ b/https_examples.cpp @@ -20,9 +20,9 @@ int main() { //HTTPS-server at port 8080 using 4 threads HttpsServer server(8080, 1, "server.crt", "server.key"); - //Add resources using path- 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 - server.resource["/string"]["POST"]=[](HttpsServer::Response& response, shared_ptr request) { + server.resource["^/string$"]["POST"]=[](HttpsServer::Response& response, shared_ptr request) { //Retrieve string from istream (request->content) stringstream ss; request->content >> ss.rdbuf(); @@ -39,7 +39,7 @@ int main() { // "lastName": "Smith", // "age": 25 //} - server.resource["/json"]["POST"]=[](HttpsServer::Response& response, shared_ptr request) { + server.resource["^/json$"]["POST"]=[](HttpsServer::Response& response, shared_ptr request) { try { ptree pt; read_json(request->content, pt); @@ -55,7 +55,7 @@ int main() { //GET-example for the path /info //Responds with request-information - server.resource["/info"]["GET"]=[](HttpsServer::Response& response, shared_ptr request) { + server.resource["^/info$"]["GET"]=[](HttpsServer::Response& response, shared_ptr request) { stringstream content_stream; content_stream << "

Request:

"; content_stream << request->method << " " << request->path << " HTTP/" << request->http_version << "
"; @@ -69,10 +69,9 @@ int main() { 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] using regex, responds with the matched string in path (number) - //The 'r' at the beginning of the path-string indicates that the string is a regular expression + //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.resource["r^/match/([0-9]+)/?$"]["GET"]=[](HttpsServer::Response& response, shared_ptr request) { + server.resource["^/match/([0-9]+)/?$"]["GET"]=[](HttpsServer::Response& response, shared_ptr request) { string number=request->path_match[1]; response << "HTTP/1.1 200 OK\r\nContent-Length: " << number.length() << "\r\n\r\n" << number; }; diff --git a/server_http.hpp b/server_http.hpp index 37fb73d..f6c7d3a 100644 --- a/server_http.hpp +++ b/server_http.hpp @@ -144,12 +144,8 @@ namespace SimpleWeb { void start() { //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++; + regex_resource.emplace_back(std::regex(it->first), std::move(it->second)); + it=resource.erase(it); } accept(); @@ -291,14 +287,6 @@ namespace SimpleWeb { void find_resource(std::shared_ptr socket, std::shared_ptr 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()) {