HttpSession.h 3.56 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"
xiongziliang committed
33
#include "Rtmp/RtmpMediaSource.h"
xzl committed
34 35

using namespace std;
xiongziliang committed
36
using namespace ZL::Rtmp;
xzl committed
37 38 39 40 41
using namespace ZL::Network;

namespace ZL {
namespace Http {

42

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

xzl committed
50 51 52
	HttpSession(const std::shared_ptr<ThreadPool> &pTh, const Socket::Ptr &pSock);
	virtual ~HttpSession();

53
	virtual void onRecv(const Buffer::Ptr &) override;
xzl committed
54 55
	virtual void onError(const SockException &err) override;
	virtual void onManager() override;
xiongziliang committed
56 57

	static string urlDecode(const string &str);
xzl committed
58 59
protected:
	void onRecv(const char *data,int size);
xzl committed
60
private:
61 62 63 64 65 66 67
	typedef enum
	{
		Http_success = 0,
		Http_failed = 1,
		Http_moreData = 2,
	} HttpCode;
	typedef HttpSession::HttpCode (HttpSession::*HttpCMDHandle)();
xzl committed
68
	static unordered_map<string, HttpCMDHandle> g_mapCmdIndex;
69

xzl committed
70 71 72 73 74 75
	Parser m_parser;
	string m_strPath;
	string m_strRcvBuf;
	Ticker m_ticker;
	uint32_t m_iReqCnt = 0;

xiongziliang committed
76 77 78
	//flv over http
	uint32_t m_aui32FirstStamp[2] = {0};
	uint32_t m_previousTagSize = 0;
79
    MediaInfo m_mediaInfo;
xiongziliang committed
80
	RingBuffer<RtmpPacket::Ptr>::RingReader::Ptr m_pRingReader;
81

xiongziliang committed
82
	void onSendMedia(const RtmpPacket::Ptr &pkt);
83
	void sendRtmp(const RtmpPacket::Ptr &pkt, uint32_t ui32TimeStamp);
xiongziliang committed
84
	void sendRtmp(uint8_t ui8Type, const std::string& strBuf, uint32_t ui32TimeStamp);
85 86 87 88

	inline HttpCode parserHttpReq(const string &);
	inline HttpCode Handle_Req_GET();
	inline HttpCode Handle_Req_POST();
xiongziliang committed
89
	inline bool checkLiveFlvStream();
xiongziliang committed
90 91
	inline bool emitHttpEvent(bool doInvoke);
	inline void urlDecode(Parser &parser);
92
	inline bool makeMeun(const string &strFullPath,const string &vhost, string &strRet);
xzl committed
93 94
	inline void sendNotFound(bool bClose);
	inline void sendResponse(const char *pcStatus,const KeyValue &header,const string &strContent);
95
	inline static KeyValue makeHttpHeader(bool bClose=false,int64_t iContentSize=-1,const char *pcContentType="text/html");
xiongziliang committed
96 97 98
	void responseDelay(const string &Origin,bool bClose,
					   const string &codeOut,const KeyValue &headerOut,
					   const string &contentOut);
xzl committed
99 100 101 102 103 104
};

} /* namespace Http */
} /* namespace ZL */

#endif /* SRC_HTTP_HTTPSESSION_H_ */