mirror of
https://gitee.com/an-tao/drogon.git
synced 2024-12-02 03:38:03 +08:00
add Session in HttpRequest
This commit is contained in:
parent
7384f34dc0
commit
72b2a60b53
@ -5,6 +5,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <drogon/Session.h>
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
@ -28,6 +29,7 @@ namespace drogon
|
||||
virtual const std::string& query() const=0;
|
||||
virtual const std::string& path() const=0;
|
||||
virtual Version getVersion() const=0;
|
||||
virtual SessionPtr session() const=0;
|
||||
virtual ~HttpRequest(){}
|
||||
};
|
||||
}
|
@ -237,7 +237,7 @@ namespace drogon
|
||||
std::string body_;
|
||||
size_t left_body_length_;
|
||||
size_t current_chunk_length_;
|
||||
uint8_t contentType_;
|
||||
uint8_t contentType_=CT_TEXT_HTML;
|
||||
//trantor::Date receiveTime_;
|
||||
|
||||
void setContentType(const std::string& contentType)
|
||||
|
@ -11,8 +11,8 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#define PATH_LIST_BEGIN private:\
|
||||
virtual std::vector<std::string> paths() override \
|
||||
#define PATH_LIST_BEGIN public:\
|
||||
static std::vector<std::string> paths() \
|
||||
{\
|
||||
std::vector<std::string> vet;
|
||||
|
||||
@ -31,15 +31,15 @@ namespace drogon
|
||||
virtual ~HttpSimpleController(){}
|
||||
|
||||
private:
|
||||
virtual std::vector<std::string> paths()=0;
|
||||
//virtual std::vector<std::string> paths()=0;
|
||||
class pathRegister {
|
||||
public:
|
||||
pathRegister(){
|
||||
std::cout<<"pathRegister!!"<<HttpSimpleController<T>::classTypeName()<<std::endl;
|
||||
// _register("hahaha",{std::string((char *)PATH)...});
|
||||
auto ctl= static_cast<HttpSimpleController<T>*>(new T);
|
||||
auto vPaths=ctl->paths();
|
||||
delete ctl;
|
||||
// auto ctl= static_cast<HttpSimpleController<T>*>(new T);
|
||||
auto vPaths=T::paths();
|
||||
//delete ctl;
|
||||
for(auto path:vPaths)
|
||||
{
|
||||
HttpAppFramework::instance().registerHttpSimpleController(path,HttpSimpleController<T>::classTypeName());
|
||||
|
@ -5,6 +5,11 @@
|
||||
// that can be found in the License file.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#ifdef USE_STD_ANY
|
||||
|
||||
#include <any>
|
||||
@ -22,49 +27,53 @@ typedef boost::any Any;
|
||||
#endif
|
||||
|
||||
typedef std::map<std::string,Any> SessionMap;
|
||||
class Session
|
||||
namespace drogon
|
||||
{
|
||||
public:
|
||||
template <typename T> T get(const std::string &key) const{
|
||||
std::lock_guard<std::mutex> lck(mutex_);
|
||||
auto it=sessionMap_.find(key);
|
||||
if(it!=sessionMap_.end())
|
||||
class Session
|
||||
{
|
||||
public:
|
||||
template <typename T> T get(const std::string &key) const{
|
||||
std::lock_guard<std::mutex> lck(mutex_);
|
||||
auto it=sessionMap_.find(key);
|
||||
if(it!=sessionMap_.end())
|
||||
{
|
||||
return Any_cast<T>(it->second);
|
||||
}
|
||||
T tmp;
|
||||
return tmp;
|
||||
};
|
||||
Any &operator[](const std::string &key){
|
||||
std::lock_guard<std::mutex> lck(mutex_);
|
||||
return sessionMap_[key];
|
||||
};
|
||||
void insert(const std::string& key,const Any &obj)
|
||||
{
|
||||
return Any_cast<T>(it->second);
|
||||
}
|
||||
T tmp;
|
||||
return tmp;
|
||||
};
|
||||
Any &operator[](const std::string &key){
|
||||
std::lock_guard<std::mutex> lck(mutex_);
|
||||
return sessionMap_[key];
|
||||
};
|
||||
void insert(const std::string& key,const Any &obj)
|
||||
{
|
||||
std::lock_guard<std::mutex> lck(mutex_);
|
||||
sessionMap_[key]=obj;
|
||||
};
|
||||
void erase(const std::string& key)
|
||||
{
|
||||
std::lock_guard<std::mutex> lck(mutex_);
|
||||
sessionMap_.erase(key);
|
||||
}
|
||||
bool find(const std::string& key)
|
||||
{
|
||||
std::lock_guard<std::mutex> lck(mutex_);
|
||||
if(sessionMap_.find(key) == sessionMap_.end())
|
||||
std::lock_guard<std::mutex> lck(mutex_);
|
||||
sessionMap_[key]=obj;
|
||||
};
|
||||
void erase(const std::string& key)
|
||||
{
|
||||
return false;
|
||||
std::lock_guard<std::mutex> lck(mutex_);
|
||||
sessionMap_.erase(key);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void clear()
|
||||
{
|
||||
std::lock_guard<std::mutex> lck(mutex_);
|
||||
sessionMap_.clear();
|
||||
}
|
||||
protected:
|
||||
SessionMap sessionMap_;
|
||||
int timeoutInterval_;
|
||||
mutable std::mutex mutex_;
|
||||
};
|
||||
bool find(const std::string& key)
|
||||
{
|
||||
std::lock_guard<std::mutex> lck(mutex_);
|
||||
if(sessionMap_.find(key) == sessionMap_.end())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void clear()
|
||||
{
|
||||
std::lock_guard<std::mutex> lck(mutex_);
|
||||
sessionMap_.clear();
|
||||
}
|
||||
protected:
|
||||
SessionMap sessionMap_;
|
||||
int timeoutInterval_;
|
||||
mutable std::mutex mutex_;
|
||||
};
|
||||
typedef std::shared_ptr<Session> SessionPtr;
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
//
|
||||
// Use of this source code is governed by a MIT license
|
||||
// that can be found in the License file.
|
||||
#include "HttpRequestImpl.h"
|
||||
|
||||
#include <drogon/DrClassMap.h>
|
||||
#include <drogon/HttpAppFramework.h>
|
||||
@ -178,6 +179,7 @@ void HttpAppFrameworkImpl::onAsyncRequest(const HttpRequest& req,std::function<v
|
||||
}
|
||||
|
||||
}
|
||||
((HttpRequestImpl &)req).setSession((*_sessionMapPtr)[session_id]);
|
||||
/*filters
|
||||
std::vector<std::string> filters=(*_filterMap)[req.path().c_str()];
|
||||
for(std::string filterClassName:filters)
|
||||
|
@ -280,6 +280,16 @@ namespace drogon
|
||||
}
|
||||
|
||||
void appendToBuffer(MsgBuffer* output) const;
|
||||
|
||||
virtual SessionPtr session() const override
|
||||
{
|
||||
return _sessionPtr;
|
||||
}
|
||||
|
||||
void setSession(const SessionPtr &session)
|
||||
{
|
||||
_sessionPtr=session;
|
||||
}
|
||||
private:
|
||||
Method method_;
|
||||
Version version_;
|
||||
@ -290,6 +300,8 @@ namespace drogon
|
||||
std::map<std::string, std::string> headers_;
|
||||
std::map<std::string, std::string> cookies_;
|
||||
std::map<std::string, std::string> premeter_;
|
||||
|
||||
SessionPtr _sessionPtr;
|
||||
protected:
|
||||
std::string content_;
|
||||
size_t contentLen;
|
||||
|
Loading…
Reference in New Issue
Block a user