Skip to content

Commit

Permalink
automatically try to set sessionptr if doesnt exist and not saving an…
Browse files Browse the repository at this point in the history
…on users
  • Loading branch information
juanetch committed Feb 8, 2022
1 parent b1df2bf commit 781b262
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 23 deletions.
4 changes: 2 additions & 2 deletions lib/inc/drogon/HttpAppFramework.h
Original file line number Diff line number Diff line change
Expand Up @@ -749,14 +749,14 @@ class DROGON_EXPORT HttpAppFramework : public trantor::NonCopyable
/**
*
* @note
* Saving of anonymous sessions is disabled by default
* Saving of anonymous sessions is enabled by default
*/
virtual HttpAppFramework &enableSaveAnonSession() = 0;

/// Disable saving of anonymous sessions.
/**
* @note
* Saving of anonymous sessions is disabled by default
* Saving of anonymous sessions is enabled by default
*/
virtual HttpAppFramework &disableSaveAnonSession() = 0;

Expand Down
4 changes: 2 additions & 2 deletions lib/inc/drogon/HttpRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,10 @@ class DROGON_EXPORT HttpRequest
}

/// Get the session to which the request belongs.
virtual const SessionPtr &session() const = 0;
virtual SessionPtr &session() = 0;

/// Get the session to which the request belongs.
const SessionPtr &getSession() const
SessionPtr &getSession()
{
return session();
}
Expand Down
4 changes: 0 additions & 4 deletions lib/src/ConfigLoader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -250,13 +250,9 @@ static void loadApp(const Json::Value &app)
drogon::app().enableSession(timeout);
auto saveAnonSession = app.get("session_save_anon", true).asBool();
if (saveAnonSession)
{
drogon::app().enableSaveAnonSession();
}
else
{
drogon::app().disableSaveAnonSession();
}
}
else
drogon::app().disableSession();
Expand Down
19 changes: 13 additions & 6 deletions lib/src/HttpRequestImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -349,17 +349,21 @@ class HttpRequestImpl : public HttpRequest

void appendToBuffer(trantor::MsgBuffer *output) const;

virtual const SessionPtr &session() const override
virtual SessionPtr &session() override
{
if (!sessionPtr_)
{
setSession();
}
return sessionPtr_;
}

void setSession(const SessionPtr &session)
virtual void setSession(const SessionPtr &session) override
{
sessionPtr_ = session;
}

bool setSession()
virtual bool setSession() override
{
if (sessionManagerPtr_)
{
Expand All @@ -372,9 +376,12 @@ class HttpRequestImpl : public HttpRequest
return false;
}

void setSessionManager(SessionManager *sessionManagerPtr)
virtual void setSessionManager(SessionManager *sessionManagerPtr)
{
sessionManagerPtr_ = sessionManagerPtr;
if (sessionManagerPtr)
{
sessionManagerPtr_ = sessionManagerPtr;
}
}

virtual const AttributesPtr &attributes() const override
Expand Down Expand Up @@ -561,7 +568,7 @@ class HttpRequestImpl : public HttpRequest
mutable ContentType contentType_{CT_TEXT_PLAIN};
mutable bool flagForParsingContentType_{false};
mutable std::string contentTypeString_;
SessionManager *sessionManagerPtr_;
SessionManager *sessionManagerPtr_{nullptr};
};

using HttpRequestImplPtr = std::shared_ptr<HttpRequestImpl>;
Expand Down
16 changes: 7 additions & 9 deletions lib/tests/integration_test/server/TimeFilter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@ void TimeFilter::doFilter(const HttpRequestPtr &req,
FilterChainCallback &&ccb)
{
trantor::Date now = trantor::Date::date();
if (!req->session())
// automatically set if not saving anon users
auto is_session_set = req->session();
if (!is_session_set)
{
bool is_session_set = req->setSession();
if (!is_session_set)
{
// no session support by framework,pls enable session
auto resp = HttpResponse::newNotFoundResponse();
cb(resp);
return;
}
// no session support by framework,pls enable session
auto resp = HttpResponse::newNotFoundResponse();
cb(resp);
return;
}
auto lastDate = req->session()->getOptional<trantor::Date>(VDate);
if (lastDate)
Expand Down

0 comments on commit 781b262

Please sign in to comment.