2018-09-25 10:19:11 +08:00
|
|
|
|
/*
|
|
|
|
|
* MIT License
|
|
|
|
|
*
|
2019-05-08 15:40:07 +08:00
|
|
|
|
* Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
|
2018-09-25 10:19:11 +08:00
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2018-09-24 00:50:02 +08:00
|
|
|
|
|
|
|
|
|
#include "HttpCookie.h"
|
|
|
|
|
#include "Util/util.h"
|
2019-03-14 09:59:07 +08:00
|
|
|
|
#include "Util/logger.h"
|
2018-09-24 00:50:02 +08:00
|
|
|
|
|
2018-11-06 20:40:53 +08:00
|
|
|
|
#if defined(_WIN32)
|
|
|
|
|
#include "strptime_win.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
using namespace toolkit;
|
2018-10-24 17:17:55 +08:00
|
|
|
|
namespace mediakit {
|
2018-09-25 10:19:11 +08:00
|
|
|
|
|
2018-09-24 00:50:02 +08:00
|
|
|
|
void HttpCookie::setPath(const string &path){
|
|
|
|
|
_path = path;
|
|
|
|
|
}
|
|
|
|
|
void HttpCookie::setHost(const string &host){
|
|
|
|
|
_host = host;
|
|
|
|
|
}
|
2019-03-14 09:59:07 +08:00
|
|
|
|
static uint32_t timeStrToInt(const string &date){
|
2018-09-24 00:50:02 +08:00
|
|
|
|
struct tm tt;
|
2019-03-14 09:59:07 +08:00
|
|
|
|
strptime(date.data(),"%a, %b %d %Y %H:%M:%S %Z",&tt);
|
|
|
|
|
return mktime(&tt);
|
|
|
|
|
}
|
|
|
|
|
void HttpCookie::setExpires(const string &expires,const string &server_date){
|
|
|
|
|
_expire = timeStrToInt(expires);
|
|
|
|
|
if(!server_date.empty()){
|
|
|
|
|
_expire = time(NULL) + (_expire - timeStrToInt(server_date));
|
|
|
|
|
// DebugL << (timeStrToInt(expires) - timeStrToInt(server_date)) / 60;
|
|
|
|
|
}
|
2018-09-24 00:50:02 +08:00
|
|
|
|
}
|
|
|
|
|
void HttpCookie::setKeyVal(const string &key,const string &val){
|
|
|
|
|
_key = key;
|
|
|
|
|
_val = val;
|
|
|
|
|
}
|
|
|
|
|
HttpCookie::operator bool (){
|
|
|
|
|
return !_host.empty() && !_key.empty() && !_val.empty() && (_expire > time(NULL));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const string &HttpCookie::getVal() const {
|
|
|
|
|
return _val;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const string &HttpCookie::getKey() const{
|
|
|
|
|
return _key;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HttpCookieStorage &HttpCookieStorage::Instance(){
|
|
|
|
|
static HttpCookieStorage instance;
|
|
|
|
|
return instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HttpCookieStorage::set(const HttpCookie::Ptr &cookie) {
|
|
|
|
|
lock_guard<mutex> lck(_mtx_cookie);
|
|
|
|
|
if(!cookie || !(*cookie)){
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-06-13 11:45:13 +08:00
|
|
|
|
_all_cookie[cookie->_host][cookie->_path][cookie->_key] = cookie;
|
2018-09-24 00:50:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vector<HttpCookie::Ptr> HttpCookieStorage::get(const string &host, const string &path) {
|
|
|
|
|
vector<HttpCookie::Ptr> ret(0);
|
|
|
|
|
lock_guard<mutex> lck(_mtx_cookie);
|
|
|
|
|
auto it = _all_cookie.find(host);
|
|
|
|
|
if(it == _all_cookie.end()){
|
2019-06-13 11:45:13 +08:00
|
|
|
|
//未找到该host相关记录
|
2018-09-24 00:50:02 +08:00
|
|
|
|
return ret;
|
|
|
|
|
}
|
2019-06-13 11:45:13 +08:00
|
|
|
|
//遍历该host下所有path
|
|
|
|
|
for(auto &pr : it->second){
|
|
|
|
|
if(path.find(pr.first) != 0){
|
|
|
|
|
//这个path不匹配
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
//遍历该path下的各个cookie
|
|
|
|
|
for(auto it_cookie = pr.second.begin() ; it_cookie != pr.second.end() ; ){
|
|
|
|
|
if(!*(it_cookie->second)){
|
|
|
|
|
//该cookie已经过期,移除之
|
|
|
|
|
it_cookie = pr.second.erase(it_cookie);
|
|
|
|
|
continue;
|
2018-09-24 00:50:02 +08:00
|
|
|
|
}
|
2019-06-13 11:45:13 +08:00
|
|
|
|
//保存有效cookie
|
|
|
|
|
ret.emplace_back(it_cookie->second);
|
|
|
|
|
++it_cookie;
|
2018-09-24 00:50:02 +08:00
|
|
|
|
}
|
2019-06-13 11:45:13 +08:00
|
|
|
|
}
|
2018-09-24 00:50:02 +08:00
|
|
|
|
return ret;
|
|
|
|
|
}
|
2018-09-25 10:19:11 +08:00
|
|
|
|
|
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
} /* namespace mediakit */
|