service/DavPushClient.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "DavPushClient.h" | ||
| 2 | |||
| 3 | #include "DavNetworkLimits.h" | ||
| 4 | #include "DavPushXml.h" | ||
| 5 | |||
| 6 | #include <QDateTime> | ||
| 7 | #include <QLoggingCategory> | ||
| 8 | #include <QNetworkAccessManager> | ||
| 9 | #include <QNetworkReply> | ||
| 10 | #include <QNetworkRequest> | ||
| 11 | #include <QUrl> | ||
| 12 | |||
| 13 |
4/7✓ Branch 2 → 3 taken 2 times.
✓ Branch 2 → 8 taken 97 times.
✓ Branch 4 → 5 taken 2 times.
✗ Branch 4 → 8 not taken.
✓ Branch 5 → 6 taken 2 times.
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 12 not taken.
|
99 | Q_LOGGING_CATEGORY(lcDavPushClient, "mailjd.davpush.client") |
| 14 | |||
| 15 | 53 | DavPushClient::DavPushClient(const QString &serverUrl, const QString &username, const QString &password, | |
| 16 | 53 | QObject *parent) | |
| 17 | 53 | : QObject(parent), m_serverUrl(DavNetworkLimits::requestServerUrl(serverUrl)), m_username(username), | |
| 18 |
1/1✓ Branch 3 → 4 taken 53 times.
|
106 | m_password(password) { |
| 19 |
1/1✓ Branch 7 → 8 taken 53 times.
|
53 | qRegisterMetaType<DavPushClient::Result>(); |
| 20 |
3/6✓ Branch 8 → 9 taken 53 times.
✓ Branch 9 → 10 taken 53 times.
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 12 taken 53 times.
✗ Branch 14 → 15 not taken.
✗ Branch 14 → 16 not taken.
|
53 | m_nam = new QNetworkAccessManager(this); |
| 21 |
1/1✓ Branch 12 → 13 taken 53 times.
|
53 | m_nam->setRedirectPolicy(QNetworkRequest::ManualRedirectPolicy); |
| 22 | 53 | } | |
| 23 | |||
| 24 | 53 | void DavPushClient::setNetworkAccessManager(QNetworkAccessManager *nam) { | |
| 25 |
4/8✓ Branch 2 → 3 taken 53 times.
✗ Branch 2 → 6 not taken.
✓ Branch 4 → 5 taken 53 times.
✗ Branch 4 → 6 not taken.
✓ Branch 7 → 8 taken 53 times.
✗ Branch 7 → 10 not taken.
✓ Branch 8 → 9 taken 53 times.
✗ Branch 8 → 10 not taken.
|
53 | if (m_nam && m_nam->parent() == this) delete m_nam; |
| 26 | 53 | m_nam = nam; | |
| 27 |
1/2✓ Branch 10 → 11 taken 53 times.
✗ Branch 10 → 13 not taken.
|
53 | if (m_nam) { |
| 28 | 53 | m_nam->setParent(this); | |
| 29 | 53 | m_nam->setRedirectPolicy(QNetworkRequest::ManualRedirectPolicy); | |
| 30 | } | ||
| 31 | 53 | } | |
| 32 | |||
| 33 | 126 | qint64 DavPushClient::now() const { | |
| 34 |
2/2✓ Branch 3 → 4 taken 6 times.
✓ Branch 3 → 6 taken 120 times.
|
126 | return m_clock ? m_clock() : QDateTime::currentSecsSinceEpoch(); |
| 35 | } | ||
| 36 | |||
| 37 | 102 | QByteArray DavPushClient::authHeader() const { | |
| 38 | // Same rule as CalDavClient: credentials only over HTTPS. | ||
| 39 |
9/21✓ Branch 3 → 4 taken 102 times.
✗ Branch 3 → 10 not taken.
✓ Branch 5 → 6 taken 102 times.
✓ Branch 6 → 7 taken 102 times.
✓ Branch 7 → 8 taken 102 times.
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 102 times.
✓ Branch 12 → 13 taken 102 times.
✗ Branch 12 → 14 not taken.
✓ Branch 14 → 15 taken 102 times.
✗ Branch 14 → 16 not taken.
✓ Branch 16 → 17 taken 102 times.
✗ Branch 16 → 18 not taken.
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 20 taken 102 times.
✗ Branch 35 → 36 not taken.
✗ Branch 35 → 37 not taken.
✗ Branch 39 → 40 not taken.
✗ Branch 39 → 41 not taken.
✗ Branch 43 → 44 not taken.
✗ Branch 43 → 45 not taken.
|
102 | if (m_username.isEmpty() || QUrl(m_serverUrl).scheme().toLower() != QLatin1String("https")) return {}; |
| 40 |
5/5✓ Branch 22 → 23 taken 102 times.
✓ Branch 23 → 24 taken 102 times.
✓ Branch 24 → 25 taken 102 times.
✓ Branch 26 → 27 taken 102 times.
✓ Branch 27 → 28 taken 102 times.
|
204 | return "Basic " + (m_username + QLatin1Char(':') + m_password).toUtf8().toBase64(); |
| 41 | } | ||
| 42 | |||
| 43 | 85 | quint64 DavPushClient::registerSubscription(const QString &collectionPath, const QString &pushResource, | |
| 44 | const QByteArray &publicKey, const QByteArray &authSecret) { | ||
| 45 | 85 | const quint64 request = ++m_nextRequest; | |
| 46 |
1/1✓ Branch 2 → 3 taken 85 times.
|
85 | const QString url = DavNetworkLimits::resolveSameOrigin(m_serverUrl, collectionPath); |
| 47 | // S106-069: the registration body carries the Web Push auth secret; like | ||
| 48 | // the credentials it never travels to an http:// DAV origin. | ||
| 49 |
3/3✓ Branch 4 → 5 taken 85 times.
✓ Branch 5 → 6 taken 85 times.
✓ Branch 6 → 7 taken 85 times.
|
85 | const bool encryptedOrigin = QUrl(m_serverUrl).scheme().toLower() == QLatin1String("https"); |
| 50 |
9/9✓ Branch 12 → 13 taken 84 times.
✓ Branch 12 → 17 taken 1 time.
✓ Branch 14 → 15 taken 84 times.
✓ Branch 15 → 16 taken 83 times.
✓ Branch 15 → 17 taken 1 time.
✓ Branch 16 → 17 taken 1 time.
✓ Branch 16 → 18 taken 82 times.
✓ Branch 19 → 20 taken 3 times.
✓ Branch 19 → 46 taken 82 times.
|
85 | if (url.isEmpty() || !pushResource.startsWith(QLatin1String("https://")) || !encryptedOrigin) { |
| 51 | 3 | Result result; | |
| 52 | 3 | result.request = request; | |
| 53 | 3 | result.outcome = Outcome::Incompatible; | |
| 54 | 3 | result.collectionPath = collectionPath; | |
| 55 |
2/2✓ Branch 22 → 23 taken 1 time.
✓ Branch 22 → 26 taken 2 times.
|
10 | result.detail = url.isEmpty() ? QStringLiteral("collection outside the DAV origin") |
| 56 |
4/4✓ Branch 26 → 27 taken 1 time.
✓ Branch 26 → 30 taken 1 time.
✓ Branch 39 → 40 taken 1 time.
✓ Branch 39 → 41 taken 2 times.
|
5 | : !encryptedOrigin ? QStringLiteral("push needs an encrypted (HTTPS) DAV connection") |
| 57 |
4/4✓ Branch 35 → 36 taken 1 time.
✓ Branch 35 → 37 taken 2 times.
✓ Branch 37 → 38 taken 1 time.
✓ Branch 37 → 39 taken 2 times.
|
6 | : QStringLiteral("push resource must use HTTPS"); |
| 58 |
1/1✓ Branch 42 → 43 taken 3 times.
|
6 | QMetaObject::invokeMethod(this, [this, result] { emit registered(result); }, Qt::QueuedConnection); |
| 59 | 3 | return request; | |
| 60 | 3 | } | |
| 61 |
2/2✓ Branch 46 → 47 taken 82 times.
✓ Branch 47 → 48 taken 82 times.
|
82 | QNetworkRequest httpRequest{QUrl(url)}; |
| 62 |
3/3✓ Branch 49 → 50 taken 82 times.
✓ Branch 50 → 51 taken 82 times.
✓ Branch 51 → 52 taken 82 times.
|
82 | httpRequest.setRawHeader("Authorization", authHeader()); |
| 63 |
3/3✓ Branch 54 → 55 taken 82 times.
✓ Branch 55 → 56 taken 82 times.
✓ Branch 56 → 57 taken 82 times.
|
82 | httpRequest.setRawHeader("Content-Type", "application/xml; charset=utf-8"); |
| 64 | QNetworkReply *reply = | ||
| 65 |
2/2✓ Branch 59 → 60 taken 82 times.
✓ Branch 60 → 61 taken 82 times.
|
82 | m_nam->post(httpRequest, DavPushXml::registerBody(pushResource, publicKey, authSecret)); |
| 66 |
1/1✓ Branch 62 → 63 taken 82 times.
|
82 | DavNetworkLimits::apply(reply); |
| 67 |
1/1✓ Branch 64 → 65 taken 82 times.
|
82 | connect(reply, &QNetworkReply::finished, this, |
| 68 | 244 | [this, reply, request, collectionPath] { finishRegister(reply, request, collectionPath); }); | |
| 69 | 82 | return request; | |
| 70 | 85 | } | |
| 71 | |||
| 72 | 21 | quint64 DavPushClient::unregisterSubscription(const QString ®istrationUrl) { | |
| 73 | 21 | const quint64 request = ++m_nextRequest; | |
| 74 |
1/1✓ Branch 2 → 3 taken 21 times.
|
21 | const QString url = DavNetworkLimits::resolveSameOrigin(m_serverUrl, registrationUrl); |
| 75 |
2/2✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 16 taken 20 times.
|
21 | if (url.isEmpty()) { |
| 76 | 1 | Result result; | |
| 77 | 1 | result.request = request; | |
| 78 | 1 | result.outcome = Outcome::Incompatible; | |
| 79 | 1 | result.detail = QStringLiteral("registration URL outside the DAV origin"); | |
| 80 |
1/1✓ Branch 12 → 13 taken 1 time.
|
2 | QMetaObject::invokeMethod(this, [this, result] { emit unregistered(result); }, Qt::QueuedConnection); |
| 81 | 1 | return request; | |
| 82 | 1 | } | |
| 83 |
2/2✓ Branch 16 → 17 taken 20 times.
✓ Branch 17 → 18 taken 20 times.
|
20 | QNetworkRequest httpRequest{QUrl(url)}; |
| 84 |
3/3✓ Branch 19 → 20 taken 20 times.
✓ Branch 20 → 21 taken 20 times.
✓ Branch 21 → 22 taken 20 times.
|
20 | httpRequest.setRawHeader("Authorization", authHeader()); |
| 85 |
1/1✓ Branch 24 → 25 taken 20 times.
|
20 | QNetworkReply *reply = m_nam->deleteResource(httpRequest); |
| 86 |
1/1✓ Branch 25 → 26 taken 20 times.
|
20 | DavNetworkLimits::apply(reply); |
| 87 |
1/1✓ Branch 26 → 27 taken 20 times.
|
20 | connect(reply, &QNetworkReply::finished, this, [this, reply, request] { |
| 88 |
1/1✓ Branch 2 → 3 taken 19 times.
|
19 | reply->deleteLater(); |
| 89 |
1/1✓ Branch 3 → 4 taken 19 times.
|
19 | Result result = classify(reply, request); |
| 90 | // Removed now or already gone: both end the own registration. | ||
| 91 |
2/2✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 6 taken 18 times.
|
19 | if (result.outcome == Outcome::NotFound) result.outcome = Outcome::Ok; |
| 92 |
7/7✓ Branch 6 → 7 taken 19 times.
✓ Branch 10 → 11 taken 19 times.
✓ Branch 11 → 12 taken 19 times.
✓ Branch 13 → 14 taken 19 times.
✓ Branch 14 → 15 taken 19 times.
✓ Branch 20 → 8 taken 19 times.
✓ Branch 20 → 21 taken 19 times.
|
38 | qCInfo(lcDavPushClient) << "Unsubscribe" << result.outcome << result.status; |
| 93 |
1/1✓ Branch 21 → 22 taken 19 times.
|
19 | emit unregistered(result); |
| 94 | 19 | }); | |
| 95 | 20 | return request; | |
| 96 | 21 | } | |
| 97 | |||
| 98 | 99 | DavPushClient::Result DavPushClient::classify(QNetworkReply *reply, quint64 request) const { | |
| 99 | 99 | Result result; | |
| 100 | 99 | result.request = request; | |
| 101 |
2/2✓ Branch 2 → 3 taken 99 times.
✓ Branch 3 → 4 taken 99 times.
|
99 | result.status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); |
| 102 |
1/1✓ Branch 5 → 6 taken 99 times.
|
99 | const QString limit = DavNetworkLimits::failureReason(reply); |
| 103 |
3/6✓ Branch 7 → 8 taken 99 times.
✗ Branch 7 → 9 not taken.
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 99 times.
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 19 taken 99 times.
|
99 | if (!limit.isEmpty() || result.status == 0) { |
| 104 | ✗ | result.outcome = Outcome::Transport; | |
| 105 | ✗ | result.detail = !limit.isEmpty() ? limit : reply->errorString(); | |
| 106 | ✗ | return result; | |
| 107 | } | ||
| 108 |
3/4✓ Branch 19 → 20 taken 99 times.
✗ Branch 19 → 22 not taken.
✓ Branch 20 → 21 taken 83 times.
✓ Branch 20 → 22 taken 16 times.
|
99 | if (result.status >= 200 && result.status < 300) { |
| 109 | 83 | result.outcome = Outcome::Ok; | |
| 110 | 83 | return result; | |
| 111 | } | ||
| 112 |
2/2✓ Branch 22 → 23 taken 16 times.
✓ Branch 23 → 24 taken 16 times.
|
16 | const QByteArray body = reply->readAll().left(2000); |
| 113 | 16 | bool ok = false; | |
| 114 |
2/2✓ Branch 29 → 30 taken 16 times.
✓ Branch 30 → 31 taken 16 times.
|
32 | const int retryAfter = reply->rawHeader("Retry-After").toInt(&ok); |
| 115 |
2/2✓ Branch 32 → 33 taken 1 time.
✓ Branch 32 → 34 taken 15 times.
|
16 | result.retryAfterSeconds = ok ? retryAfter : 0; |
| 116 |
5/5✓ Branch 35 → 36 taken 2 times.
✓ Branch 35 → 37 taken 3 times.
✓ Branch 35 → 38 taken 4 times.
✓ Branch 35 → 39 taken 2 times.
✓ Branch 35 → 40 taken 5 times.
|
16 | switch (result.status) { |
| 117 | 2 | case 401: result.outcome = Outcome::Unauthorized; break; | |
| 118 | 3 | case 403: result.outcome = Outcome::Forbidden; break; | |
| 119 | 4 | case 404: case 410: result.outcome = Outcome::NotFound; break; | |
| 120 | 2 | case 429: case 503: result.outcome = Outcome::RateLimited; break; | |
| 121 | 5 | default: | |
| 122 |
2/2✓ Branch 40 → 41 taken 4 times.
✓ Branch 40 → 42 taken 1 time.
|
5 | result.outcome = result.status >= 500 ? Outcome::ServerError : Outcome::Incompatible; |
| 123 | } | ||
| 124 |
2/2✓ Branch 48 → 49 taken 16 times.
✓ Branch 57 → 58 taken 16 times.
|
80 | result.detail = QStringLiteral("HTTP %1%2").arg(result.status).arg( |
| 125 |
4/6✓ Branch 52 → 53 taken 1 time.
✓ Branch 52 → 56 taken 15 times.
✓ Branch 61 → 62 taken 1 time.
✓ Branch 61 → 63 taken 15 times.
✗ Branch 83 → 84 not taken.
✗ Branch 83 → 85 not taken.
|
49 | body.contains("no-trigger-supported") ? QStringLiteral(" (no supported trigger)") : QString()); |
| 126 | 16 | return result; | |
| 127 | 99 | } | |
| 128 | |||
| 129 | 80 | void DavPushClient::finishRegister(QNetworkReply *reply, quint64 request, const QString &collectionPath) { | |
| 130 |
1/1✓ Branch 2 → 3 taken 80 times.
|
80 | reply->deleteLater(); |
| 131 |
1/1✓ Branch 3 → 4 taken 80 times.
|
80 | Result result = classify(reply, request); |
| 132 | 80 | result.collectionPath = collectionPath; | |
| 133 |
2/2✓ Branch 5 → 6 taken 67 times.
✓ Branch 5 → 105 taken 13 times.
|
80 | if (result.outcome == Outcome::Ok) { |
| 134 |
3/3✓ Branch 10 → 11 taken 67 times.
✓ Branch 11 → 12 taken 67 times.
✓ Branch 12 → 13 taken 67 times.
|
134 | const QString location = QString::fromUtf8(reply->rawHeader("Location")).trimmed(); |
| 135 |
3/3✓ Branch 19 → 20 taken 67 times.
✓ Branch 20 → 21 taken 67 times.
✓ Branch 21 → 22 taken 67 times.
|
134 | const auto expires = DavPushXml::parseHttpDate(QString::fromLatin1(reply->rawHeader("Expires"))); |
| 136 |
3/3✓ Branch 28 → 29 taken 67 times.
✓ Branch 29 → 30 taken 67 times.
✓ Branch 30 → 31 taken 67 times.
|
134 | const auto serverDate = DavPushXml::parseHttpDate(QString::fromLatin1(reply->rawHeader("Date"))); |
| 137 | // S106-068: a relative Location resolves against the request URI (the | ||
| 138 | // collection, RFC 9110 §10.2.2), not the server root. | ||
| 139 |
2/2✓ Branch 33 → 34 taken 67 times.
✓ Branch 34 → 35 taken 67 times.
|
67 | const QUrl collectionUrl(DavNetworkLimits::resolveSameOrigin(m_serverUrl, collectionPath)); |
| 140 | 67 | const QString resolved = location.isEmpty() ? QString() | |
| 141 | 66 | : DavNetworkLimits::resolveSameOrigin(m_serverUrl, | |
| 142 |
12/18✓ Branch 37 → 38 taken 1 time.
✓ Branch 37 → 39 taken 66 times.
✓ Branch 39 → 40 taken 66 times.
✓ Branch 40 → 41 taken 66 times.
✓ Branch 42 → 43 taken 66 times.
✓ Branch 43 → 44 taken 66 times.
✓ Branch 44 → 45 taken 66 times.
✓ Branch 44 → 46 taken 1 time.
✓ Branch 46 → 47 taken 66 times.
✓ Branch 46 → 48 taken 1 time.
✓ Branch 48 → 49 taken 66 times.
✓ Branch 48 → 50 taken 1 time.
✗ Branch 148 → 149 not taken.
✗ Branch 148 → 150 not taken.
✗ Branch 153 → 154 not taken.
✗ Branch 153 → 155 not taken.
✗ Branch 157 → 158 not taken.
✗ Branch 157 → 159 not taken.
|
67 | collectionUrl.resolved(QUrl(location)).toString(QUrl::FullyEncoded)); |
| 143 |
2/2✓ Branch 51 → 52 taken 2 times.
✓ Branch 51 → 67 taken 65 times.
|
67 | if (resolved.isEmpty()) { |
| 144 | 2 | result.outcome = Outcome::Incompatible; | |
| 145 |
2/2✓ Branch 53 → 54 taken 1 time.
✓ Branch 53 → 57 taken 1 time.
|
8 | result.detail = location.isEmpty() ? QStringLiteral("registration without Location") |
| 146 |
4/4✓ Branch 62 → 63 taken 1 time.
✓ Branch 62 → 64 taken 1 time.
✓ Branch 64 → 65 taken 1 time.
✓ Branch 64 → 66 taken 1 time.
|
4 | : QStringLiteral("registration URL outside the DAV origin"); |
| 147 |
2/2✓ Branch 68 → 69 taken 1 time.
✓ Branch 68 → 76 taken 64 times.
|
65 | } else if (!expires) { |
| 148 | 1 | result.outcome = Outcome::Incompatible; | |
| 149 | 1 | result.detail = QStringLiteral("registration without a usable Expires"); | |
| 150 | } else { | ||
| 151 | // Exactly as sent by the server (Push-Dont-Notify compares strings). | ||
| 152 |
4/4✓ Branch 76 → 77 taken 64 times.
✓ Branch 77 → 78 taken 64 times.
✓ Branch 78 → 79 taken 1 time.
✓ Branch 78 → 80 taken 63 times.
|
64 | result.location = QUrl(location).isRelative() ? resolved : location; |
| 153 | // The server's lifetime, measured on its own clock, on ours. | ||
| 154 |
3/3✓ Branch 85 → 86 taken 2 times.
✓ Branch 85 → 88 taken 62 times.
✓ Branch 88 → 89 taken 62 times.
|
64 | const qint64 lifetime = *expires - (serverDate ? *serverDate : now()); |
| 155 |
1/1✓ Branch 90 → 91 taken 64 times.
|
64 | result.expiresAt = now() + qMax<qint64>(0, lifetime); |
| 156 |
2/2✓ Branch 92 → 93 taken 1 time.
✓ Branch 92 → 100 taken 63 times.
|
64 | if (lifetime <= 0) { |
| 157 | 1 | result.outcome = Outcome::Incompatible; | |
| 158 | 1 | result.detail = QStringLiteral("registration already expired"); | |
| 159 | } | ||
| 160 | } | ||
| 161 | 67 | } | |
| 162 |
8/8✓ Branch 105 → 106 taken 80 times.
✓ Branch 109 → 110 taken 80 times.
✓ Branch 110 → 111 taken 80 times.
✓ Branch 112 → 113 taken 80 times.
✓ Branch 113 → 114 taken 80 times.
✓ Branch 114 → 115 taken 80 times.
✓ Branch 120 → 107 taken 80 times.
✓ Branch 120 → 121 taken 80 times.
|
160 | qCInfo(lcDavPushClient) << "Register" << result.outcome << result.status << result.detail; |
| 163 |
1/1✓ Branch 121 → 122 taken 80 times.
|
80 | emit registered(result); |
| 164 | 80 | } | |
| 165 |