Distribution_Service/CC_SDK/Include/CCServlet/CCResponse.h

187 lines
5.5 KiB
C
Raw Normal View History

2025-11-11 17:46:19 +08:00
#ifndef CCWeb_Response_H
#define CCWeb_Response_H
#pragma once
// 检查宏定义
#ifdef Status
#undef Status
#endif
#include "CCRequest.h"
#include <openssl/ssl.h>
#include <openssl/err.h>
2025-12-03 18:08:23 +08:00
#include "TL/Queue.h"
2025-11-11 17:46:19 +08:00
/**
* @namespace CTL
* @brief Web
*/
namespace CTL {
/**
* @class Response
* @brief HTTP
*/
class Response{
public:
2025-12-03 18:08:23 +08:00
~Response();
2025-11-11 17:46:19 +08:00
/**
* @brief
*
2025-12-03 18:08:23 +08:00
* @param channel
2025-11-11 17:46:19 +08:00
* @param cors (CORS) CORS nullptr
*/
2025-12-03 18:08:23 +08:00
void Init(HttpSocketChannel* channel, CORS* cors);
/**
* @brief
*
* @param url
*/
void setUrl(const String& url);
2025-11-11 17:46:19 +08:00
/**
* @brief
*
* @param Path
* @param Status_t HTTP 200
* @return true false
*/
bool ResponseFile(const CTL::String& Path, int Status_t = 200);
/**
* @brief
*
* @param Data
* @param Status_t HTTP 200
* @return true false
*/
bool Resource(const CCVector<char>& Data, int Status_t = 200);
/**
* @brief
*
* @param Path
* @param SendBufferSize 4096
* @return true false
*/
bool ResourceDownload(const CTL::String& Path, int SendBufferSize = 4096);
/**
* @brief
*
* @param Key
* @param value
*/
void SetHeader(const CTL::String& Key, const CTL::String& value);
/**
* @brief
*
* @param Data
* @return true false
*/
bool Write(const CTL::String& Data);
/**
* @brief
*
* @param buffer
* @param size
* @return true false
*/
2026-03-20 09:51:56 +08:00
bool SendData(const char* buffer, size_t size) const;
2025-11-11 17:46:19 +08:00
/**
* @brief
*
* @param Data
* @param Status_t HTTP
* @return true false
*/
bool Write(const CCVector<char>& Data, int Status_t);
/**
* @brief
*
* @param Data
* @param Status_t HTTP
* @return true false
*/
bool Write(const CTL::String& Data, int Status_t);
/**
* @brief
*/
2026-03-20 09:51:56 +08:00
void ResponseRouting() const;
2025-11-11 17:46:19 +08:00
/**
* @brief
*
* @param Status_t HTTP
* @param ContentType
*/
void WriteHeader(int Status_t, const CTL::String& ContentType);
/**
* @brief
*
* @param request
* @param path
* @param Content_Type "-.-"
*/
void RangeMediaFile(Request request, const CTL::String& path, CTL::String Content_Type = "-.-");
/**
* @brief 访URL
*
* @param url
*/
void SendRedirect(const String& url);
2026-03-20 09:51:56 +08:00
/**
* @brief
*
* @param key
* @return
*/
String getHeader(const String& key);
2025-11-11 17:46:19 +08:00
private:
/**
* @brief
*
* @param Status_t HTTP
* @param HeaderFlag HTTP
*/
void SendHeader(int Status_t, const CTL::String& HeaderFlag = "OK");
/**
2025-12-03 18:08:23 +08:00
* @brief
2025-11-11 17:46:19 +08:00
*
2025-12-03 18:08:23 +08:00
* @param Status_t HTTP
* @param HeaderFlag HTTP
2025-11-11 17:46:19 +08:00
*/
2025-12-03 18:08:23 +08:00
String GetHeader(int Status_t, const CTL::String& HeaderFlag = "OK");
2025-11-11 17:46:19 +08:00
private:
2025-12-03 18:08:23 +08:00
inline static CCMutex _mutex;
// 文件头信息映射表。
2025-11-11 17:46:19 +08:00
CCMap<CTL::String, CTL::String> FileHeaders;
2025-12-03 18:08:23 +08:00
// 自定义响应头信息映射表。
2025-11-11 17:46:19 +08:00
CCMap<CTL::String, CTL::String> Headers;
2025-12-03 18:08:23 +08:00
// 客户端套接字。
HttpSocketChannel* channel = nullptr;
// CORS 设置。
2025-11-11 17:46:19 +08:00
CORS* CORS_ = nullptr;
2025-12-03 18:08:23 +08:00
String m_url;
// EventLoop* m_eventLoop = nullptr;
public:
2025-11-11 17:46:19 +08:00
};
}
#endif