From 40ac1b95ff57fa88adbe452de59e341adeafc46a Mon Sep 17 00:00:00 2001 From: Chee Yee Date: Wed, 15 Feb 2023 20:35:22 -0800 Subject: [PATCH] prepare for a http client --- CMakeLists.txt | 3 +- source/actions.cpp | 4 +- source/http/client.cpp | 100 +++++++++++++++++++++++++++++++++++++++ source/http/client.hpp | 68 ++++++++++++++++++++++++++ source/installer.cpp | 4 +- source/webdav/client.cpp | 14 +++--- source/windows.cpp | 2 +- 7 files changed, 181 insertions(+), 14 deletions(-) create mode 100644 source/http/client.cpp create mode 100644 source/http/client.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 54dd055..819506a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,8 +7,6 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fpermissive") include_directories( source - source/web - source/webdav source/pugixml ) @@ -20,6 +18,7 @@ add_executable(ezremote_client source/web/request.cpp source/web/urn.cpp source/webdav/client.cpp + source/http/client.cpp source/actions.cpp source/config.cpp source/fs.cpp diff --git a/source/actions.cpp b/source/actions.cpp index 6aca8ba..c6a362e 100644 --- a/source/actions.cpp +++ b/source/actions.cpp @@ -12,8 +12,8 @@ #include "lang.h" #include "actions.h" #include "installer.h" -#include "request.hpp" -#include "urn.hpp" +#include "web/request.hpp" +#include "web/urn.hpp" #include "rtc.h" #include "ftpclient.h" #include "smbclient.h" diff --git a/source/http/client.cpp b/source/http/client.cpp new file mode 100644 index 0000000..67e1674 --- /dev/null +++ b/source/http/client.cpp @@ -0,0 +1,100 @@ +#include +#include "web/callback.hpp" +#include "web/fsinfo.hpp" +#include "web/header.hpp" +#include "web/pugiext.hpp" +#include "web/request.hpp" +#include "web/urn.hpp" +#include "util.h" +#include +#include + +namespace HTTP +{ + using Web::Data; + using Web::Header; + using Web::Request; + using Web::Urn::Path; + + using progress_funptr = int (*)(void *context, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow); + + static size_t header_callback(char *buffer, size_t size, size_t nitems, void *userdata) + { + std::string header(reinterpret_cast(buffer), size * nitems); + dict_t *headers = (dict_t *)userdata; + size_t seperator = header.find_first_of(":"); + + if (seperator != std::string::npos) + { + std::string key = header.substr(0, seperator); + key = Util::Trim(key, " "); + key = Util::ToLower(key); + std::string value = header.substr(seperator + 1); + value = Util::Trim(value, " "); + headers->erase(key); + headers->insert(std::make_pair(key, value)); + } + + return (size * nitems); + } + + Client::Client(const dict_t &options) + { + this->username = get(options, "username"); + this->password = get(options, "password"); + this->proxy_hostname = get(options, "proxy_hostname"); + this->proxy_username = get(options, "proxy_username"); + this->proxy_password = get(options, "proxy_password"); + this->cert_path = get(options, "cert_path"); + this->key_path = get(options, "key_path"); + } + + dict_t Client::options() + { + return dict_t{ + {"username", this->username}, + {"password", this->password}, + {"proxy_hostname", this->proxy_hostname}, + {"proxy_username", this->proxy_username}, + {"proxy_password", this->proxy_password}, + {"cert_path", this->cert_path}, + {"key_path", this->key_path}, + }; + } + + bool Client::Head(const std::string &url, dict_t *request_headers, Response &response) + { + return true; + }; + + bool Client::Get(const std::string &url, dict_t *request_headers, Response &Response) + { + return true; + }; + + bool Client::Delete(const std::string &url, void *post_data, dict_t *request_headers, Response &Response) + { + return true; + }; + + bool Client::Post(const std::string &url, void *post_data, dict_t *request_headers, Response &Response) + { + return true; + }; + + bool Client::Patch(const std::string &url, void *post_data, dict_t *request_headers, Response &Response) + { + return true; + }; + + bool Client::Put(const std::string &url, void *post_data, dict_t *request_headers, Response &Response) + { + return true; + }; + + bool Client::download(const std::string &url, const std::string &local_file, + progress_data_t progress_data, progress_t progress) + { + return true; + }; +} \ No newline at end of file diff --git a/source/http/client.hpp b/source/http/client.hpp new file mode 100644 index 0000000..16cae19 --- /dev/null +++ b/source/http/client.hpp @@ -0,0 +1,68 @@ +#ifndef HTTP_CLIENT_HPP +#define HTTP_CLIENT_HPP + +#include +#include +#include +#include +#include +#include +#include "web/callback.hpp" + +namespace HTTP +{ + using progress_data_t = void *; + + using progress_t = int(void *context, + curl_off_t dltotal, + curl_off_t dlnow, + curl_off_t ultotal, + curl_off_t ulnow); + + using callback_t = std::function; + using dict_t = std::map; + + auto inline get(const dict_t &options, const std::string &&name) -> std::string + { + auto it = options.find(name); + if (it == options.end()) + return ""; + else + return it->second; + } + + struct Response + { + int err; + int status_code; + Web::Data body; + dict_t headers; + }; + + class Client + { + public: + Client(const dict_t &options); + bool Head(const std::string &url, dict_t *request_headers, Response &response); + bool Get(const std::string &url, dict_t *request_headers, Response &Response); + bool Delete(const std::string &url, void *post_data, dict_t *request_headers, Response &Response); + bool Post(const std::string &url, void *post_data, dict_t *request_headers, Response &Response); + bool Patch(const std::string &url, void *post_data, dict_t *request_headers, Response &Response); + bool Put(const std::string &url, void *post_data, dict_t *request_headers, Response &Response); + bool download(const std::string &url, const std::string &local_file, + progress_data_t progress_data = nullptr, progress_t progress = nullptr); + dict_t options(); + + private: + std::string username; + std::string password; + + std::string proxy_hostname; + std::string proxy_username; + std::string proxy_password; + + std::string cert_path; + std::string key_path; + }; +} +#endif \ No newline at end of file diff --git a/source/installer.cpp b/source/installer.cpp index 4f31301..52db4d0 100644 --- a/source/installer.cpp +++ b/source/installer.cpp @@ -10,8 +10,8 @@ #include #include #include -#include -#include +#include +#include #include "installer.h" #include "util.h" #include "config.h" diff --git a/source/webdav/client.cpp b/source/webdav/client.cpp index b50200f..cafdd1e 100644 --- a/source/webdav/client.cpp +++ b/source/webdav/client.cpp @@ -20,14 +20,14 @@ # ############################################################################*/ -#include +#include -#include "callback.hpp" -#include "fsinfo.hpp" -#include "header.hpp" -#include "pugiext.hpp" -#include "request.hpp" -#include "urn.hpp" +#include "web/callback.hpp" +#include "web/fsinfo.hpp" +#include "web/header.hpp" +#include "web/pugiext.hpp" +#include "web/request.hpp" +#include "web/urn.hpp" #include "util.h" #include #include diff --git a/source/windows.cpp b/source/windows.cpp index e9e7571..7eb7115 100644 --- a/source/windows.cpp +++ b/source/windows.cpp @@ -250,7 +250,7 @@ namespace Windows BeginGroupPanel(title, ImVec2(1905, 100)); ImGui::SetCursorPosY(ImGui::GetCursorPosY() + 10); char id[256]; - std::string hidden_password = std::string("xxxxxxx"); + std::string hidden_password = (strlen(remote_settings->password) > 0)? std::string("*******") : ""; ImVec2 pos; ImGui::SetCursorPosX(ImGui::GetCursorPosX() + 4);