2019-11-30 11:38:00 +08:00
|
|
|
|
/*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
2019-11-30 11:38:00 +08:00
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
2019-11-30 11:38:00 +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.
|
2019-11-30 11:38:00 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef ZLMEDIAKIT_HTTPFILEMANAGER_H
|
|
|
|
|
#define ZLMEDIAKIT_HTTPFILEMANAGER_H
|
|
|
|
|
|
|
|
|
|
#include "HttpBody.h"
|
|
|
|
|
#include "HttpCookie.h"
|
|
|
|
|
#include "Common/Parser.h"
|
2022-11-19 09:33:10 +08:00
|
|
|
|
#include "Network/Session.h"
|
2019-11-30 11:38:00 +08:00
|
|
|
|
#include "Util/function_traits.h"
|
|
|
|
|
|
|
|
|
|
namespace mediakit {
|
|
|
|
|
|
|
|
|
|
class HttpResponseInvokerImp{
|
|
|
|
|
public:
|
2021-01-02 21:24:06 +08:00
|
|
|
|
typedef std::function<void(int code, const StrCaseMap &headerOut, const HttpBody::Ptr &body)> HttpResponseInvokerLambda0;
|
2022-02-02 20:34:50 +08:00
|
|
|
|
typedef std::function<void(int code, const StrCaseMap &headerOut, const std::string &body)> HttpResponseInvokerLambda1;
|
2019-11-30 11:38:00 +08:00
|
|
|
|
|
2023-04-22 21:59:26 +08:00
|
|
|
|
HttpResponseInvokerImp() = default;
|
|
|
|
|
~HttpResponseInvokerImp() = default;
|
2019-11-30 11:38:00 +08:00
|
|
|
|
template<typename C>
|
2022-02-02 20:34:50 +08:00
|
|
|
|
HttpResponseInvokerImp(const C &c):HttpResponseInvokerImp(typename toolkit::function_traits<C>::stl_function_type(c)) {}
|
2019-11-30 11:38:00 +08:00
|
|
|
|
HttpResponseInvokerImp(const HttpResponseInvokerLambda0 &lambda);
|
|
|
|
|
HttpResponseInvokerImp(const HttpResponseInvokerLambda1 &lambda);
|
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void operator()(int code, const StrCaseMap &headerOut, const toolkit::Buffer::Ptr &body) const;
|
2021-01-02 21:24:06 +08:00
|
|
|
|
void operator()(int code, const StrCaseMap &headerOut, const HttpBody::Ptr &body) const;
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void operator()(int code, const StrCaseMap &headerOut, const std::string &body) const;
|
2021-01-02 21:24:06 +08:00
|
|
|
|
|
2022-02-11 16:21:19 +08:00
|
|
|
|
void responseFile(const StrCaseMap &requestHeader,const StrCaseMap &responseHeader,const std::string &file, bool use_mmap = true, bool is_path = true) const;
|
2019-11-30 11:38:00 +08:00
|
|
|
|
operator bool();
|
|
|
|
|
private:
|
|
|
|
|
HttpResponseInvokerLambda0 _lambad;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 该对象用于控制http静态文件夹服务器的访问权限
|
|
|
|
|
*/
|
|
|
|
|
class HttpFileManager {
|
|
|
|
|
public:
|
2022-02-02 20:34:50 +08:00
|
|
|
|
typedef std::function<void(int code, const std::string &content_type, const StrCaseMap &responseHeader, const HttpBody::Ptr &body)> invoker;
|
2019-11-30 11:38:00 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 访问文件或文件夹
|
|
|
|
|
* @param sender 事件触发者
|
|
|
|
|
* @param parser http请求
|
|
|
|
|
* @param cb 回调对象
|
|
|
|
|
*/
|
2022-11-19 09:33:10 +08:00
|
|
|
|
static void onAccessPath(toolkit::Session &sender, Parser &parser, const invoker &cb);
|
2020-01-02 18:24:11 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取mime值
|
|
|
|
|
* @param name 文件后缀
|
|
|
|
|
* @return mime值
|
|
|
|
|
*/
|
2022-02-02 20:34:50 +08:00
|
|
|
|
static const std::string &getContentType(const char *name);
|
2023-07-26 16:33:21 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 该ip是否再白名单中
|
|
|
|
|
* @param ip 支持ipv4和ipv6
|
|
|
|
|
*/
|
|
|
|
|
static bool isIPAllowed(const std::string &ip);
|
|
|
|
|
|
2019-11-30 11:38:00 +08:00
|
|
|
|
private:
|
|
|
|
|
HttpFileManager() = delete;
|
|
|
|
|
~HttpFileManager() = delete;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif //ZLMEDIAKIT_HTTPFILEMANAGER_H
|