Slightly faster resource lookup.
This commit is contained in:
parent
8818a4c8e0
commit
525eb2983b
3 changed files with 32 additions and 17 deletions
|
|
@ -71,7 +71,7 @@ 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.resource["^/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;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -71,7 +71,7 @@ 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.resource["^/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;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -137,17 +137,31 @@ namespace SimpleWeb {
|
||||||
std::function<void(ServerBase<socket_type>::Response&, std::shared_ptr<ServerBase<socket_type>::Request>)> > default_resource;
|
std::function<void(ServerBase<socket_type>::Response&, std::shared_ptr<ServerBase<socket_type>::Request>)> > default_resource;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<std::pair<std::regex, std::unordered_map<std::string,
|
std::vector<std::pair<std::string, std::vector<std::pair<std::regex,
|
||||||
std::function<void(ServerBase<socket_type>::Response&, std::shared_ptr<ServerBase<socket_type>::Request>)> > > > regex_resource;
|
std::function<void(ServerBase<socket_type>::Response&, std::shared_ptr<ServerBase<socket_type>::Request>)> > > > > opt_resource;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void start() {
|
void start() {
|
||||||
//Move the resources with regular expressions to regex_resource for more efficient request processing
|
//Copy the resources to opt_resource for more efficient request processing
|
||||||
for(auto it=resource.begin();it!=resource.end();) {
|
opt_resource.clear();
|
||||||
regex_resource.emplace_back(std::regex(it->first), std::move(it->second));
|
for(auto& res: resource) {
|
||||||
it=resource.erase(it);
|
for(auto& res_method: res.second) {
|
||||||
|
auto it=opt_resource.end();
|
||||||
|
for(auto opt_it=opt_resource.begin();opt_it!=opt_resource.end();opt_it++) {
|
||||||
|
if(res_method.first==opt_it->first) {
|
||||||
|
it=opt_it;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(it==opt_resource.end()) {
|
||||||
|
opt_resource.emplace_back();
|
||||||
|
it=opt_resource.begin()+(opt_resource.size()-1);
|
||||||
|
it->first=res_method.first;
|
||||||
|
}
|
||||||
|
it->second.emplace_back(std::regex(res.first), res_method.second);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
accept();
|
accept();
|
||||||
|
|
||||||
//If num_threads>1, start m_io_service.run() in (num_threads-1) threads for thread-pooling
|
//If num_threads>1, start m_io_service.run() in (num_threads-1) threads for thread-pooling
|
||||||
|
|
@ -287,14 +301,15 @@ 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
|
||||||
for(auto& res: regex_resource) {
|
for(auto& res: opt_resource) {
|
||||||
auto it_method=res.second.find(request->method);
|
if(request->method==res.first) {
|
||||||
if(it_method!=res.second.end()) {
|
for(auto& res_path: res.second) {
|
||||||
std::smatch sm_res;
|
std::smatch sm_res;
|
||||||
if(std::regex_match(request->path, sm_res, res.first)) {
|
if(std::regex_match(request->path, sm_res, res_path.first)) {
|
||||||
request->path_match=std::move(sm_res);
|
request->path_match=std::move(sm_res);
|
||||||
write_response(socket, request, it_method->second);
|
write_response(socket, request, res_path.second);
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue