All resources now again uses regex for path, since requests/second were no longer much affected by this.

This commit is contained in:
eidheim 2015-02-22 12:06:30 +01:00
commit 7aae65b1b5
3 changed files with 14 additions and 28 deletions

View file

@ -20,9 +20,9 @@ int main() {
//HTTP-server at port 8080 using 4 threads //HTTP-server at port 8080 using 4 threads
HttpServer server(8080, 4); 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 //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"]=[](HttpServer::Response& response, shared_ptr<HttpServer::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();
@ -39,7 +39,7 @@ 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"]=[](HttpServer::Response& response, shared_ptr<HttpServer::Request> request) {
try { try {
ptree pt; ptree pt;
read_json(request->content, pt); read_json(request->content, pt);
@ -55,7 +55,7 @@ int main() {
//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"]=[](HttpServer::Response& response, shared_ptr<HttpServer::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>";
@ -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(); 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) //GET-example for the path /match/[number], 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
//For instance a request GET /match/123 will receive: 123 //For instance a request GET /match/123 will receive: 123
server.resource["r^/match/([0-9]+)/?$"]["GET"]=[](HttpServer::Response& response, shared_ptr<HttpServer::Request> request) { server.resource["^/match/([0-9]+)/?$"]["GET"]=[](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;
}; };

View file

@ -20,9 +20,9 @@ int main() {
//HTTPS-server at port 8080 using 4 threads //HTTPS-server at port 8080 using 4 threads
HttpsServer server(8080, 1, "server.crt", "server.key"); 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 //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"]=[](HttpsServer::Response& response, shared_ptr<HttpsServer::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();
@ -39,7 +39,7 @@ 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"]=[](HttpsServer::Response& response, shared_ptr<HttpsServer::Request> request) {
try { try {
ptree pt; ptree pt;
read_json(request->content, pt); read_json(request->content, pt);
@ -55,7 +55,7 @@ int main() {
//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"]=[](HttpsServer::Response& response, shared_ptr<HttpsServer::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>";
@ -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(); 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) //GET-example for the path /match/[number], 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
//For instance a request GET /match/123 will receive: 123 //For instance a request GET /match/123 will receive: 123
server.resource["r^/match/([0-9]+)/?$"]["GET"]=[](HttpsServer::Response& response, shared_ptr<HttpsServer::Request> request) { server.resource["^/match/([0-9]+)/?$"]["GET"]=[](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;
}; };

View file

@ -144,12 +144,8 @@ namespace SimpleWeb {
void start() { void start() {
//Move the resources with regular expressions to regex_resource for more efficient request processing //Move the resources with regular expressions to regex_resource for more efficient request processing
for(auto it=resource.begin();it!=resource.end();) { 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), std::move(it->second));
regex_resource.emplace_back(std::regex(it->first.substr(1)), std::move(it->second)); it=resource.erase(it);
it=resource.erase(it);
}
else
it++;
} }
accept(); accept();
@ -291,14 +287,6 @@ namespace SimpleWeb {
void find_resource(std::shared_ptr<socket_type> socket, std::shared_ptr<Request> request) { void find_resource(std::shared_ptr<socket_type> socket, std::shared_ptr<Request> request) {
//Find path- and method-match, and call write_response //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) { for(auto& res: regex_resource) {
auto it_method=res.second.find(request->method); auto it_method=res.second.find(request->method);
if(it_method!=res.second.end()) { if(it_method!=res.second.end()) {