2017-10-09 22:11:01 +08:00
|
|
|
|
/*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
2017-09-27 16:20:30 +08:00
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
2017-09-27 16:20:30 +08:00
|
|
|
|
*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Use of this source code is governed by MIT license that can be found in the
|
|
|
|
|
|
* LICENSE file in the root of the source tree. All contributing project authors
|
|
|
|
|
|
* may be found in the AUTHORS file in the root of the source tree.
|
2017-04-01 16:35:56 +08:00
|
|
|
|
*/
|
2020-04-04 20:30:09 +08:00
|
|
|
|
|
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>
|
2022-11-19 09:33:10 +08:00
|
|
|
|
#include "Network/Session.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"
|
2020-09-20 00:21:46 +08:00
|
|
|
|
#include "TS/TSMediaSource.h"
|
2020-09-20 19:45:37 +08:00
|
|
|
|
#include "FMP4/FMP4MediaSource.h"
|
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
|
|
|
|
|
2022-11-19 09:33:10 +08:00
|
|
|
|
class HttpSession: public toolkit::Session,
|
2018-09-21 16:11:09 +08:00
|
|
|
|
public FlvMuxer,
|
|
|
|
|
|
public HttpRequestSplitter,
|
|
|
|
|
|
public WebSocketSplitter {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
public:
|
2023-04-28 22:04:38 +08:00
|
|
|
|
using Ptr = std::shared_ptr<HttpSession>;
|
|
|
|
|
|
using KeyValue = StrCaseMap;
|
|
|
|
|
|
using HttpResponseInvoker = HttpResponseInvokerImp ;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
friend class AsyncSender;
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @param errMsg 如果为空,则代表鉴权通过,否则为错误提示
|
|
|
|
|
|
* @param accessPath 运行或禁止访问的根目录
|
|
|
|
|
|
* @param cookieLifeSecond 鉴权cookie有效期
|
|
|
|
|
|
**/
|
2023-04-28 22:04:38 +08:00
|
|
|
|
using HttpAccessPathInvoker = std::function<void(const std::string &errMsg,const std::string &accessPath, int cookieLifeSecond)>;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
HttpSession(const toolkit::Socket::Ptr &pSock);
|
2020-04-24 12:39:22 +08:00
|
|
|
|
~HttpSession() override;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void onRecv(const toolkit::Buffer::Ptr &) override;
|
|
|
|
|
|
void onError(const toolkit::SockException &err) override;
|
2020-04-24 12:39:22 +08:00
|
|
|
|
void onManager() override;
|
2022-02-02 20:34:50 +08:00
|
|
|
|
static std::string urlDecode(const std::string &str);
|
2020-08-08 12:17:06 +08:00
|
|
|
|
|
2017-04-19 17:47:07 +08:00
|
|
|
|
protected:
|
2020-03-20 11:51:24 +08:00
|
|
|
|
//FlvMuxer override
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void onWrite(const toolkit::Buffer::Ptr &data, bool flush) override ;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
void onDetach() override;
|
|
|
|
|
|
std::shared_ptr<FlvMuxer> getSharedPtr() override;
|
|
|
|
|
|
|
|
|
|
|
|
//HttpRequestSplitter override
|
2021-01-19 16:05:38 +08:00
|
|
|
|
ssize_t onRecvHeader(const char *data,size_t len) override;
|
2021-01-17 18:31:50 +08:00
|
|
|
|
void onRecvContent(const char *data,size_t len) override;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 重载之用于处理不定长度的content
|
|
|
|
|
|
* 这个函数可用于处理大文件上传、http-flv推流
|
|
|
|
|
|
* @param header http请求头
|
|
|
|
|
|
* @param data content分片数据
|
|
|
|
|
|
* @param len content分片数据大小
|
|
|
|
|
|
* @param totalSize content总大小,如果为0则是不限长度content
|
|
|
|
|
|
* @param recvedSize 已收数据大小
|
|
|
|
|
|
*/
|
|
|
|
|
|
virtual void onRecvUnlimitedContent(const Parser &header,
|
|
|
|
|
|
const char *data,
|
2021-01-17 18:31:50 +08:00
|
|
|
|
size_t len,
|
|
|
|
|
|
size_t totalSize,
|
|
|
|
|
|
size_t recvedSize){
|
2022-02-02 20:34:50 +08:00
|
|
|
|
shutdown(toolkit::SockException(toolkit::Err_shutdown,"http post content is too huge,default closed"));
|
2020-03-20 11:51:24 +08:00
|
|
|
|
}
|
2018-09-20 17:50:58 +08:00
|
|
|
|
|
2020-03-20 11:51:24 +08:00
|
|
|
|
/**
|
2019-09-16 17:42:52 +08:00
|
|
|
|
* websocket客户端连接上事件
|
|
|
|
|
|
* @param header http头
|
|
|
|
|
|
* @return true代表允许websocket连接,否则拒绝
|
|
|
|
|
|
*/
|
|
|
|
|
|
virtual bool onWebSocketConnect(const Parser &header){
|
2020-04-24 12:39:22 +08:00
|
|
|
|
WarnP(this) << "http server do not support websocket default";
|
2019-09-16 17:42:52 +08:00
|
|
|
|
return false;
|
2018-09-20 18:20:43 +08:00
|
|
|
|
}
|
2019-05-28 17:14:36 +08:00
|
|
|
|
|
2020-03-20 11:51:24 +08:00
|
|
|
|
//WebSocketSplitter override
|
|
|
|
|
|
/**
|
2019-09-16 17:42:52 +08:00
|
|
|
|
* 发送数据进行websocket协议打包后回调
|
|
|
|
|
|
* @param buffer websocket协议数据
|
|
|
|
|
|
*/
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void onWebSocketEncodeData(toolkit::Buffer::Ptr buffer) override;
|
2020-08-08 12:17:06 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 接收到完整的一个webSocket数据包后回调
|
|
|
|
|
|
* @param header 数据包包头
|
|
|
|
|
|
*/
|
|
|
|
|
|
void onWebSocketDecodeComplete(const WebSocketHeader &header_in) override;
|
|
|
|
|
|
|
2022-06-18 22:10:46 +08:00
|
|
|
|
//重载获取客户端ip
|
|
|
|
|
|
std::string get_peer_ip() override;
|
|
|
|
|
|
|
2018-09-20 17:50:58 +08:00
|
|
|
|
private:
|
2021-01-19 16:05:38 +08:00
|
|
|
|
void Handle_Req_GET(ssize_t &content_len);
|
|
|
|
|
|
void Handle_Req_GET_l(ssize_t &content_len, bool sendBody);
|
|
|
|
|
|
void Handle_Req_POST(ssize_t &content_len);
|
|
|
|
|
|
void Handle_Req_HEAD(ssize_t &content_len);
|
2021-06-25 10:59:56 +08:00
|
|
|
|
void Handle_Req_OPTIONS(ssize_t &content_len);
|
2020-03-11 20:58:41 +08:00
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
bool checkLiveStream(const std::string &schema, const std::string &url_suffix, const std::function<void(const MediaSource::Ptr &src)> &cb);
|
2020-09-20 00:21:46 +08:00
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
bool checkLiveStreamFlv(const std::function<void()> &cb = nullptr);
|
|
|
|
|
|
bool checkLiveStreamTS(const std::function<void()> &cb = nullptr);
|
|
|
|
|
|
bool checkLiveStreamFMP4(const std::function<void()> &fmp4_list = nullptr);
|
2020-09-20 00:21:46 +08:00
|
|
|
|
|
2020-03-20 11:51:24 +08:00
|
|
|
|
bool checkWebSocket();
|
|
|
|
|
|
bool emitHttpEvent(bool doInvoke);
|
|
|
|
|
|
void urlDecode(Parser &parser);
|
|
|
|
|
|
void sendNotFound(bool bClose);
|
2021-01-02 21:24:06 +08:00
|
|
|
|
void sendResponse(int code, bool bClose, const char *pcContentType = nullptr,
|
2020-03-20 11:51:24 +08:00
|
|
|
|
const HttpSession::KeyValue &header = HttpSession::KeyValue(),
|
2020-08-08 12:17:06 +08:00
|
|
|
|
const HttpBody::Ptr &body = nullptr, bool no_content_length = false);
|
2019-09-04 18:57:54 +08:00
|
|
|
|
|
2020-03-20 11:51:24 +08:00
|
|
|
|
//设置socket标志
|
|
|
|
|
|
void setSocketFlags();
|
2020-08-08 12:17:06 +08:00
|
|
|
|
|
2019-05-28 17:14:36 +08:00
|
|
|
|
private:
|
2020-09-20 00:21:46 +08:00
|
|
|
|
bool _is_live_stream = false;
|
|
|
|
|
|
bool _live_over_websocket = false;
|
|
|
|
|
|
//消耗的总流量
|
|
|
|
|
|
uint64_t _total_bytes_usage = 0;
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::string _origin;
|
2019-05-28 17:14:36 +08:00
|
|
|
|
Parser _parser;
|
2022-02-02 20:34:50 +08:00
|
|
|
|
toolkit::Ticker _ticker;
|
2019-05-28 17:14:36 +08:00
|
|
|
|
MediaInfo _mediaInfo;
|
2020-09-20 00:21:46 +08:00
|
|
|
|
TSMediaSource::RingType::RingReader::Ptr _ts_reader;
|
2020-09-20 19:45:37 +08:00
|
|
|
|
FMP4MediaSource::RingType::RingReader::Ptr _fmp4_reader;
|
2019-05-28 17:14:36 +08:00
|
|
|
|
//处理content数据的callback
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::function<bool (const char *data,size_t len) > _contentCallBack;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2022-11-19 09:33:10 +08:00
|
|
|
|
using HttpsSession = toolkit::SessionWithSSL<HttpSession>;
|
2018-12-20 10:31:31 +08:00
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
} /* namespace mediakit */
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
|
|
|
|
|
#endif /* SRC_HTTP_HTTPSESSION_H_ */
|