From 2269dc4cbc00ddde6a333aef82cddc21bd203a5b Mon Sep 17 00:00:00 2001 From: JohnDoe6345789 Date: Fri, 26 Dec 2025 00:39:02 +0000 Subject: [PATCH] code: types,type,package (2 files) --- dbal/cpp/src/daemon/http/http_types.hpp | 117 ++++++++++++++++++ .../server/get-package-content-type.ts | 24 ++++ 2 files changed, 141 insertions(+) create mode 100644 dbal/cpp/src/daemon/http/http_types.hpp create mode 100644 frontends/nextjs/src/lib/packages/server/get-package-content-type.ts diff --git a/dbal/cpp/src/daemon/http/http_types.hpp b/dbal/cpp/src/daemon/http/http_types.hpp new file mode 100644 index 000000000..5842963d3 --- /dev/null +++ b/dbal/cpp/src/daemon/http/http_types.hpp @@ -0,0 +1,117 @@ +/** + * @file http_types.hpp + * @brief HTTP request/response types and structures + * + * Defines the core data structures for HTTP handling. + */ +#ifndef DBAL_HTTP_TYPES_HPP +#define DBAL_HTTP_TYPES_HPP + +#include +#include +#include +#include + +namespace dbal { +namespace daemon { +namespace http { + +/** + * @struct HttpRequest + * @brief Parsed HTTP request structure + */ +struct HttpRequest { + std::string method; ///< HTTP method (GET, POST, etc.) + std::string path; ///< Request path (e.g., /api/health) + std::string version; ///< HTTP version (e.g., HTTP/1.1) + std::map headers; ///< Request headers + std::string body; + + /** + * Get real client IP from reverse proxy headers + */ + std::string realIP() const { + auto it = headers.find("X-Real-IP"); + if (it != headers.end()) return it->second; + it = headers.find("X-Forwarded-For"); + if (it != headers.end()) { + // Get first IP from comma-separated list + size_t comma = it->second.find(','); + return comma != std::string::npos ? it->second.substr(0, comma) : it->second; + } + return ""; + } + + /** + * Get forwarded protocol from reverse proxy headers + */ + std::string forwardedProto() const { + auto it = headers.find("X-Forwarded-Proto"); + return it != headers.end() ? it->second : "http"; + } +}; + +/** + * @struct HttpResponse + * @brief HTTP response structure + */ +struct HttpResponse { + int status_code; + std::string status_text; + std::map headers; + std::string body; + + HttpResponse() : status_code(200), status_text("OK") { + headers["Content-Type"] = "application/json"; + headers["Server"] = "DBAL/1.0.0"; + } + + /** + * Serialize response to HTTP wire format + */ + std::string serialize() const { + std::ostringstream oss; + oss << "HTTP/1.1 " << status_code << " " << status_text << "\r\n"; + + // Add Content-Length if not already set + auto cl_it = headers.find("Content-Length"); + if (cl_it == headers.end()) { + oss << "Content-Length: " << body.length() << "\r\n"; + } + + for (const auto& h : headers) { + oss << h.first << ": " << h.second << "\r\n"; + } + + oss << "\r\n" << body; + return oss.str(); + } + + /** + * Create error response + */ + static HttpResponse error(int code, const std::string& text, const std::string& message) { + HttpResponse response; + response.status_code = code; + response.status_text = text; + response.body = R"({"error":")" + message + "\"}"; + return response; + } + + /** + * Create JSON response + */ + static HttpResponse json(const std::string& body, int code = 200) { + HttpResponse response; + response.status_code = code; + response.status_text = code == 200 ? "OK" : "Error"; + response.body = body; + return response; + } +}; + +} // namespace http +} // namespace daemon +} // namespace dbal + +#endif diff --git a/frontends/nextjs/src/lib/packages/server/get-package-content-type.ts b/frontends/nextjs/src/lib/packages/server/get-package-content-type.ts new file mode 100644 index 000000000..db7d48fce --- /dev/null +++ b/frontends/nextjs/src/lib/packages/server/get-package-content-type.ts @@ -0,0 +1,24 @@ +import path from 'path' + +const contentTypeMap: Record = { + '.json': 'application/json', + '.lua': 'text/plain', + '.md': 'text/markdown', + '.txt': 'text/plain', + '.js': 'text/javascript', + '.ts': 'text/plain', + '.tsx': 'text/plain', + '.css': 'text/css', + '.html': 'text/html', + '.svg': 'image/svg+xml', + '.png': 'image/png', + '.jpg': 'image/jpeg', + '.jpeg': 'image/jpeg', + '.cpp': 'text/plain', + '.h': 'text/plain', +} + +export function getPackageContentType(filePath: string): string { + const ext = path.extname(filePath).toLowerCase() + return contentTypeMap[ext] || 'application/octet-stream' +}