HttpSession.h 2.94 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 32 33 34 35 36 37 38 39
#include "Rtsp/Rtsp.h"
#include "Network/TcpLimitedSession.h"

using namespace std;
using namespace ZL::Network;

namespace ZL {
namespace Http {

40

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

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

xzl committed
51 52 53 54 55
	virtual void onRecv(const Socket::Buffer::Ptr &) override;
	virtual void onError(const SockException &err) override;
	virtual void onManager() override;
protected:
	void onRecv(const char *data,int size);
xzl committed
56
private:
57 58 59 60 61 62 63
	typedef enum
	{
		Http_success = 0,
		Http_failed = 1,
		Http_moreData = 2,
	} HttpCode;
	typedef HttpSession::HttpCode (HttpSession::*HttpCMDHandle)();
xzl committed
64
	static unordered_map<string, HttpCMDHandle> g_mapCmdIndex;
65

xzl committed
66 67 68 69 70 71
	Parser m_parser;
	string m_strPath;
	string m_strRcvBuf;
	Ticker m_ticker;
	uint32_t m_iReqCnt = 0;

72 73 74 75

	inline HttpCode parserHttpReq(const string &);
	inline HttpCode Handle_Req_GET();
	inline HttpCode Handle_Req_POST();
xzl committed
76 77 78
	inline bool makeMeun(const string &strFullPath, string &strRet);
	inline void sendNotFound(bool bClose);
	inline void sendResponse(const char *pcStatus,const KeyValue &header,const string &strContent);
79 80
	inline static KeyValue makeHttpHeader(bool bClose=false,int64_t iContentSize=-1,const char *pcContentType="text/html");
	void responseDelay(bool bClose,string &codeOut,KeyValue &headerOut, string &contentOut);
xzl committed
81 82 83 84 85 86
};

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

#endif /* SRC_HTTP_HTTPSESSION_H_ */