refactor web code

This commit is contained in:
Chee Yee
2023-02-15 01:30:36 -08:00
parent 88859b17f4
commit 2cf692df0c
17 changed files with 76 additions and 70 deletions
+6 -5
View File
@@ -7,18 +7,19 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fpermissive")
include_directories(
source
source/web
source/webdav
source/pugixml
)
add_executable(ezremote_client
source/pugixml/pugixml.cpp
source/webdav/callback.cpp
source/web/callback.cpp
source/web/fsinfo.cpp
source/web/header.cpp
source/web/request.cpp
source/web/urn.cpp
source/webdav/client.cpp
source/webdav/fsinfo.cpp
source/webdav/header.cpp
source/webdav/request.cpp
source/webdav/urn.cpp
source/actions.cpp
source/config.cpp
source/fs.cpp
+1
View File
@@ -114,3 +114,4 @@ STR_CHECKING_REMOTE_SERVER_MSG=Checking remote server for Remote Package Install
STR_ENABLE_RPI=RPI
STR_ENABLE_RPI_FTP_SMB_MSG=This option enables Remote Package Installation. This requires a HTTP Server setup on the same host sharing the same folder.
STR_ENABLE_RPI_WEBDAV_MSG=This option enables Remote Package Installation. This requires a WebDAV Server with anonymous access that does not need username/password.
STR_FILES=Files
+2 -2
View File
@@ -109,7 +109,7 @@ namespace INSTALLER
std::string host = full_url.substr(0, root_pos);
std::string path = full_url.substr(root_pos);
WebDAV::Urn::Path uri(path);
Web::Urn::Path uri(path);
CURL *curl = curl_easy_init();
path = uri.quote(curl);
curl_easy_cleanup(curl);
@@ -129,7 +129,7 @@ namespace INSTALLER
host = host.substr(0, port_pos);
std::string path = std::string(filename);
WebDAV::Urn::Path uri(path);
Web::Urn::Path uri(path);
CURL *curl = curl_easy_init();
path = uri.quote(curl);
return "http://" + host + ":" + std::to_string(remote_settings->http_port) + path;
+1
View File
@@ -128,6 +128,7 @@ char lang_strings[LANG_STRINGS_NUM][LANG_STR_SIZE] = {
"This requires a HTTP Server setup on the same host sharing the same folder.", // STR_ENABLE_RPI_FTP_SMB_MSG
"This option enables Remote Package Installation. "
"This requires a WebDAV Server with anonymous access that does not need username/password.", // STR_ENABLE_RPI_WEBDAV_MSG
"Files", // STR_FILES
};
bool needs_extended_font = false;
+3 -2
View File
@@ -119,7 +119,8 @@
FUNC(STR_CHECKING_REMOTE_SERVER_MSG) \
FUNC(STR_ENABLE_RPI) \
FUNC(STR_ENABLE_RPI_FTP_SMB_MSG) \
FUNC(STR_ENABLE_RPI_WEBDAV_MSG)
FUNC(STR_ENABLE_RPI_WEBDAV_MSG) \
FUNC(STR_FILES)
#define GET_VALUE(x) x,
#define GET_STRING(x) #x,
@@ -129,7 +130,7 @@ enum
FOREACH_STR(GET_VALUE)
};
#define LANG_STRINGS_NUM 116
#define LANG_STRINGS_NUM 117
#define LANG_ID_SIZE 64
#define LANG_STR_SIZE 384
extern char lang_identifiers[LANG_STRINGS_NUM][LANG_ID_SIZE];
@@ -26,7 +26,7 @@
#include "callback.hpp"
namespace WebDAV
namespace Web
{
namespace Callback
{
@@ -106,4 +106,4 @@ namespace WebDAV
}
} // namespace Append
} // namespace Callback
} // namespace WebDAV
} // namespace Web
@@ -20,10 +20,10 @@
#
############################################################################*/
#ifndef WEBDAV_CALLBACK_HPP
#define WEBDAV_CALLBACK_HPP
#ifndef WEB_CALLBACK_HPP
#define WEB_CALLBACK_HPP
namespace WebDAV
namespace Web
{
struct Data
{
@@ -62,6 +62,6 @@ namespace WebDAV
size_t buffer(char* data, size_t size, size_t count, void* buffer);
}
}
} // namespace WebDAV
} // namespace Web
#endif
@@ -23,7 +23,7 @@
#include "fsinfo.hpp"
#include <fstream>
namespace WebDAV
namespace Web
{
namespace FileInfo
{
@@ -39,4 +39,4 @@ namespace WebDAV
return static_cast<unsigned long long>(file.tellg());
}
} // namespace FileInfo
} // namespace WebDAV
} // namespace Web
@@ -20,19 +20,19 @@
#
############################################################################*/
#ifndef WEBDAV_FSINFO_HPP
#define WEBDAV_FSINFO_HPP
#ifndef WEB_FSINFO_HPP
#define WEB_FSINFO_HPP
#include <fstream>
#include <string>
namespace WebDAV
namespace Web
{
namespace FileInfo
{
auto exists(const std::string& path) -> bool;
auto size(const std::string& path_file) -> unsigned long long;
} // namespace FileInfo
} // namespace WebDAV
} // namespace Web
#endif
@@ -24,7 +24,7 @@
#include <curl/curl.h>
namespace WebDAV
namespace Web
{
Header::Header(const std::initializer_list<std::string>& init_list) noexcept : handle(nullptr)
{
@@ -66,4 +66,4 @@ namespace WebDAV
{
this->handle = curl_slist_append(reinterpret_cast<curl_slist*>(this->handle), item.c_str());
}
} // namespace WebDAV
} // namespace Web
@@ -20,13 +20,13 @@
#
############################################################################*/
#ifndef WEBDAV_HEADER_HPP
#define WEBDAV_HEADER_HPP
#ifndef WEB_HEADER_HPP
#define WEB_HEADER_HPP
#include <initializer_list>
#include <string>
namespace WebDAV
namespace Web
{
class Header final
{
@@ -46,6 +46,6 @@ namespace WebDAV
private:
auto swap(Header& other) noexcept -> void;
};
} // namespace WebDAV
} // namespace Web
#endif
@@ -20,8 +20,8 @@
#
############################################################################*/
#ifndef WEBDAV_PUGIEXT_HPP
#define WEBDAV_PUGIEXT_HPP
#ifndef WEB_PUGIEXT_HPP
#define WEB_PUGIEXT_HPP
#include <pugixml.hpp>
@@ -24,7 +24,7 @@
#include "fsinfo.hpp"
#include "util.h"
namespace WebDAV
namespace Web
{
static int sockopt_callback(void *clientp, curl_socket_t curlfd, curlsocktype purpose)
{
@@ -56,9 +56,9 @@ namespace WebDAV
Request::Request(dict_t &&options_) : options(options_)
{
auto webdav_hostname = get(options, "webdav_hostname");
auto webdav_username = get(options, "webdav_username");
auto webdav_password = get(options, "webdav_password");
auto hostname = get(options, "hostname");
auto username = get(options, "username");
auto password = get(options, "password");
auto proxy_hostname = get(options, "proxy_hostname");
auto proxy_username = get(options, "proxy_username");
@@ -85,11 +85,11 @@ namespace WebDAV
this->set(CURLOPT_SSLKEY, const_cast<char *>(key_path.c_str()));
}
this->set(CURLOPT_URL, const_cast<char *>(webdav_hostname.c_str()));
if (!webdav_username.empty())
this->set(CURLOPT_URL, const_cast<char *>(hostname.c_str()));
if (!username.empty())
{
this->set(CURLOPT_HTTPAUTH, static_cast<int>(CURLAUTH_BASIC));
auto token = webdav_username + ":" + webdav_password;
auto token = username + ":" + password;
this->set(CURLOPT_USERPWD, const_cast<char *>(token.c_str()));
}
this->set(CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
@@ -113,7 +113,7 @@ namespace WebDAV
}
else
{
if (!webdav_username.empty() && !webdav_password.empty())
if (!username.empty() && !password.empty())
{
auto token = proxy_username + ":" + proxy_password;
this->set(CURLOPT_PROXYUSERPWD, const_cast<char *>(token.c_str()));
@@ -201,4 +201,4 @@ namespace WebDAV
{
return this->res;
}
} // namespace WebDAV
} // namespace Web
@@ -20,14 +20,14 @@
#
############################################################################*/
#ifndef WEBDAV_REQUEST_HPP
#define WEBDAV_REQUEST_HPP
#ifndef WEB_REQUEST_HPP
#define WEB_REQUEST_HPP
#include <curl/curl.h>
#include <map>
#include <string>
namespace WebDAV
namespace Web
{
bool inline check_code(CURLcode code)
{
@@ -69,6 +69,6 @@ namespace WebDAV
bool cert_required() const noexcept;
auto swap(Request &other) noexcept -> void;
};
} // namespace WebDAV
} // namespace Web
#endif
+3 -3
View File
@@ -33,7 +33,7 @@ using std::vector;
#include "urn.hpp"
namespace WebDAV
namespace Web
{
namespace Urn
{
@@ -242,9 +242,9 @@ namespace WebDAV
return lhs_path == rhs_path;
}
} // namespace Urn
} // namespace WebDAV
} // namespace Web
auto operator<<(std::ostream& stream, const WebDAV::Urn::Path& path) -> std::ostream&
auto operator<<(std::ostream& stream, const Web::Urn::Path& path) -> std::ostream&
{
return stream << path.path();
}
+4 -4
View File
@@ -20,14 +20,14 @@
#
############################################################################*/
#ifndef WEBDAV_URN_HPP
#define WEBDAV_URN_HPP
#ifndef WEB_URN_HPP
#define WEB_URN_HPP
#include <cstddef>
#include <iostream>
#include <string>
namespace WebDAV
namespace Web
{
namespace Urn
{
@@ -63,6 +63,6 @@ namespace WebDAV
}
}
auto operator<<(std::ostream& stream, const WebDAV::Urn::Path& path) -> std::ostream&;
auto operator<<(std::ostream& stream, const Web::Urn::Path& path) -> std::ostream&;
#endif
+23 -21
View File
@@ -34,7 +34,10 @@
namespace WebDAV
{
using Urn::Path;
using Web::Urn::Path;
using Web::Request;
using Web::Data;
using Web::Header;
using progress_funptr = int (*)(void *context, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow);
@@ -62,10 +65,9 @@ namespace WebDAV
Client::options()
{
return dict_t{
{"webdav_hostname", this->webdav_hostname},
{"webdav_root", this->webdav_root},
{"webdav_username", this->webdav_username},
{"webdav_password", this->webdav_password},
{"hostname", this->webdav_hostname},
{"username", this->webdav_username},
{"password", this->webdav_password},
{"proxy_hostname", this->proxy_hostname},
{"proxy_username", this->proxy_username},
{"proxy_password", this->proxy_password},
@@ -104,7 +106,7 @@ namespace WebDAV
request.set(CURLOPT_URL, url.c_str());
request.set(CURLOPT_HEADER, 0L);
request.set(CURLOPT_WRITEDATA, reinterpret_cast<size_t>(&file_stream));
request.set(CURLOPT_WRITEFUNCTION, reinterpret_cast<size_t>(Callback::Write::stream));
request.set(CURLOPT_WRITEFUNCTION, reinterpret_cast<size_t>(Web::Callback::Write::stream));
#ifdef WDC_VERBOSE
request.set(CURLOPT_VERBOSE, 1);
#endif
@@ -149,7 +151,7 @@ namespace WebDAV
request.set(CURLOPT_URL, url.c_str());
request.set(CURLOPT_HEADER, 0L);
request.set(CURLOPT_WRITEDATA, reinterpret_cast<size_t>(&data));
request.set(CURLOPT_WRITEFUNCTION, reinterpret_cast<size_t>(Callback::Append::buffer));
request.set(CURLOPT_WRITEFUNCTION, reinterpret_cast<size_t>(Web::Callback::Append::buffer));
#ifdef WDC_VERBOSE
request.set(CURLOPT_VERBOSE, 1);
#endif
@@ -207,7 +209,7 @@ bool
request.set(CURLOPT_HEADER, 0L);
request.set(CURLOPT_HTTPHEADER, list);
request.set(CURLOPT_WRITEDATA, reinterpret_cast<size_t>(&data));
request.set(CURLOPT_WRITEFUNCTION, reinterpret_cast<size_t>(Callback::Append::buffer));
request.set(CURLOPT_WRITEFUNCTION, reinterpret_cast<size_t>(Web::Callback::Append::buffer));
#ifdef WDC_VERBOSE
request.set(CURLOPT_VERBOSE, 1);
#endif
@@ -255,7 +257,7 @@ bool
request.set(CURLOPT_URL, url.c_str());
request.set(CURLOPT_HEADER, 0L);
request.set(CURLOPT_WRITEDATA, reinterpret_cast<size_t>(&stream));
request.set(CURLOPT_WRITEFUNCTION, reinterpret_cast<size_t>(Callback::Write::stream));
request.set(CURLOPT_WRITEFUNCTION, reinterpret_cast<size_t>(Web::Callback::Write::stream));
#ifdef WDC_VERBOSE
request.set(CURLOPT_VERBOSE, 1);
#endif
@@ -283,7 +285,7 @@ bool
progress_data_t progress_data,
progress_t progress)
{
bool is_existed = FileInfo::exists(local_file);
bool is_existed = Web::FileInfo::exists(local_file);
if (!is_existed)
return false;
@@ -291,7 +293,7 @@ bool
auto file_urn = root_urn + remote_file;
std::ifstream file_stream(local_file, std::ios::binary);
auto size = FileInfo::size(local_file);
auto size = Web::FileInfo::size(local_file);
Request request(this->options());
@@ -302,11 +304,11 @@ bool
request.set(CURLOPT_UPLOAD, 1L);
request.set(CURLOPT_URL, url.c_str());
request.set(CURLOPT_READDATA, reinterpret_cast<size_t>(&file_stream));
request.set(CURLOPT_READFUNCTION, reinterpret_cast<size_t>(Callback::Read::stream));
request.set(CURLOPT_READFUNCTION, reinterpret_cast<size_t>(Web::Callback::Read::stream));
request.set(CURLOPT_INFILESIZE_LARGE, static_cast<curl_off_t>(size));
request.set(CURLOPT_BUFFERSIZE, static_cast<long>(Client::buffer_size));
request.set(CURLOPT_WRITEDATA, reinterpret_cast<size_t>(&response));
request.set(CURLOPT_WRITEFUNCTION, reinterpret_cast<size_t>(Callback::Append::buffer));
request.set(CURLOPT_WRITEFUNCTION, reinterpret_cast<size_t>(Web::Callback::Append::buffer));
#ifdef WDC_VERBOSE
request.set(CURLOPT_VERBOSE, 1);
#endif
@@ -348,11 +350,11 @@ bool
request.set(CURLOPT_UPLOAD, 1L);
request.set(CURLOPT_URL, url.c_str());
request.set(CURLOPT_READDATA, reinterpret_cast<size_t>(&data));
request.set(CURLOPT_READFUNCTION, reinterpret_cast<size_t>(Callback::Read::buffer));
request.set(CURLOPT_READFUNCTION, reinterpret_cast<size_t>(Web::Callback::Read::buffer));
request.set(CURLOPT_INFILESIZE_LARGE, static_cast<curl_off_t>(buffer_size));
request.set(CURLOPT_BUFFERSIZE, static_cast<long>(Client::buffer_size));
request.set(CURLOPT_WRITEDATA, reinterpret_cast<size_t>(&response));
request.set(CURLOPT_WRITEFUNCTION, reinterpret_cast<size_t>(Callback::Append::buffer));
request.set(CURLOPT_WRITEFUNCTION, reinterpret_cast<size_t>(Web::Callback::Append::buffer));
#ifdef WDC_VERBOSE
request.set(CURLOPT_VERBOSE, 1);
#endif
@@ -396,11 +398,11 @@ bool
request.set(CURLOPT_UPLOAD, 1L);
request.set(CURLOPT_URL, url.c_str());
request.set(CURLOPT_READDATA, reinterpret_cast<size_t>(&stream));
request.set(CURLOPT_READFUNCTION, reinterpret_cast<size_t>(Callback::Read::stream));
request.set(CURLOPT_READFUNCTION, reinterpret_cast<size_t>(Web::Callback::Read::stream));
request.set(CURLOPT_INFILESIZE_LARGE, static_cast<curl_off_t>(stream_size));
request.set(CURLOPT_BUFFERSIZE, static_cast<long>(Client::buffer_size));
request.set(CURLOPT_WRITEDATA, reinterpret_cast<size_t>(&response));
request.set(CURLOPT_WRITEFUNCTION, reinterpret_cast<size_t>(Callback::Append::buffer));
request.set(CURLOPT_WRITEFUNCTION, reinterpret_cast<size_t>(Web::Callback::Append::buffer));
#ifdef WDC_VERBOSE
request.set(CURLOPT_VERBOSE, 1);
#endif
@@ -471,7 +473,7 @@ bool
request.set(CURLOPT_POSTFIELDSIZE, static_cast<long>(size));
request.set(CURLOPT_HEADER, 0);
request.set(CURLOPT_WRITEDATA, reinterpret_cast<size_t>(&data));
request.set(CURLOPT_WRITEFUNCTION, reinterpret_cast<size_t>(Callback::Append::buffer));
request.set(CURLOPT_WRITEFUNCTION, reinterpret_cast<size_t>(Web::Callback::Append::buffer));
#ifdef WDC_VERBOSE
request.set(CURLOPT_VERBOSE, 1);
#endif
@@ -517,7 +519,7 @@ bool
request.set(CURLOPT_URL, url.c_str());
request.set(CURLOPT_HTTPHEADER, reinterpret_cast<curl_slist *>(header.handle));
request.set(CURLOPT_WRITEDATA, reinterpret_cast<size_t>(&data));
request.set(CURLOPT_WRITEFUNCTION, reinterpret_cast<size_t>(Callback::Append::buffer));
request.set(CURLOPT_WRITEFUNCTION, reinterpret_cast<size_t>(Web::Callback::Append::buffer));
#ifdef WDC_VERBOSE
request.set(CURLOPT_VERBOSE, 1);
#endif
@@ -549,7 +551,7 @@ bool
request.set(CURLOPT_URL, url.c_str());
request.set(CURLOPT_HTTPHEADER, reinterpret_cast<curl_slist *>(header.handle));
request.set(CURLOPT_WRITEDATA, reinterpret_cast<size_t>(&data));
request.set(CURLOPT_WRITEFUNCTION, reinterpret_cast<size_t>(Callback::Append::buffer));
request.set(CURLOPT_WRITEFUNCTION, reinterpret_cast<size_t>(Web::Callback::Append::buffer));
#ifdef WDC_VERBOSE
request.set(CURLOPT_VERBOSE, 1);
#endif
@@ -671,7 +673,7 @@ bool
request.set(CURLOPT_HTTPHEADER, reinterpret_cast<curl_slist *>(header.handle));
request.set(CURLOPT_HEADER, 0);
request.set(CURLOPT_WRITEDATA, reinterpret_cast<size_t>(&data));
request.set(CURLOPT_WRITEFUNCTION, reinterpret_cast<size_t>(Callback::Append::buffer));
request.set(CURLOPT_WRITEFUNCTION, reinterpret_cast<size_t>(Web::Callback::Append::buffer));
#ifdef WDC_VERBOSE
request.set(CURLOPT_VERBOSE, 1);
#endif