iw4x-client/src/Utils/WebIO.hpp

117 lines
3.4 KiB
C++
Raw Normal View History

2017-01-19 22:23:59 +01:00
/*
This project is released under the GPL 2.0 license.
Some parts are based on research by Bas Timmer and the OpenSteamworks project.
Please do no evil.
Initial author: (https://github.com/)momo5502
Started: 2015-03-01
Notes:
Small FTP and HTTP utility class using WinAPI
*/
2017-01-20 14:36:52 +01:00
#pragma once
2017-01-19 22:23:59 +01:00
namespace Utils
{
class WebIO
{
public:
2023-01-28 23:38:24 +00:00
using params = std::map<std::string, std::string>;
2017-01-19 22:23:59 +01:00
WebIO();
2018-12-17 14:29:18 +01:00
WebIO(const std::string& useragent);
WebIO(const std::string& useragent, const std::string& url);
2017-01-19 22:23:59 +01:00
~WebIO();
void setURL(std::string url);
2018-12-17 14:29:18 +01:00
void setCredentials(const std::string& username, const std::string& password);
2017-01-19 22:23:59 +01:00
2018-12-17 14:29:18 +01:00
std::string postFile(const std::string& url, const std::string& data, const std::string& fieldName, const std::string& fileName);
std::string postFile(const std::string& data, std::string fieldName, std::string fileName);
2017-01-19 22:23:59 +01:00
2023-01-28 23:38:24 +00:00
std::string post(const std::string& url, const params& params, bool* success = nullptr);
2018-12-17 14:29:18 +01:00
std::string post(const std::string& url, const std::string& body, bool* success = nullptr);
2023-01-28 23:38:24 +00:00
std::string post(const params& params, bool* success = nullptr);
2018-12-17 14:29:18 +01:00
std::string post(const std::string& body, bool* success = nullptr);
2017-01-19 22:23:59 +01:00
2018-12-17 14:29:18 +01:00
std::string get(const std::string& url, bool* success = nullptr);
2017-05-15 21:57:45 +02:00
std::string get(bool* success = nullptr);
2017-01-19 22:23:59 +01:00
2023-01-28 23:38:24 +00:00
WebIO* setTimeout(DWORD msec);
2017-01-19 22:23:59 +01:00
// FTP
bool connect();
void disconnect(); // Not necessary
2018-12-17 14:29:18 +01:00
bool setDirectory(const std::string&directory);
2017-01-19 22:23:59 +01:00
bool setRelativeDirectory(std::string directory);
2023-01-28 23:38:24 +00:00
bool getDirectory(std::string* directory) const;
bool createDirectory(const std::string& directory) const;
2018-12-17 14:29:18 +01:00
bool deleteDirectory(const std::string& directory);
2023-01-28 23:38:24 +00:00
bool renameDirectory(const std::string& directory, const std::string& newDir) const;
2017-01-19 22:23:59 +01:00
2022-02-27 16:49:12 +00:00
bool listDirectories(const std::string& directory, std::vector<std::string>& list);
bool listFiles(const std::string& directory, std::vector<std::string>& list);
2017-01-19 22:23:59 +01:00
2023-01-28 23:38:24 +00:00
bool deleteFile(const std::string& file) const;
bool renameFile(const std::string& file, const std::string& newFile) const;
bool downloadFile(const std::string& file, const std::string& localFile) const;
bool uploadFile(const std::string& file, const std::string& localFile) const;
2017-01-19 22:23:59 +01:00
2018-12-17 14:29:18 +01:00
bool uploadFileData(const std::string& file,const std::string& data);
2022-02-27 16:49:12 +00:00
bool downloadFileData(const std::string& file, std::string& data);
2017-01-19 22:23:59 +01:00
void setProgressCallback(const Slot<void(std::size_t, std::size_t)>& callback);
2023-01-28 23:38:24 +00:00
void cancelDownload();
2017-05-15 21:57:45 +02:00
2017-01-19 22:23:59 +01:00
private:
enum Command
{
COMMAND_POST,
COMMAND_GET,
};
struct WebURL
{
std::string protocol;
std::string server;
std::string document;
std::string port;
std::string raw;
};
2023-01-28 23:38:24 +00:00
bool cancel_;
2017-05-15 21:57:45 +02:00
2023-01-28 23:38:24 +00:00
bool isFTP_;
std::string username_;
std::string password_;
2017-01-19 22:23:59 +01:00
2023-01-28 23:38:24 +00:00
WebURL url_;
2017-01-19 22:23:59 +01:00
2023-01-28 23:38:24 +00:00
HINTERNET hSession_;
HINTERNET hConnect_;
HINTERNET hFile_;
2017-01-19 22:23:59 +01:00
2023-01-28 23:38:24 +00:00
DWORD timeout_;
2017-01-19 22:23:59 +01:00
Slot<void(size_t, size_t)> progressCallback;
2017-05-15 21:57:45 +02:00
2023-01-28 23:38:24 +00:00
static std::string buildPostBody(const params& params);
2017-01-19 22:23:59 +01:00
2023-01-28 23:38:24 +00:00
[[nodiscard]] bool isSecuredConnection() const;
2017-01-19 22:23:59 +01:00
2023-01-28 23:38:24 +00:00
std::string execute(const char* command, const std::string& body, const params& headers, bool* success = nullptr);
2017-01-19 22:23:59 +01:00
2022-02-27 16:49:12 +00:00
bool listElements(const std::string& directory, std::vector<std::string>& list, bool files);
2017-01-19 22:23:59 +01:00
2018-12-17 14:29:18 +01:00
void openSession(const std::string& useragent);
2017-01-19 22:23:59 +01:00
void closeSession();
bool openConnection();
void closeConnection();
2023-01-28 23:38:24 +00:00
static void FormatPath(std::string& path, bool win); /* if (win == true): / -> \\ */
2017-01-19 22:23:59 +01:00
};
}