mirror of
https://gitee.com/an-tao/drogon.git
synced 2024-11-30 02:37:57 +08:00
Merge pull request #6 from an-tao/http_method
Support for directly using a set of HttpMethod as a constraint for controller path configuration
This commit is contained in:
commit
6186ba5132
@ -218,7 +218,7 @@ void create_controller::newSimpleControllerHeaderFile(std::ofstream &file, const
|
||||
|
||||
file << indent << " PATH_LIST_BEGIN\n";
|
||||
file << indent << " //list path definitions here;\n";
|
||||
file << indent << " //PATH_ADD(\"/path\",\"filter1\",\"filter2\",...);\n";
|
||||
file << indent << " //PATH_ADD(\"/path\",\"filter1\",\"filter2\",HttpMethod1,HttpMethod2...);\n";
|
||||
file << indent << " PATH_LIST_END\n";
|
||||
file << indent << "};\n";
|
||||
if (indent == "")
|
||||
@ -367,7 +367,7 @@ void create_controller::newApiControllerHeaderFile(std::ofstream &file, const st
|
||||
indent.append(" ");
|
||||
file << indent << "METHOD_LIST_BEGIN\n";
|
||||
file << indent << "//use METHOD_ADD to add your custom processing function here;\n";
|
||||
file << indent << "//METHOD_ADD(" << class_name << "::get,\"/get/{2}/{1}\",\"drogon::GetFilter\");"
|
||||
file << indent << "//METHOD_ADD(" << class_name << "::get,\"/get/{2}/{1}\",Get);"
|
||||
"//path will be "
|
||||
<< namepace_path << class_name << "/get/{arg2}/{arg1}\n";
|
||||
file << indent << "//METHOD_ADD(" << class_name << "::your_method_name,\"/{1}/{2}/list\",\"drogon::GetFilter\");"
|
||||
|
@ -7,7 +7,7 @@ int main()
|
||||
|
||||
auto client = HttpClient::newHttpClient("http://www.baidu.com");
|
||||
auto req = HttpRequest::newHttpRequest();
|
||||
req->setMethod(drogon::HttpRequest::kGet);
|
||||
req->setMethod(drogon::Get);
|
||||
req->setPath("/s");
|
||||
req->setParameter("wd", "weixin");
|
||||
int count = 0;
|
||||
|
@ -8,6 +8,6 @@ class ListParaCtl : public drogon::HttpSimpleController<ListParaCtl>
|
||||
PATH_LIST_BEGIN
|
||||
//list path definations here;
|
||||
//PATH_ADD("/path","filter1","filter2",...);
|
||||
PATH_ADD("/listpara");
|
||||
PATH_ADD("/listpara",Get);
|
||||
PATH_LIST_END
|
||||
};
|
||||
|
@ -5,15 +5,15 @@ namespace example
|
||||
{
|
||||
class TestController : public drogon::HttpSimpleController<TestController>
|
||||
{
|
||||
public:
|
||||
virtual void asyncHandleHttpRequest(const HttpRequestPtr &req, const std::function<void(const HttpResponsePtr &)> &callback) override;
|
||||
PATH_LIST_BEGIN
|
||||
//list path definations here;
|
||||
//PATH_ADD("/path","filter1","filter2",...);
|
||||
PATH_ADD("/");
|
||||
PATH_ADD("/Test", "nonFilter");
|
||||
PATH_ADD("/tpost", "drogon::PostFilter");
|
||||
PATH_ADD("/slow", "TimeFilter");
|
||||
PATH_LIST_END
|
||||
public:
|
||||
virtual void asyncHandleHttpRequest(const HttpRequestPtr &req, const std::function<void(const HttpResponsePtr &)> &callback) override;
|
||||
PATH_LIST_BEGIN
|
||||
//list path definations here;
|
||||
//PATH_ADD("/path","filter1","filter2",...);
|
||||
PATH_ADD("/");
|
||||
PATH_ADD("/Test", "nonFilter");
|
||||
PATH_ADD("/tpost", "drogon::PostFilter");
|
||||
PATH_ADD("/slow", "TimeFilter", Get);
|
||||
PATH_LIST_END
|
||||
};
|
||||
} // namespace example
|
||||
|
@ -5,17 +5,16 @@ namespace api
|
||||
{
|
||||
class Attachment : public drogon::HttpApiController<Attachment>
|
||||
{
|
||||
public:
|
||||
METHOD_LIST_BEGIN
|
||||
//use METHOD_ADD to add your custom processing function here;
|
||||
METHOD_ADD(Attachment::get, "/", "drogon::GetFilter");
|
||||
METHOD_ADD(Attachment::upload, "/upload", "drogon::PostFilter");
|
||||
|
||||
METHOD_LIST_END
|
||||
//your declaration of processing function maybe like this:
|
||||
void get(const HttpRequestPtr &req,
|
||||
const std::function<void(const HttpResponsePtr &)> &callback);
|
||||
void upload(const HttpRequestPtr &req,
|
||||
const std::function<void(const HttpResponsePtr &)> &callback);
|
||||
public:
|
||||
METHOD_LIST_BEGIN
|
||||
//use METHOD_ADD to add your custom processing function here;
|
||||
METHOD_ADD(Attachment::get, "/", Post);
|
||||
|
||||
METHOD_LIST_END
|
||||
//your declaration of processing function maybe like this:
|
||||
void get(const HttpRequestPtr &req,
|
||||
const std::function<void(const HttpResponsePtr &)> &callback);
|
||||
void upload(const HttpRequestPtr &req,
|
||||
const std::function<void(const HttpResponsePtr &)> &callback);
|
||||
};
|
||||
} // namespace api
|
||||
|
@ -7,19 +7,19 @@ namespace v1
|
||||
{
|
||||
class ApiTest : public drogon::HttpApiController<ApiTest>
|
||||
{
|
||||
public:
|
||||
METHOD_LIST_BEGIN
|
||||
//use METHOD_ADD to add your custom processing function here;
|
||||
METHOD_ADD(ApiTest::get, "/get/{2}/{1}", "drogon::GetFilter"); //path will be /api/v1/apitest/get/{arg2}/{arg1}
|
||||
METHOD_ADD(ApiTest::your_method_name, "/{1}/List?P2={2}", "drogon::GetFilter"); //path will be /api/v1/apitest/{arg1}/list
|
||||
METHOD_ADD(ApiTest::staticApi, "/static");
|
||||
METHOD_ADD(ApiTest::get2, "/get/{1}", "drogon::GetFilter");
|
||||
METHOD_LIST_END
|
||||
//your declaration of processing function maybe like this:
|
||||
void get(const HttpRequestPtr &req, const std::function<void(const HttpResponsePtr &)> &callback, int p1, std::string &&p2);
|
||||
void your_method_name(const HttpRequestPtr &req, const std::function<void(const HttpResponsePtr &)> &callback, double p1, int p2) const;
|
||||
void staticApi(const HttpRequestPtr &req, const std::function<void(const HttpResponsePtr &)> &callback);
|
||||
void get2(const HttpRequestPtr &req, const std::function<void(const HttpResponsePtr &)> &callback, std::string &&p1);
|
||||
public:
|
||||
METHOD_LIST_BEGIN
|
||||
//use METHOD_ADD to add your custom processing function here;
|
||||
METHOD_ADD(ApiTest::get, "/get/{2}/{1}", "drogon::GetFilter"); //path will be /api/v1/apitest/get/{arg2}/{arg1}
|
||||
METHOD_ADD(ApiTest::your_method_name, "/{1}/List?P2={2}", Get); //path will be /api/v1/apitest/{arg1}/list
|
||||
METHOD_ADD(ApiTest::staticApi, "/static", Get, Post);
|
||||
METHOD_ADD(ApiTest::get2, "/get/{1}", "drogon::GetFilter");
|
||||
METHOD_LIST_END
|
||||
//your declaration of processing function maybe like this:
|
||||
void get(const HttpRequestPtr &req, const std::function<void(const HttpResponsePtr &)> &callback, int p1, std::string &&p2);
|
||||
void your_method_name(const HttpRequestPtr &req, const std::function<void(const HttpResponsePtr &)> &callback, double p1, int p2) const;
|
||||
void staticApi(const HttpRequestPtr &req, const std::function<void(const HttpResponsePtr &)> &callback);
|
||||
void get2(const HttpRequestPtr &req, const std::function<void(const HttpResponsePtr &)> &callback, std::string &&p1);
|
||||
};
|
||||
} // namespace v1
|
||||
} // namespace api
|
||||
|
@ -24,10 +24,10 @@
|
||||
#define METHOD_LIST_BEGIN \
|
||||
static void initMethods() \
|
||||
{
|
||||
#define METHOD_ADD(method, pattern, filters...) \
|
||||
{ \
|
||||
std::string methodName = ""; \
|
||||
registerMethod(methodName, pattern, &method, {filters}); \
|
||||
|
||||
#define METHOD_ADD(method, pattern, filters...) \
|
||||
{ \
|
||||
registerMethod(&method, pattern, {filters}); \
|
||||
}
|
||||
|
||||
#define METHOD_LIST_END \
|
||||
@ -43,15 +43,13 @@ class HttpApiController : public DrObject<T>
|
||||
{
|
||||
protected:
|
||||
template <typename FUNCTION>
|
||||
static void registerMethod(const std::string &methodName, const std::string &pattern, FUNCTION &&function, const std::vector<std::string> &filters)
|
||||
static void registerMethod(FUNCTION &&function,
|
||||
const std::string &pattern,
|
||||
const std::vector<any> &filtersAndMethods = std::vector<any>())
|
||||
{
|
||||
std::string path = std::string("/") + HttpApiController<T>::classTypeName();
|
||||
LOG_TRACE << "classname:" << HttpApiController<T>::classTypeName();
|
||||
if (!methodName.empty())
|
||||
{
|
||||
path.append("/");
|
||||
path.append(methodName);
|
||||
}
|
||||
|
||||
//transform(path.begin(), path.end(), path.begin(), tolower);
|
||||
std::string::size_type pos;
|
||||
while ((pos = path.find("::")) != std::string::npos)
|
||||
@ -61,11 +59,11 @@ class HttpApiController : public DrObject<T>
|
||||
if (pattern[0] == '/')
|
||||
HttpAppFramework::registerHttpApiMethod(path + pattern,
|
||||
std::forward<FUNCTION>(function),
|
||||
filters);
|
||||
filtersAndMethods);
|
||||
else
|
||||
HttpAppFramework::registerHttpApiMethod(path + "/" + pattern,
|
||||
std::forward<FUNCTION>(function),
|
||||
filters);
|
||||
filtersAndMethods);
|
||||
}
|
||||
|
||||
private:
|
||||
|
@ -74,11 +74,11 @@ class HttpAppFramework : public trantor::NonCopyable
|
||||
std::vector<std::string>()) = 0;
|
||||
virtual void registerHttpSimpleController(const std::string &pathName,
|
||||
const std::string &crtlName,
|
||||
const std::vector<std::string> &filters = std::vector<std::string>()) = 0;
|
||||
const std::vector<any> &filtersAndMethods = std::vector<any>()) = 0;
|
||||
template <typename FUNCTION>
|
||||
static void registerHttpApiMethod(const std::string &pathPattern,
|
||||
FUNCTION &&function,
|
||||
const std::vector<std::string> &filters = std::vector<std::string>())
|
||||
const std::vector<any> &filtersAndMethods = std::vector<any>())
|
||||
{
|
||||
LOG_TRACE << "pathPattern:" << pathPattern;
|
||||
HttpApiBinderBasePtr binder;
|
||||
@ -86,7 +86,31 @@ class HttpAppFramework : public trantor::NonCopyable
|
||||
binder = std::make_shared<
|
||||
HttpApiBinder<FUNCTION>>(std::forward<FUNCTION>(function));
|
||||
|
||||
instance().registerHttpApiController(pathPattern, binder, filters);
|
||||
std::vector<HttpMethod> validMethods;
|
||||
std::vector<std::string> filters;
|
||||
for (auto &filterOrMethod : filtersAndMethods)
|
||||
{
|
||||
if (filterOrMethod.type() == typeid(std::string))
|
||||
{
|
||||
filters.push_back(*any_cast<std::string>(&filterOrMethod));
|
||||
}
|
||||
else if (filterOrMethod.type() == typeid(const char *))
|
||||
{
|
||||
filters.push_back(*any_cast<const char *>(&filterOrMethod));
|
||||
}
|
||||
else if (filterOrMethod.type() == typeid(HttpMethod))
|
||||
{
|
||||
validMethods.push_back(*any_cast<HttpMethod>(&filterOrMethod));
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cerr << "Invalid controller constraint type:" << filterOrMethod.type().name() << std::endl;
|
||||
LOG_ERROR << "Invalid controller constraint type";
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
instance().registerHttpApiController(pathPattern, binder, validMethods, filters);
|
||||
}
|
||||
virtual void enableSession(const size_t timeout = 0) = 0;
|
||||
virtual void disableSession() = 0;
|
||||
@ -115,6 +139,7 @@ class HttpAppFramework : public trantor::NonCopyable
|
||||
private:
|
||||
virtual void registerHttpApiController(const std::string &pathPattern,
|
||||
const HttpApiBinderBasePtr &binder,
|
||||
const std::vector<HttpMethod> &validMethods = std::vector<HttpMethod>(),
|
||||
const std::vector<std::string> &filters = std::vector<std::string>()) = 0;
|
||||
};
|
||||
} // namespace drogon
|
||||
|
@ -25,6 +25,15 @@ namespace drogon
|
||||
{
|
||||
class HttpRequest;
|
||||
typedef std::shared_ptr<HttpRequest> HttpRequestPtr;
|
||||
enum HttpMethod
|
||||
{
|
||||
Get = 0,
|
||||
Post,
|
||||
Head,
|
||||
Put,
|
||||
Delete,
|
||||
Invalid
|
||||
};
|
||||
/*
|
||||
* abstract class for webapp developer to get Http client request;
|
||||
* */
|
||||
@ -37,17 +46,9 @@ class HttpRequest
|
||||
kHttp10 = 1,
|
||||
kHttp11 = 2
|
||||
};
|
||||
enum Method
|
||||
{
|
||||
kInvalid,
|
||||
kGet,
|
||||
kPost,
|
||||
kHead,
|
||||
kPut,
|
||||
kDelete
|
||||
};
|
||||
|
||||
virtual const char *methodString() const = 0;
|
||||
virtual Method method() const = 0;
|
||||
virtual HttpMethod method() const = 0;
|
||||
virtual std::string getHeader(const std::string &field) const = 0;
|
||||
virtual std::string getCookie(const std::string &field) const = 0;
|
||||
virtual const std::map<std::string, std::string> &headers() const = 0;
|
||||
@ -63,7 +64,7 @@ class HttpRequest
|
||||
virtual ~HttpRequest() {}
|
||||
virtual const std::shared_ptr<Json::Value> getJsonObject() const = 0;
|
||||
|
||||
virtual void setMethod(const Method method) = 0;
|
||||
virtual void setMethod(const HttpMethod method) = 0;
|
||||
virtual void setPath(const std::string &path) = 0;
|
||||
virtual void setParameter(const std::string &key, const std::string &value) = 0;
|
||||
static HttpRequestPtr newHttpRequest();
|
||||
|
@ -20,16 +20,13 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#define PATH_LIST_BEGIN \
|
||||
static std::vector<std::pair<std::string, std::vector<std::string>>> paths() \
|
||||
{ \
|
||||
std::vector<std::pair<std::string, std::vector<std::string>>> vet;
|
||||
#define PATH_LIST_BEGIN \
|
||||
static void ___paths___() \
|
||||
{
|
||||
|
||||
#define PATH_ADD(path, filters...) \
|
||||
vet.push_back({path, {filters}})
|
||||
#define PATH_ADD(path, filters...) __registerSelf(path, {filters});
|
||||
|
||||
#define PATH_LIST_END \
|
||||
return vet; \
|
||||
}
|
||||
namespace drogon
|
||||
{
|
||||
@ -48,6 +45,11 @@ class HttpSimpleController : public DrObject<T>, public HttpSimpleControllerBase
|
||||
|
||||
protected:
|
||||
HttpSimpleController() {}
|
||||
static void __registerSelf(const std::string &path, const std::vector<any> &filtersAndMethods)
|
||||
{
|
||||
LOG_TRACE << "register simple controller(" << HttpSimpleController<T>::classTypeName() << ") on path:" << path;
|
||||
HttpAppFramework::instance().registerHttpSimpleController(path, HttpSimpleController<T>::classTypeName(), filtersAndMethods);
|
||||
}
|
||||
|
||||
private:
|
||||
class pathRegister
|
||||
@ -55,13 +57,7 @@ class HttpSimpleController : public DrObject<T>, public HttpSimpleControllerBase
|
||||
public:
|
||||
pathRegister()
|
||||
{
|
||||
auto vPaths = T::paths();
|
||||
|
||||
for (auto path : vPaths)
|
||||
{
|
||||
LOG_TRACE << "register simple controller(" << HttpSimpleController<T>::classTypeName() << ") on path:" << path.first;
|
||||
HttpAppFramework::instance().registerHttpSimpleController(path.first, HttpSimpleController<T>::classTypeName(), path.second);
|
||||
}
|
||||
T::___paths___();
|
||||
}
|
||||
|
||||
protected:
|
||||
|
@ -19,7 +19,7 @@ void DeleteFilter::doFilter(const HttpRequestPtr &req,
|
||||
const FilterCallback &fcb,
|
||||
const FilterChainCallback &fccb)
|
||||
{
|
||||
if (req->method() == HttpRequest::kDelete)
|
||||
if (req->method() == Delete)
|
||||
{
|
||||
fccb();
|
||||
return;
|
||||
@ -27,4 +27,4 @@ void DeleteFilter::doFilter(const HttpRequestPtr &req,
|
||||
auto res = drogon::HttpResponse::newHttpResponse();
|
||||
res->setStatusCode(HttpResponse::k405MethodNotAllowed);
|
||||
fcb(res);
|
||||
}
|
||||
}
|
||||
|
@ -21,7 +21,7 @@ const std::map<std::string, std::string> &FileUpload::getPremeter() const
|
||||
};
|
||||
int FileUpload::parse(const HttpRequestPtr &req)
|
||||
{
|
||||
if (req->method() != HttpRequest::kPost)
|
||||
if (req->method() != Post)
|
||||
return -1;
|
||||
std::string contentType = req->getHeader("Content-Type");
|
||||
if (contentType.empty())
|
||||
|
@ -19,7 +19,7 @@ void GetFilter::doFilter(const HttpRequestPtr &req,
|
||||
const FilterCallback &fcb,
|
||||
const FilterChainCallback &fccb)
|
||||
{
|
||||
if (req->method() == HttpRequest::kGet)
|
||||
if (req->method() == Get)
|
||||
{
|
||||
fccb();
|
||||
return;
|
||||
@ -27,4 +27,4 @@ void GetFilter::doFilter(const HttpRequestPtr &req,
|
||||
auto res = drogon::HttpResponse::newHttpResponse();
|
||||
res->setStatusCode(HttpResponse::k405MethodNotAllowed);
|
||||
fcb(res);
|
||||
}
|
||||
}
|
||||
|
@ -138,7 +138,7 @@ void HttpAppFrameworkImpl::registerWebSocketController(const std::string &pathNa
|
||||
}
|
||||
void HttpAppFrameworkImpl::registerHttpSimpleController(const std::string &pathName,
|
||||
const std::string &ctrlName,
|
||||
const std::vector<std::string> &filters)
|
||||
const std::vector<any> &filtersAndMethods)
|
||||
{
|
||||
assert(!pathName.empty());
|
||||
assert(!ctrlName.empty());
|
||||
@ -146,11 +146,43 @@ void HttpAppFrameworkImpl::registerHttpSimpleController(const std::string &pathN
|
||||
std::string path(pathName);
|
||||
std::transform(pathName.begin(), pathName.end(), path.begin(), tolower);
|
||||
std::lock_guard<std::mutex> guard(_simpCtrlMutex);
|
||||
std::vector<HttpMethod> validMethods;
|
||||
std::vector<std::string> filters;
|
||||
for (auto &filterOrMethod : filtersAndMethods)
|
||||
{
|
||||
if (filterOrMethod.type() == typeid(std::string))
|
||||
{
|
||||
filters.push_back(*any_cast<std::string>(&filterOrMethod));
|
||||
}
|
||||
else if (filterOrMethod.type() == typeid(const char *))
|
||||
{
|
||||
filters.push_back(*any_cast<const char *>(&filterOrMethod));
|
||||
}
|
||||
else if (filterOrMethod.type() == typeid(HttpMethod))
|
||||
{
|
||||
validMethods.push_back(*any_cast<HttpMethod>(&filterOrMethod));
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cerr << "Invalid controller constraint type:" << filterOrMethod.type().name() << std::endl;
|
||||
LOG_ERROR << "Invalid controller constraint type";
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
_simpCtrlMap[path].controllerName = ctrlName;
|
||||
_simpCtrlMap[path].filtersName = filters;
|
||||
if (validMethods.size() > 0)
|
||||
{
|
||||
_simpCtrlMap[path]._validMethodsFlags.resize(Invalid, 0);
|
||||
for (auto method : validMethods)
|
||||
{
|
||||
_simpCtrlMap[path]._validMethodsFlags[method] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
void HttpAppFrameworkImpl::addApiPath(const std::string &path,
|
||||
const HttpApiBinderBasePtr &binder,
|
||||
const std::vector<HttpMethod> &validMethods,
|
||||
const std::vector<std::string> &filters)
|
||||
{
|
||||
//path will be like /api/v1/service/method/{1}/{2}/xxx...
|
||||
@ -207,11 +239,22 @@ void HttpAppFrameworkImpl::addApiPath(const std::string &path,
|
||||
_binder.binderPtr = binder;
|
||||
_binder.filtersName = filters;
|
||||
_binder.pathParameterPattern = std::regex_replace(originPath, regex, "([^/]*)");
|
||||
std::lock_guard<std::mutex> guard(_apiCtrlMutex);
|
||||
_apiCtrlVector.push_back(std::move(_binder));
|
||||
if (validMethods.size() > 0)
|
||||
{
|
||||
_binder._validMethodsFlags.resize(Invalid, 0);
|
||||
for (auto method : validMethods)
|
||||
{
|
||||
_binder._validMethodsFlags[method] = 1;
|
||||
}
|
||||
}
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(_apiCtrlMutex);
|
||||
_apiCtrlVector.push_back(std::move(_binder));
|
||||
}
|
||||
}
|
||||
void HttpAppFrameworkImpl::registerHttpApiController(const std::string &pathPattern,
|
||||
const HttpApiBinderBasePtr &binder,
|
||||
const std::vector<HttpMethod> &validMethods,
|
||||
const std::vector<std::string> &filters)
|
||||
{
|
||||
assert(!pathPattern.empty());
|
||||
@ -219,7 +262,7 @@ void HttpAppFrameworkImpl::registerHttpApiController(const std::string &pathPatt
|
||||
std::string path(pathPattern);
|
||||
|
||||
//std::transform(path.begin(), path.end(), path.begin(), tolower);
|
||||
addApiPath(path, binder, filters);
|
||||
addApiPath(path, binder, validMethods, filters);
|
||||
}
|
||||
void HttpAppFrameworkImpl::setThreadNum(size_t threadNum)
|
||||
{
|
||||
@ -856,7 +899,20 @@ void HttpAppFrameworkImpl::onAsyncRequest(const HttpRequestPtr &req, const std::
|
||||
|
||||
if (_simpCtrlMap.find(pathLower) != _simpCtrlMap.end())
|
||||
{
|
||||
auto &filters = _simpCtrlMap[pathLower].filtersName;
|
||||
auto &ctrlInfo = _simpCtrlMap[pathLower];
|
||||
if (ctrlInfo._validMethodsFlags.size() > 0)
|
||||
{
|
||||
assert(ctrlInfo._validMethodsFlags.size() > req->method());
|
||||
if (ctrlInfo._validMethodsFlags[req->method()] == 0)
|
||||
{
|
||||
//Invalid Http Method
|
||||
auto res = drogon::HttpResponse::newHttpResponse();
|
||||
res->setStatusCode(HttpResponse::k405MethodNotAllowed);
|
||||
callback(res);
|
||||
return;
|
||||
}
|
||||
}
|
||||
auto &filters = ctrlInfo.filtersName;
|
||||
doFilters(filters, req, callback, needSetJsessionid, session_id, [=]() {
|
||||
auto &ctrlItem = _simpCtrlMap[pathLower];
|
||||
const std::string &ctrlName = ctrlItem.controllerName;
|
||||
@ -945,6 +1001,18 @@ void HttpAppFrameworkImpl::onAsyncRequest(const HttpRequestPtr &req, const std::
|
||||
size_t ctlIndex = i - 1;
|
||||
auto &binder = _apiCtrlVector[ctlIndex];
|
||||
LOG_TRACE << "got api access,regex=" << binder.pathParameterPattern;
|
||||
if (binder._validMethodsFlags.size() > 0)
|
||||
{
|
||||
assert(binder._validMethodsFlags.size() > req->method());
|
||||
if (binder._validMethodsFlags[req->method()] == 0)
|
||||
{
|
||||
//Invalid Http Method
|
||||
auto res = drogon::HttpResponse::newHttpResponse();
|
||||
res->setStatusCode(HttpResponse::k405MethodNotAllowed);
|
||||
callback(res);
|
||||
return;
|
||||
}
|
||||
}
|
||||
auto &filters = binder.filtersName;
|
||||
doFilters(filters, req, callback, needSetJsessionid, session_id, [=]() {
|
||||
auto &binder = _apiCtrlVector[ctlIndex];
|
||||
|
@ -51,8 +51,8 @@ class HttpAppFrameworkImpl : public HttpAppFramework
|
||||
std::vector<std::string>()) override;
|
||||
virtual void registerHttpSimpleController(const std::string &pathName,
|
||||
const std::string &crtlName,
|
||||
const std::vector<std::string> &filters =
|
||||
std::vector<std::string>()) override;
|
||||
const std::vector<any> &filtersAndMethods =
|
||||
std::vector<any>()) override;
|
||||
virtual void enableSession(const size_t timeout = 0) override
|
||||
{
|
||||
_useSession = true;
|
||||
@ -88,6 +88,7 @@ class HttpAppFrameworkImpl : public HttpAppFramework
|
||||
private:
|
||||
virtual void registerHttpApiController(const std::string &pathPattern,
|
||||
const HttpApiBinderBasePtr &binder,
|
||||
const std::vector<HttpMethod> &validMethods = std::vector<HttpMethod>(),
|
||||
const std::vector<std::string> &filters = std::vector<std::string>()) override;
|
||||
|
||||
std::vector<std::tuple<std::string, uint16_t, bool, std::string, std::string>> _listeners;
|
||||
@ -101,6 +102,7 @@ class HttpAppFrameworkImpl : public HttpAppFramework
|
||||
void readSendFile(const std::string &filePath, const HttpRequestPtr &req, const HttpResponsePtr &resp);
|
||||
void addApiPath(const std::string &path,
|
||||
const HttpApiBinderBasePtr &binder,
|
||||
const std::vector<HttpMethod> &validMethods,
|
||||
const std::vector<std::string> &filters);
|
||||
void initRegex();
|
||||
//if uuid package found,we can use a uuid string as session id;
|
||||
@ -129,6 +131,7 @@ class HttpAppFrameworkImpl : public HttpAppFramework
|
||||
{
|
||||
std::string controllerName;
|
||||
std::vector<std::string> filtersName;
|
||||
std::vector<int> _validMethodsFlags;
|
||||
std::shared_ptr<HttpSimpleControllerBase> controller;
|
||||
std::weak_ptr<HttpResponse> responsePtr;
|
||||
std::mutex _mutex;
|
||||
@ -152,6 +155,7 @@ class HttpAppFrameworkImpl : public HttpAppFramework
|
||||
std::vector<std::string> filtersName;
|
||||
std::unique_ptr<std::mutex> binderMtx = std::unique_ptr<std::mutex>(new std::mutex);
|
||||
std::weak_ptr<HttpResponse> responsePtr;
|
||||
std::vector<int> _validMethodsFlags;
|
||||
std::regex _regex;
|
||||
};
|
||||
//std::unordered_map<std::string,ApiBinder>_apiCtrlMap;
|
||||
|
@ -34,7 +34,7 @@ void HttpRequestImpl::parsePremeter()
|
||||
|
||||
std::transform(type.begin(), type.end(), type.begin(), tolower);
|
||||
const std::string &input = query();
|
||||
if (_method == kGet || (_method == kPost && (type == "" || type.find("application/x-www-form-urlencoded") != std::string::npos)))
|
||||
if (_method == Get || (_method == Post && (type == "" || type.find("application/x-www-form-urlencoded") != std::string::npos)))
|
||||
{
|
||||
|
||||
std::string::size_type pos = 0;
|
||||
@ -104,21 +104,21 @@ void HttpRequestImpl::appendToBuffer(MsgBuffer *output) const
|
||||
{
|
||||
switch (_method)
|
||||
{
|
||||
case kDelete:
|
||||
output->append("DELETE ");
|
||||
break;
|
||||
case kGet:
|
||||
case Get:
|
||||
output->append("GET ");
|
||||
break;
|
||||
case kHead:
|
||||
output->append("HEAD ");
|
||||
break;
|
||||
case kPost:
|
||||
case Post:
|
||||
output->append("POST ");
|
||||
break;
|
||||
case kPut:
|
||||
case Head:
|
||||
output->append("HEAD ");
|
||||
break;
|
||||
case Put:
|
||||
output->append("PUT ");
|
||||
break;
|
||||
case Delete:
|
||||
output->append("DELETE ");
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
@ -192,7 +192,7 @@ void HttpRequestImpl::appendToBuffer(MsgBuffer *output) const
|
||||
HttpRequestPtr HttpRequest::newHttpRequest()
|
||||
{
|
||||
auto req = std::make_shared<HttpRequestImpl>();
|
||||
req->setMethod(drogon::HttpRequest::kGet);
|
||||
req->setMethod(drogon::Get);
|
||||
req->setVersion(drogon::HttpRequest::kHttp11);
|
||||
return req;
|
||||
}
|
||||
|
@ -47,7 +47,7 @@ class HttpRequestImpl : public HttpRequest
|
||||
friend class HttpContext;
|
||||
|
||||
HttpRequestImpl()
|
||||
: _method(kInvalid),
|
||||
: _method(Invalid),
|
||||
_version(kUnknown),
|
||||
contentLen(0)
|
||||
{
|
||||
@ -66,33 +66,33 @@ class HttpRequestImpl : public HttpRequest
|
||||
bool setMethod(const char *start, const char *end)
|
||||
{
|
||||
|
||||
assert(_method == kInvalid);
|
||||
assert(_method == Invalid);
|
||||
std::string m(start, end);
|
||||
if (m == "GET")
|
||||
{
|
||||
_method = kGet;
|
||||
_method = Get;
|
||||
}
|
||||
else if (m == "POST")
|
||||
{
|
||||
_method = kPost;
|
||||
_method = Post;
|
||||
}
|
||||
else if (m == "HEAD")
|
||||
{
|
||||
_method = kHead;
|
||||
_method = Head;
|
||||
}
|
||||
else if (m == "PUT")
|
||||
{
|
||||
_method = kPut;
|
||||
_method = Put;
|
||||
}
|
||||
else if (m == "DELETE")
|
||||
{
|
||||
_method = kDelete;
|
||||
_method = Delete;
|
||||
}
|
||||
else
|
||||
{
|
||||
_method = kInvalid;
|
||||
_method = Invalid;
|
||||
}
|
||||
if (_method != kInvalid)
|
||||
if (_method != Invalid)
|
||||
{
|
||||
content_ = "";
|
||||
_query = "";
|
||||
@ -100,10 +100,10 @@ class HttpRequestImpl : public HttpRequest
|
||||
_parameters.clear();
|
||||
_headers.clear();
|
||||
}
|
||||
return _method != kInvalid;
|
||||
return _method != Invalid;
|
||||
}
|
||||
|
||||
virtual void setMethod(const Method method) override
|
||||
virtual void setMethod(const HttpMethod method) override
|
||||
{
|
||||
_method = method;
|
||||
content_ = "";
|
||||
@ -114,7 +114,7 @@ class HttpRequestImpl : public HttpRequest
|
||||
return;
|
||||
}
|
||||
|
||||
Method method() const override
|
||||
HttpMethod method() const override
|
||||
{
|
||||
return _method;
|
||||
}
|
||||
@ -124,19 +124,19 @@ class HttpRequestImpl : public HttpRequest
|
||||
const char *result = "UNKNOWN";
|
||||
switch (_method)
|
||||
{
|
||||
case kGet:
|
||||
case Get:
|
||||
result = "GET";
|
||||
break;
|
||||
case kPost:
|
||||
case Post:
|
||||
result = "POST";
|
||||
break;
|
||||
case kHead:
|
||||
case Head:
|
||||
result = "HEAD";
|
||||
break;
|
||||
case kPut:
|
||||
case Put:
|
||||
result = "PUT";
|
||||
break;
|
||||
case kDelete:
|
||||
case Delete:
|
||||
result = "DELETE";
|
||||
break;
|
||||
default:
|
||||
@ -179,7 +179,7 @@ class HttpRequestImpl : public HttpRequest
|
||||
{
|
||||
if (_query != "")
|
||||
return _query;
|
||||
if (_method == kPost)
|
||||
if (_method == Post)
|
||||
return content_;
|
||||
return _query;
|
||||
}
|
||||
@ -360,7 +360,7 @@ class HttpRequestImpl : public HttpRequest
|
||||
}
|
||||
|
||||
private:
|
||||
Method _method = kGet;
|
||||
HttpMethod _method = Get;
|
||||
Version _version = kHttp11;
|
||||
std::string _path;
|
||||
std::string _query;
|
||||
|
@ -162,10 +162,10 @@ void HttpServer::onRequest(const TcpConnectionPtr &conn, const HttpRequestPtr &r
|
||||
bool _close = connection == "close" ||
|
||||
(req->getVersion() == HttpRequestImpl::kHttp10 && connection != "Keep-Alive");
|
||||
|
||||
bool _isHeadMethod = (req->method() == HttpRequest::kHead);
|
||||
bool _isHeadMethod = (req->method() == Head);
|
||||
if (_isHeadMethod)
|
||||
{
|
||||
req->setMethod(HttpRequest::kGet);
|
||||
req->setMethod(Get);
|
||||
}
|
||||
HttpContext *context = any_cast<HttpContext>(conn->getMutableContext());
|
||||
//request will be received in same thread,so we don't need mutex;
|
||||
|
@ -19,7 +19,7 @@ void PostFilter::doFilter(const HttpRequestPtr &req,
|
||||
const FilterCallback &fcb,
|
||||
const FilterChainCallback &fccb)
|
||||
{
|
||||
if (req->method() == HttpRequest::kPost)
|
||||
if (req->method() == Post)
|
||||
{
|
||||
fccb();
|
||||
return;
|
||||
@ -27,4 +27,4 @@ void PostFilter::doFilter(const HttpRequestPtr &req,
|
||||
auto res = drogon::HttpResponse::newHttpResponse();
|
||||
res->setStatusCode(HttpResponse::k405MethodNotAllowed);
|
||||
fcb(res);
|
||||
}
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ void PutFilter::doFilter(const HttpRequestPtr &req,
|
||||
const FilterCallback &fcb,
|
||||
const FilterChainCallback &fccb)
|
||||
{
|
||||
if (req->method() == HttpRequest::kPut)
|
||||
if (req->method() == Put)
|
||||
{
|
||||
fccb();
|
||||
return;
|
||||
@ -27,4 +27,4 @@ void PutFilter::doFilter(const HttpRequestPtr &req,
|
||||
auto res = drogon::HttpResponse::newHttpResponse();
|
||||
res->setStatusCode(HttpResponse::k405MethodNotAllowed);
|
||||
fcb(res);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user