Skip to content

Commit ad06387

Browse files
author
Chris Townsend
committed
url_downloader: Detect proxy env var and use it if found
Fixes #885
1 parent 9125a51 commit ad06387

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

include/multipass/url_downloader.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2017-2019 Canonical, Ltd.
2+
* Copyright (C) 2017-2020 Canonical, Ltd.
33
*
44
* This program is free software; you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License as published by
@@ -23,9 +23,11 @@
2323

2424
#include <QByteArray>
2525
#include <QDateTime>
26+
#include <QNetworkProxy>
2627

2728
#include <atomic>
2829
#include <chrono>
30+
#include <memory>
2931

3032
class QUrl;
3133
class QString;
@@ -52,6 +54,7 @@ class URLDownloader
5254

5355
const Path cache_dir_path;
5456
std::chrono::milliseconds timeout;
57+
std::unique_ptr<QNetworkProxy> network_proxy;
5558
};
5659
}
5760
#endif // MULTIPASS_URL_DOWNLOADER_H

src/network/url_downloader.cpp

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2017-2019 Canonical, Ltd.
2+
* Copyright (C) 2017-2020 Canonical, Ltd.
33
*
44
* This program is free software; you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License as published by
@@ -99,21 +99,57 @@ QByteArray download(QNetworkAccessManager* manager, const Time& timeout, QUrl co
9999

100100
const auto msg = reply->errorString().toStdString();
101101

102+
if (reply->error() == QNetworkReply::ProxyAuthenticationRequiredError)
103+
reply->abort();
104+
102105
if (abort_download)
103106
throw mp::AbortedDownloadException{msg};
104107
else
105108
throw mp::DownloadException{url.toString().toStdString(), download_timeout.isActive() ? msg : "Network timeout"};
106109
}
107110
return reply->readAll();
108111
}
112+
113+
std::unique_ptr<QNetworkProxy> discover_http_proxy()
114+
{
115+
std::unique_ptr<QNetworkProxy> proxy_ptr{nullptr};
116+
117+
QString http_proxy{qgetenv("http_proxy")};
118+
if (http_proxy.isEmpty())
119+
{
120+
// Some OS's are case senstive
121+
http_proxy = qgetenv("HTTP_PROXY");
122+
}
123+
124+
if (!http_proxy.isEmpty())
125+
{
126+
if (!http_proxy.startsWith("http://"))
127+
{
128+
http_proxy.prepend("http://");
129+
}
130+
131+
QUrl proxy_url{http_proxy};
132+
const auto host = proxy_url.host();
133+
const auto port = proxy_url.port();
134+
135+
auto network_proxy = QNetworkProxy(QNetworkProxy::HttpProxy, host, static_cast<quint16>(port),
136+
proxy_url.userName(), proxy_url.password());
137+
138+
QNetworkProxy::setApplicationProxy(network_proxy);
139+
140+
proxy_ptr = std::make_unique<QNetworkProxy>(network_proxy);
141+
}
142+
143+
return proxy_ptr;
144+
}
109145
} // namespace
110146

111147
mp::URLDownloader::URLDownloader(std::chrono::milliseconds timeout) : URLDownloader{Path(), timeout}
112148
{
113149
}
114150

115151
mp::URLDownloader::URLDownloader(const mp::Path& cache_dir, std::chrono::milliseconds timeout)
116-
: cache_dir_path{QDir(cache_dir).filePath("network-cache")}, timeout{timeout}
152+
: cache_dir_path{QDir(cache_dir).filePath("network-cache")}, timeout{timeout}, network_proxy{discover_http_proxy()}
117153
{
118154
}
119155

0 commit comments

Comments
 (0)