2018-09-25 09:55:41 +08:00
|
|
|
|
/*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
2018-09-25 09:55:41 +08:00
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
2018-09-25 09:55:41 +08:00
|
|
|
|
*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Use of this source code is governed by MIT license that can be found in the
|
|
|
|
|
* LICENSE file in the root of the source tree. All contributing project authors
|
|
|
|
|
* may be found in the AUTHORS file in the root of the source tree.
|
2018-09-25 09:55:41 +08:00
|
|
|
|
*/
|
|
|
|
|
|
2018-09-20 17:50:58 +08:00
|
|
|
|
#include "HttpRequestSplitter.h"
|
2018-09-21 16:11:09 +08:00
|
|
|
|
#include "Util/logger.h"
|
|
|
|
|
#include "Util/util.h"
|
2018-10-24 17:17:55 +08:00
|
|
|
|
using namespace toolkit;
|
2018-09-20 17:50:58 +08:00
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
namespace mediakit {
|
2018-09-25 09:55:41 +08:00
|
|
|
|
|
2021-01-17 18:31:50 +08:00
|
|
|
|
void HttpRequestSplitter::input(const char *data,size_t len) {
|
2018-09-23 00:55:00 +08:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2018-09-28 21:16:39 +08:00
|
|
|
|
splitPacket:
|
2018-09-20 17:50:58 +08:00
|
|
|
|
|
2018-09-26 23:12:03 +08:00
|
|
|
|
/*确保ptr最后一个字节是0,防止strstr越界
|
|
|
|
|
*由于ZLToolKit确保内存最后一个字节是保留未使用字节并置0,
|
|
|
|
|
*所以此处可以不用再次置0
|
|
|
|
|
*但是上层数据可能来自其他渠道,保险起见还是置0
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
char &tail_ref = ((char *) ptr)[len];
|
|
|
|
|
char tail_tmp = tail_ref;
|
|
|
|
|
tail_ref = 0;
|
|
|
|
|
|
2018-09-20 17:50:58 +08:00
|
|
|
|
//数据按照请求头处理
|
2018-09-23 14:49:49 +08:00
|
|
|
|
const char *index = nullptr;
|
2018-12-14 14:59:12 +08:00
|
|
|
|
_remain_data_size = len;
|
|
|
|
|
while (_content_len == 0 && _remain_data_size > 0 && (index = onSearchPacketTail(ptr,_remain_data_size)) != nullptr) {
|
2020-11-01 03:41:35 +08:00
|
|
|
|
if(index == ptr){
|
|
|
|
|
break;
|
|
|
|
|
}
|
2018-09-20 17:50:58 +08:00
|
|
|
|
//_content_len == 0,这是请求头
|
2018-12-14 14:59:12 +08:00
|
|
|
|
const char *header_ptr = ptr;
|
2021-01-17 18:31:50 +08:00
|
|
|
|
auto header_size = index - ptr;
|
2018-10-30 09:35:19 +08:00
|
|
|
|
ptr = index;
|
2018-12-14 14:59:12 +08:00
|
|
|
|
_remain_data_size = len - (ptr - data);
|
|
|
|
|
_content_len = onRecvHeader(header_ptr, header_size);
|
2018-09-20 17:50:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-14 14:59:12 +08:00
|
|
|
|
if(_remain_data_size <= 0){
|
2018-09-23 00:55:00 +08:00
|
|
|
|
//没有剩余数据,清空缓存
|
|
|
|
|
_remain_data.clear();
|
2018-09-20 18:20:43 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-01 09:53:58 +08:00
|
|
|
|
/*
|
|
|
|
|
* 恢复末尾字节
|
|
|
|
|
* 移动到这来,目的是防止HttpRequestSplitter::reset()导致内存失效
|
|
|
|
|
*/
|
|
|
|
|
tail_ref = tail_tmp;
|
|
|
|
|
|
2018-09-23 00:55:00 +08:00
|
|
|
|
if(_content_len == 0){
|
|
|
|
|
//尚未找到http头,缓存定位到剩余数据部分
|
2020-11-01 03:41:35 +08:00
|
|
|
|
_remain_data.assign(ptr,_remain_data_size);
|
2018-09-23 00:55:00 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//已经找到http头了
|
2018-09-20 17:50:58 +08:00
|
|
|
|
if(_content_len > 0){
|
|
|
|
|
//数据按照固定长度content处理
|
2018-12-14 14:59:12 +08:00
|
|
|
|
if(_remain_data_size < _content_len){
|
2018-09-23 00:55:00 +08:00
|
|
|
|
//数据不够,缓存定位到剩余数据部分
|
2020-11-01 03:41:35 +08:00
|
|
|
|
_remain_data.assign(ptr, _remain_data_size);
|
2018-09-20 17:50:58 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
//收到content数据,并且接受content完毕
|
2018-09-23 00:55:00 +08:00
|
|
|
|
onRecvContent(ptr,_content_len);
|
|
|
|
|
|
2018-12-14 14:59:12 +08:00
|
|
|
|
_remain_data_size -= _content_len;
|
2018-09-23 00:55:00 +08:00
|
|
|
|
ptr += _content_len;
|
2018-09-20 17:50:58 +08:00
|
|
|
|
//content处理完毕,后面数据当做请求头处理
|
|
|
|
|
_content_len = 0;
|
|
|
|
|
|
2018-12-14 14:59:12 +08:00
|
|
|
|
if(_remain_data_size > 0){
|
2018-09-20 17:50:58 +08:00
|
|
|
|
//还有数据没有处理完毕
|
2020-11-01 03:41:35 +08:00
|
|
|
|
_remain_data.assign(ptr,_remain_data_size);
|
2018-09-23 00:55:00 +08:00
|
|
|
|
data = ptr = (char *)_remain_data.data();
|
|
|
|
|
len = _remain_data.size();
|
2018-09-20 17:50:58 +08:00
|
|
|
|
goto splitPacket;
|
|
|
|
|
}
|
2018-09-28 21:16:39 +08:00
|
|
|
|
_remain_data.clear();
|
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处理
|
2018-12-14 14:59:12 +08:00
|
|
|
|
onRecvContent(ptr,_remain_data_size);//消费掉所有剩余数据
|
2018-09-23 00:55:00 +08:00
|
|
|
|
_remain_data.clear();
|
2018-09-21 09:41:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-17 18:31:50 +08:00
|
|
|
|
void HttpRequestSplitter::setContentLen(size_t content_len) {
|
2018-09-21 09:41:40 +08:00
|
|
|
|
_content_len = content_len;
|
|
|
|
|
}
|
2018-09-23 21:10:17 +08:00
|
|
|
|
|
|
|
|
|
void HttpRequestSplitter::reset() {
|
|
|
|
|
_content_len = 0;
|
2019-01-03 15:05:52 +08:00
|
|
|
|
_remain_data_size = 0;
|
2018-09-23 21:10:17 +08:00
|
|
|
|
_remain_data.clear();
|
|
|
|
|
}
|
2018-09-25 09:55:41 +08:00
|
|
|
|
|
2021-01-17 18:31:50 +08:00
|
|
|
|
const char *HttpRequestSplitter::onSearchPacketTail(const char *data,size_t len) {
|
2018-10-30 09:35:19 +08:00
|
|
|
|
auto pos = strstr(data,"\r\n\r\n");
|
|
|
|
|
if(pos == nullptr){
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
return pos + 4;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-17 18:31:50 +08:00
|
|
|
|
size_t HttpRequestSplitter::remainDataSize() {
|
2018-12-14 14:59:12 +08:00
|
|
|
|
return _remain_data_size;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-25 09:55:41 +08:00
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
} /* namespace mediakit */
|
2018-09-25 09:55:41 +08:00
|
|
|
|
|