HttpRequestSplitter.cpp 3.64 KB
Newer Older
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
3 4 5
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
xiongziliang committed
6 7 8
 * 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.
9 10
 */

11
#include "HttpRequestSplitter.h"
12 13
#include "Util/logger.h"
#include "Util/util.h"
xiongziliang committed
14
using namespace toolkit;
15

xiongziliang committed
16
namespace mediakit {
17

18 19 20 21 22 23
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();
24 25
    }

xiongziliang committed
26
    splitPacket:
27

28 29 30 31 32 33 34 35 36 37
    /*确保ptr最后一个字节是0,防止strstr越界
     *由于ZLToolKit确保内存最后一个字节是保留未使用字节并置0,
     *所以此处可以不用再次置0
     *但是上层数据可能来自其他渠道,保险起见还是置0
     */

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

38
    //数据按照请求头处理
xiongziliang committed
39
    const char *index = nullptr;
40 41
    _remain_data_size = len;
    while (_content_len == 0 && _remain_data_size > 0 && (index = onSearchPacketTail(ptr,_remain_data_size)) != nullptr) {
42
        //_content_len == 0,这是请求头
43 44
        const char *header_ptr = ptr;
        int64_t header_size = index - ptr;
45
        ptr = index;
46 47
        _remain_data_size = len - (ptr - data);
        _content_len = onRecvHeader(header_ptr, header_size);
48 49
    }

50
    if(_remain_data_size <= 0){
51 52
        //没有剩余数据,清空缓存
        _remain_data.clear();
xiongziliang committed
53 54 55
        return;
    }

56 57 58 59 60 61
    /*
     * 恢复末尾字节
     * 移动到这来,目的是防止HttpRequestSplitter::reset()导致内存失效
     */
    tail_ref = tail_tmp;

62 63
    if(_content_len == 0){
        //尚未找到http头,缓存定位到剩余数据部分
64
        string str(ptr,_remain_data_size);
xiongziliang committed
65
        _remain_data = str;
66 67 68 69
        return;
    }

    //已经找到http头了
70 71
    if(_content_len > 0){
        //数据按照固定长度content处理
72
        if(_remain_data_size < _content_len){
73
            //数据不够,缓存定位到剩余数据部分
74
            string str(ptr,_remain_data_size);
xiongziliang committed
75
            _remain_data = str;
76 77 78
            return;
        }
        //收到content数据,并且接受content完毕
79 80
        onRecvContent(ptr,_content_len);

81
        _remain_data_size -= _content_len;
82
        ptr += _content_len;
83 84 85
        //content处理完毕,后面数据当做请求头处理
        _content_len = 0;

86
        if(_remain_data_size > 0){
87
            //还有数据没有处理完毕
88
            string str(ptr,_remain_data_size);
xiongziliang committed
89
            _remain_data = str;
90 91 92

            data = ptr = (char *)_remain_data.data();
            len = _remain_data.size();
93 94
            goto splitPacket;
        }
xiongziliang committed
95
        _remain_data.clear();
96
        return;
97
    }
98 99 100


    //_content_len < 0;数据按照不固定长度content处理
101
    onRecvContent(ptr,_remain_data_size);//消费掉所有剩余数据
102
    _remain_data.clear();
103 104 105 106 107
}

void HttpRequestSplitter::setContentLen(int64_t content_len) {
    _content_len = content_len;
}
108 109 110

void HttpRequestSplitter::reset() {
    _content_len = 0;
111
    _remain_data_size = 0;
112 113
    _remain_data.clear();
}
114

115 116 117 118 119 120 121 122
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;
}

123 124 125 126
int64_t HttpRequestSplitter::remainDataSize() {
    return _remain_data_size;
}

127

xiongziliang committed
128
} /* namespace mediakit */
129