From 8e92c42c29f0c2f6a228c459042eb911c6f511c3 Mon Sep 17 00:00:00 2001 From: antao Date: Wed, 17 Oct 2018 09:40:15 +0800 Subject: [PATCH] Optimize regular expressions --- lib/src/HttpAppFrameworkImpl.cc | 13 +++++++------ lib/src/HttpAppFrameworkImpl.h | 1 + 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/src/HttpAppFrameworkImpl.cc b/lib/src/HttpAppFrameworkImpl.cc index 3215d61e..3538e276 100755 --- a/lib/src/HttpAppFrameworkImpl.cc +++ b/lib/src/HttpAppFrameworkImpl.cc @@ -1,7 +1,7 @@ /** * - * @file - * @author An Tao + * HttpAppFrameworkImpl.cc + * An Tao * @section LICENSE * * Copyright 2018, An Tao. All rights reserved. @@ -112,12 +112,13 @@ void HttpAppFrameworkImpl::initRegex() { std::regex 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(")|"); } if (regString.length() > 0) - regString.resize(regString.length() - 1); //remove last '|' + regString.resize(regString.length() - 1); //remove the last '|' LOG_TRACE << "regex string:" << regString; - _apiRegex = std::regex(regString); + _apiRegex = std::regex(regString,std::regex_constants::icase); } void HttpAppFrameworkImpl::registerWebSocketController(const std::string &pathName, 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++) { - if (result[i].str().empty()) + if (!result[i].matched) continue; if (result[i].str() == req->path() && i <= _apiCtrlVector.size()) { @@ -973,7 +974,7 @@ void HttpAppFrameworkImpl::onAsyncRequest(const HttpRequestPtr &req, const std:: std::vector params(binder.parameterPlaces.size()); 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++) { diff --git a/lib/src/HttpAppFrameworkImpl.h b/lib/src/HttpAppFrameworkImpl.h index c1d39fd0..1d0a627c 100644 --- a/lib/src/HttpAppFrameworkImpl.h +++ b/lib/src/HttpAppFrameworkImpl.h @@ -152,6 +152,7 @@ class HttpAppFrameworkImpl : public HttpAppFramework std::vector filtersName; std::unique_ptr binderMtx = std::unique_ptr(new std::mutex); std::weak_ptr responsePtr; + std::regex _regex; }; //std::unordered_map_apiCtrlMap; std::vector _apiCtrlVector;