#ifndef JTJAI_MEDIA_HTTP_SERVER_H #define JTJAI_MEDIA_HTTP_SERVER_H #include #include #include #include #include #include #include #include #include namespace jtjai_media { // 视频文件信息结构 struct VideoFileInfo { std::string filename; // 文件名 std::string full_path; // 完整路径 std::string timestamp_dir; // 所属时间戳目录 int64_t file_size; // 文件大小(字节) std::string created_time; // 创建时间 std::string stream_index; // 流索引(从文件名推断) }; // HTTP请求结构 struct HttpRequest { std::string method; // GET, POST, DELETE等 std::string path; // 请求路径 std::string query_string; // 查询字符串 std::string body; // 请求体 std::map headers; // 请求头 }; // HTTP响应结构 struct HttpResponse { int status_code = 200; // 状态码 std::string status_message = "OK"; // 状态消息 std::string content_type = "application/json"; // 内容类型 std::string body; // 响应体 HttpResponse() = default; HttpResponse(int code, const std::string& msg, const std::string& content) : status_code(code), status_message(msg), body(content) {} }; class HttpServer { public: explicit HttpServer(const std::string& output_directory, int port = 8080); ~HttpServer(); // 启动服务器 bool start(); // 停止服务器 void stop(); // 检查服务器是否在运行 bool is_running() const { return is_running_.load(); } // 获取服务器端口 int get_port() const { return port_; } private: std::string output_directory_; // 输出目录 int port_; // 监听端口 std::atomic is_running_; // 运行状态 std::unique_ptr server_thread_; // 服务器线程 // 服务器主循环 void server_main_loop(); // 处理客户端连接 void handle_client(boost::asio::ip::tcp::socket socket); // 解析HTTP请求 HttpRequest parse_request(const std::string& request_data); // 构建HTTP响应 std::string build_response(const HttpResponse& response); // 路由处理 HttpResponse route_request(const HttpRequest& request); // API处理函数 HttpResponse handle_list_videos(const HttpRequest& request); HttpResponse handle_list_timestamps(const HttpRequest& request); HttpResponse handle_delete_video(const HttpRequest& request); HttpResponse handle_delete_timestamp(const HttpRequest& request); HttpResponse handle_get_video_info(const HttpRequest& request); HttpResponse handle_video_file(const HttpRequest& request); HttpResponse handle_static_file(const HttpRequest& request); // 工具函数 std::vector list_timestamp_directories(); std::vector list_videos_in_directory(const std::string& dir_path); std::vector list_all_videos(); bool delete_file(const std::string& file_path); bool delete_directory(const std::string& dir_path); std::string get_file_created_time(const std::string& file_path); int64_t get_file_size(const std::string& file_path); std::map parse_query_string(const std::string& query); std::string url_decode(const std::string& str); HttpResponse generate_index_page(); HttpResponse generate_api_doc(); }; } // namespace jtjai_media #endif // JTJAI_MEDIA_HTTP_SERVER_H