2019-08-08 19:01:45 +08:00
|
|
|
|
/*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
2019-06-12 17:53:48 +08:00
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
2019-06-12 17:53:48 +08:00
|
|
|
|
*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Use of this source code is governed by MIT license that can be found in the
|
|
|
|
|
* LICENSE file in the root of the source tree. All contributing project authors
|
|
|
|
|
* may be found in the AUTHORS file in the root of the source tree.
|
2019-06-12 17:53:48 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef SRC_HTTP_COOKIEMANAGER_H
|
|
|
|
|
#define SRC_HTTP_COOKIEMANAGER_H
|
|
|
|
|
|
2022-02-11 14:33:11 +08:00
|
|
|
|
#include "Common/Parser.h"
|
|
|
|
|
#include "Network/Socket.h"
|
|
|
|
|
#include "Util/TimeTicker.h"
|
2019-06-12 17:53:48 +08:00
|
|
|
|
#include "Util/mini.h"
|
2019-08-20 12:09:43 +08:00
|
|
|
|
#include "Util/util.h"
|
2022-02-11 14:33:11 +08:00
|
|
|
|
#include <memory>
|
|
|
|
|
#include <unordered_map>
|
2019-06-12 17:53:48 +08:00
|
|
|
|
|
|
|
|
|
#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的一些相关属性
|
|
|
|
|
*/
|
2022-02-11 14:33:11 +08:00
|
|
|
|
class HttpServerCookie : public toolkit::noncopyable {
|
2019-06-12 17:53:48 +08:00
|
|
|
|
public:
|
2022-02-11 14:33:11 +08:00
|
|
|
|
using Ptr = std::shared_ptr<HttpServerCookie>;
|
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
|
|
|
|
|
2022-02-11 14:33:11 +08:00
|
|
|
|
HttpServerCookie(
|
|
|
|
|
const std::shared_ptr<HttpCookieManager> &manager, const std::string &cookie_name, const std::string &uid,
|
|
|
|
|
const std::string &cookie, uint64_t max_elapsed);
|
|
|
|
|
~HttpServerCookie();
|
2019-06-12 17:53:48 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取uid
|
|
|
|
|
* @return uid
|
|
|
|
|
*/
|
2022-02-02 20:34:50 +08:00
|
|
|
|
const std::string &getUid() const;
|
2019-06-12 17:53:48 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取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/
|
|
|
|
|
*/
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::string getCookie(const std::string &path) const;
|
2019-06-12 17:53:48 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取cookie随机字符串
|
|
|
|
|
* @return cookie随机字符串
|
|
|
|
|
*/
|
2022-02-11 14:33:11 +08:00
|
|
|
|
const std::string &getCookie() const;
|
2019-06-12 17:53:48 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2019-06-13 12:00:41 +08:00
|
|
|
|
* 获取该cookie名
|
2019-06-12 17:53:48 +08:00
|
|
|
|
* @return
|
|
|
|
|
*/
|
2022-02-11 14:33:11 +08:00
|
|
|
|
const std::string &getCookieName() const;
|
2019-06-12 17:53:48 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 更新该cookie的过期时间,可以让此cookie不失效
|
|
|
|
|
*/
|
|
|
|
|
void updateTime();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 判断该cookie是否过期
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
bool isExpired();
|
2019-06-14 18:42:09 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2022-02-11 14:33:11 +08:00
|
|
|
|
* 设置附加数据
|
2019-06-14 18:42:09 +08:00
|
|
|
|
*/
|
2022-02-11 14:33:11 +08:00
|
|
|
|
void setAttach(std::shared_ptr<void> attach);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* 获取附加数据
|
|
|
|
|
*/
|
|
|
|
|
template <class T>
|
2022-10-18 19:23:20 +08:00
|
|
|
|
T& getAttach() {
|
|
|
|
|
return *static_cast<T *>(_attach.get());
|
2022-02-11 14:33:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-12 17:53:48 +08:00
|
|
|
|
private:
|
2022-02-11 14:33:11 +08:00
|
|
|
|
std::string cookieExpireTime() const;
|
|
|
|
|
|
2019-06-12 17:53:48 +08:00
|
|
|
|
private:
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::string _uid;
|
|
|
|
|
std::string _cookie_name;
|
|
|
|
|
std::string _cookie_uuid;
|
2019-06-12 17:53:48 +08:00
|
|
|
|
uint64_t _max_elapsed;
|
2022-02-02 20:34:50 +08:00
|
|
|
|
toolkit::Ticker _ticker;
|
2022-02-11 14:33:11 +08:00
|
|
|
|
std::shared_ptr<void> _attach;
|
2019-06-13 12:00:41 +08:00
|
|
|
|
std::weak_ptr<HttpCookieManager> _manager;
|
2019-06-12 17:53:48 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* cookie随机字符串生成器
|
|
|
|
|
*/
|
2022-08-08 17:36:07 +08:00
|
|
|
|
class RandStrGenerator {
|
2019-06-12 17:53:48 +08:00
|
|
|
|
public:
|
2022-08-08 17:36:07 +08:00
|
|
|
|
RandStrGenerator() = default;
|
|
|
|
|
~RandStrGenerator() = default;
|
2019-06-12 17:53:48 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取不碰撞的随机字符串
|
|
|
|
|
* @return 随机字符串
|
|
|
|
|
*/
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::string obtain();
|
2019-06-12 17:53:48 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 释放随机字符串
|
|
|
|
|
* @param str 随机字符串
|
|
|
|
|
*/
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void release(const std::string &str);
|
2022-02-11 14:33:11 +08:00
|
|
|
|
|
2019-06-12 17:53:48 +08:00
|
|
|
|
private:
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::string obtain_l();
|
2022-02-11 14:33:11 +08:00
|
|
|
|
|
2019-06-12 17:53:48 +08:00
|
|
|
|
private:
|
|
|
|
|
//碰撞库
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::unordered_set<std::string> _obtained;
|
2019-06-12 17:53:48 +08:00
|
|
|
|
//增长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
|
|
|
|
friend class HttpServerCookie;
|
2022-02-11 14:33:11 +08:00
|
|
|
|
using Ptr = std::shared_ptr<HttpCookieManager>;
|
2019-06-13 12:00:41 +08:00
|
|
|
|
~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对象
|
|
|
|
|
*/
|
2022-02-11 14:33:11 +08:00
|
|
|
|
HttpServerCookie::Ptr addCookie(
|
|
|
|
|
const std::string &cookie_name, const std::string &uid, uint64_t max_elapsed = COOKIE_DEFAULT_LIFE,
|
|
|
|
|
std::shared_ptr<void> attach = nullptr,
|
|
|
|
|
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
|
|
|
|
|
*/
|
2022-02-11 14:33:11 +08:00
|
|
|
|
HttpServerCookie::Ptr getCookie(const std::string &cookie_name, const std::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对象
|
|
|
|
|
*/
|
2022-02-11 14:33:11 +08:00
|
|
|
|
HttpServerCookie::Ptr getCookie(const std::string &cookie_name, const StrCaseMap &http_header);
|
2019-06-12 17:53:48 +08:00
|
|
|
|
|
2019-06-14 15:19:02 +08:00
|
|
|
|
/**
|
|
|
|
|
* 根据uid获取cookie
|
|
|
|
|
* @param cookie_name cookie名,例如MY_SESSION
|
|
|
|
|
* @param uid 用户id
|
|
|
|
|
* @return cookie对象
|
|
|
|
|
*/
|
2022-02-11 14:33:11 +08:00
|
|
|
|
HttpServerCookie::Ptr getCookieByUid(const std::string &cookie_name, const std::string &uid);
|
2019-06-14 15:19:02 +08:00
|
|
|
|
|
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);
|
2022-02-11 14:33:11 +08:00
|
|
|
|
|
2019-06-12 17:53:48 +08:00
|
|
|
|
private:
|
2019-06-13 12:00:41 +08:00
|
|
|
|
HttpCookieManager();
|
2022-02-11 14:33:11 +08:00
|
|
|
|
|
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随机字符串
|
|
|
|
|
*/
|
2022-02-11 14:33:11 +08:00
|
|
|
|
void onAddCookie(const std::string &cookie_name, const std::string &uid, const std::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随机字符串
|
|
|
|
|
*/
|
2022-02-11 14:33:11 +08:00
|
|
|
|
void onDelCookie(const std::string &cookie_name, const std::string &uid, const std::string &cookie);
|
2019-06-12 17:53:48 +08:00
|
|
|
|
|
2019-06-14 15:19:02 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取某用户名下最先登录时的cookie,目的是实现某用户下最多登录若干个设备
|
|
|
|
|
* @param cookie_name cookie名,例如MY_SESSION
|
|
|
|
|
* @param uid 用户id
|
|
|
|
|
* @param max_client 最多登录的设备个数
|
|
|
|
|
* @return 最早的cookie随机字符串
|
|
|
|
|
*/
|
2022-02-11 14:33:11 +08:00
|
|
|
|
std::string getOldestCookie(const std::string &cookie_name, const std::string &uid, int max_client = 1);
|
2019-06-14 15:19:02 +08:00
|
|
|
|
|
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
|
|
|
|
|
*/
|
2022-02-11 14:33:11 +08:00
|
|
|
|
bool delCookie(const std::string &cookie_name, const std::string &cookie);
|
|
|
|
|
|
2019-06-12 17:53:48 +08:00
|
|
|
|
private:
|
2022-02-11 14:33:11 +08:00
|
|
|
|
std::unordered_map<
|
|
|
|
|
std::string /*cookie_name*/, std::unordered_map<std::string /*cookie*/, HttpServerCookie::Ptr /*cookie_data*/>>
|
|
|
|
|
_map_cookie;
|
|
|
|
|
std::unordered_map<
|
|
|
|
|
std::string /*cookie_name*/,
|
|
|
|
|
std::unordered_map<std::string /*uid*/, std::map<uint64_t /*cookie time stamp*/, std::string /*cookie*/>>>
|
|
|
|
|
_map_uid_to_cookie;
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::recursive_mutex _mtx_cookie;
|
|
|
|
|
toolkit::Timer::Ptr _timer;
|
2022-08-08 17:36:07 +08:00
|
|
|
|
RandStrGenerator _generator;
|
2019-06-12 17:53:48 +08:00
|
|
|
|
};
|
|
|
|
|
|
2022-02-11 14:33:11 +08:00
|
|
|
|
} // namespace mediakit
|
2019-06-12 17:53:48 +08:00
|
|
|
|
|
2022-02-11 14:33:11 +08:00
|
|
|
|
#endif // SRC_HTTP_COOKIEMANAGER_H
|