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
63ca646d
Commit
63ca646d
authored
Nov 13, 2018
by
xiongziliang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
http客户端支持Transfer-Encoding: chunked
parent
57dcb63b
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
110 行增加
和
1 行删除
+110
-1
src/Http/HttpChunkedSplitter.cpp
+29
-0
src/Http/HttpChunkedSplitter.h
+60
-0
src/Http/HttpClient.cpp
+19
-1
src/Http/HttpClient.h
+2
-0
没有找到文件。
src/Http/HttpChunkedSplitter.cpp
0 → 100644
查看文件 @
63ca646d
//
// Created by xzl on 2018/11/13.
//
#include "HttpChunkedSplitter.h"
namespace
mediakit
{
const
char
*
HttpChunkedSplitter
::
onSearchPacketTail
(
const
char
*
data
,
int
len
)
{
auto
pos
=
strstr
(
data
,
"
\r\n
"
);
if
(
!
pos
){
return
nullptr
;
}
return
pos
+
2
;
}
void
HttpChunkedSplitter
::
onRecvContent
(
const
char
*
data
,
uint64_t
len
)
{
onRecvChunk
(
data
,
len
-
2
);
}
int64_t
HttpChunkedSplitter
::
onRecvHeader
(
const
char
*
data
,
uint64_t
len
)
{
string
str
(
data
,
len
-
2
);
int
ret
;
sscanf
(
str
.
data
(),
"%X"
,
&
ret
);
return
ret
+
2
;
}
}
//
namespace
mediakit
\ No newline at end of file
src/Http/HttpChunkedSplitter.h
0 → 100644
查看文件 @
63ca646d
/*
* MIT License
*
* Copyright (c) 2016 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 ZLMEDIAKIT_HTTPCHUNKEDSPLITTER_H
#define ZLMEDIAKIT_HTTPCHUNKEDSPLITTER_H
#include <functional>
#include "HttpRequestSplitter.h"
namespace
mediakit
{
class
HttpChunkedSplitter
:
public
HttpRequestSplitter
{
public
:
/**
* len == 0时代表结束
*/
typedef
std
::
function
<
void
(
const
char
*
data
,
uint64_t
len
)
>
onChunkData
;
HttpChunkedSplitter
(
const
onChunkData
&
cb
){
_onChunkData
=
cb
;
};
~
HttpChunkedSplitter
()
override
{}
;
protected
:
int64_t
onRecvHeader
(
const
char
*
data
,
uint64_t
len
)
override
;
void
onRecvContent
(
const
char
*
data
,
uint64_t
len
)
override
;
const
char
*
onSearchPacketTail
(
const
char
*
data
,
int
len
)
override
;
protected
:
virtual
void
onRecvChunk
(
const
char
*
data
,
uint64_t
len
){
if
(
_onChunkData
){
_onChunkData
(
data
,
len
);
}
};
private
:
onChunkData
_onChunkData
;
};
}
//namespace mediakit
#endif //ZLMEDIAKIT_HTTPCHUNKEDSPLITTER_H
src/Http/HttpClient.cpp
查看文件 @
63ca646d
...
@@ -117,6 +117,7 @@ void HttpClient::onConnect(const SockException &ex) {
...
@@ -117,6 +117,7 @@ void HttpClient::onConnect(const SockException &ex) {
_totalBodySize
=
0
;
_totalBodySize
=
0
;
_recvedBodySize
=
0
;
_recvedBodySize
=
0
;
HttpRequestSplitter
::
reset
();
HttpRequestSplitter
::
reset
();
_chunkedSplitter
.
reset
();
_StrPrinter
printer
;
_StrPrinter
printer
;
printer
<<
_method
+
" "
<<
_path
+
" HTTP/1.1
\r\n
"
;
printer
<<
_method
+
" "
<<
_path
+
" HTTP/1.1
\r\n
"
;
...
@@ -156,6 +157,20 @@ int64_t HttpClient::onRecvHeader(const char *data, uint64_t len) {
...
@@ -156,6 +157,20 @@ int64_t HttpClient::onRecvHeader(const char *data, uint64_t len) {
_totalBodySize
=
atoll
(
_parser
[
"Content-Length"
].
data
());
_totalBodySize
=
atoll
(
_parser
[
"Content-Length"
].
data
());
}
}
if
(
_parser
[
"Transfer-Encoding"
]
==
"chunked"
){
//如果Transfer-Encoding字段等于chunked,则认为后续的content是不限制长度的
_totalBodySize
=
-
1
;
_chunkedSplitter
=
std
::
make_shared
<
HttpChunkedSplitter
>
([
this
](
const
char
*
data
,
uint64_t
len
){
if
(
len
>
0
){
auto
recvedBodySize
=
_recvedBodySize
+
len
;
onResponseBody
(
data
,
len
,
recvedBodySize
,
INT64_MAX
);
_recvedBodySize
=
recvedBodySize
;
}
else
{
onResponseCompleted_l
();
}
});
}
if
(
_totalBodySize
==
0
){
if
(
_totalBodySize
==
0
){
//后续没content,本次http请求结束
//后续没content,本次http请求结束
onResponseCompleted_l
();
onResponseCompleted_l
();
...
@@ -171,6 +186,10 @@ int64_t HttpClient::onRecvHeader(const char *data, uint64_t len) {
...
@@ -171,6 +186,10 @@ int64_t HttpClient::onRecvHeader(const char *data, uint64_t len) {
}
}
void
HttpClient
::
onRecvContent
(
const
char
*
data
,
uint64_t
len
)
{
void
HttpClient
::
onRecvContent
(
const
char
*
data
,
uint64_t
len
)
{
if
(
_chunkedSplitter
){
_chunkedSplitter
->
input
(
data
,
len
);
return
;
}
auto
recvedBodySize
=
_recvedBodySize
+
len
;
auto
recvedBodySize
=
_recvedBodySize
+
len
;
if
(
_totalBodySize
<
0
){
if
(
_totalBodySize
<
0
){
//不限长度的content,最大支持INT64_MAX个字节
//不限长度的content,最大支持INT64_MAX个字节
...
@@ -231,7 +250,6 @@ void HttpClient::onManager() {
...
@@ -231,7 +250,6 @@ void HttpClient::onManager() {
void
HttpClient
::
onResponseCompleted_l
()
{
void
HttpClient
::
onResponseCompleted_l
()
{
_totalBodySize
=
0
;
_totalBodySize
=
0
;
_recvedBodySize
=
0
;
_recvedBodySize
=
0
;
HttpRequestSplitter
::
reset
();
onResponseCompleted
();
onResponseCompleted
();
}
}
...
...
src/Http/HttpClient.h
查看文件 @
63ca646d
...
@@ -36,6 +36,7 @@
...
@@ -36,6 +36,7 @@
#include "Network/TcpClient.h"
#include "Network/TcpClient.h"
#include "HttpRequestSplitter.h"
#include "HttpRequestSplitter.h"
#include "HttpCookie.h"
#include "HttpCookie.h"
#include "HttpChunkedSplitter.h"
using
namespace
std
;
using
namespace
std
;
using
namespace
toolkit
;
using
namespace
toolkit
;
...
@@ -314,6 +315,7 @@ private:
...
@@ -314,6 +315,7 @@ private:
string
_lastHost
;
string
_lastHost
;
Ticker
_aliveTicker
;
Ticker
_aliveTicker
;
float
_fTimeOutSec
=
0
;
float
_fTimeOutSec
=
0
;
std
::
shared_ptr
<
HttpChunkedSplitter
>
_chunkedSplitter
;
};
};
}
/* namespace mediakit */
}
/* namespace mediakit */
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论