CreoOtkPluging/HttpServer.h
sladro 45f455e0c2 修复HTTP请求体缓冲区限制问题 - 支持大规模批量删除操作
根本问题:
- HTTP服务器固定4KB缓冲区导致大JSON请求被截断
- 批量删除多个组件时请求体超过缓冲区限制

核心修复:
- HttpServer: 实现ReadCompleteRequest方法支持动态缓冲区
- 基于Content-Length头完整读取HTTP请求体
- Config: 添加MAX_REQUEST_SIZE=1MB上限保护机制
- 支持任意数量组件路径的批量删除操作

修复效果:
- 解决11个、25个路径批量删除失败问题
- 修复薄壳化分析等大JSON请求处理
- 保持所有现有功能正常运行
- 向后兼容,不影响现有API调用

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-09 16:54:03 +08:00

59 lines
1.6 KiB
C++

#pragma once
#include <winsock2.h>
#include <ws2tcpip.h>
#include <string>
#include <map>
#include <functional>
#include "Config.h"
// HTTP请求结构
struct HttpRequest {
std::string method;
std::string path;
std::string query;
std::map<std::string, std::string> headers;
std::string body;
};
// HTTP响应结构
struct HttpResponse {
int status_code = 200;
std::map<std::string, std::string> headers;
std::string body;
HttpResponse() {
headers["Content-Type"] = "application/json; charset=utf-8";
headers["Access-Control-Allow-Origin"] = "*";
headers["Access-Control-Allow-Methods"] = "GET, POST, OPTIONS";
headers["Access-Control-Allow-Headers"] = "Content-Type";
}
};
// HTTP服务器类
class HttpServer {
public:
HttpServer();
~HttpServer();
bool Start();
void Stop();
bool IsRunning() const { return running_; }
// 设置路由处理器
void SetRouteHandler(const std::string& path,
std::function<HttpResponse(const HttpRequest&)> handler);
private:
static DWORD WINAPI ServerThread(LPVOID lpParam);
void HandleClient(SOCKET client_socket);
std::string ReadCompleteRequest(SOCKET client_socket);
HttpRequest ParseRequest(const std::string& raw_request);
void SendResponse(SOCKET client_socket, const HttpResponse& response);
SOCKET server_socket_;
volatile bool running_;
HANDLE thread_handle_;
std::map<std::string, std::function<HttpResponse(const HttpRequest&)>> route_handlers_;
};