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

xzl committed
11 12 13
#ifndef SRC_HTTP_HTTPSESSION_H_
#define SRC_HTTP_HTTPSESSION_H_

xiongzilaing committed
14
#include <functional>
15
#include "Network/TcpSession.h"
xiongziliang committed
16
#include "Rtmp/RtmpMediaSource.h"
xiongziliang committed
17
#include "Rtmp/FlvMuxer.h"
18
#include "HttpRequestSplitter.h"
19
#include "WebSocketSplitter.h"
20
#include "HttpCookieManager.h"
21
#include "HttpFileManager.h"
22
#include "TS/TSMediaSource.h"
23
#include "FMP4/FMP4MediaSource.h"
xzl committed
24 25

using namespace std;
xiongziliang committed
26
using namespace toolkit;
xzl committed
27

xiongziliang committed
28
namespace mediakit {
xzl committed
29

30 31 32 33
class HttpSession: public TcpSession,
                   public FlvMuxer,
                   public HttpRequestSplitter,
                   public WebSocketSplitter {
xzl committed
34
public:
35 36 37 38 39 40 41 42 43 44 45
    typedef StrCaseMap KeyValue;
    typedef HttpResponseInvokerImp HttpResponseInvoker;
    friend class AsyncSender;
    /**
     * @param errMsg 如果为空,则代表鉴权通过,否则为错误提示
     * @param accessPath 运行或禁止访问的根目录
     * @param cookieLifeSecond 鉴权cookie有效期
     **/
    typedef std::function<void(const string &errMsg,const string &accessPath, int cookieLifeSecond)> HttpAccessPathInvoker;

    HttpSession(const Socket::Ptr &pSock);
xiongziliang committed
46
    ~HttpSession() override;
47

xiongziliang committed
48 49 50
    void onRecv(const Buffer::Ptr &) override;
    void onError(const SockException &err) override;
    void onManager() override;
51
    static string urlDecode(const string &str);
52

xzl committed
53
protected:
54
    //FlvMuxer override
xiongziliang committed
55
    void onWrite(const Buffer::Ptr &data, bool flush) override ;
56 57 58 59
    void onDetach() override;
    std::shared_ptr<FlvMuxer> getSharedPtr() override;

    //HttpRequestSplitter override
60
    ssize_t onRecvHeader(const char *data,size_t len) override;
61
    void onRecvContent(const char *data,size_t len) override;
62 63 64 65 66 67 68 69 70 71 72 73

    /**
     * 重载之用于处理不定长度的content
     * 这个函数可用于处理大文件上传、http-flv推流
     * @param header http请求头
     * @param data content分片数据
     * @param len content分片数据大小
     * @param totalSize content总大小,如果为0则是不限长度content
     * @param recvedSize 已收数据大小
     */
    virtual void onRecvUnlimitedContent(const Parser &header,
                                        const char *data,
74 75 76
                                        size_t len,
                                        size_t totalSize,
                                        size_t recvedSize){
xiongziliang committed
77
        shutdown(SockException(Err_shutdown,"http post content is too huge,default closed"));
78
    }
79

80
    /**
81 82 83 84 85
     * websocket客户端连接上事件
     * @param header http头
     * @return true代表允许websocket连接,否则拒绝
     */
    virtual bool onWebSocketConnect(const Parser &header){
xiongziliang committed
86
        WarnP(this) << "http server do not support websocket default";
87
        return false;
xiongziliang committed
88
    }
89

90 91
    //WebSocketSplitter override
    /**
92 93 94
     * 发送数据进行websocket协议打包后回调
     * @param buffer websocket协议数据
     */
95
    void onWebSocketEncodeData(Buffer::Ptr buffer) override;
96 97 98 99 100 101 102

    /**
     * 接收到完整的一个webSocket数据包后回调
     * @param header 数据包包头
     */
    void onWebSocketDecodeComplete(const WebSocketHeader &header_in) override;

103
private:
104 105 106 107
    void Handle_Req_GET(ssize_t &content_len);
    void Handle_Req_GET_l(ssize_t &content_len, bool sendBody);
    void Handle_Req_POST(ssize_t &content_len);
    void Handle_Req_HEAD(ssize_t &content_len);
108

109 110 111 112
    bool checkLiveStream(const string &schema, const string  &url_suffix, const function<void(const MediaSource::Ptr &src)> &cb);

    bool checkLiveStreamFlv(const function<void()> &cb = nullptr);
    bool checkLiveStreamTS(const function<void()> &cb = nullptr);
113
    bool checkLiveStreamFMP4(const function<void()> &fmp4_list = nullptr);
114

115 116 117 118
    bool checkWebSocket();
    bool emitHttpEvent(bool doInvoke);
    void urlDecode(Parser &parser);
    void sendNotFound(bool bClose);
119
    void sendResponse(int code, bool bClose, const char *pcContentType = nullptr,
120
                      const HttpSession::KeyValue &header = HttpSession::KeyValue(),
121
                      const HttpBody::Ptr &body = nullptr, bool no_content_length = false);
122

123 124
    //设置socket标志
    void setSocketFlags();
125

126
private:
127 128 129 130
    bool _is_live_stream = false;
    bool _live_over_websocket = false;
    //消耗的总流量
    uint64_t _total_bytes_usage = 0;
131
    string _origin;
132 133 134
    Parser _parser;
    Ticker _ticker;
    MediaInfo _mediaInfo;
135
    TSMediaSource::RingType::RingReader::Ptr _ts_reader;
136
    FMP4MediaSource::RingType::RingReader::Ptr _fmp4_reader;
137
    //处理content数据的callback
138
    function<bool (const char *data,size_t len) > _contentCallBack;
xzl committed
139 140
};

141

xiongziliang committed
142 143
typedef TcpSessionWithSSL<HttpSession> HttpsSession;

xiongziliang committed
144
} /* namespace mediakit */
xzl committed
145 146

#endif /* SRC_HTTP_HTTPSESSION_H_ */