Distribution_Service/CC_SDK/Include/CCServlet/CCRequest.h

229 lines
5.9 KiB
C
Raw Permalink Normal View History

2025-11-11 17:46:19 +08:00
#ifndef CCWeb_Request_H
#define CCWeb_Request_H
#pragma once
#include "CORS.h"
#include "CCNetwork.h"
#include "CCHttpResolver.h"
#include "CC.h"
#include "CCThread.h"
#include "CCFile.h"
#include "CCObject.h"
#include <CCFIleInStream.h>
#include <CCFileOutStream.h>
2025-12-03 18:08:23 +08:00
#include <utility>
2025-11-11 17:46:19 +08:00
#include "CCJSONObject.h"
#include "openssl/ssl.h"
#include "openssl/err.h"
2025-12-03 18:08:23 +08:00
#include "HttpSocketChannel.h"
#include "EventLoop.h"
2025-11-11 17:46:19 +08:00
namespace CTL {
// 请求数据结构体用于存储HTTP请求的相关信息
struct RequestData_t{
CTL::String RawData; // 原始请求数据
CTL::String Method; // HTTP方法GET, POST等
CTL::String Path; // 请求路径
CTL::String Version; // HTTP版本
CCMap<CTL::String, CTL::String> Headers; // 请求头信息
CCMap<StdString, StdString> Params; // 请求参数
CTL::String Body; // 请求体内容
CCVector<char> Buffer; // 数据缓冲区
CTL::String Buf; // 临时缓冲区
CCMap<CTL::String, CTL::String> FileHeaders; // 文件头信息
};
/**
* HTTP请求
*/
class Request{
using ReceiveFileCallback = std::function<bool(const CCVector<char>&, size_t)>;
2026-03-24 14:43:26 +08:00
static CCMutex File_Mutex_T;
static std::atomic<uint8_t> IsRun;
static int MAX_UPLOAD_FILE_CONCURRENT;
2025-11-11 17:46:19 +08:00
// CORS对象指针
CORS* cors = nullptr;
// int File_Buffer_Max = 4096;
bool IsFileUploadSize = true;
2025-12-03 18:08:23 +08:00
HttpSocketChannel* channel = nullptr;
2026-03-20 09:51:56 +08:00
bool keep_alive = false;
2025-11-11 17:46:19 +08:00
public:
/**
*
*/
Request() = default;
/**
*
*/
RequestData_t RequestData;
/**
*
* @param key
* @param Body Body中获取参数
* @return
*/
CTL::String GetParameter(const CTL::String& key,bool Body = false);
/**
*
* @param key
* @return
*/
CTL::String GetFileInfo(const CTL::String& key);
/**
*
* @param key
* @return
*/
CTL::String GetHeader(const CTL::String& key);
/**
* JSON对象
* @return JSON对象
*/
2025-12-03 18:08:23 +08:00
[[nodiscard]] CTL::JSONObject GetJson() const;
2025-11-11 17:46:19 +08:00
/**
*
* @param Path
* @return
*/
bool GetMediaFiles(const CTL::String& Path);
/**
*
* @param callback --> bool --> CCVector<char> --> size_t
* @return
*/
bool GetMediaFiles(const ReceiveFileCallback& callback);
/**
*
2025-12-03 18:08:23 +08:00
* @param channel
2025-11-11 17:46:19 +08:00
* @param cors CORS对象指针
*/
2025-12-03 18:08:23 +08:00
void Init(HttpSocketChannel* channel, CORS* cors);
2025-11-11 17:46:19 +08:00
/**
*
* @param filePath
* @return
*/
static CTL::String GetFileSuffix(const CTL::String& filePath);
/**
*
* @param str
* @return
*/
static CTL::String GetFileType(const CTL::String& str);
/**
*
* @param path
* @return
*/
static unsigned int GetFileSize(const CTL::String& path);
/**
* URL解码
* @param in
* @return
*/
static CTL::String URL_Decode(const CTL::String& in);
/**
*
* @param in
* @param map
* @return
*/
static CTL::String GetFileDateHeader(CTL::String& in, std::map<CTL::String, CTL::String>& map);
/**
*
* @param input
* @param key
* @param value
*/
static void GetFormData(CTL::String& input, CTL::String& key, CTL::String& value);
/**
*
* @param input
* @return
*/
static CCMap<CTL::String, CTL::String> parseKeyValuePairs(const CTL::String& input);
/**
*
* @param Num
*/
static void SetMaxConcurrentFileUploads(int Num);
/**
*
* @param Size
*/
void SetFileUploadSize(int Size);
2026-03-20 09:51:56 +08:00
/**
*
* @param Flag
*/
void SetKeepAlive(bool Flag);
/**
*
* @return
*/
[[nodiscard]] bool isKeepAlive() const;
2025-11-11 17:46:19 +08:00
private:
static void FileWriting(const CTL::String& path,const CCVector<char>& buffer, size_t size);
2026-03-20 09:51:56 +08:00
static String getFileName(const CTL::String& contentDisposition);
2025-11-11 17:46:19 +08:00
};
2025-12-03 18:08:23 +08:00
class BoundaryMatcher {
String boundary_pattern;
size_t matched_length = 0;
public:
explicit BoundaryMatcher(String pattern) : boundary_pattern(std::move(pattern)) {}
bool match_data(const CCVector<char>& data) {
for (const char i : data) {
if (matched_length < boundary_pattern.length() &&
i == boundary_pattern[matched_length]) {
matched_length++;
if (matched_length == boundary_pattern.length()) {
return true; // 完全匹配
}
}
else {
matched_length = 0; // 不匹配时重置
}
}
return false;
}
void reset() {
matched_length = 0;
}
[[nodiscard]] size_t get_matched_length() const {
return matched_length;
}
};
2025-11-11 17:46:19 +08:00
/**
*
* @param key
* @return
*/
inline CTL::String Request::GetHeader(const CTL::String& key) {
return RequestData.Headers[key];
}
}
2025-12-03 18:08:23 +08:00
2025-11-11 17:46:19 +08:00
#endif