Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Z
ZLMediaKit
概览
Overview
Details
Activity
Cycle Analytics
版本库
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
问题
0
Issues
0
列表
Board
标记
里程碑
合并请求
0
Merge Requests
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
Snippets
成员
Collapse sidebar
Close sidebar
活动
图像
聊天
创建新问题
作业
提交
Issue Boards
Open sidebar
张翔宇
ZLMediaKit
Commits
832c8d0d
Commit
832c8d0d
authored
Feb 11, 2022
by
ziyue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化mktime性能问题
parent
d541ed25
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
65 行增加
和
20 行删除
+65
-20
src/Http/HttpCookie.cpp
+65
-20
没有找到文件。
src/Http/HttpCookie.cpp
查看文件 @
832c8d0d
...
@@ -10,7 +10,7 @@
...
@@ -10,7 +10,7 @@
#include "HttpCookie.h"
#include "HttpCookie.h"
#include "Util/util.h"
#include "Util/util.h"
#include "Util/
logger
.h"
#include "Util/
onceToken
.h"
#if defined(_WIN32)
#if defined(_WIN32)
#include "Util/strptime_win.h"
#include "Util/strptime_win.h"
...
@@ -21,28 +21,75 @@ using namespace std;
...
@@ -21,28 +21,75 @@ using namespace std;
namespace
mediakit
{
namespace
mediakit
{
void
HttpCookie
::
setPath
(
const
string
&
path
){
void
HttpCookie
::
setPath
(
const
string
&
path
)
{
_path
=
path
;
_path
=
path
;
}
}
void
HttpCookie
::
setHost
(
const
string
&
host
){
void
HttpCookie
::
setHost
(
const
string
&
host
)
{
_host
=
host
;
_host
=
host
;
}
}
static
time_t
timeStrToInt
(
const
string
&
date
){
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
;
struct
tm
tt
;
strptime
(
date
.
data
(),
"%a, %b %d %Y %H:%M:%S %Z"
,
&
tt
);
strptime
(
date
.
data
(),
"%a, %b %d %Y %H:%M:%S %Z"
,
&
tt
);
return
mktime
(
&
tt
);
// mktime内部有使用互斥锁,非常影响性能
return
time_to_epoch
(
&
tt
,
s_gmtoff
/
3600
);
// mktime(&tt);
}
}
void
HttpCookie
::
setExpires
(
const
string
&
expires
,
const
string
&
server_date
){
void
HttpCookie
::
setExpires
(
const
string
&
expires
,
const
string
&
server_date
)
{
_expire
=
timeStrToInt
(
expires
);
_expire
=
timeStrToInt
(
expires
);
if
(
!
server_date
.
empty
())
{
if
(
!
server_date
.
empty
())
{
_expire
=
time
(
NULL
)
+
(
_expire
-
timeStrToInt
(
server_date
));
_expire
=
time
(
NULL
)
+
(
_expire
-
timeStrToInt
(
server_date
));
}
}
}
}
void
HttpCookie
::
setKeyVal
(
const
string
&
key
,
const
string
&
val
){
void
HttpCookie
::
setKeyVal
(
const
string
&
key
,
const
string
&
val
)
{
_key
=
key
;
_key
=
key
;
_val
=
val
;
_val
=
val
;
}
}
HttpCookie
::
operator
bool
(){
HttpCookie
::
operator
bool
()
{
return
!
_host
.
empty
()
&&
!
_key
.
empty
()
&&
!
_val
.
empty
()
&&
(
_expire
>
time
(
NULL
));
return
!
_host
.
empty
()
&&
!
_key
.
empty
()
&&
!
_val
.
empty
()
&&
(
_expire
>
time
(
NULL
));
}
}
...
@@ -50,19 +97,18 @@ const string &HttpCookie::getVal() const {
...
@@ -50,19 +97,18 @@ const string &HttpCookie::getVal() const {
return
_val
;
return
_val
;
}
}
const
string
&
HttpCookie
::
getKey
()
const
{
const
string
&
HttpCookie
::
getKey
()
const
{
return
_key
;
return
_key
;
}
}
HttpCookieStorage
&
HttpCookieStorage
::
Instance
()
{
HttpCookieStorage
&
HttpCookieStorage
::
Instance
(){
static
HttpCookieStorage
instance
;
static
HttpCookieStorage
instance
;
return
instance
;
return
instance
;
}
}
void
HttpCookieStorage
::
set
(
const
HttpCookie
::
Ptr
&
cookie
)
{
void
HttpCookieStorage
::
set
(
const
HttpCookie
::
Ptr
&
cookie
)
{
lock_guard
<
mutex
>
lck
(
_mtx_cookie
);
lock_guard
<
mutex
>
lck
(
_mtx_cookie
);
if
(
!
cookie
||
!
(
*
cookie
))
{
if
(
!
cookie
||
!
(
*
cookie
))
{
return
;
return
;
}
}
_all_cookie
[
cookie
->
_host
][
cookie
->
_path
][
cookie
->
_key
]
=
cookie
;
_all_cookie
[
cookie
->
_host
][
cookie
->
_path
][
cookie
->
_key
]
=
cookie
;
...
@@ -72,19 +118,19 @@ vector<HttpCookie::Ptr> HttpCookieStorage::get(const string &host, const string
...
@@ -72,19 +118,19 @@ vector<HttpCookie::Ptr> HttpCookieStorage::get(const string &host, const string
vector
<
HttpCookie
::
Ptr
>
ret
(
0
);
vector
<
HttpCookie
::
Ptr
>
ret
(
0
);
lock_guard
<
mutex
>
lck
(
_mtx_cookie
);
lock_guard
<
mutex
>
lck
(
_mtx_cookie
);
auto
it
=
_all_cookie
.
find
(
host
);
auto
it
=
_all_cookie
.
find
(
host
);
if
(
it
==
_all_cookie
.
end
())
{
if
(
it
==
_all_cookie
.
end
())
{
//未找到该host相关记录
//未找到该host相关记录
return
ret
;
return
ret
;
}
}
//遍历该host下所有path
//遍历该host下所有path
for
(
auto
&
pr
:
it
->
second
)
{
for
(
auto
&
pr
:
it
->
second
)
{
if
(
path
.
find
(
pr
.
first
)
!=
0
)
{
if
(
path
.
find
(
pr
.
first
)
!=
0
)
{
//这个path不匹配
//这个path不匹配
continue
;
continue
;
}
}
//遍历该path下的各个cookie
//遍历该path下的各个cookie
for
(
auto
it_cookie
=
pr
.
second
.
begin
()
;
it_cookie
!=
pr
.
second
.
end
()
;
)
{
for
(
auto
it_cookie
=
pr
.
second
.
begin
();
it_cookie
!=
pr
.
second
.
end
();)
{
if
(
!*
(
it_cookie
->
second
))
{
if
(
!*
(
it_cookie
->
second
))
{
//该cookie已经过期,移除之
//该cookie已经过期,移除之
it_cookie
=
pr
.
second
.
erase
(
it_cookie
);
it_cookie
=
pr
.
second
.
erase
(
it_cookie
);
continue
;
continue
;
...
@@ -97,5 +143,4 @@ vector<HttpCookie::Ptr> HttpCookieStorage::get(const string &host, const string
...
@@ -97,5 +143,4 @@ vector<HttpCookie::Ptr> HttpCookieStorage::get(const string &host, const string
return
ret
;
return
ret
;
}
}
}
/* namespace mediakit */
}
/* namespace mediakit */
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论