Skip to content

Commit

Permalink
refactor webdav client
Browse files Browse the repository at this point in the history
  • Loading branch information
cy33hc committed Feb 23, 2024
1 parent d35b311 commit 1e702aa
Show file tree
Hide file tree
Showing 24 changed files with 346 additions and 3,010 deletions.
11 changes: 2 additions & 9 deletions CMakeLists.txt
Expand Up @@ -22,12 +22,6 @@ add_executable(ezremote_client
source/imgui/imgui_widgets.cpp
source/imgui/imgui.cpp
source/pugixml/pugixml.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/http/httplib.cpp
source/clients/baseclient.cpp
source/clients/apache.cpp
Expand All @@ -38,9 +32,9 @@ add_executable(ezremote_client
source/clients/npxserve.cpp
source/clients/nfsclient.cpp
source/clients/smbclient.cpp
source/clients/webdavclient.cpp
source/clients/sftpclient.cpp
source/clients/rclone.cpp
source/clients/webdav.cpp
source/filehost/alldebrid.cpp
source/filehost/realdebrid.cpp
source/filehost/directhost.cpp
Expand Down Expand Up @@ -72,7 +66,7 @@ add_executable(ezremote_client

add_self(ezremote_client)

add_pkg(ezremote_client ${CMAKE_SOURCE_DIR}/data "RMTC00001" "ezRemote Client" "01.19" 32 0)
add_pkg(ezremote_client ${CMAKE_SOURCE_DIR}/data "RMTC00001" "ezRemote Client" "01.20" 32 0)

target_link_libraries(ezremote_client
c
Expand All @@ -88,7 +82,6 @@ target_link_libraries(ezremote_client
jbc
crypto
ssl
curl
lexbor
smb2
nfs
Expand Down
21 changes: 7 additions & 14 deletions source/actions.cpp
Expand Up @@ -8,7 +8,7 @@
#include "clients/gdrive.h"
#include "clients/ftpclient.h"
#include "clients/smbclient.h"
#include "clients/webdavclient.h"
#include "clients/webdav.h"
#include "clients/apache.h"
#include "clients/nginx.h"
#include "clients/npxserve.h"
Expand All @@ -25,8 +25,6 @@
#include "lang.h"
#include "actions.h"
#include "installer.h"
#include "web/request.hpp"
#include "web/urn.hpp"
#include "system.h"
#include "sfo.h"
#include "zip_util.h"
Expand Down Expand Up @@ -1102,14 +1100,14 @@ namespace Actions
std::string host = full_url.substr(0, path_pos);
std::string path = full_url.substr(path_pos);

WebDAV::WebDavClient tmp_client;
tmp_client.Connect(host.c_str(), install_pkg_url.username, install_pkg_url.password, false);
WebDAVClient tmp_client;
tmp_client.Connect(host.c_str(), install_pkg_url.username, install_pkg_url.password);

sprintf(activity_message, "%s URL to %s", lang_strings[STR_DOWNLOADING], filename);
int s = sizeof(pkg_header);
memset(&header, 0, s);
WebDAV::dict_t response_headers{};
int ret = tmp_client.GetHeaders(path.c_str(), &response_headers);

int ret = tmp_client.Size(path, &bytes_to_download);
if (!ret)
{
sprintf(status_message, "%s - %s", lang_strings[STR_FAILED], lang_strings[STR_CANNOT_READ_PKG_HDR_MSG]);
Expand All @@ -1119,13 +1117,8 @@ namespace Actions
return NULL;
}

auto content_length = WebDAV::get(response_headers, "content-length");
if (content_length.length() > 0)
bytes_to_download = std::stol(content_length);
else
bytes_to_download = 1;
file_transfering = 1;
int is_performed = tmp_client.Get(filename, path.c_str());
int is_performed = tmp_client.Get(path, filename);

if (is_performed == 0)
{
Expand Down Expand Up @@ -1213,7 +1206,7 @@ namespace Actions
}
else if (strncmp(remote_settings->server, "webdavs://", 10) == 0 || strncmp(remote_settings->server, "webdav://", 9) == 0)
{
remoteclient = new WebDAV::WebDavClient();
remoteclient = new WebDAVClient();
}
else if (strncmp(remote_settings->server, "smb://", 6) == 0)
{
Expand Down
47 changes: 13 additions & 34 deletions source/clients/baseclient.cpp
Expand Up @@ -10,6 +10,7 @@
using httplib::Client;
using httplib::Headers;
using httplib::Result;
using httplib::DataSink;

BaseClient::BaseClient(){};

Expand All @@ -21,15 +22,15 @@ BaseClient::~BaseClient()

int BaseClient::Connect(const std::string &url, const std::string &username, const std::string &password)
{
std::string scheme_host_port = url;
this->host_url = url;
size_t scheme_pos = url.find("://");
size_t root_pos = url.find("/", scheme_pos + 3);
if (root_pos != std::string::npos)
{
scheme_host_port = url.substr(0, root_pos);
this->host_url = url.substr(0, root_pos);
this->base_path = url.substr(root_pos);
}
client = new httplib::Client(scheme_host_port);
client = new httplib::Client(this->host_url);
if (username.length() > 0)
client->set_basic_auth(username, password);
client->set_keep_alive(true);
Expand Down Expand Up @@ -82,10 +83,13 @@ int BaseClient::Size(const std::string &path, int64_t *size)
{
if (auto res = client->Head(GetFullPath(path)))
{
std::string content_length = res->get_header_value("Content-Length");
if (content_length.length() > 0)
*size = atoll(content_length.c_str());
return 1;
if (HTTP_SUCCESS(res->status))
{
std::string content_length = res->get_header_value("Content-Length");
if (content_length.length() > 0)
*size = atoll(content_length.c_str());
return 1;
}
}
else
{
Expand Down Expand Up @@ -309,35 +313,10 @@ uint32_t BaseClient::SupportedActions()

std::string BaseClient::EncodeUrl(const std::string &url)
{
CURL *curl = curl_easy_init();
if (curl)
{
char *output = curl_easy_escape(curl, url.c_str(), url.length());
if (output)
{
std::string encoded_url = std::string(output);
curl_free(output);
return encoded_url;
}
curl_easy_cleanup(curl);
}
return "";
return httplib::detail::encode_url(url);
}

std::string BaseClient::DecodeUrl(const std::string &url)
{
CURL *curl = curl_easy_init();
if (curl)
{
int decode_len;
char *output = curl_easy_unescape(curl, url.c_str(), url.length(), &decode_len);
if (output)
{
std::string decoded_url = std::string(output, decode_len);
curl_free(output);
return decoded_url;
}
curl_easy_cleanup(curl);
}
return "";
return httplib::detail::decode_url(url, true);
}
1 change: 1 addition & 0 deletions source/clients/baseclient.h
Expand Up @@ -45,6 +45,7 @@ class BaseClient : public RemoteClient
protected:
httplib::Client *client;
std::string base_path;
std::string host_url;
char response[512];
bool connected = false;
};
Expand Down

0 comments on commit 1e702aa

Please sign in to comment.