summaryrefslogtreecommitdiff
blob: 6252d28f7b937d40756e2e7a09ae5a87d963da57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
From f687c5abd0c5e9bd5a6688b6d9d50f2536b7d33d Mon Sep 17 00:00:00 2001
From: Aleix Pol <aleixpol@kde.org>
Date: Fri, 24 Sep 2021 14:31:05 +0200
Subject: [PATCH] Include a user agent on KNS requests

---
 src/core/jobs/httpworker.cpp | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/src/core/jobs/httpworker.cpp b/src/core/jobs/httpworker.cpp
index 4c218b08..b81edd2d 100644
--- a/src/core/jobs/httpworker.cpp
+++ b/src/core/jobs/httpworker.cpp
@@ -7,7 +7,9 @@
 #include "httpworker.h"
 
 #include "knewstuffcore_debug.h"
+#include "knewstuffcore_version.h"
 
+#include <QCoreApplication>
 #include <QFile>
 #include <QMutex>
 #include <QMutexLocker>
@@ -93,6 +95,15 @@ void HTTPWorker::setUrl(const QUrl &url)
     d->source = url;
 }
 
+static void addUserAgent(QNetworkRequest &request)
+{
+    QString agentHeader = QStringLiteral("KNewStuff/%1").arg(QLatin1String(KNEWSTUFFCORE_VERSION_STRING));
+    if (QCoreApplication::instance()) {
+        agentHeader += QStringLiteral("-%1/%2").arg(QCoreApplication::instance()->applicationName(), QCoreApplication::instance()->applicationVersion());
+    }
+    request.setHeader(QNetworkRequest::UserAgentHeader, agentHeader);
+}
+
 void HTTPWorker::startRequest()
 {
     if (d->reply) {
@@ -101,6 +112,7 @@ void HTTPWorker::startRequest()
     }
 
     QNetworkRequest request(d->source);
+    addUserAgent(request);
     d->reply = s_httpWorkerNAM->get(request);
     connect(d->reply, &QNetworkReply::readyRead, this, &HTTPWorker::handleReadyRead);
     connect(d->reply, &QNetworkReply::finished, this, &HTTPWorker::handleFinished);
@@ -144,6 +156,7 @@ void HTTPWorker::handleFinished()
                                    << d->reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
             d->reply->deleteLater();
             QNetworkRequest request(d->redirectUrl);
+            addUserAgent(request);
             d->reply = s_httpWorkerNAM->get(request);
             connect(d->reply, &QNetworkReply::readyRead, this, &HTTPWorker::handleReadyRead);
             connect(d->reply, &QNetworkReply::finished, this, &HTTPWorker::handleFinished);
-- 
GitLab