// Copyright (C) 2024 Jérôme "SirLynix" Leclercq (lynix680@gmail.com) // This file is part of the "Nazara Engine - Network module" // For conditions of distribution and use, see copyright notice in Config.hpp #pragma once #ifndef NAZARA_NETWORK_WEBREQUEST_HPP #define NAZARA_NETWORK_WEBREQUEST_HPP #include #include #include #include #include #include #include #include #include #ifndef NAZARA_PLATFORM_WEB struct curl_slist; #else struct emscripten_fetch_attr_t; #endif namespace Nz { class WebService; class NAZARA_NETWORK_API WebRequest { friend WebService; public: using DataCallback = std::function; using ProgressCallback = std::function; using ResultCallback = std::function; WebRequest(WebService& owner); WebRequest(const WebRequest&) = delete; WebRequest(WebRequest&&) = default; ~WebRequest(); void ForceProtocol(NetProtocol protocol); inline void SetDataCallback(DataCallback callback); inline void SetHeader(std::string header, std::string value); void SetJSonContent(std::string encodedJSon); void SetMaximumFileSize(UInt64 maxFileSize); inline void SetOptions(WebRequestOptionFlags options); inline void SetProgressCallback(ProgressCallback callback); inline void SetResultCallback(ResultCallback callback); void SetServiceName(std::string serviceName); void SetURL(const std::string& url); void SetupGet(); void SetupPost(); WebRequest& operator=(const WebRequest&) = delete; WebRequest& operator=(WebRequest&&) = delete; private: inline bool OnBodyResponse(const char* data, std::size_t length); #ifndef NAZARA_PLATFORM_WEB CURL* Prepare(); #else inline emscripten_fetch_t* GetFetchHandle() const; inline Nz::Time GetRequestTime() const; emscripten_fetch_t* Prepare(emscripten_fetch_attr_t* fetchAttr); inline void StopClock(); #endif inline void TriggerErrorCallback(std::string errorMessage); inline void TriggerSuccessCallback(); #ifdef NAZARA_PLATFORM_WEB std::string m_httpMethod; std::string m_url; std::vector m_requestHeaders; #endif std::string m_content; std::string m_responseBody; std::unordered_map m_headers; WebService& m_webService; DataCallback m_dataCallback; #ifndef NAZARA_PLATFORM_WEB MovablePtr m_curlHandle; MovablePtr m_headerList; #else HighPrecisionClock m_clock; MovablePtr m_fetchHandle; #endif ProgressCallback m_progressCallback; ResultCallback m_resultCallback; WebRequestOptionFlags m_options; bool m_isUserAgentSet; }; } #include #endif // NAZARA_NETWORK_WEBREQUEST_HPP