HttpCookieManager.h 7.48 KB
Newer Older
xiongziliang committed
1
/*
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 * 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"
33
#include "Util/util.h"
34 35
#include "Util/TimeTicker.h"
#include "Network/Socket.h"
xiongziliang committed
36
#include "Common/Parser.h"
37 38 39 40 41 42 43

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

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

44 45 46
namespace mediakit {

class HttpCookieManager;
47 48 49 50

/**
 * cookie对象,用于保存cookie的一些相关属性
 */
51
class HttpServerCookie : public AnyStorage , public noncopyable{
52
public:
53
    typedef std::shared_ptr<HttpServerCookie> Ptr;
54 55 56
    /**
     * 构建cookie
     * @param manager cookie管理者对象
57
     * @param cookie_name cookie名,例如MY_SESSION
58
     * @param uid 用户唯一id
59
     * @param cookie cookie随机字符串
60 61
     * @param max_elapsed 最大过期时间,单位秒
     */
62 63 64 65 66 67 68

    HttpServerCookie(const std::shared_ptr<HttpCookieManager> &manager,
                     const string &cookie_name,
                     const string &uid,
                     const string &cookie,
                     uint64_t max_elapsed);
    ~HttpServerCookie() ;
69 70 71 72 73 74 75 76 77 78

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

    /**
     * 获取http中Set-Cookie字段的值
     * @param cookie_name 该cookie的名称,譬如 MY_SESSION
79
     * @param path http访问路径
80 81
     * @return 例如 MY_SESSION=XXXXXX;expires=Wed, Jun 12 2019 06:30:48 GMT;path=/index/files/
     */
82
    string getCookie(const string &path) const;
83 84 85 86 87 88 89 90

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

    /**
91
     * 获取该cookie名
92 93
     * @return
     */
94
    const string& getCookieName() const;
95 96 97 98 99 100 101 102 103 104 105

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

    /**
     * 判断该cookie是否过期
     * @return
     */
    bool isExpired();
106 107 108 109 110 111

    /**
     * 获取区域锁
     * @return
     */
    std::shared_ptr<lock_guard<mutex> > getLock();
112 113


114 115 116 117
private:
    string cookieExpireTime() const ;
private:
    string _uid;
118
    string _cookie_name;
119 120 121
    string _cookie_uuid;
    uint64_t _max_elapsed;
    Ticker _ticker;
122
    mutex _mtx;
123
    std::weak_ptr<HttpCookieManager> _manager;
124 125 126 127 128
};

/**
 * cookie随机字符串生成器
 */
129
class RandStrGeneator{
130
public:
131 132
    RandStrGeneator() = default;
    ~RandStrGeneator() = default;
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157

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

    /**
     *  获取单例
     */
167
    static HttpCookieManager &Instance();
168 169 170

    /**
     * 添加cookie
171
     * @param cookie_name cookie名,例如MY_SESSION
172 173 174 175 176
     * @param uid 用户id,如果为空则为匿名登录
     * @param max_client 该账号最多登录多少个设备
     * @param max_elapsed 该cookie过期时间,单位秒
     * @return cookie对象
     */
177
    HttpServerCookie::Ptr addCookie(const string &cookie_name,const string &uid, uint64_t max_elapsed = COOKIE_DEFAULT_LIFE,int max_client = 1);
178 179 180

    /**
     * 根据cookie随机字符串查找cookie对象
181
     * @param cookie_name cookie名,例如MY_SESSION
182 183 184
     * @param cookie cookie随机字符串
     * @return cookie对象,可以为nullptr
     */
185
    HttpServerCookie::Ptr getCookie(const string &cookie_name,const string &cookie);
186 187 188

    /**
     * 从http头中获取cookie对象
189
     * @param cookie_name cookie名,例如MY_SESSION
190 191 192
     * @param http_header http头
     * @return cookie对象
     */
193
    HttpServerCookie::Ptr getCookie(const string &cookie_name,const StrCaseMap &http_header);
194 195

    /**
196 197 198 199 200 201 202 203
     * 根据uid获取cookie
     * @param cookie_name cookie名,例如MY_SESSION
     * @param uid 用户id
     * @return cookie对象
     */
    HttpServerCookie::Ptr getCookieByUid(const string &cookie_name,const string &uid);

    /**
204 205 206 207
     * 删除cookie,用户登出时使用
     * @param cookie cookie对象,可以为nullptr
     * @return
     */
208
    bool delCookie(const HttpServerCookie::Ptr &cookie);
209
private:
210
    HttpCookieManager();
211 212 213
    void onManager();
    /**
     * 构造cookie对象时触发,目的是记录某账号下多个cookie
214
     * @param cookie_name cookie名,例如MY_SESSION
215 216 217
     * @param uid 用户id
     * @param cookie cookie随机字符串
     */
218
    void onAddCookie(const string &cookie_name,const string &uid,const string &cookie);
219 220 221

    /**
     * 析构cookie对象时触发
222
     * @param cookie_name cookie名,例如MY_SESSION
223 224 225
     * @param uid 用户id
     * @param cookie cookie随机字符串
     */
226
    void onDelCookie(const string &cookie_name,const string &uid,const string &cookie);
227 228

    /**
229 230 231 232 233 234 235 236 237
     * 获取某用户名下最先登录时的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);

    /**
238
     * 删除cookie
239
     * @param cookie_name cookie名,例如MY_SESSION
240 241 242
     * @param cookie cookie随机字符串
     * @return 成功true
     */
243
    bool delCookie(const string &cookie_name,const string &cookie);
244
private:
245 246
    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;
247 248
    recursive_mutex _mtx_cookie;
    Timer::Ptr _timer;
249
    RandStrGeneator _geneator;
250 251
};

252
}//namespace mediakit
253 254 255


#endif //SRC_HTTP_COOKIEMANAGER_H