HttpCookieManager.h 6.67 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
3
 *
4
 * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
5
 *
xiongziliang committed
6 7 8
 * 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.
9 10 11 12 13 14 15 16
 */

#ifndef SRC_HTTP_COOKIEMANAGER_H
#define SRC_HTTP_COOKIEMANAGER_H

#include <memory>
#include <unordered_map>
#include "Util/mini.h"
17
#include "Util/util.h"
18 19
#include "Util/TimeTicker.h"
#include "Network/Socket.h"
xiongziliang committed
20
#include "Common/Parser.h"
21 22 23 24 25 26 27

using namespace std;
using namespace toolkit;
using namespace mediakit;

#define COOKIE_DEFAULT_LIFE (7 * 24 * 60 * 60)

28 29 30
namespace mediakit {

class HttpCookieManager;
31 32 33 34

/**
 * cookie对象,用于保存cookie的一些相关属性
 */
35
class HttpServerCookie : public AnyStorage , public noncopyable{
36
public:
37
    typedef std::shared_ptr<HttpServerCookie> Ptr;
38 39 40
    /**
     * 构建cookie
     * @param manager cookie管理者对象
41
     * @param cookie_name cookie名,例如MY_SESSION
42
     * @param uid 用户唯一id
43
     * @param cookie cookie随机字符串
44 45
     * @param max_elapsed 最大过期时间,单位秒
     */
46 47 48 49 50 51 52

    HttpServerCookie(const std::shared_ptr<HttpCookieManager> &manager,
                     const string &cookie_name,
                     const string &uid,
                     const string &cookie,
                     uint64_t max_elapsed);
    ~HttpServerCookie() ;
53 54 55 56 57 58 59 60 61 62

    /**
     * 获取uid
     * @return uid
     */
    const string &getUid() const;

    /**
     * 获取http中Set-Cookie字段的值
     * @param cookie_name 该cookie的名称,譬如 MY_SESSION
63
     * @param path http访问路径
64 65
     * @return 例如 MY_SESSION=XXXXXX;expires=Wed, Jun 12 2019 06:30:48 GMT;path=/index/files/
     */
66
    string getCookie(const string &path) const;
67 68 69 70 71 72 73 74

    /**
     * 获取cookie随机字符串
     * @return cookie随机字符串
     */
    const string& getCookie() const;

    /**
75
     * 获取该cookie名
76 77
     * @return
     */
78
    const string& getCookieName() const;
79 80 81 82 83 84 85 86 87 88 89

    /**
     * 更新该cookie的过期时间,可以让此cookie不失效
     */
    void updateTime();

    /**
     * 判断该cookie是否过期
     * @return
     */
    bool isExpired();
90 91 92 93 94

    /**
     * 获取区域锁
     * @return
     */
95
    std::shared_ptr<lock_guard<recursive_mutex> > getLock();
96 97 98 99
private:
    string cookieExpireTime() const ;
private:
    string _uid;
100
    string _cookie_name;
101 102 103
    string _cookie_uuid;
    uint64_t _max_elapsed;
    Ticker _ticker;
104
    recursive_mutex _mtx;
105
    std::weak_ptr<HttpCookieManager> _manager;
106 107 108 109 110
};

/**
 * cookie随机字符串生成器
 */
111
class RandStrGeneator{
112
public:
113 114
    RandStrGeneator() = default;
    ~RandStrGeneator() = default;
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

    /**
     * 获取不碰撞的随机字符串
     * @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的生成以及过期管理,同时实现了同账号异地挤占登录功能
 * 该对象实现了同账号最多登录若干个设备
 */
140
class HttpCookieManager : public std::enable_shared_from_this<HttpCookieManager> {
141
public:
142 143 144
    typedef std::shared_ptr<HttpCookieManager> Ptr;
    friend class HttpServerCookie;
    ~HttpCookieManager();
145 146 147 148

    /**
     *  获取单例
     */
149
    static HttpCookieManager &Instance();
150 151 152

    /**
     * 添加cookie
153
     * @param cookie_name cookie名,例如MY_SESSION
154 155 156 157 158
     * @param uid 用户id,如果为空则为匿名登录
     * @param max_client 该账号最多登录多少个设备
     * @param max_elapsed 该cookie过期时间,单位秒
     * @return cookie对象
     */
159
    HttpServerCookie::Ptr addCookie(const string &cookie_name,const string &uid, uint64_t max_elapsed = COOKIE_DEFAULT_LIFE,int max_client = 1);
160 161 162

    /**
     * 根据cookie随机字符串查找cookie对象
163
     * @param cookie_name cookie名,例如MY_SESSION
164 165 166
     * @param cookie cookie随机字符串
     * @return cookie对象,可以为nullptr
     */
167
    HttpServerCookie::Ptr getCookie(const string &cookie_name,const string &cookie);
168 169 170

    /**
     * 从http头中获取cookie对象
171
     * @param cookie_name cookie名,例如MY_SESSION
172 173 174
     * @param http_header http头
     * @return cookie对象
     */
175
    HttpServerCookie::Ptr getCookie(const string &cookie_name,const StrCaseMap &http_header);
176 177

    /**
178 179 180 181 182 183 184 185
     * 根据uid获取cookie
     * @param cookie_name cookie名,例如MY_SESSION
     * @param uid 用户id
     * @return cookie对象
     */
    HttpServerCookie::Ptr getCookieByUid(const string &cookie_name,const string &uid);

    /**
186 187 188 189
     * 删除cookie,用户登出时使用
     * @param cookie cookie对象,可以为nullptr
     * @return
     */
190
    bool delCookie(const HttpServerCookie::Ptr &cookie);
191
private:
192
    HttpCookieManager();
193 194 195
    void onManager();
    /**
     * 构造cookie对象时触发,目的是记录某账号下多个cookie
196
     * @param cookie_name cookie名,例如MY_SESSION
197 198 199
     * @param uid 用户id
     * @param cookie cookie随机字符串
     */
200
    void onAddCookie(const string &cookie_name,const string &uid,const string &cookie);
201 202 203

    /**
     * 析构cookie对象时触发
204
     * @param cookie_name cookie名,例如MY_SESSION
205 206 207
     * @param uid 用户id
     * @param cookie cookie随机字符串
     */
208
    void onDelCookie(const string &cookie_name,const string &uid,const string &cookie);
209 210

    /**
211 212 213 214 215 216 217 218 219
     * 获取某用户名下最先登录时的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);

    /**
220
     * 删除cookie
221
     * @param cookie_name cookie名,例如MY_SESSION
222 223 224
     * @param cookie cookie随机字符串
     * @return 成功true
     */
225
    bool delCookie(const string &cookie_name,const string &cookie);
226
private:
227 228
    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;
229 230
    recursive_mutex _mtx_cookie;
    Timer::Ptr _timer;
231
    RandStrGeneator _geneator;
232 233
};

234
}//namespace mediakit
235 236 237


#endif //SRC_HTTP_COOKIEMANAGER_H