HttpRequestSplitter.h 2.24 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 12 13 14 15 16

#ifndef ZLMEDIAKIT_HTTPREQUESTSPLITTER_H
#define ZLMEDIAKIT_HTTPREQUESTSPLITTER_H

#include <string>
using namespace std;

xiongziliang committed
17
namespace mediakit {
18

19 20 21 22 23 24 25 26
class HttpRequestSplitter {
public:
    HttpRequestSplitter(){};
    virtual ~HttpRequestSplitter(){};

    /**
     * 添加数据
     * @param data 需要添加的数据
27
     * @param len 数据长度
28
     */
29
    virtual void input(const char *data,uint64_t len);
30 31 32
protected:
    /**
     * 收到请求头
33 34 35
     * @param data 请求头数据
     * @param len 请求头长度
     *
36
     * @return 请求头后的content长度,
37
     *  <0 : 代表后面所有数据都是content,此时后面的content将分段通过onRecvContent函数回调出去
38
     *  0 : 代表为后面数据还是请求头,
39
     *  >0 : 代表后面数据为固定长度content,此时将缓存content并等到所有content接收完毕一次性通过onRecvContent函数回调出去
40
     */
41
    virtual int64_t onRecvHeader(const char *data,uint64_t len) = 0;
42 43 44 45

    /**
     * 收到content分片或全部数据
     * onRecvHeader函数返回>0,则为全部数据
46 47
     * @param data content分片或全部数据
     * @param len 数据长度
48
     */
49
    virtual void onRecvContent(const char *data,uint64_t len) {};
50 51

    /**
52 53 54 55 56 57 58 59
     * 判断数据中是否有包尾
     * @param data 数据指针
     * @param len 数据长度
     * @return nullptr代表未找到包位,否则返回包尾指针
     */
    virtual const char *onSearchPacketTail(const char *data,int len);

    /**
60 61 62
     * 设置content len
     */
    void setContentLen(int64_t content_len);
63 64 65 66 67

    /**
     * 恢复初始设置
     */
     void reset();
68 69 70 71 72

     /**
      * 剩余数据大小
      */
     int64_t remainDataSize();
73 74 75
private:
    string _remain_data;
    int64_t _content_len = 0;
76
    int64_t _remain_data_size = 0;
77 78
};

xiongziliang committed
79
} /* namespace mediakit */
80 81

#endif //ZLMEDIAKIT_HTTPREQUESTSPLITTER_H