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

#ifndef ZLMEDIAKIT_HTTPREQUESTSPLITTER_H
#define ZLMEDIAKIT_HTTPREQUESTSPLITTER_H

#include <string>
15
#include "Network/Buffer.h"
16
using namespace std;
17
using namespace toolkit;
18

xiongziliang committed
19
namespace mediakit {
20

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

    /**
     * 添加数据
     * @param data 需要添加的数据
29
     * @param len 数据长度
30
     */
31 32
    virtual void input(const char *data,size_t len);

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

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

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

    /**
63 64
     * 设置content len
     */
65
    void setContentLen(ssize_t content_len);
66 67 68 69 70

    /**
     * 恢复初始设置
     */
     void reset();
71 72 73 74

     /**
      * 剩余数据大小
      */
75 76
     size_t remainDataSize();

77
private:
78
    ssize_t _content_len = 0;
79
    size_t _remain_data_size = 0;
80
    BufferLikeString _remain_data;
81 82
};

xiongziliang committed
83
} /* namespace mediakit */
84 85

#endif //ZLMEDIAKIT_HTTPREQUESTSPLITTER_H