From 43aeea8eb7965e41f18ae14f3cd3db5e41e77ed7 Mon Sep 17 00:00:00 2001 From: xiphon Date: Wed, 29 Jul 2020 17:43:02 +0000 Subject: Network: instantiable QML type, introduce proxyAddress property # Conflicts: # main.qml --- src/qt/network.cpp | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) (limited to 'src/qt/network.cpp') diff --git a/src/qt/network.cpp b/src/qt/network.cpp index 1a674508..93f18300 100644 --- a/src/qt/network.cpp +++ b/src/qt/network.cpp @@ -35,7 +35,7 @@ using epee::net_utils::http::fields_list; using epee::net_utils::http::http_response_info; -using epee::net_utils::http::http_simple_client; +using epee::net_utils::http::abstract_http_client; HttpClient::HttpClient(QObject *parent /* = nullptr */) : QObject(parent) @@ -78,7 +78,7 @@ bool HttpClient::on_header(const http_response_info &headers) m_received = 0; emit receivedChanged(); - return http_simple_client::on_header(headers); + return net::http::client::on_header(headers); } bool HttpClient::handle_target_data(std::string &piece_of_transfer) @@ -91,7 +91,7 @@ bool HttpClient::handle_target_data(std::string &piece_of_transfer) m_received += piece_of_transfer.size(); emit receivedChanged(); - return http_simple_client::handle_target_data(piece_of_transfer); + return net::http::client::handle_target_data(piece_of_transfer); } Network::Network(QObject *parent) @@ -104,8 +104,12 @@ void Network::get(const QString &url, const QJSValue &callback, const QString &c { m_scheduler.run( [this, url, contentType] { + std::shared_ptr httpClient = newClient(); + if (httpClient.get() == nullptr) + { + return QJSValueList({url, "", "failed to initialize a client"}); + } std::string response; - std::shared_ptr httpClient(new http_simple_client()); QString error = get(httpClient, url, response, contentType); return QJSValueList({url, QString::fromStdString(response), error}); }, @@ -120,7 +124,12 @@ void Network::getJSON(const QString &url, const QJSValue &callback) const std::string Network::get(const QString &url, const QString &contentType /* = {} */) const { std::string response; - QString error = get(std::shared_ptr(new http_simple_client()), url, response, contentType); + std::shared_ptr httpClient = newClient(); + if (httpClient.get() == nullptr) + { + throw std::runtime_error("failed to initialize a client"); + } + QString error = get(httpClient, url, response, contentType); if (!error.isEmpty()) { throw std::runtime_error(QString("failed to fetch %1: %2").arg(url).arg(error).toStdString()); @@ -129,7 +138,7 @@ std::string Network::get(const QString &url, const QString &contentType /* = {} } QString Network::get( - std::shared_ptr httpClient, + std::shared_ptr httpClient, const QString &url, std::string &response, const QString &contentType /* = {} */) const @@ -163,3 +172,13 @@ QString Network::get( response = std::move(pri->m_body); return {}; } + +std::shared_ptr Network::newClient() const +{ + std::shared_ptr client(new net::http::client()); + if (!client->set_proxy(m_proxyAddress.toStdString())) + { + throw std::runtime_error("failed to set proxy address"); + } + return client; +} -- cgit v1.2.3