Set session to requests for websockets (#521)

This commit is contained in:
An Tao 2020-07-31 09:25:20 +08:00 committed by GitHub
parent 5e35055379
commit 820715cd62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 13 deletions

View File

@ -514,7 +514,6 @@ void HttpAppFrameworkImpl::run()
// TODO: new plugin
});
}
getLoop()->queueInLoop([this]() {
// Let listener event loops run when everything is ready.
listenerManagerPtr_->startListening();
@ -614,11 +613,27 @@ HttpAppFramework &HttpAppFrameworkImpl::setUploadPath(
}
return *this;
}
void HttpAppFrameworkImpl::findSessionForRequest(const HttpRequestImplPtr &req)
{
if (useSession_)
{
std::string sessionId = req->getCookie("JSESSIONID");
bool needSetJsessionid = false;
if (sessionId.empty())
{
sessionId = utils::getUuid();
needSetJsessionid = true;
}
req->setSession(
sessionManagerPtr_->getSession(sessionId, needSetJsessionid));
}
}
void HttpAppFrameworkImpl::onNewWebsockRequest(
const HttpRequestImplPtr &req,
std::function<void(const HttpResponsePtr &)> &&callback,
const WebSocketConnectionImplPtr &wsConnPtr)
{
findSessionForRequest(req);
// Route to controller
if (!preRoutingObservers_.empty())
{
@ -744,18 +759,7 @@ void HttpAppFrameworkImpl::onAsyncRequest(
callback(resp);
return;
}
if (useSession_)
{
std::string sessionId = req->getCookie("JSESSIONID");
bool needSetJsessionid = false;
if (sessionId.empty())
{
sessionId = utils::getUuid();
needSetJsessionid = true;
}
req->setSession(
sessionManagerPtr_->getSession(sessionId, needSetJsessionid));
}
findSessionForRequest(req);
// Route to controller
if (!preRoutingObservers_.empty())
{

View File

@ -512,6 +512,8 @@ class HttpAppFrameworkImpl : public HttpAppFramework
const std::vector<HttpMethod> &validMethods,
const std::vector<std::string> &filters);
void findSessionForRequest(const HttpRequestImplPtr &req);
// We use a uuid string as session id;
// set sessionTimeout_=0 to make location session valid forever based on
// cookies;