HttpSession.h 4.6 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * MIT License
xzl committed
3
 *
xiongziliang committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * Copyright (c) 2016 xiongziliang <771730766@qq.com>
 *
 * 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"
xzl committed
31
#include "Rtsp/Rtsp.h"
32
#include "Network/TcpSession.h"
33
#include "Network/TcpServer.h"
xiongziliang committed
34
#include "Rtmp/RtmpMediaSource.h"
35
#include "RtmpMuxer/FlvMuxer.h"
36
#include "HttpRequestSplitter.h"
37
#include "WebSocketSplitter.h"
xzl committed
38 39

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

xiongziliang committed
42
namespace mediakit {
xzl committed
43

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

xiongziliang committed
54
	HttpSession(const Socket::Ptr &pSock);
xzl committed
55 56
	virtual ~HttpSession();

57
	virtual void onRecv(const Buffer::Ptr &) override;
xzl committed
58 59
	virtual void onError(const SockException &err) override;
	virtual void onManager() override;
xiongziliang committed
60 61

	static string urlDecode(const string &str);
xzl committed
62
protected:
xiongziliang committed
63 64 65 66 67
	//FlvMuxer override
	void onWrite(const Buffer::Ptr &data) override ;
	void onWrite(const char *data,int len) override;
	void onDetach() override;
	std::shared_ptr<FlvMuxer> getSharedPtr() override;
68 69
	//HttpRequestSplitter override

70 71
	int64_t onRecvHeader(const char *data,uint64_t len) override;
	void onRecvContent(const char *data,uint64_t len) override;
72

73 74
	/**
	 * 重载之用于处理不定长度的content
xiongziliang committed
75
	 * 这个函数可用于处理大文件上传、http-flv推流
76
	 * @param header http请求头
77 78
	 * @param data content分片数据
	 * @param len content分片数据大小
79 80
	 * @param totalSize content总大小,如果为0则是不限长度content
	 * @param recvedSize 已收数据大小
81
	 */
82 83 84 85 86
	virtual void onRecvUnlimitedContent(const Parser &header,
										const char *data,
										uint64_t len,
										uint64_t totalSize,
										uint64_t recvedSize){
87 88 89 90
        WarnL << "content数据长度过大,无法处理,请重载HttpSession::onRecvUnlimitedContent";
        shutdown();
	}

91 92 93 94
    void onWebSocketDecodeHeader(const WebSocketHeader &packet) override{
        DebugL << "默认关闭WebSocket";
        shutdown();
    };
xiongziliang committed
95

96 97
	void onRecvWebSocketData(const Parser &header,const char *data,uint64_t len){
        WebSocketSplitter::decode((uint8_t *)data,len);
xiongziliang committed
98
    }
99
private:
100 101 102 103
	Parser _parser;
	string _strPath;
	Ticker _ticker;
	uint32_t _iReqCnt = 0;
xiongziliang committed
104
	//消耗的总流量
105
	uint64_t _ui64TotalBytes = 0;
xiongziliang committed
106
	//flv over http
107
    MediaInfo _mediaInfo;
108
    //处理content数据的callback
109
	function<bool (const char *data,uint64_t len) > _contentCallBack;
110 111 112
private:
	inline bool Handle_Req_GET(int64_t &content_len);
	inline bool Handle_Req_POST(int64_t &content_len);
xiongziliang committed
113
	inline bool checkLiveFlvStream();
xiongziliang committed
114
	inline bool checkWebSocket();
xiongziliang committed
115 116
	inline bool emitHttpEvent(bool doInvoke);
	inline void urlDecode(Parser &parser);
117
	inline bool makeMeun(const string &strFullPath,const string &vhost, string &strRet);
xzl committed
118 119
	inline void sendNotFound(bool bClose);
	inline void sendResponse(const char *pcStatus,const KeyValue &header,const string &strContent);
120
	inline static KeyValue makeHttpHeader(bool bClose=false,int64_t iContentSize=-1,const char *pcContentType="text/html");
xiongziliang committed
121 122 123
	void responseDelay(const string &Origin,bool bClose,
					   const string &codeOut,const KeyValue &headerOut,
					   const string &contentOut);
xzl committed
124 125
};

126

xiongziliang committed
127 128
typedef TcpSessionWithSSL<HttpSession> HttpsSession;

xiongziliang committed
129
} /* namespace mediakit */
xzl committed
130 131

#endif /* SRC_HTTP_HTTPSESSION_H_ */