优化http拆包器代码

This commit is contained in:
xiongziliang 2018-10-30 09:35:19 +08:00
parent b3f2bd5b71
commit ae1d9371fa
2 changed files with 22 additions and 5 deletions

View File

@ -54,10 +54,12 @@ void HttpRequestSplitter::input(const char *data,uint64_t len) {
//数据按照请求头处理 //数据按照请求头处理
const char *index = nullptr; 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 == 0这是请求头
_content_len = onRecvHeader(ptr, index - ptr + 4); _content_len = onRecvHeader(ptr, index - ptr);
ptr = index + 4; ptr = index;
remain = len - (ptr - data);
} }
/* /*
@ -65,7 +67,6 @@ void HttpRequestSplitter::input(const char *data,uint64_t len) {
*/ */
tail_ref = tail_tmp; tail_ref = tail_tmp;
uint64_t remain = len - (ptr - data);
if(remain <= 0){ if(remain <= 0){
//没有剩余数据,清空缓存 //没有剩余数据,清空缓存
_remain_data.clear(); _remain_data.clear();
@ -124,6 +125,14 @@ void HttpRequestSplitter::reset() {
_remain_data.clear(); _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 */ } /* namespace mediakit */

View File

@ -42,7 +42,7 @@ public:
* @param data * @param data
* @param len * @param len
*/ */
void input(const char *data,uint64_t len); virtual void input(const char *data,uint64_t len);
protected: protected:
/** /**
* *
@ -64,6 +64,14 @@ protected:
*/ */
virtual void onRecvContent(const char *data,uint64_t len) {}; 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 * content len
*/ */