2025-12-03 18:08:23 +08:00
|
|
|
#ifndef HttpClient_H
|
|
|
|
|
#define HttpClient_H
|
2025-11-11 17:46:19 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
// 包含必要的头文件
|
|
|
|
|
#include "../Module/Comm/Socket/CCSocket.h"
|
|
|
|
|
#include "CCJSONObject.h"
|
|
|
|
|
#include "CCString.h"
|
|
|
|
|
#include <sstream>
|
|
|
|
|
#include <regex>
|
|
|
|
|
#include "map"
|
2025-12-03 18:08:23 +08:00
|
|
|
#include "CCRequest.h"
|
|
|
|
|
#include "CCClientSocket.h"
|
|
|
|
|
#include "CCHttpResolver.h"
|
2025-11-11 17:46:19 +08:00
|
|
|
|
|
|
|
|
// 定义最大数据大小
|
|
|
|
|
#define MaxDataSize 2048
|
|
|
|
|
|
|
|
|
|
// 命名空间CTL开始
|
|
|
|
|
namespace CTL {
|
2025-12-03 18:08:23 +08:00
|
|
|
class Result {
|
|
|
|
|
public:
|
|
|
|
|
Result() = default;
|
|
|
|
|
Result(int status_t, const std::string &reason);
|
|
|
|
|
std::string version;
|
|
|
|
|
int status = -1;
|
|
|
|
|
std::string reason;
|
|
|
|
|
std::map<String, String> Headers;
|
|
|
|
|
std::string body;
|
|
|
|
|
std::string location; // Redirect location
|
2025-11-11 17:46:19 +08:00
|
|
|
};
|
2025-12-03 18:08:23 +08:00
|
|
|
class ReqConfig {
|
|
|
|
|
public:
|
|
|
|
|
String method; // 请求方法
|
|
|
|
|
String path; // 请求路径
|
|
|
|
|
ByteArray body; // 请求体
|
|
|
|
|
String type; // 请求体类型
|
2025-11-11 17:46:19 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// HttpClient类
|
2025-12-03 18:08:23 +08:00
|
|
|
class HttpClient{
|
|
|
|
|
std::string httpUrl;
|
|
|
|
|
std::string ip;
|
|
|
|
|
int port = 0;
|
|
|
|
|
bool is_https = false;
|
|
|
|
|
std::map<String, String> Headers;
|
|
|
|
|
ClientSocket* socket = nullptr;
|
|
|
|
|
SSL* ssl = nullptr;
|
|
|
|
|
using onReqCallback = std::function<bool(const ByteArray&, size_t)>;
|
|
|
|
|
using onResCallback = std::function<void(const CCHttpResolver&)>;
|
2025-11-11 17:46:19 +08:00
|
|
|
public:
|
2025-12-03 18:08:23 +08:00
|
|
|
explicit HttpClient(const std::string& httplink);
|
2025-11-11 17:46:19 +08:00
|
|
|
~HttpClient();
|
2025-12-03 18:08:23 +08:00
|
|
|
void setHeader(const std::string& key, const std::string& value);
|
|
|
|
|
Result Request(ReqConfig& req,const onResCallback& ResCallback,const onReqCallback& callback);
|
2025-11-11 17:46:19 +08:00
|
|
|
private:
|
2025-12-03 18:08:23 +08:00
|
|
|
bool parseURL(const std::string& url);
|
|
|
|
|
String GetRequest() const;
|
|
|
|
|
int Read(char* buffer, size_t length) const;
|
|
|
|
|
Result Write(const String& str) const;
|
|
|
|
|
Result Write(const ByteArray& str) const;
|
2025-11-11 17:46:19 +08:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 命名空间CTL结束
|
|
|
|
|
#endif
|