mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2024-11-29 22:55:52 +08:00
优化http拆包器代码
This commit is contained in:
parent
b3f2bd5b71
commit
ae1d9371fa
@ -54,10 +54,12 @@ void HttpRequestSplitter::input(const char *data,uint64_t len) {
|
||||
|
||||
//数据按照请求头处理
|
||||
const char *index = nullptr;
|
||||
while (_content_len == 0 && (index = strstr(ptr,"\r\n\r\n")) != nullptr) {
|
||||
uint64_t remain = len;
|
||||
while (_content_len == 0 && remain > 0 && (index = onSearchPacketTail(ptr,remain)) != nullptr) {
|
||||
//_content_len == 0,这是请求头
|
||||
_content_len = onRecvHeader(ptr, index - ptr + 4);
|
||||
ptr = index + 4;
|
||||
_content_len = onRecvHeader(ptr, index - ptr);
|
||||
ptr = index;
|
||||
remain = len - (ptr - data);
|
||||
}
|
||||
|
||||
/*
|
||||
@ -65,7 +67,6 @@ void HttpRequestSplitter::input(const char *data,uint64_t len) {
|
||||
*/
|
||||
tail_ref = tail_tmp;
|
||||
|
||||
uint64_t remain = len - (ptr - data);
|
||||
if(remain <= 0){
|
||||
//没有剩余数据,清空缓存
|
||||
_remain_data.clear();
|
||||
@ -124,6 +125,14 @@ void HttpRequestSplitter::reset() {
|
||||
_remain_data.clear();
|
||||
}
|
||||
|
||||
const char *HttpRequestSplitter::onSearchPacketTail(const char *data,int len) {
|
||||
auto pos = strstr(data,"\r\n\r\n");
|
||||
if(pos == nullptr){
|
||||
return nullptr;
|
||||
}
|
||||
return pos + 4;
|
||||
}
|
||||
|
||||
|
||||
} /* namespace mediakit */
|
||||
|
||||
|
@ -42,7 +42,7 @@ public:
|
||||
* @param data 需要添加的数据
|
||||
* @param len 数据长度
|
||||
*/
|
||||
void input(const char *data,uint64_t len);
|
||||
virtual void input(const char *data,uint64_t len);
|
||||
protected:
|
||||
/**
|
||||
* 收到请求头
|
||||
@ -64,6 +64,14 @@ protected:
|
||||
*/
|
||||
virtual void onRecvContent(const char *data,uint64_t len) {};
|
||||
|
||||
/**
|
||||
* 判断数据中是否有包尾
|
||||
* @param data 数据指针
|
||||
* @param len 数据长度
|
||||
* @return nullptr代表未找到包位,否则返回包尾指针
|
||||
*/
|
||||
virtual const char *onSearchPacketTail(const char *data,int len);
|
||||
|
||||
/**
|
||||
* 设置content len
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user