HttpSession.h 4.41 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
xiongziliang committed
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.
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"
xzl committed
22 23

using namespace std;
xiongziliang committed
24
using namespace toolkit;
xzl committed
25

xiongziliang committed
26
namespace mediakit {
xzl committed
27

28 29 30 31
class HttpSession: public TcpSession,
                   public FlvMuxer,
                   public HttpRequestSplitter,
                   public WebSocketSplitter {
xzl committed
32
public:
33 34 35 36 37 38 39 40 41 42 43
    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
44
    ~HttpSession() override;
45

xiongziliang committed
46 47 48
    void onRecv(const Buffer::Ptr &) override;
    void onError(const SockException &err) override;
    void onManager() override;
49
    static string urlDecode(const string &str);
xzl committed
50
protected:
51
    //FlvMuxer override
xiongziliang committed
52
    void onWrite(const Buffer::Ptr &data, bool flush) override ;
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    void onDetach() override;
    std::shared_ptr<FlvMuxer> getSharedPtr() override;

    //HttpRequestSplitter override
    int64_t onRecvHeader(const char *data,uint64_t len) override;
    void onRecvContent(const char *data,uint64_t len) override;

    /**
     * 重载之用于处理不定长度的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,
                                        uint64_t len,
                                        uint64_t totalSize,
                                        uint64_t recvedSize){
xiongziliang committed
74
        shutdown(SockException(Err_shutdown,"http post content is too huge,default closed"));
75
    }
76

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

87 88
    //WebSocketSplitter override
    /**
89 90 91
     * 发送数据进行websocket协议打包后回调
     * @param buffer websocket协议数据
     */
92
    void onWebSocketEncodeData(const Buffer::Ptr &buffer) override;
93
private:
94
    void Handle_Req_GET(int64_t &content_len);
95 96 97
    void Handle_Req_GET_l(int64_t &content_len, bool sendBody);
    void Handle_Req_POST(int64_t &content_len);
    void Handle_Req_HEAD(int64_t &content_len);
98 99

    bool checkLiveFlvStream(const function<void()> &cb = nullptr);
100 101 102 103 104 105
    bool checkWebSocket();
    bool emitHttpEvent(bool doInvoke);
    void urlDecode(Parser &parser);
    void sendNotFound(bool bClose);
    void sendResponse(const char *pcStatus, bool bClose, const char *pcContentType = nullptr,
                      const HttpSession::KeyValue &header = HttpSession::KeyValue(),
106
                      const HttpBody::Ptr &body = nullptr,bool is_http_flv = false);
107

108 109
    //设置socket标志
    void setSocketFlags();
110
private:
111
    string _origin;
112 113 114 115 116 117 118 119
    Parser _parser;
    Ticker _ticker;
    //消耗的总流量
    uint64_t _ui64TotalBytes = 0;
    //flv over http
    MediaInfo _mediaInfo;
    //处理content数据的callback
    function<bool (const char *data,uint64_t len) > _contentCallBack;
120 121
    bool _flv_over_websocket = false;
    bool _is_flv_stream = false;
xzl committed
122 123
};

124

xiongziliang committed
125 126
typedef TcpSessionWithSSL<HttpSession> HttpsSession;

xiongziliang committed
127
} /* namespace mediakit */
xzl committed
128 129

#endif /* SRC_HTTP_HTTPSESSION_H_ */