ZLMediaKit/src/Http/HttpCookieManager.h

253 lines
7.5 KiB
C
Raw Normal View History

2019-06-12 17:53:48 +08:00
/*
* MIT License
*
* Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef SRC_HTTP_COOKIEMANAGER_H
#define SRC_HTTP_COOKIEMANAGER_H
#include <memory>
#include <unordered_map>
#include "Util/mini.h"
#include "Util/TimeTicker.h"
#include "Network/Socket.h"
#include "Rtsp/Rtsp.h"
using namespace std;
using namespace toolkit;
using namespace mediakit;
#define COOKIE_DEFAULT_LIFE (7 * 24 * 60 * 60)
2019-06-13 12:00:41 +08:00
namespace mediakit {
class HttpCookieManager;
2019-06-12 17:53:48 +08:00
/**
* cookie对象cookie的一些相关属性
*/
2019-06-15 16:33:12 +08:00
class HttpServerCookie : public map<string,string> , public noncopyable{
2019-06-12 17:53:48 +08:00
public:
2019-06-13 12:00:41 +08:00
typedef std::shared_ptr<HttpServerCookie> Ptr;
2019-06-12 17:53:48 +08:00
/**
* cookie
* @param manager cookie管理者对象
2019-06-13 12:00:41 +08:00
* @param cookie_name cookie名MY_SESSION
2019-06-12 17:53:48 +08:00
* @param uid id
2019-06-13 12:00:41 +08:00
* @param cookie cookie随机字符串
2019-06-12 17:53:48 +08:00
* @param max_elapsed
*/
2019-06-13 12:00:41 +08:00
HttpServerCookie(const std::shared_ptr<HttpCookieManager> &manager,
const string &cookie_name,
const string &uid,
const string &cookie,
uint64_t max_elapsed);
~HttpServerCookie() ;
2019-06-12 17:53:48 +08:00
/**
* uid
* @return uid
*/
const string &getUid() const;
/**
* http中Set-Cookie字段的值
* @param cookie_name cookie的名称 MY_SESSION
2019-06-13 12:00:41 +08:00
* @param path http访问路径
2019-06-12 17:53:48 +08:00
* @return MY_SESSION=XXXXXX;expires=Wed, Jun 12 2019 06:30:48 GMT;path=/index/files/
*/
2019-06-13 12:00:41 +08:00
string getCookie(const string &path) const;
2019-06-12 17:53:48 +08:00
/**
* cookie随机字符串
* @return cookie随机字符串
*/
const string& getCookie() const;
/**
2019-06-13 12:00:41 +08:00
* cookie名
2019-06-12 17:53:48 +08:00
* @return
*/
2019-06-13 12:00:41 +08:00
const string& getCookieName() const;
2019-06-12 17:53:48 +08:00
/**
* cookie的过期时间cookie不失效
*/
void updateTime();
/**
* cookie是否过期
* @return
*/
bool isExpired();
/**
*
* @return
*/
std::shared_ptr<lock_guard<mutex> > getLock();
2019-06-12 17:53:48 +08:00
private:
string cookieExpireTime() const ;
private:
string _uid;
2019-06-13 12:00:41 +08:00
string _cookie_name;
2019-06-12 17:53:48 +08:00
string _cookie_uuid;
uint64_t _max_elapsed;
Ticker _ticker;
mutex _mtx;
2019-06-13 12:00:41 +08:00
std::weak_ptr<HttpCookieManager> _manager;
2019-06-12 17:53:48 +08:00
};
/**
* cookie随机字符串生成器
*/
2019-06-13 12:00:41 +08:00
class RandStrGeneator{
2019-06-12 17:53:48 +08:00
public:
2019-06-13 12:00:41 +08:00
RandStrGeneator() = default;
~RandStrGeneator() = default;
2019-06-12 17:53:48 +08:00
/**
*
* @return
*/
string obtain();
/**
*
* @param str
*/
void release(const string &str);
private:
string obtain_l();
private:
//碰撞库
unordered_set<string> _obtained;
//增长index防止碰撞用
int _index = 0;
};
/**
* cookie管理器cookie的生成以及过期管理
*
*/
2019-06-13 12:00:41 +08:00
class HttpCookieManager : public std::enable_shared_from_this<HttpCookieManager> {
2019-06-12 17:53:48 +08:00
public:
2019-06-13 12:00:41 +08:00
typedef std::shared_ptr<HttpCookieManager> Ptr;
friend class HttpServerCookie;
~HttpCookieManager();
2019-06-12 17:53:48 +08:00
/**
*
*/
2019-06-13 12:00:41 +08:00
static HttpCookieManager &Instance();
2019-06-12 17:53:48 +08:00
/**
* cookie
2019-06-13 12:00:41 +08:00
* @param cookie_name cookie名MY_SESSION
2019-06-12 17:53:48 +08:00
* @param uid id
* @param max_client
* @param max_elapsed cookie过期时间
* @return cookie对象
*/
2019-06-13 12:00:41 +08:00
HttpServerCookie::Ptr addCookie(const string &cookie_name,const string &uid, uint64_t max_elapsed = COOKIE_DEFAULT_LIFE,int max_client = 1);
2019-06-12 17:53:48 +08:00
/**
* cookie随机字符串查找cookie对象
2019-06-13 12:00:41 +08:00
* @param cookie_name cookie名MY_SESSION
2019-06-12 17:53:48 +08:00
* @param cookie cookie随机字符串
* @return cookie对象nullptr
*/
2019-06-13 12:00:41 +08:00
HttpServerCookie::Ptr getCookie(const string &cookie_name,const string &cookie);
2019-06-12 17:53:48 +08:00
/**
* http头中获取cookie对象
2019-06-13 12:00:41 +08:00
* @param cookie_name cookie名MY_SESSION
2019-06-12 17:53:48 +08:00
* @param http_header http头
* @return cookie对象
*/
2019-06-13 12:00:41 +08:00
HttpServerCookie::Ptr getCookie(const string &cookie_name,const StrCaseMap &http_header);
2019-06-12 17:53:48 +08:00
/**
* uid获取cookie
* @param cookie_name cookie名MY_SESSION
* @param uid id
* @return cookie对象
*/
HttpServerCookie::Ptr getCookieByUid(const string &cookie_name,const string &uid);
2019-06-12 17:53:48 +08:00
/**
* cookie使
* @param cookie cookie对象nullptr
* @return
*/
2019-06-13 12:00:41 +08:00
bool delCookie(const HttpServerCookie::Ptr &cookie);
2019-06-12 17:53:48 +08:00
private:
2019-06-13 12:00:41 +08:00
HttpCookieManager();
2019-06-12 17:53:48 +08:00
void onManager();
/**
* cookie对象时触发cookie
2019-06-13 12:00:41 +08:00
* @param cookie_name cookie名MY_SESSION
2019-06-12 17:53:48 +08:00
* @param uid id
* @param cookie cookie随机字符串
*/
2019-06-13 12:00:41 +08:00
void onAddCookie(const string &cookie_name,const string &uid,const string &cookie);
2019-06-12 17:53:48 +08:00
/**
* cookie对象时触发
2019-06-13 12:00:41 +08:00
* @param cookie_name cookie名MY_SESSION
2019-06-12 17:53:48 +08:00
* @param uid id
* @param cookie cookie随机字符串
*/
2019-06-13 12:00:41 +08:00
void onDelCookie(const string &cookie_name,const string &uid,const string &cookie);
2019-06-12 17:53:48 +08:00
/**
* cookie
* @param cookie_name cookie名MY_SESSION
* @param uid id
* @param max_client
* @return cookie随机字符串
*/
string getOldestCookie(const string &cookie_name,const string &uid, int max_client = 1);
2019-06-12 17:53:48 +08:00
/**
* cookie
2019-06-13 12:00:41 +08:00
* @param cookie_name cookie名MY_SESSION
2019-06-12 17:53:48 +08:00
* @param cookie cookie随机字符串
* @return true
*/
2019-06-13 12:00:41 +08:00
bool delCookie(const string &cookie_name,const string &cookie);
2019-06-12 17:53:48 +08:00
private:
2019-06-13 12:00:41 +08:00
unordered_map<string/*cookie_name*/,unordered_map<string/*cookie*/,HttpServerCookie::Ptr/*cookie_data*/> >_map_cookie;
unordered_map<string/*cookie_name*/,unordered_map<string/*uid*/,map<uint64_t/*cookie time stamp*/,string/*cookie*/> > >_map_uid_to_cookie;
2019-06-12 17:53:48 +08:00
recursive_mutex _mtx_cookie;
Timer::Ptr _timer;
2019-06-13 12:00:41 +08:00
RandStrGeneator _geneator;
2019-06-12 17:53:48 +08:00
};
2019-06-13 12:00:41 +08:00
}//namespace mediakit
2019-06-12 17:53:48 +08:00
#endif //SRC_HTTP_COOKIEMANAGER_H