HttpRequestSplitter.cpp 4.46 KB
Newer Older
1 2 3
/*
 * MIT License
 *
xiongziliang committed
4
 * Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * 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.
 */

27 28

#include "HttpRequestSplitter.h"
29 30
#include "Util/logger.h"
#include "Util/util.h"
xiongziliang committed
31
using namespace toolkit;
32

xiongziliang committed
33
namespace mediakit {
34

35 36 37 38 39 40
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();
41 42
    }

xiongziliang committed
43
    splitPacket:
44

45 46 47 48 49 50 51 52 53 54
    /*确保ptr最后一个字节是0,防止strstr越界
     *由于ZLToolKit确保内存最后一个字节是保留未使用字节并置0,
     *所以此处可以不用再次置0
     *但是上层数据可能来自其他渠道,保险起见还是置0
     */

    char &tail_ref = ((char *) ptr)[len];
    char tail_tmp = tail_ref;
    tail_ref = 0;

55
    //数据按照请求头处理
xiongziliang committed
56
    const char *index = nullptr;
57 58
    _remain_data_size = len;
    while (_content_len == 0 && _remain_data_size > 0 && (index = onSearchPacketTail(ptr,_remain_data_size)) != nullptr) {
59
        //_content_len == 0,这是请求头
60 61
        const char *header_ptr = ptr;
        int64_t header_size = index - ptr;
62
        ptr = index;
63 64
        _remain_data_size = len - (ptr - data);
        _content_len = onRecvHeader(header_ptr, header_size);
65 66
    }

67
    if(_remain_data_size <= 0){
68 69
        //没有剩余数据,清空缓存
        _remain_data.clear();
xiongziliang committed
70 71 72
        return;
    }

73 74 75 76 77 78
    /*
     * 恢复末尾字节
     * 移动到这来,目的是防止HttpRequestSplitter::reset()导致内存失效
     */
    tail_ref = tail_tmp;

79 80
    if(_content_len == 0){
        //尚未找到http头,缓存定位到剩余数据部分
81
        string str(ptr,_remain_data_size);
xiongziliang committed
82
        _remain_data = str;
83 84 85 86
        return;
    }

    //已经找到http头了
87 88
    if(_content_len > 0){
        //数据按照固定长度content处理
89
        if(_remain_data_size < _content_len){
90
            //数据不够,缓存定位到剩余数据部分
91
            string str(ptr,_remain_data_size);
xiongziliang committed
92
            _remain_data = str;
93 94 95
            return;
        }
        //收到content数据,并且接受content完毕
96 97
        onRecvContent(ptr,_content_len);

98
        _remain_data_size -= _content_len;
99
        ptr += _content_len;
100 101 102
        //content处理完毕,后面数据当做请求头处理
        _content_len = 0;

103
        if(_remain_data_size > 0){
104
            //还有数据没有处理完毕
105
            string str(ptr,_remain_data_size);
xiongziliang committed
106
            _remain_data = str;
107 108 109

            data = ptr = (char *)_remain_data.data();
            len = _remain_data.size();
110 111
            goto splitPacket;
        }
xiongziliang committed
112
        _remain_data.clear();
113
        return;
114
    }
115 116 117


    //_content_len < 0;数据按照不固定长度content处理
118
    onRecvContent(ptr,_remain_data_size);//消费掉所有剩余数据
119
    _remain_data.clear();
120 121 122 123 124
}

void HttpRequestSplitter::setContentLen(int64_t content_len) {
    _content_len = content_len;
}
125 126 127

void HttpRequestSplitter::reset() {
    _content_len = 0;
128
    _remain_data_size = 0;
129 130
    _remain_data.clear();
}
131

132 133 134 135 136 137 138 139
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;
}

140 141 142 143
int64_t HttpRequestSplitter::remainDataSize() {
    return _remain_data_size;
}

144

xiongziliang committed
145
} /* namespace mediakit */
146