ZLMediaKit/src/Http/HttpCookie.cpp

147 lines
4.2 KiB
C++
Raw Normal View History

2018-09-25 10:19:11 +08:00
/*
2020-04-04 20:30:09 +08:00
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
2018-09-25 10:19:11 +08:00
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
2018-09-25 10:19:11 +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.
2018-09-25 10:19:11 +08:00
*/
#include "HttpCookie.h"
#include "Util/util.h"
2022-02-11 11:54:12 +08:00
#include "Util/onceToken.h"
2018-11-06 20:40:53 +08:00
#if defined(_WIN32)
#include "Util/strptime_win.h"
2018-11-06 20:40:53 +08:00
#endif
using namespace toolkit;
using namespace std;
2018-10-24 17:17:55 +08:00
namespace mediakit {
2018-09-25 10:19:11 +08:00
2022-02-11 11:54:12 +08:00
void HttpCookie::setPath(const string &path) {
_path = path;
}
2022-02-11 11:54:12 +08:00
void HttpCookie::setHost(const string &host) {
_host = host;
}
2022-02-11 11:54:12 +08:00
static long s_gmtoff = 0; //时间差
static onceToken s_token([]() {
#ifdef _WIN32
TIME_ZONE_INFORMATION tzinfo;
DWORD dwStandardDaylight;
long bias;
dwStandardDaylight = GetTimeZoneInformation(&tzinfo);
bias = tzinfo.Bias;
if (dwStandardDaylight == TIME_ZONE_ID_STANDARD) {
bias += tzinfo.StandardBias;
}
if (dwStandardDaylight == TIME_ZONE_ID_DAYLIGHT) {
bias += tzinfo.DaylightBias;
}
s_gmtoff = -bias * 60; //时间差(分钟)
#else
s_gmtoff = getLocalTime(time(nullptr)).tm_gmtoff;
#endif // _WIN32
});
// from https://gmbabar.wordpress.com/2010/12/01/mktime-slow-use-custom-function/#comment-58
static time_t time_to_epoch(const struct tm *ltm, int utcdiff) {
const int mon_days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
long tyears, tdays, leaps, utc_hrs;
int i;
tyears = ltm->tm_year - 70; // tm->tm_year is from 1900.
leaps = (tyears + 2) / 4; // no of next two lines until year 2100.
// i = (ltm->tm_year 100) / 100;
// leaps -= ( (i/4)*3 + i%4 );
tdays = 0;
for (i = 0; i < ltm->tm_mon; i++)
tdays += mon_days[i];
tdays += ltm->tm_mday - 1; // days of month passed.
tdays = tdays + (tyears * 365) + leaps;
utc_hrs = ltm->tm_hour + utcdiff; // for your time zone.
return (tdays * 86400) + (utc_hrs * 3600) + (ltm->tm_min * 60) + ltm->tm_sec;
}
static time_t timeStrToInt(const string &date) {
struct tm tt;
2022-02-11 11:54:12 +08:00
strptime(date.data(), "%a, %b %d %Y %H:%M:%S %Z", &tt);
// mktime内部有使用互斥锁非常影响性能
return time_to_epoch(&tt, s_gmtoff / 3600); // mktime(&tt);
2019-03-14 09:59:07 +08:00
}
2022-02-11 11:54:12 +08:00
void HttpCookie::setExpires(const string &expires, const string &server_date) {
2019-03-14 09:59:07 +08:00
_expire = timeStrToInt(expires);
2022-02-11 11:54:12 +08:00
if (!server_date.empty()) {
_expire = time(NULL) + (_expire - timeStrToInt(server_date));
2019-03-14 09:59:07 +08:00
}
}
2022-02-11 11:54:12 +08:00
void HttpCookie::setKeyVal(const string &key, const string &val) {
_key = key;
_val = val;
}
2022-02-11 11:54:12 +08:00
HttpCookie::operator bool() {
return !_host.empty() && !_key.empty() && !_val.empty() && (_expire > time(NULL));
}
const string &HttpCookie::getVal() const {
return _val;
}
2022-02-11 11:54:12 +08:00
const string &HttpCookie::getKey() const {
return _key;
}
2022-02-11 11:54:12 +08:00
HttpCookieStorage &HttpCookieStorage::Instance() {
static HttpCookieStorage instance;
return instance;
}
void HttpCookieStorage::set(const HttpCookie::Ptr &cookie) {
lock_guard<mutex> lck(_mtx_cookie);
2022-02-11 11:54:12 +08:00
if (!cookie || !(*cookie)) {
return;
}
2019-06-13 11:45:13 +08:00
_all_cookie[cookie->_host][cookie->_path][cookie->_key] = cookie;
}
vector<HttpCookie::Ptr> HttpCookieStorage::get(const string &host, const string &path) {
vector<HttpCookie::Ptr> ret(0);
lock_guard<mutex> lck(_mtx_cookie);
2022-02-11 11:54:12 +08:00
auto it = _all_cookie.find(host);
if (it == _all_cookie.end()) {
2019-06-13 11:45:13 +08:00
//未找到该host相关记录
return ret;
}
2019-06-13 11:45:13 +08:00
//遍历该host下所有path
2022-02-11 11:54:12 +08:00
for (auto &pr : it->second) {
if (path.find(pr.first) != 0) {
2019-06-13 11:45:13 +08:00
//这个path不匹配
continue;
}
//遍历该path下的各个cookie
2022-02-11 11:54:12 +08:00
for (auto it_cookie = pr.second.begin(); it_cookie != pr.second.end();) {
if (!*(it_cookie->second)) {
2019-06-13 11:45:13 +08:00
//该cookie已经过期移除之
it_cookie = pr.second.erase(it_cookie);
continue;
}
2019-06-13 11:45:13 +08:00
//保存有效cookie
ret.emplace_back(it_cookie->second);
++it_cookie;
}
2019-06-13 11:45:13 +08:00
}
return ret;
}
2018-09-25 10:19:11 +08:00
2018-10-24 17:17:55 +08:00
} /* namespace mediakit */