HttpSession.h 6.03 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * MIT License
xzl committed
3
 *
xiongziliang committed
4
 * Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
xiongziliang committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
xzl committed
25 26 27 28
 */
#ifndef SRC_HTTP_HTTPSESSION_H_
#define SRC_HTTP_HTTPSESSION_H_

xiongzilaing committed
29
#include <functional>
xiongziliang committed
30
#include "Common/config.h"
xiongziliang committed
31
#include "Common/Parser.h"
32
#include "Network/TcpSession.h"
33
#include "Network/TcpServer.h"
xiongziliang committed
34
#include "Rtmp/RtmpMediaSource.h"
xiongziliang committed
35
#include "Rtmp/FlvMuxer.h"
36
#include "HttpRequestSplitter.h"
37
#include "WebSocketSplitter.h"
38
#include "HttpCookieManager.h"
xzl committed
39 40

using namespace std;
xiongziliang committed
41
using namespace toolkit;
xzl committed
42

xiongziliang committed
43
namespace mediakit {
xzl committed
44

45 46 47 48
class HttpSession: public TcpSession,
                   public FlvMuxer,
                   public HttpRequestSplitter,
                   public WebSocketSplitter {
xzl committed
49
public:
xiongziliang committed
50
	typedef StrCaseMap KeyValue;
51 52 53 54
	typedef std::function<void(const string &codeOut,
							   const KeyValue &headerOut,
							   const string &contentOut)>  HttpResponseInvoker;

55 56 57 58 59 60
	/**
	 * @param errMsg 如果为空,则代表鉴权通过,否则为错误提示
	 * @param accessPath 运行或禁止访问的根目录
	 * @param cookieLifeSecond 鉴权cookie有效期
	 **/
	typedef std::function<void(const string &errMsg,const string &accessPath, int cookieLifeSecond)> HttpAccessPathInvoker;
61

xiongziliang committed
62
	HttpSession(const Socket::Ptr &pSock);
xzl committed
63 64
	virtual ~HttpSession();

65
	virtual void onRecv(const Buffer::Ptr &) override;
xzl committed
66 67
	virtual void onError(const SockException &err) override;
	virtual void onManager() override;
xiongziliang committed
68 69

	static string urlDecode(const string &str);
xzl committed
70
protected:
xiongziliang committed
71 72 73 74
	//FlvMuxer override
	void onWrite(const Buffer::Ptr &data) override ;
	void onDetach() override;
	std::shared_ptr<FlvMuxer> getSharedPtr() override;
75

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

80 81
	/**
	 * 重载之用于处理不定长度的content
xiongziliang committed
82
	 * 这个函数可用于处理大文件上传、http-flv推流
83
	 * @param header http请求头
84 85
	 * @param data content分片数据
	 * @param len content分片数据大小
86 87
	 * @param totalSize content总大小,如果为0则是不限长度content
	 * @param recvedSize 已收数据大小
88
	 */
89 90 91 92 93
	virtual void onRecvUnlimitedContent(const Parser &header,
										const char *data,
										uint64_t len,
										uint64_t totalSize,
										uint64_t recvedSize){
xiongziliang committed
94
        shutdown(SockException(Err_shutdown,"http post content is too huge,default closed"));
95 96
	}

97 98 99 100 101 102 103 104
	/**
     * websocket客户端连接上事件
     * @param header http头
     * @return true代表允许websocket连接,否则拒绝
     */
    virtual bool onWebSocketConnect(const Parser &header){
        WarnL << "http server do not support websocket default";
        return false;
xiongziliang committed
105
    }
106

107
	//WebSocketSplitter override
108
	/**
109 110 111
     * 发送数据进行websocket协议打包后回调
     * @param buffer websocket协议数据
     */
112
	void onWebSocketEncodeData(const Buffer::Ptr &buffer) override;
113
private:
114 115 116 117 118 119 120 121 122
	void Handle_Req_GET(int64_t &content_len);
	void Handle_Req_POST(int64_t &content_len);
	bool checkLiveFlvStream(const function<void()> &cb = nullptr);
	bool checkWebSocket();
	bool emitHttpEvent(bool doInvoke);
	void urlDecode(Parser &parser);
	void sendNotFound(bool bClose);
	void sendResponse(const char *pcStatus,const KeyValue &header,const string &strContent);
	KeyValue makeHttpHeader(bool bClose=false,int64_t iContentSize=-1,const char *pcContentType="text/html");
xiongziliang committed
123
    void responseDelay(bool bClose,
124 125 126
                       const string &codeOut,
                       const KeyValue &headerOut,
                       const string &contentOut);
127 128

    /**
129 130 131 132 133 134 135
     * 判断http客户端是否有权限访问文件的逻辑步骤
     *
     * 1、根据http请求头查找cookie,找到进入步骤3
     * 2、根据http url参数(如果没有根据ip+端口号)查找cookie,如果还是未找到cookie则进入步骤5
     * 3、cookie标记是否有权限访问文件,如果有权限,直接返回文件
     * 4、cookie中记录的url参数是否跟本次url参数一致,如果一致直接返回客户端错误码
     * 5、触发kBroadcastHttpAccess事件
136 137 138 139
     * @param path 文件或目录
     * @param is_dir path是否为目录
     * @param callback 有权限或无权限的回调
     */
140
	void canAccessPath(const string &path,bool is_dir,const function<void(const string &errMsg,const HttpServerCookie::Ptr &cookie)> &callback);
141

142 143 144 145 146
    /**
     * 获取用户唯一识别id
     * 有url参数返回参数,无参数返回ip+端口号
     * @return
     */
147
	string getClientUid();
148 149 150

	//设置socket标志
	void setSocketFlags();
151
private:
xiongziliang committed
152
	string _origin;
153 154 155 156 157 158 159 160 161
    Parser _parser;
    Ticker _ticker;
    uint32_t _iReqCnt = 0;
    //消耗的总流量
    uint64_t _ui64TotalBytes = 0;
    //flv over http
    MediaInfo _mediaInfo;
    //处理content数据的callback
    function<bool (const char *data,uint64_t len) > _contentCallBack;
162
	bool _flv_over_websocket = false;
xzl committed
163 164
};

165

xiongziliang committed
166 167
typedef TcpSessionWithSSL<HttpSession> HttpsSession;

xiongziliang committed
168
} /* namespace mediakit */
xzl committed
169 170

#endif /* SRC_HTTP_HTTPSESSION_H_ */