/* TessesFramework a library to make C++ easier for me, used in CrossLang: https://git.tesses.org/tesses50/crosslang Copyright (C) 2026 Mike Nolan This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #pragma once #include "../Streams/Stream.hpp" #include "HttpUtils.hpp" // clang-format off #include "TessesFramework/Filesystem/LocalFS.hpp" #include "TessesFramework/Filesystem/VFSFix.hpp" // clang-format on #include "WebSocket.hpp" namespace Tesses::Framework::Http { class HttpRequestBody { public: virtual void HandleHeaders(HttpDictionary &dict); virtual void Write(std::shared_ptr strm) = 0; virtual ~HttpRequestBody(); }; class StreamHttpRequestBody : public HttpRequestBody { std::shared_ptr strm; std::string mimeType; public: StreamHttpRequestBody( std::shared_ptr strm, std::string mimeType); void HandleHeaders(HttpDictionary &dict); void Write(std::shared_ptr strm); ~StreamHttpRequestBody(); }; class TextHttpRequestBody : public HttpRequestBody { std::string text; std::string mimeType; public: TextHttpRequestBody(std::string text, std::string mimeType); void HandleHeaders(HttpDictionary &dict); void Write(std::shared_ptr strm); ~TextHttpRequestBody(); }; class HttpRequest { public: HttpRequest(); std::string trusted_root_cert_bundle; bool ignoreSSLErrors; bool followRedirects; std::string method; std::string url; std::string unixSocket; HttpDictionary requestHeaders; HttpRequestBody *body; static std::shared_ptr EstablishConnection(Uri uri, bool ignoreSSLErrors, std::string trusted_root_cert_bundle); static std::shared_ptr EstablishUnixPathConnection(std::string unixPath, Uri uri, bool ignoreSSLErrors, std::string trusted_root_cert_bundle); void SendRequest(std::shared_ptr strm); }; class HttpResponse { private: bool owns; std::shared_ptr handleStrm; public: HttpResponse(std::shared_ptr strm); HttpResponse(HttpRequest &request); std::string version; StatusCode statusCode; HttpDictionary responseHeaders; std::string ReadAsString(); std::shared_ptr ReadAsStream(); void CopyToStream(std::shared_ptr strm); std::shared_ptr GetInternalStream(); ~HttpResponse(); }; void DownloadToStreamSimple( std::string url, std::shared_ptr strm); void DownloadToFileSimple( std::string url, std::shared_ptr vfs, Tesses::Framework::Filesystem::VFSPath path); void DownloadToFileSimple(std::string url, Tesses::Framework::Filesystem::VFSPath path); std::string DownloadToStringSimple(std::string url); bool WebSocketClientSuccessDefault(HttpDictionary &dict, bool v); void WebSocketClient(std::string url, HttpDictionary &requestHeaders, std::shared_ptr wsc, std::function cb = WebSocketClientSuccessDefault); void DownloadUnixSocketToStreamSimple( std::string unixSocket, std::string url, std::shared_ptr strm); void DownloadUnixSocketToFileSimple( std::string unixSocket, std::string url, std::shared_ptr vfs, Tesses::Framework::Filesystem::VFSPath path); void DownloadUnixSocketToFileSimple( std::string unixSocket, std::string url, Tesses::Framework::Filesystem::VFSPath path); std::string DownloadUnixSocketToStringSimple(std::string unixSocket, std::string url); void WebSocketUnixSocketClient(std::string unixSocket, std::string url, HttpDictionary &requestHeaders, std::shared_ptr wsc, std::function cb = WebSocketClientSuccessDefault); } // namespace Tesses::Framework::Http