Optimize regular expressions

This commit is contained in:
antao 2018-10-17 09:40:15 +08:00
parent a50ba92ee0
commit 8e92c42c29
2 changed files with 8 additions and 6 deletions

View File

@ -1,7 +1,7 @@
/** /**
* *
* @file * HttpAppFrameworkImpl.cc
* @author An Tao * An Tao
* @section LICENSE * @section LICENSE
* *
* Copyright 2018, An Tao. All rights reserved. * Copyright 2018, An Tao. All rights reserved.
@ -112,12 +112,13 @@ void HttpAppFrameworkImpl::initRegex()
{ {
std::regex reg("\\(\\[\\^/\\]\\*\\)"); std::regex reg("\\(\\[\\^/\\]\\*\\)");
std::string tmp = std::regex_replace(binder.pathParameterPattern, reg, "[^/]*"); std::string tmp = std::regex_replace(binder.pathParameterPattern, reg, "[^/]*");
binder._regex=std::regex(binder.pathParameterPattern,std::regex_constants::icase);
regString.append("(").append(tmp).append(")|"); regString.append("(").append(tmp).append(")|");
} }
if (regString.length() > 0) if (regString.length() > 0)
regString.resize(regString.length() - 1); //remove last '|' regString.resize(regString.length() - 1); //remove the last '|'
LOG_TRACE << "regex string:" << regString; LOG_TRACE << "regex string:" << regString;
_apiRegex = std::regex(regString); _apiRegex = std::regex(regString,std::regex_constants::icase);
} }
void HttpAppFrameworkImpl::registerWebSocketController(const std::string &pathName, void HttpAppFrameworkImpl::registerWebSocketController(const std::string &pathName,
const std::string &ctrlName, const std::string &ctrlName,
@ -937,7 +938,7 @@ void HttpAppFrameworkImpl::onAsyncRequest(const HttpRequestPtr &req, const std::
{ {
for (size_t i = 1; i < result.size(); i++) for (size_t i = 1; i < result.size(); i++)
{ {
if (result[i].str().empty()) if (!result[i].matched)
continue; continue;
if (result[i].str() == req->path() && i <= _apiCtrlVector.size()) if (result[i].str() == req->path() && i <= _apiCtrlVector.size())
{ {
@ -973,7 +974,7 @@ void HttpAppFrameworkImpl::onAsyncRequest(const HttpRequestPtr &req, const std::
std::vector<std::string> params(binder.parameterPlaces.size()); std::vector<std::string> params(binder.parameterPlaces.size());
std::smatch r; std::smatch r;
if (std::regex_match(req->path(), r, std::regex(binder.pathParameterPattern))) if (std::regex_match(req->path(), r, binder._regex))
{ {
for (size_t j = 1; j < r.size(); j++) for (size_t j = 1; j < r.size(); j++)
{ {

View File

@ -152,6 +152,7 @@ class HttpAppFrameworkImpl : public HttpAppFramework
std::vector<std::string> filtersName; std::vector<std::string> filtersName;
std::unique_ptr<std::mutex> binderMtx = std::unique_ptr<std::mutex>(new std::mutex); std::unique_ptr<std::mutex> binderMtx = std::unique_ptr<std::mutex>(new std::mutex);
std::weak_ptr<HttpResponse> responsePtr; std::weak_ptr<HttpResponse> responsePtr;
std::regex _regex;
}; };
//std::unordered_map<std::string,ApiBinder>_apiCtrlMap; //std::unordered_map<std::string,ApiBinder>_apiCtrlMap;
std::vector<ApiBinder> _apiCtrlVector; std::vector<ApiBinder> _apiCtrlVector;