@@ -1,120 +1,123 | |||
|
1 | 1 | #include <Network/Downloader.h> |
|
2 | 2 | #include <memory> |
|
3 | 3 | |
|
4 | 4 | #include <QNetworkRequest> |
|
5 | 5 | #include <QNetworkReply> |
|
6 | 6 | #include <QNetworkAccessManager> |
|
7 | 7 | #include <QAuthenticator> |
|
8 | 8 | #include <QVariant> |
|
9 | 9 | #include <QHash> |
|
10 | 10 | #include <QPair> |
|
11 | 11 | #include <QCoreApplication> |
|
12 | 12 | #include <QReadWriteLock> |
|
13 | 13 | |
|
14 | 14 | class Downloader::p_Downloader |
|
15 | 15 | { |
|
16 | 16 | using login_pair=QPair<QString,QString>; |
|
17 | 17 | QNetworkAccessManager manager; |
|
18 | 18 | QHash<QString,login_pair> auth; |
|
19 | 19 | QReadWriteLock pending_requests_lock; |
|
20 | 20 | QHash<QUuid,QNetworkReply*> pending_requests; |
|
21 | 21 | |
|
22 | 22 | QNetworkRequest buildRequest(const QString& url, const QString &user="", const QString &passwd="") |
|
23 | 23 | { |
|
24 | 24 | QNetworkRequest request; |
|
25 | 25 | request.setUrl(QUrl(url)); |
|
26 | 26 | request.setRawHeader("User-Agent", "SciQLop 1.0"); |
|
27 | 27 | if(user!="" and passwd!="") |
|
28 | 28 | { |
|
29 | 29 | //might grow quickly since we can have tons of URLs for the same host |
|
30 | 30 | auth[url]=login_pair(user,passwd); |
|
31 | 31 | QString login = "Basic "+user+":"+passwd; |
|
32 | 32 | request.setRawHeader("Authorization",login.toLocal8Bit()); |
|
33 | 33 | } |
|
34 | 34 | return request; |
|
35 | 35 | } |
|
36 | 36 | |
|
37 | 37 | public: |
|
38 | 38 | explicit p_Downloader() |
|
39 | 39 | { |
|
40 | 40 | |
|
41 | 41 | auto login_bambda = [this](QNetworkReply * reply, QAuthenticator * authenticator) |
|
42 | 42 | { |
|
43 | 43 | if(auth.contains(reply->url().toString())) |
|
44 | 44 | { |
|
45 | 45 | auto login = auth[reply->url().toString()]; |
|
46 | 46 | authenticator->setUser(login.first); |
|
47 | 47 | authenticator->setPassword(login.second); |
|
48 | 48 | } |
|
49 | 49 | }; |
|
50 | 50 | |
|
51 | 51 | QObject::connect(&manager, &QNetworkAccessManager::authenticationRequired, login_bambda); |
|
52 | 52 | } |
|
53 | 53 | |
|
54 | 54 | Response get(const QString& url, const QString &user="", const QString &passwd="") |
|
55 | 55 | { |
|
56 | 56 | QNetworkRequest request = buildRequest(url, user, passwd); |
|
57 | 57 | QNetworkReply *reply = manager.get(request); |
|
58 | 58 | while (!reply->isFinished()) |
|
59 | 59 | QCoreApplication::processEvents(); |
|
60 | 60 | QVariant status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute); |
|
61 | 61 | Response resp = Response(reply->readAll(), status_code.toInt()); |
|
62 | 62 | delete reply; |
|
63 | if(user!="" and passwd!="") | |
|
64 | auth.remove(url); | |
|
63 | 65 | return resp; |
|
64 | 66 | } |
|
65 | 67 | |
|
66 | 68 | QUuid getAsync(const QString &url, std::function<void (QUuid ,Response)> callback, const QString &user, const QString &passwd) |
|
67 | 69 | { |
|
68 | 70 | auto uuid = QUuid::createUuid(); |
|
69 | 71 | QNetworkRequest request = buildRequest(url, user, passwd); |
|
70 | 72 | QNetworkReply *reply = manager.get(request); |
|
71 | auto callback_wrapper = [uuid, callback, this](){ | |
|
73 | auto callback_wrapper = [url, uuid, callback, this](){ | |
|
72 | 74 | QNetworkReply* reply; |
|
73 | 75 | { |
|
74 | 76 | QWriteLocker locker(&pending_requests_lock); |
|
75 | 77 | reply = pending_requests.take(uuid); |
|
76 | 78 | } |
|
77 | 79 | QVariant status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute); |
|
78 | 80 | Response resp = Response(reply->readAll(), status_code.toInt()); |
|
81 | auth.remove(url); | |
|
79 | 82 | delete reply; |
|
80 | 83 | callback(uuid, resp); |
|
81 | 84 | }; |
|
82 | 85 | QObject::connect(reply, &QNetworkReply::finished, callback_wrapper); |
|
83 | 86 | { |
|
84 | 87 | QWriteLocker locker(&pending_requests_lock); |
|
85 | 88 | pending_requests[uuid] = reply; |
|
86 | 89 | } |
|
87 | 90 | return uuid; |
|
88 | 91 | } |
|
89 | 92 | bool downloadFinished(QUuid uuid) |
|
90 | 93 | { |
|
91 | 94 | QReadLocker locker(&pending_requests_lock); |
|
92 | 95 | if(pending_requests.contains(uuid)) |
|
93 | 96 | { |
|
94 | 97 | auto req = pending_requests[uuid]; |
|
95 | 98 | return req->isFinished(); |
|
96 | 99 | } |
|
97 | 100 | return true; |
|
98 | 101 | } |
|
99 | 102 | }; |
|
100 | 103 | |
|
101 | 104 | Response Downloader::get(const QString &url, const QString &user, const QString &passwd) |
|
102 | 105 | { |
|
103 | 106 | return Downloader::instance().impl->get(url, user, passwd); |
|
104 | 107 | } |
|
105 | 108 | |
|
106 | 109 | QUuid Downloader::getAsync(const QString &url, std::function<void (QUuid ,Response)> callback, const QString &user, const QString &passwd) |
|
107 | 110 | { |
|
108 | 111 | return Downloader::instance().impl->getAsync(url, callback, user, passwd); |
|
109 | 112 | } |
|
110 | 113 | |
|
111 | 114 | bool Downloader::downloadFinished(QUuid uuid) |
|
112 | 115 | { |
|
113 | 116 | return Downloader::instance().impl->downloadFinished(uuid); |
|
114 | 117 | } |
|
115 | 118 | |
|
116 | 119 | Downloader::Downloader() |
|
117 | 120 | :impl(spimpl::make_unique_impl<p_Downloader>()) |
|
118 | 121 | { |
|
119 | 122 | } |
|
120 | 123 |
General Comments 0
You need to be logged in to leave comments.
Login now