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
2068c873
Commit
2068c873
authored
Jul 30, 2019
by
xiongziliang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
完善跨域支持
parent
d01fc3a3
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
18 行增加
和
15 行删除
+18
-15
src/Http/HttpSession.cpp
+16
-13
src/Http/HttpSession.h
+2
-2
没有找到文件。
src/Http/HttpSession.cpp
查看文件 @
2068c873
...
...
@@ -134,7 +134,10 @@ int64_t HttpSession::onRecvHeader(const char *header,uint64_t len) {
return
0
;
}
//默认后面数据不是content而是header
//跨域
_origin
=
_parser
[
"Origin"
];
//默认后面数据不是content而是header
int64_t
content_len
=
0
;
auto
&
fun
=
it
->
second
;
try
{
...
...
@@ -775,6 +778,11 @@ inline HttpSession::KeyValue HttpSession::makeHttpHeader(bool bClose, int64_t iC
if
(
iContentSize
>
0
){
headerOut
.
emplace
(
"Content-Length"
,
StrPrinter
<<
iContentSize
<<
endl
);
}
if
(
!
_origin
.
empty
()){
headerOut
.
emplace
(
"Access-Control-Allow-Origin"
,
_origin
);
headerOut
.
emplace
(
"Access-Control-Allow-Credentials"
,
"true"
);
}
return
headerOut
;
}
...
...
@@ -802,20 +810,19 @@ inline bool HttpSession::emitHttpEvent(bool doInvoke){
GET_CONFIG
(
uint32_t
,
reqCnt
,
Http
::
kMaxReqCount
);
bool
bClose
=
(
strcasecmp
(
_parser
[
"Connection"
].
data
(),
"close"
)
==
0
)
||
(
++
_iReqCnt
>
reqCnt
);
auto
Origin
=
_parser
[
"Origin"
];
/////////////////////异步回复Invoker///////////////////////////////
weak_ptr
<
HttpSession
>
weakSelf
=
dynamic_pointer_cast
<
HttpSession
>
(
shared_from_this
());
HttpResponseInvoker
invoker
=
[
weakSelf
,
bClose
,
Origin
](
const
string
&
codeOut
,
const
KeyValue
&
headerOut
,
const
string
&
contentOut
){
HttpResponseInvoker
invoker
=
[
weakSelf
,
bClose
](
const
string
&
codeOut
,
const
KeyValue
&
headerOut
,
const
string
&
contentOut
){
auto
strongSelf
=
weakSelf
.
lock
();
if
(
!
strongSelf
)
{
return
;
}
strongSelf
->
async
([
weakSelf
,
bClose
,
codeOut
,
headerOut
,
contentOut
,
Origin
]()
{
strongSelf
->
async
([
weakSelf
,
bClose
,
codeOut
,
headerOut
,
contentOut
]()
{
auto
strongSelf
=
weakSelf
.
lock
();
if
(
!
strongSelf
)
{
return
;
}
strongSelf
->
responseDelay
(
Origin
,
bClose
,
codeOut
,
headerOut
,
contentOut
);
strongSelf
->
responseDelay
(
bClose
,
codeOut
,
headerOut
,
contentOut
);
if
(
bClose
){
strongSelf
->
shutdown
(
SockException
(
Err_shutdown
,
"Connection: close"
));
}
...
...
@@ -906,19 +913,15 @@ inline void HttpSession::Handle_Req_POST(int64_t &content_len) {
}
//有后续content数据要处理,暂时不关闭连接
}
void
HttpSession
::
responseDelay
(
const
string
&
Origin
,
bool
bClose
,
const
string
&
codeOut
,
const
KeyValue
&
headerOut
,
void
HttpSession
::
responseDelay
(
bool
bClose
,
const
string
&
codeOut
,
const
KeyValue
&
headerOut
,
const
string
&
contentOut
){
if
(
codeOut
.
empty
()){
sendNotFound
(
bClose
);
return
;
}
auto
headerOther
=
makeHttpHeader
(
bClose
,
contentOut
.
size
(),
"text/plain"
);
if
(
!
Origin
.
empty
()){
headerOther
[
"Access-Control-Allow-Origin"
]
=
Origin
;
headerOther
[
"Access-Control-Allow-Credentials"
]
=
"true"
;
}
auto
headerOther
=
makeHttpHeader
(
bClose
,
contentOut
.
size
(),
"text/plain"
);
for
(
auto
&
pr
:
headerOther
){
//添加默认http头,默认http头不能覆盖用户自定义的头
const_cast
<
KeyValue
&>
(
headerOut
).
emplace
(
pr
.
first
,
pr
.
second
);
...
...
src/Http/HttpSession.h
查看文件 @
2068c873
...
...
@@ -112,8 +112,7 @@ private:
inline
void
sendNotFound
(
bool
bClose
);
inline
void
sendResponse
(
const
char
*
pcStatus
,
const
KeyValue
&
header
,
const
string
&
strContent
);
inline
static
KeyValue
makeHttpHeader
(
bool
bClose
=
false
,
int64_t
iContentSize
=-
1
,
const
char
*
pcContentType
=
"text/html"
);
void
responseDelay
(
const
string
&
Origin
,
bool
bClose
,
void
responseDelay
(
bool
bClose
,
const
string
&
codeOut
,
const
KeyValue
&
headerOut
,
const
string
&
contentOut
);
...
...
@@ -139,6 +138,7 @@ private:
*/
inline
string
getClientUid
();
private
:
string
_origin
;
Parser
_parser
;
Ticker
_ticker
;
uint32_t
_iReqCnt
=
0
;
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论