|
1 | 1 | /*
|
2 |
| - * Copyright (C) 2017-2019 Canonical, Ltd. |
| 2 | + * Copyright (C) 2017-2020 Canonical, Ltd. |
3 | 3 | *
|
4 | 4 | * This program is free software; you can redistribute it and/or modify
|
5 | 5 | * 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
|
99 | 99 |
|
100 | 100 | const auto msg = reply->errorString().toStdString();
|
101 | 101 |
|
| 102 | + if (reply->error() == QNetworkReply::ProxyAuthenticationRequiredError) |
| 103 | + reply->abort(); |
| 104 | + |
102 | 105 | if (abort_download)
|
103 | 106 | throw mp::AbortedDownloadException{msg};
|
104 | 107 | else
|
105 | 108 | throw mp::DownloadException{url.toString().toStdString(), download_timeout.isActive() ? msg : "Network timeout"};
|
106 | 109 | }
|
107 | 110 | return reply->readAll();
|
108 | 111 | }
|
| 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 | +} |
109 | 145 | } // namespace
|
110 | 146 |
|
111 | 147 | mp::URLDownloader::URLDownloader(std::chrono::milliseconds timeout) : URLDownloader{Path(), timeout}
|
112 | 148 | {
|
113 | 149 | }
|
114 | 150 |
|
115 | 151 | 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()} |
117 | 153 | {
|
118 | 154 | }
|
119 | 155 |
|
|
0 commit comments