2017-10-09 22:11:01 +08:00
|
|
|
|
/*
|
2017-09-27 16:20:30 +08:00
|
|
|
|
* MIT License
|
2017-04-01 16:35:56 +08:00
|
|
|
|
*
|
2017-09-27 16:20:30 +08:00
|
|
|
|
* 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.
|
2017-04-01 16:35:56 +08:00
|
|
|
|
*/
|
|
|
|
|
|
#ifndef SRC_HTTP_HTTPSESSION_H_
|
|
|
|
|
|
#define SRC_HTTP_HTTPSESSION_H_
|
|
|
|
|
|
|
2017-04-25 11:35:41 +08:00
|
|
|
|
#include <functional>
|
2017-05-02 17:15:12 +08:00
|
|
|
|
#include "Common/config.h"
|
2017-04-01 16:35:56 +08:00
|
|
|
|
#include "Rtsp/Rtsp.h"
|
2018-02-23 15:36:51 +08:00
|
|
|
|
#include "Network/TcpSession.h"
|
2017-12-15 16:01:21 +08:00
|
|
|
|
#include "Rtmp/RtmpMediaSource.h"
|
2018-08-30 19:29:54 +08:00
|
|
|
|
#include "Rtmp/FlvMuxer.h"
|
2018-09-20 17:50:58 +08:00
|
|
|
|
#include "HttpRequestSplitter.h"
|
2018-09-21 16:11:09 +08:00
|
|
|
|
#include "WebSocketSplitter.h"
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
2017-12-15 16:01:21 +08:00
|
|
|
|
using namespace ZL::Rtmp;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
using namespace ZL::Network;
|
|
|
|
|
|
|
|
|
|
|
|
namespace ZL {
|
|
|
|
|
|
namespace Http {
|
|
|
|
|
|
|
2018-09-21 16:11:09 +08:00
|
|
|
|
class HttpSession: public TcpSession,
|
|
|
|
|
|
public FlvMuxer,
|
|
|
|
|
|
public HttpRequestSplitter,
|
|
|
|
|
|
public WebSocketSplitter {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
public:
|
2017-05-05 18:02:54 +08:00
|
|
|
|
typedef StrCaseMap KeyValue;
|
2017-04-10 17:24:06 +08:00
|
|
|
|
typedef std::function<void(const string &codeOut,
|
|
|
|
|
|
const KeyValue &headerOut,
|
|
|
|
|
|
const string &contentOut)> HttpResponseInvoker;
|
|
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
HttpSession(const std::shared_ptr<ThreadPool> &pTh, const Socket::Ptr &pSock);
|
|
|
|
|
|
virtual ~HttpSession();
|
|
|
|
|
|
|
2018-02-23 15:36:51 +08:00
|
|
|
|
virtual void onRecv(const Buffer::Ptr &) override;
|
2017-04-19 17:47:07 +08:00
|
|
|
|
virtual void onError(const SockException &err) override;
|
|
|
|
|
|
virtual void onManager() override;
|
2017-12-08 22:37:17 +08:00
|
|
|
|
|
|
|
|
|
|
static string urlDecode(const string &str);
|
2017-04-19 17:47:07 +08:00
|
|
|
|
protected:
|
2018-09-20 17:50:58 +08:00
|
|
|
|
//用于HttpsSession调用
|
2017-04-19 17:47:07 +08:00
|
|
|
|
void onRecv(const char *data,int size);
|
2018-08-30 19:29:54 +08:00
|
|
|
|
//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;
|
2018-09-20 17:50:58 +08:00
|
|
|
|
//HttpRequestSplitter override
|
|
|
|
|
|
|
2018-09-23 00:55:00 +08:00
|
|
|
|
int64_t onRecvHeader(const char *data,uint64_t len) override;
|
|
|
|
|
|
void onRecvContent(const char *data,uint64_t len) override;
|
2017-04-10 17:24:06 +08:00
|
|
|
|
|
2018-09-20 17:50:58 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 重载之用于处理不定长度的content
|
2018-09-20 18:20:43 +08:00
|
|
|
|
* 这个函数可用于处理大文件上传、http-flv推流
|
2018-09-20 17:50:58 +08:00
|
|
|
|
* @param header http请求头
|
2018-09-23 00:55:00 +08:00
|
|
|
|
* @param data content分片数据
|
|
|
|
|
|
* @param len content分片数据大小
|
2018-09-21 09:41:40 +08:00
|
|
|
|
* @param totalSize content总大小,如果为0则是不限长度content
|
|
|
|
|
|
* @param recvedSize 已收数据大小
|
2018-09-20 17:50:58 +08:00
|
|
|
|
*/
|
2018-09-23 00:55:00 +08:00
|
|
|
|
virtual void onRecvUnlimitedContent(const Parser &header,
|
|
|
|
|
|
const char *data,
|
|
|
|
|
|
uint64_t len,
|
|
|
|
|
|
uint64_t totalSize,
|
|
|
|
|
|
uint64_t recvedSize){
|
2018-09-20 17:50:58 +08:00
|
|
|
|
WarnL << "content数据长度过大,无法处理,请重载HttpSession::onRecvUnlimitedContent";
|
|
|
|
|
|
shutdown();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-23 00:55:00 +08:00
|
|
|
|
void onWebSocketDecodeHeader(const WebSocketHeader &packet) override{
|
|
|
|
|
|
DebugL << "默认关闭WebSocket";
|
|
|
|
|
|
shutdown();
|
|
|
|
|
|
};
|
2018-09-20 18:20:43 +08:00
|
|
|
|
|
2018-09-23 00:55:00 +08:00
|
|
|
|
void onRecvWebSocketData(const Parser &header,const char *data,uint64_t len){
|
|
|
|
|
|
WebSocketSplitter::decode((uint8_t *)data,len);
|
2018-09-20 18:20:43 +08:00
|
|
|
|
}
|
2018-09-20 17:50:58 +08:00
|
|
|
|
private:
|
2017-04-01 16:35:56 +08:00
|
|
|
|
Parser m_parser;
|
|
|
|
|
|
string m_strPath;
|
|
|
|
|
|
Ticker m_ticker;
|
|
|
|
|
|
uint32_t m_iReqCnt = 0;
|
2018-08-30 19:29:54 +08:00
|
|
|
|
//消耗的总流量
|
|
|
|
|
|
uint64_t m_ui64TotalBytes = 0;
|
2017-12-15 16:01:21 +08:00
|
|
|
|
//flv over http
|
2018-02-06 15:28:27 +08:00
|
|
|
|
MediaInfo m_mediaInfo;
|
2018-09-20 17:50:58 +08:00
|
|
|
|
//处理content数据的callback
|
2018-09-23 00:55:00 +08:00
|
|
|
|
function<bool (const char *data,uint64_t len) > m_contentCallBack;
|
2018-09-20 17:50:58 +08:00
|
|
|
|
private:
|
|
|
|
|
|
inline bool Handle_Req_GET(int64_t &content_len);
|
|
|
|
|
|
inline bool Handle_Req_POST(int64_t &content_len);
|
2017-12-15 16:01:21 +08:00
|
|
|
|
inline bool checkLiveFlvStream();
|
2018-09-20 18:20:43 +08:00
|
|
|
|
inline bool checkWebSocket();
|
2017-12-08 22:37:17 +08:00
|
|
|
|
inline bool emitHttpEvent(bool doInvoke);
|
|
|
|
|
|
inline void urlDecode(Parser &parser);
|
2018-02-02 18:06:08 +08:00
|
|
|
|
inline bool makeMeun(const string &strFullPath,const string &vhost, string &strRet);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
inline void sendNotFound(bool bClose);
|
|
|
|
|
|
inline void sendResponse(const char *pcStatus,const KeyValue &header,const string &strContent);
|
2017-04-10 17:24:06 +08:00
|
|
|
|
inline static KeyValue makeHttpHeader(bool bClose=false,int64_t iContentSize=-1,const char *pcContentType="text/html");
|
2017-12-09 16:43:47 +08:00
|
|
|
|
void responseDelay(const string &Origin,bool bClose,
|
|
|
|
|
|
const string &codeOut,const KeyValue &headerOut,
|
|
|
|
|
|
const string &contentOut);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2018-09-26 23:12:03 +08:00
|
|
|
|
|
2018-09-20 18:20:43 +08:00
|
|
|
|
/**
|
2018-09-26 23:12:03 +08:00
|
|
|
|
* 通过该模板类可以透明化WebSocket协议,
|
|
|
|
|
|
* 用户只要实现WebSock协议下的具体业务协议,譬如基于WebSocket协议的Rtmp协议等
|
|
|
|
|
|
* @tparam SessionType 业务协议的TcpSession类
|
2018-09-20 18:20:43 +08:00
|
|
|
|
*/
|
2018-09-26 23:12:03 +08:00
|
|
|
|
template <typename SessionType>
|
|
|
|
|
|
class WebSocketSession : public HttpSession {
|
2018-09-20 18:20:43 +08:00
|
|
|
|
public:
|
2018-09-26 23:12:03 +08:00
|
|
|
|
WebSocketSession(const std::shared_ptr<ThreadPool> &pTh, const Socket::Ptr &pSock) : HttpSession(pTh,pSock){
|
|
|
|
|
|
_session = std::make_shared<SessionImp>(pTh,pSock);
|
|
|
|
|
|
}
|
|
|
|
|
|
virtual ~WebSocketSession(){};
|
|
|
|
|
|
|
|
|
|
|
|
//收到eof或其他导致脱离TcpServer事件的回调
|
|
|
|
|
|
void onError(const SockException &err) override{
|
|
|
|
|
|
HttpSession::onError(err);
|
|
|
|
|
|
_session->onError(err);
|
|
|
|
|
|
}
|
|
|
|
|
|
//每隔一段时间触发,用来做超时管理
|
|
|
|
|
|
void onManager() override{
|
|
|
|
|
|
HttpSession::onManager();
|
|
|
|
|
|
_session->onManager();
|
|
|
|
|
|
}
|
|
|
|
|
|
//在创建TcpSession后,TcpServer会把自身的配置参数通过该函数传递给TcpSession
|
|
|
|
|
|
void attachServer(const TcpServer &server) override{
|
|
|
|
|
|
HttpSession::attachServer(server);
|
|
|
|
|
|
_session->attachServer(server);
|
|
|
|
|
|
|
|
|
|
|
|
//此处截取数据并进行websocket协议打包
|
|
|
|
|
|
weak_ptr<WebSocketSession> weakSelf = dynamic_pointer_cast<WebSocketSession>(shared_from_this());
|
|
|
|
|
|
_session->setOnBeforeSendCB([weakSelf](const Buffer::Ptr &buf){
|
|
|
|
|
|
auto strongSelf = weakSelf.lock();
|
|
|
|
|
|
if(strongSelf){
|
|
|
|
|
|
bool mask_flag = strongSelf->_mask_flag;
|
|
|
|
|
|
strongSelf->_mask_flag = false;
|
|
|
|
|
|
strongSelf->WebSocketSplitter::encode((uint8_t *)buf->data(),buf->size());
|
|
|
|
|
|
strongSelf->_mask_flag = mask_flag;
|
|
|
|
|
|
}
|
|
|
|
|
|
return buf->size();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
//作为该TcpSession的唯一标识符
|
|
|
|
|
|
string getIdentifier() const override{
|
|
|
|
|
|
return _session->getIdentifier();
|
|
|
|
|
|
}
|
2018-09-20 18:20:43 +08:00
|
|
|
|
protected:
|
2018-09-26 23:12:03 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 开始收到一个webSocket数据包
|
|
|
|
|
|
* @param packet
|
|
|
|
|
|
*/
|
2018-09-21 18:48:35 +08:00
|
|
|
|
void onWebSocketDecodeHeader(const WebSocketHeader &packet) override{
|
2018-09-26 23:12:03 +08:00
|
|
|
|
//新包,原来的包残余数据清空掉
|
|
|
|
|
|
_remian_data.clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 收到websocket数据包负载
|
|
|
|
|
|
* @param packet
|
|
|
|
|
|
* @param ptr
|
|
|
|
|
|
* @param len
|
|
|
|
|
|
* @param recved
|
|
|
|
|
|
*/
|
2018-09-21 18:48:35 +08:00
|
|
|
|
void onWebSocketDecodePlayload(const WebSocketHeader &packet,const uint8_t *ptr,uint64_t len,uint64_t recved) override {
|
2018-09-26 23:12:03 +08:00
|
|
|
|
if(packet._playload_len == recved){
|
|
|
|
|
|
//收到完整的包
|
|
|
|
|
|
if(_remian_data.empty()){
|
|
|
|
|
|
onRecvWholePacket((char *)ptr,len);
|
|
|
|
|
|
}else{
|
|
|
|
|
|
_remian_data.append((char *)ptr,len);
|
|
|
|
|
|
onRecvWholePacket(_remian_data);
|
|
|
|
|
|
_remian_data.clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
//部分数据
|
|
|
|
|
|
_remian_data.append((char *)ptr,len);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-09-21 18:48:35 +08:00
|
|
|
|
|
2018-09-26 23:12:03 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 发送数据进行websocket协议打包后回调
|
|
|
|
|
|
* @param ptr
|
|
|
|
|
|
* @param len
|
|
|
|
|
|
*/
|
|
|
|
|
|
void onWebSocketEncodeData(const uint8_t *ptr,uint64_t len) override{
|
|
|
|
|
|
_session->realSend(_session->obtainBuffer((char *)ptr,len));
|
|
|
|
|
|
}
|
2018-09-21 18:48:35 +08:00
|
|
|
|
|
2018-09-26 23:12:03 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 收到一个完整的websock数据包
|
|
|
|
|
|
* @param data
|
|
|
|
|
|
* @param len
|
|
|
|
|
|
*/
|
|
|
|
|
|
void onRecvWholePacket(const char *data,uint64_t len){
|
|
|
|
|
|
BufferRaw::Ptr buffer = _session->obtainBuffer(data,len);
|
|
|
|
|
|
_session->onRecv(buffer);
|
|
|
|
|
|
}
|
2018-09-21 18:48:35 +08:00
|
|
|
|
|
2018-09-26 23:12:03 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 收到一个完整的websock数据包
|
|
|
|
|
|
* @param str
|
|
|
|
|
|
*/
|
|
|
|
|
|
void onRecvWholePacket(const string &str){
|
|
|
|
|
|
BufferString::Ptr buffer = std::make_shared<BufferString>(str);
|
|
|
|
|
|
_session->onRecv(buffer);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
typedef function<int(const Buffer::Ptr &buf)> onBeforeSendCB;
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 该类实现了TcpSession派生类发送数据的截取
|
|
|
|
|
|
* 目的是发送业务数据前进行websocket协议的打包
|
|
|
|
|
|
*/
|
|
|
|
|
|
class SessionImp : public SessionType{
|
|
|
|
|
|
public:
|
|
|
|
|
|
SessionImp(const std::shared_ptr<ThreadPool> &pTh, const Socket::Ptr &pSock) : SessionType(pTh,pSock){};
|
|
|
|
|
|
~SessionImp(){}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 截取到数据后,再进行webSocket协议打包
|
|
|
|
|
|
* 然后真正的发送数据到socket
|
|
|
|
|
|
* @param buf 数据
|
|
|
|
|
|
* @return 数据字节数
|
|
|
|
|
|
*/
|
|
|
|
|
|
int realSend(const Buffer::Ptr &buf){
|
|
|
|
|
|
return SessionType::send(buf);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 设置发送数据截取回调函数
|
|
|
|
|
|
* @param cb 截取回调函数
|
|
|
|
|
|
*/
|
|
|
|
|
|
void setOnBeforeSendCB(const onBeforeSendCB &cb){
|
|
|
|
|
|
_beforeSendCB = cb;
|
|
|
|
|
|
}
|
|
|
|
|
|
protected:
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 重载send函数截取数据
|
|
|
|
|
|
* @param buf 需要截取的数据
|
|
|
|
|
|
* @return 数据字节数
|
|
|
|
|
|
*/
|
|
|
|
|
|
int send(const Buffer::Ptr &buf) override {
|
|
|
|
|
|
if(_beforeSendCB){
|
|
|
|
|
|
return _beforeSendCB(buf);
|
|
|
|
|
|
}
|
|
|
|
|
|
return SessionType::send(buf);
|
|
|
|
|
|
}
|
|
|
|
|
|
private:
|
|
|
|
|
|
onBeforeSendCB _beforeSendCB;
|
2018-09-21 18:48:35 +08:00
|
|
|
|
};
|
2018-09-26 23:12:03 +08:00
|
|
|
|
private:
|
|
|
|
|
|
std::shared_ptr<SessionImp> _session;
|
|
|
|
|
|
string _remian_data;
|
|
|
|
|
|
};
|
2018-09-21 18:48:35 +08:00
|
|
|
|
|
2018-09-26 23:12:03 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 回显会话
|
|
|
|
|
|
*/
|
|
|
|
|
|
class EchoSession : public TcpSession {
|
|
|
|
|
|
public:
|
|
|
|
|
|
EchoSession(const std::shared_ptr<ThreadPool> &pTh, const Socket::Ptr &pSock) : TcpSession(pTh,pSock){};
|
|
|
|
|
|
virtual ~EchoSession(){};
|
|
|
|
|
|
|
|
|
|
|
|
void onRecv(const Buffer::Ptr &buffer) override {
|
|
|
|
|
|
send(buffer);
|
|
|
|
|
|
}
|
|
|
|
|
|
void onError(const SockException &err) override{
|
|
|
|
|
|
WarnL << err.what();
|
|
|
|
|
|
}
|
|
|
|
|
|
//每隔一段时间触发,用来做超时管理
|
|
|
|
|
|
void onManager() override{
|
|
|
|
|
|
DebugL;
|
|
|
|
|
|
}
|
2018-09-20 18:20:43 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2018-09-26 23:12:03 +08:00
|
|
|
|
|
|
|
|
|
|
typedef WebSocketSession<EchoSession> EchoWebSocketSession;
|
|
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
} /* namespace Http */
|
|
|
|
|
|
} /* namespace ZL */
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* SRC_HTTP_HTTPSESSION_H_ */
|