2018-09-20 17:50:58 +08:00
|
|
|
|
//
|
|
|
|
|
// Created by xzl on 2018/9/20.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#include "HttpRequestSplitter.h"
|
2018-09-21 16:11:09 +08:00
|
|
|
|
#include "Util/logger.h"
|
|
|
|
|
#include "Util/util.h"
|
|
|
|
|
using namespace ZL::Util;
|
2018-09-20 17:50:58 +08:00
|
|
|
|
|
2018-09-23 00:55:00 +08:00
|
|
|
|
void HttpRequestSplitter::input(const char *data,uint64_t len) {
|
|
|
|
|
const char *ptr = data;
|
|
|
|
|
if(!_remain_data.empty()){
|
|
|
|
|
_remain_data.append(data,len);
|
|
|
|
|
data = ptr = _remain_data.data();
|
|
|
|
|
len = _remain_data.size();
|
2018-09-20 17:50:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
splitPacket:
|
|
|
|
|
|
|
|
|
|
//数据按照请求头处理
|
2018-09-23 00:55:00 +08:00
|
|
|
|
char *index = nullptr;
|
|
|
|
|
while (_content_len == 0 && (index = strstr(ptr,"\r\n\r\n")) != nullptr) {
|
2018-09-20 17:50:58 +08:00
|
|
|
|
//_content_len == 0,这是请求头
|
2018-09-23 00:55:00 +08:00
|
|
|
|
_content_len = onRecvHeader(ptr, index - ptr + 4);
|
|
|
|
|
ptr = index + 4;
|
2018-09-20 17:50:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-23 00:55:00 +08:00
|
|
|
|
uint64_t remain = len - (ptr - data);
|
|
|
|
|
if(remain <= 0){
|
|
|
|
|
//没有剩余数据,清空缓存
|
|
|
|
|
_remain_data.clear();
|
2018-09-20 18:20:43 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-23 00:55:00 +08:00
|
|
|
|
if(_content_len == 0){
|
|
|
|
|
//尚未找到http头,缓存定位到剩余数据部分
|
|
|
|
|
_remain_data.assign(ptr,remain);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//已经找到http头了
|
2018-09-20 17:50:58 +08:00
|
|
|
|
if(_content_len > 0){
|
|
|
|
|
//数据按照固定长度content处理
|
2018-09-23 00:55:00 +08:00
|
|
|
|
if(remain < _content_len){
|
|
|
|
|
//数据不够,缓存定位到剩余数据部分
|
|
|
|
|
_remain_data.assign(ptr,remain);
|
2018-09-20 17:50:58 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
//收到content数据,并且接受content完毕
|
2018-09-23 00:55:00 +08:00
|
|
|
|
onRecvContent(ptr,_content_len);
|
|
|
|
|
|
|
|
|
|
remain -= _content_len;
|
|
|
|
|
ptr += _content_len;
|
2018-09-20 17:50:58 +08:00
|
|
|
|
//content处理完毕,后面数据当做请求头处理
|
|
|
|
|
_content_len = 0;
|
|
|
|
|
|
2018-09-23 00:55:00 +08:00
|
|
|
|
if(remain > 0){
|
2018-09-20 17:50:58 +08:00
|
|
|
|
//还有数据没有处理完毕
|
2018-09-23 00:55:00 +08:00
|
|
|
|
_remain_data.assign(ptr,remain);
|
|
|
|
|
|
|
|
|
|
data = ptr = (char *)_remain_data.data();
|
|
|
|
|
len = _remain_data.size();
|
2018-09-20 17:50:58 +08:00
|
|
|
|
goto splitPacket;
|
|
|
|
|
}
|
2018-09-23 00:55:00 +08:00
|
|
|
|
return;
|
2018-09-20 17:50:58 +08:00
|
|
|
|
}
|
2018-09-23 00:55:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//_content_len < 0;数据按照不固定长度content处理
|
|
|
|
|
onRecvContent(ptr,remain);//消费掉所有剩余数据
|
|
|
|
|
_remain_data.clear();
|
2018-09-21 09:41:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HttpRequestSplitter::setContentLen(int64_t content_len) {
|
|
|
|
|
_content_len = content_len;
|
|
|
|
|
}
|