Merge pull request #148 from an-tao/cookies_bug_fix

Disable cookies on 404 pages
This commit is contained in:
An Tao 2019-05-07 22:51:58 +08:00 committed by GitHub
commit 52bc179b51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 13 additions and 19 deletions

View File

@ -36,7 +36,7 @@ static void doFilterChains(const std::vector<std::shared_ptr<HttpFilterBase>> &f
auto &filter = filters[index];
filter->doFilter(req,
[needSetJsessionid, callbackPtr, sessionIdPtr](const HttpResponsePtr &res) {
if (needSetJsessionid)
if (needSetJsessionid && res->statusCode() != k404NotFound)
res->addCookie("JSESSIONID", *sessionIdPtr);
(*callbackPtr)(res);
},

View File

@ -817,7 +817,7 @@ void HttpAppFrameworkImpl::onAsyncRequest(const HttpRequestImplPtr &req, std::fu
0,
req,
std::make_shared<std::function<void(const HttpResponsePtr &)>>([callbackPtr, needSetJsessionid, sessionIdPtr](const HttpResponsePtr &resp) {
if (!needSetJsessionid)
if (!needSetJsessionid || resp->statusCode() == k404NotFound)
(*callbackPtr)(resp);
else
{

View File

@ -266,8 +266,6 @@ void HttpControllersRouter::route(const HttpRequestImplPtr &req,
{
//No controller found
auto res = drogon::HttpResponse::newNotFoundResponse();
if (needSetJsessionid)
res->addCookie("JSESSIONID", sessionId);
callback(res);
}
}
@ -275,8 +273,6 @@ void HttpControllersRouter::route(const HttpRequestImplPtr &req,
{
//No controller found
auto res = drogon::HttpResponse::newNotFoundResponse();
if (needSetJsessionid)
res->addCookie("JSESSIONID", sessionId);
callback(res);
}
}
@ -294,7 +290,7 @@ void HttpControllersRouter::doControllerHandler(const CtrlBinderPtr &ctrlBinderP
//use cached response!
LOG_TRACE << "Use cached response";
if (!needSetJsessionid)
if (!needSetJsessionid || responsePtr->statusCode() == k404NotFound)
invokeCallback(callback, req, responsePtr);
else
{
@ -360,7 +356,7 @@ void HttpControllersRouter::doControllerHandler(const CtrlBinderPtr &ctrlBinderP
});
}
}
if (needSetJsessionid)
if (needSetJsessionid && resp->statusCode() != k404NotFound)
{
if (resp->expiredTime() >= 0)
{
@ -427,7 +423,7 @@ void HttpControllersRouter::doPreHandlingAdvices(const CtrlBinderPtr &ctrlBinder
0,
req,
std::make_shared<std::function<void(const HttpResponsePtr &)>>([callbackPtr, needSetJsessionid, sessionIdPtr](const HttpResponsePtr &resp) {
if (!needSetJsessionid)
if (!needSetJsessionid || resp->statusCode() == k404NotFound)
(*callbackPtr)(resp);
else
{

View File

@ -33,7 +33,7 @@ class HttpResponseImpl : public HttpResponse
{
friend class HttpResponseParser;
public:
public:
HttpResponseImpl()
: _statusCode(kUnknown),
_creationDate(trantor::Date::now()),
@ -240,12 +240,12 @@ class HttpResponseImpl : public HttpResponse
virtual void addCookie(const std::string &key, const std::string &value) override
{
_cookies.insert(std::make_pair(key, Cookie(key, value)));
_cookies[key] = Cookie(key, value);
}
virtual void addCookie(const Cookie &cookie) override
{
_cookies.insert(std::make_pair(cookie.key(), cookie));
_cookies[cookie.key()] = cookie;
}
virtual const Cookie &getCookie(const std::string &key, const Cookie &defaultCookie = Cookie()) const override
@ -378,10 +378,10 @@ class HttpResponseImpl : public HttpResponse
_bodyPtr = gunzipBody;
}
protected:
protected:
void makeHeaderString(const std::shared_ptr<std::string> &headerStringPtr) const;
private:
private:
std::unordered_map<std::string, std::string> _headers;
std::unordered_map<std::string, Cookie> _cookies;

View File

@ -176,7 +176,7 @@ void HttpSimpleControllersRouter::doControllerHandler(const CtrlBinderPtr &ctrlB
{
//use cached response!
LOG_TRACE << "Use cached response";
if (!needSetJsessionid)
if (!needSetJsessionid || responsePtr->statusCode() == k404NotFound)
invokeCallback(callback, req, responsePtr);
else
{
@ -208,7 +208,7 @@ void HttpSimpleControllersRouter::doControllerHandler(const CtrlBinderPtr &ctrlB
});
}
}
if (needSetJsessionid)
if (needSetJsessionid && resp->statusCode() != k404NotFound)
{
if (resp->expiredTime() >= 0)
{
@ -229,8 +229,6 @@ void HttpSimpleControllersRouter::doControllerHandler(const CtrlBinderPtr &ctrlB
const std::string &ctrlName = ctrlBinderPtr->_controllerName;
LOG_ERROR << "can't find controller " << ctrlName;
auto res = drogon::HttpResponse::newNotFoundResponse();
if (needSetJsessionid)
res->addCookie("JSESSIONID", sessionId);
invokeCallback(callback, req, res);
}
}
@ -326,7 +324,7 @@ void HttpSimpleControllersRouter::doPreHandlingAdvices(const CtrlBinderPtr &ctrl
0,
req,
std::make_shared<std::function<void(const HttpResponsePtr &)>>([callbackPtr, needSetJsessionid, sessionIdPtr](const HttpResponsePtr &resp) {
if (!needSetJsessionid)
if (!needSetJsessionid || resp->statusCode() == k404NotFound)
(*callbackPtr)(resp);
else
{