优化文件上传速度,增加请求头预校验功能
This commit is contained in:
+120
-91
@@ -1,4 +1,4 @@
|
||||
#include "webcc/router.h"
|
||||
#include "webcc/router.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
@@ -7,117 +7,146 @@
|
||||
#include "webcc/logger.h"
|
||||
#include "router.h"
|
||||
|
||||
namespace webcc {
|
||||
namespace webcc
|
||||
{
|
||||
|
||||
bool Router::Route(string_view url, ViewPtr view, const Strings& methods) {
|
||||
assert(view);
|
||||
bool Router::Route(string_view url, ViewPtr view, const Strings& methods)
|
||||
{
|
||||
assert(view);
|
||||
|
||||
// TODO: More error check
|
||||
/*
|
||||
* Repeat Checker by Unknown Object at 2022-02-12
|
||||
*/
|
||||
//------------------RC Start--------------------------
|
||||
int cnt = 0;
|
||||
for (cnt = 0; cnt < routes_.size(); cnt++)
|
||||
if(ToString(routes_[cnt].url) == ToString(url))
|
||||
break;
|
||||
if (cnt != routes_.size()) //repeat found
|
||||
{
|
||||
routes_[cnt].view = view;
|
||||
routes_[cnt].methods = methods;
|
||||
return true;
|
||||
}
|
||||
//-------------------RC End---------------------------
|
||||
// TODO: More error check
|
||||
/*
|
||||
* Repeat Checker by Unknown Object at 2022-02-12
|
||||
*/
|
||||
//------------------RC Start--------------------------
|
||||
int cnt = 0;
|
||||
for (cnt = 0; cnt < routes_.size(); cnt++)
|
||||
if (ToString(routes_[cnt].url) == ToString(url))
|
||||
break;
|
||||
if (cnt != routes_.size()) //repeat found
|
||||
{
|
||||
routes_[cnt].view = view;
|
||||
routes_[cnt].methods = methods;
|
||||
return true;
|
||||
}
|
||||
//-------------------RC End---------------------------
|
||||
|
||||
routes_.push_back({ ToString(url), {}, view, methods });
|
||||
routes_.push_back({ ToString(url), {}, view, methods });
|
||||
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Router::Route(const UrlRegex& regex_url, ViewPtr view,
|
||||
const Strings& methods) {
|
||||
assert(view);
|
||||
bool Router::Route(const UrlRegex& regex_url, ViewPtr view,
|
||||
const Strings& methods)
|
||||
{
|
||||
assert(view);
|
||||
|
||||
// TODO: More error check
|
||||
// TODO: More error check
|
||||
|
||||
try {
|
||||
routes_.push_back({ "", regex_url(), view, methods });
|
||||
try
|
||||
{
|
||||
routes_.push_back({ "", regex_url(), view, methods });
|
||||
|
||||
} catch (const std::regex_error& e) {
|
||||
LOG_ERRO("Not a valid regular expression: %s", e.what());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
catch (const std::regex_error& e)
|
||||
{
|
||||
LOG_ERRO("Not a valid regular expression: %s", e.what());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
ViewPtr Router::FindView(const std::string& method, const std::string& url,
|
||||
UrlArgs* args) {
|
||||
assert(args != nullptr);
|
||||
ViewPtr Router::FindView(const std::string& method, const std::string& url,
|
||||
UrlArgs* args)
|
||||
{
|
||||
assert(args != nullptr);
|
||||
|
||||
for (auto& route : routes_) {
|
||||
if (std::find(route.methods.begin(), route.methods.end(), method) ==
|
||||
route.methods.end()) {
|
||||
continue;
|
||||
}
|
||||
for (auto& route : routes_)
|
||||
{
|
||||
if (std::find(route.methods.begin(), route.methods.end(), method) ==
|
||||
route.methods.end())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (route.url.empty()) {
|
||||
std::smatch match;
|
||||
if (route.url.empty())
|
||||
{
|
||||
std::smatch match;
|
||||
|
||||
if (std::regex_match(url, match, route.url_regex)) {
|
||||
// Any sub-matches?
|
||||
// Start from 1 because match[0] is the whole string itself.
|
||||
for (size_t i = 1; i < match.size(); ++i) {
|
||||
args->push_back(match[i].str());
|
||||
}
|
||||
if (std::regex_match(url, match, route.url_regex))
|
||||
{
|
||||
// Any sub-matches?
|
||||
// Start from 1 because match[0] is the whole string itself.
|
||||
for (size_t i = 1; i < match.size(); ++i)
|
||||
{
|
||||
args->push_back(match[i].str());
|
||||
}
|
||||
|
||||
return route.view;
|
||||
}
|
||||
} else {
|
||||
if (boost::iequals(route.url, url)) {
|
||||
return route.view;
|
||||
}
|
||||
}
|
||||
}
|
||||
return route.view;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (boost::iequals(route.url, url))
|
||||
{
|
||||
return route.view;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ViewPtr();
|
||||
}
|
||||
return ViewPtr();
|
||||
}
|
||||
|
||||
bool Router::MatchView(const std::string& method, const std::string& url,
|
||||
bool* stream) {
|
||||
assert(stream != nullptr);
|
||||
*stream = false;
|
||||
bool Router::MatchView(const std::string& method, const std::string& url,
|
||||
bool* stream, ViewPtr* out_view)
|
||||
{
|
||||
assert(stream != nullptr);
|
||||
*stream = false;
|
||||
|
||||
for (auto& route : routes_) {
|
||||
if (std::find(route.methods.begin(), route.methods.end(), method) ==
|
||||
route.methods.end()) {
|
||||
continue;
|
||||
}
|
||||
for (auto& route : routes_)
|
||||
{
|
||||
if (std::find(route.methods.begin(), route.methods.end(), method) ==
|
||||
route.methods.end())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (route.url.empty()) {
|
||||
std::smatch match;
|
||||
if (route.url.empty())
|
||||
{
|
||||
std::smatch match;
|
||||
|
||||
if (std::regex_match(url, match, route.url_regex)) {
|
||||
*stream = route.view->Stream(method);
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
if (boost::iequals(route.url, url)) {
|
||||
*stream = route.view->Stream(method);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (std::regex_match(url, match, route.url_regex))
|
||||
{
|
||||
*stream = route.view->Stream(method);
|
||||
if (out_view != nullptr)
|
||||
*out_view = route.view; // 【新增】
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (boost::iequals(route.url, url))
|
||||
{
|
||||
*stream = route.view->Stream(method);
|
||||
if (out_view != nullptr)
|
||||
*out_view = route.view; // 【新增】
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t Router::GetViewCount() {
|
||||
return routes_.size();
|
||||
}
|
||||
size_t Router::GetViewCount()
|
||||
{
|
||||
return routes_.size();
|
||||
}
|
||||
|
||||
ViewPtr& Router::AccessView(size_t index) {
|
||||
return routes_.at(index).view;
|
||||
}
|
||||
ViewPtr& Router::AccessView(size_t index)
|
||||
{
|
||||
return routes_.at(index).view;
|
||||
}
|
||||
|
||||
} // namespace webcc
|
||||
|
||||
Reference in New Issue
Block a user