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
|
|
|
|
*
|
2019-05-08 15:40:07 +08:00
|
|
|
|
* Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
|
2017-09-27 16:20:30 +08:00
|
|
|
|
*
|
|
|
|
|
* 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>
|
2018-02-23 15:36:51 +08:00
|
|
|
|
#include "Network/TcpSession.h"
|
2017-12-15 16:01:21 +08:00
|
|
|
|
#include "Rtmp/RtmpMediaSource.h"
|
2019-06-28 17:30:13 +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"
|
2019-06-13 12:00:41 +08:00
|
|
|
|
#include "HttpCookieManager.h"
|
2019-11-30 11:38:00 +08:00
|
|
|
|
#include "HttpFileManager.h"
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
|
|
|
|
using namespace std;
|
2018-10-24 17:17:55 +08:00
|
|
|
|
using namespace toolkit;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
namespace mediakit {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
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;
|
2019-10-28 16:50:15 +08:00
|
|
|
|
typedef HttpResponseInvokerImp HttpResponseInvoker;
|
2017-04-10 17:24:06 +08:00
|
|
|
|
|
2019-06-14 18:42:09 +08:00
|
|
|
|
/**
|
|
|
|
|
* @param errMsg 如果为空,则代表鉴权通过,否则为错误提示
|
|
|
|
|
* @param accessPath 运行或禁止访问的根目录
|
|
|
|
|
* @param cookieLifeSecond 鉴权cookie有效期
|
|
|
|
|
**/
|
|
|
|
|
typedef std::function<void(const string &errMsg,const string &accessPath, int cookieLifeSecond)> HttpAccessPathInvoker;
|
2019-06-12 17:53:48 +08:00
|
|
|
|
|
2018-12-20 10:42:51 +08:00
|
|
|
|
HttpSession(const Socket::Ptr &pSock);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
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-08-30 19:29:54 +08:00
|
|
|
|
//FlvMuxer override
|
|
|
|
|
void onWrite(const Buffer::Ptr &data) override ;
|
|
|
|
|
void onDetach() override;
|
|
|
|
|
std::shared_ptr<FlvMuxer> getSharedPtr() override;
|
2018-09-20 17:50:58 +08:00
|
|
|
|
|
2019-09-16 17:42:52 +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){
|
2019-05-29 18:08:50 +08:00
|
|
|
|
shutdown(SockException(Err_shutdown,"http post content is too huge,default closed"));
|
2018-09-20 17:50:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-16 17:42:52 +08:00
|
|
|
|
/**
|
|
|
|
|
* websocket客户端连接上事件
|
|
|
|
|
* @param header http头
|
|
|
|
|
* @return true代表允许websocket连接,否则拒绝
|
|
|
|
|
*/
|
|
|
|
|
virtual bool onWebSocketConnect(const Parser &header){
|
|
|
|
|
WarnL << "http server do not support websocket default";
|
|
|
|
|
return false;
|
2018-09-20 18:20:43 +08:00
|
|
|
|
}
|
2019-05-28 17:14:36 +08:00
|
|
|
|
|
2019-09-16 17:42:52 +08:00
|
|
|
|
//WebSocketSplitter override
|
2019-08-12 18:14:51 +08:00
|
|
|
|
/**
|
2019-09-16 17:42:52 +08:00
|
|
|
|
* 发送数据进行websocket协议打包后回调
|
|
|
|
|
* @param buffer websocket协议数据
|
|
|
|
|
*/
|
2019-08-13 12:00:21 +08:00
|
|
|
|
void onWebSocketEncodeData(const Buffer::Ptr &buffer) override;
|
2018-09-20 17:50:58 +08:00
|
|
|
|
private:
|
2019-09-16 17:42:52 +08:00
|
|
|
|
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);
|
2019-10-29 00:35:44 +08:00
|
|
|
|
void sendResponse(const char *pcStatus, bool bClose, const char *pcContentType = nullptr,
|
|
|
|
|
const HttpSession::KeyValue &header = HttpSession::KeyValue(),
|
2019-12-24 10:25:28 +08:00
|
|
|
|
const HttpBody::Ptr &body = nullptr,bool is_http_flv = false);
|
2019-09-04 18:57:54 +08:00
|
|
|
|
|
|
|
|
|
//设置socket标志
|
|
|
|
|
void setSocketFlags();
|
2019-05-28 17:14:36 +08:00
|
|
|
|
private:
|
2019-07-30 16:07:00 +08:00
|
|
|
|
string _origin;
|
2019-05-28 17:14:36 +08:00
|
|
|
|
Parser _parser;
|
|
|
|
|
Ticker _ticker;
|
|
|
|
|
//消耗的总流量
|
|
|
|
|
uint64_t _ui64TotalBytes = 0;
|
|
|
|
|
//flv over http
|
|
|
|
|
MediaInfo _mediaInfo;
|
|
|
|
|
//处理content数据的callback
|
|
|
|
|
function<bool (const char *data,uint64_t len) > _contentCallBack;
|
2019-08-12 18:14:51 +08:00
|
|
|
|
bool _flv_over_websocket = false;
|
2019-10-23 12:00:53 +08:00
|
|
|
|
bool _is_flv_stream = false;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
};
|
|
|
|
|
|
2018-09-26 23:12:03 +08:00
|
|
|
|
|
2018-12-20 10:31:31 +08:00
|
|
|
|
typedef TcpSessionWithSSL<HttpSession> HttpsSession;
|
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
} /* namespace mediakit */
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
|
|
|
|
#endif /* SRC_HTTP_HTTPSESSION_H_ */
|