Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use POCO instead of QNetworkAccessManager to download urls #3708

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions 3rd-party/vcpkg-ports/poco/portfile.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,17 @@ vcpkg_cmake_configure(
-DPOCO_MT=${POCO_MT}
-DENABLE_TESTS=OFF
# Allow enabling and disabling components
-DENABLE_NETSSL=OFF
-DENABLE_NETSSL=ON
-DENABLE_CRYPTO=OFF
-DENABLE_JWT=OFF
-DENABLE_ENCODINGS=OFF
-DENABLE_XML=OFF
-DENABLE_JSON=OFF
-DENABLE_XML=ON
-DENABLE_JSON=ON
-DENABLE_MONGODB=OFF
-DENABLE_DATA_SQLITE=OFF
-DENABLE_REDIS=OFF
-DENABLE_UTIL=OFF
-DENABLE_NET=OFF
-DENABLE_NET=ON
-DENABLE_ZIP=ON
-DENABLE_PAGECOMPILER=OFF
-DENABLE_PAGECOMPILER_FILE2PAGE=OFF
Expand Down
3 changes: 2 additions & 1 deletion 3rd-party/vcpkg-ports/poco/vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
"name": "poco",
"default-features": false,
"features": [
"crypto"
"crypto",
"netssl"
]
}
]
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ add_subdirectory(3rd-party)
find_package(Qt6 COMPONENTS Core Concurrent Network REQUIRED)

# POCO config
find_package(Poco REQUIRED COMPONENTS Foundation Zip)
find_package(Poco REQUIRED COMPONENTS Foundation Zip Net NetSSL)

function(determine_version OUTPUT_VARIABLE)
# use upstream repo as the authoritative reference when checking for release status
Expand Down
15 changes: 10 additions & 5 deletions include/multipass/cli/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include <QString>

#include <grpc++/grpc++.h>
#include <Poco/Net/SocketAddress.h>
#include <Poco/Net/StreamSocket.h>

namespace multipass
{
Expand Down Expand Up @@ -102,14 +104,17 @@ class Command : private DisabledCopyMove
if (tokens[0] == "unix")
{
socket_address = tokens[1];
QLocalSocket multipassd_socket;
multipassd_socket.connectToServer(QString::fromStdString(socket_address));
if (!multipassd_socket.waitForConnected() &&
multipassd_socket.error() == QLocalSocket::SocketAccessError)
try
{
Poco::Net::SocketAddress local_address(socket_address);
Poco::Net::StreamSocket socket;
socket.connect(local_address, Poco::Timespan(5, 0)); // 5 seconds timeout
}
catch (const Poco::Exception& e)
{
grpc::Status denied_status{
grpc::StatusCode::PERMISSION_DENIED, "multipass socket access denied",
fmt::format("Please check that you have read/write permissions to '{}'", socket_address)};
fmt::format("Please check that you have read/write permissions to '{}': {}", socket_address, e.displayText())};
return handle_failure(denied_status);
}
}
Expand Down
37 changes: 29 additions & 8 deletions include/multipass/network_access_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,47 @@
#ifndef MULTIPASS_NETWORK_ACCESS_MANAGER_H
#define MULTIPASS_NETWORK_ACCESS_MANAGER_H

#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <Poco/Net/HTTPClientSession.h>
#include <Poco/Net/HTTPRequest.h>
#include <Poco/Net/HTTPResponse.h>
#include <Poco/Net/StreamSocket.h>
#include <Poco/Net/HTMLForm.h>
#include <Poco/Net/PartSource.h>

#include <QString>
#include <QUrl>
#include <QByteArray>
#include <map>
#include <vector>
#include <memory>

namespace multipass
{

class NetworkAccessManager : public QNetworkAccessManager
class NetworkAccessManager
{
Q_OBJECT
public:
using UPtr = std::unique_ptr<NetworkAccessManager>;
NetworkAccessManager();
~NetworkAccessManager();

NetworkAccessManager(QObject* parent = nullptr);
QByteArray sendRequest(const QUrl& url, const std::string& method, const QByteArray& data = QByteArray(),
const std::map<std::string, std::string>& headers = {});

protected:
QNetworkReply* createRequest(Operation op, const QNetworkRequest& orig_request,
QIODevice* outgoingData = nullptr) override;
// New method for multipart requests
QByteArray sendMultipartRequest(const QUrl& url, const std::string& method,
const std::vector<std::pair<std::string, Poco::Net::PartSource*>>& parts,
const std::map<std::string, std::string>& headers = {});

private:
QByteArray sendUnixRequest(const QUrl& url, const std::string& method, const QByteArray& data,
const std::map<std::string, std::string>& headers);

QByteArray sendUnixMultipartRequest(const QUrl& url, const std::string& method,
const std::vector<std::pair<std::string, Poco::Net::PartSource*>>& parts,
const std::map<std::string, std::string>& headers);
};

} // namespace multipass

#endif // MULTIPASS_NETWORK_ACCESS_MANAGER_H
22 changes: 7 additions & 15 deletions include/multipass/url_downloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,29 @@
#include "disabled_copy_move.h"
#include "path.h"
#include "progress_monitor.h"
#include "singleton.h"

#include <QByteArray>
#include <QDateTime>
#include <QNetworkAccessManager>
#include <QUrl>
#include <QString>

#include <atomic>
#include <chrono>

#define MP_NETMGRFACTORY multipass::NetworkManagerFactory::instance()

class QUrl;
class QString;
namespace multipass
{
class NetworkManagerFactory : public Singleton<NetworkManagerFactory>
{
public:
NetworkManagerFactory(const Singleton<NetworkManagerFactory>::PrivatePass&) noexcept;

virtual std::unique_ptr<QNetworkAccessManager> make_network_manager(const Path& cache_dir_path) const;
};

class URLDownloader : private DisabledCopyMove
{
public:
URLDownloader(std::chrono::milliseconds timeout);
URLDownloader(const Path& cache_dir, std::chrono::milliseconds timeout);
virtual ~URLDownloader() = default;
virtual void download_to(const QUrl& url, const QString& file_name, int64_t size, const int download_type,

virtual void download_to(const QUrl& url, const QString& file_name, int64_t size, int download_type,
const ProgressMonitor& monitor);
virtual QByteArray download(const QUrl& url);
virtual QByteArray download(const QUrl& url, const bool is_force_update_from_network);
virtual QByteArray download(const QUrl& url, bool is_force_update_from_network);
virtual QDateTime last_modified(const QUrl& url);
virtual void abort_all_downloads();

Expand All @@ -64,5 +54,7 @@ class URLDownloader : private DisabledCopyMove
const Path cache_dir_path;
std::chrono::milliseconds timeout;
};

} // namespace multipass

#endif // MULTIPASS_URL_DOWNLOADER_H
4 changes: 3 additions & 1 deletion src/network/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ target_link_libraries(network
fmt
logger
Qt6::Core
Qt6::Network)
Qt6::Network
Poco::Net
Poco::NetSSL)
Loading