From 140c7f430cf9372c3706db4af9f58654a3ee2d0f Mon Sep 17 00:00:00 2001 From: Cameron Clough Date: Thu, 18 Nov 2021 14:53:21 +0000 Subject: [PATCH] api: send User-Agent on network requests (#22965) * api: set User-Agent on network requests The format of the User-Agent header matches the format in `common/api/__init__.py`. Closes #22954. * Update selfdrive/ui/qt/util.cc * fix parentheses * refactor into function Co-authored-by: Willem Melching --- selfdrive/ui/qt/api.cc | 1 + selfdrive/ui/qt/util.cc | 11 ++++++++++- selfdrive/ui/qt/util.h | 2 ++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/selfdrive/ui/qt/api.cc b/selfdrive/ui/qt/api.cc index af959e6cd9..6339884aec 100644 --- a/selfdrive/ui/qt/api.cc +++ b/selfdrive/ui/qt/api.cc @@ -90,6 +90,7 @@ void HttpRequest::sendRequest(const QString &requestURL, const HttpRequest::Meth QNetworkRequest request; request.setUrl(QUrl(requestURL)); + request.setRawHeader("User-Agent", getUserAgent().toUtf8()); if (!token.isEmpty()) { request.setRawHeader(QByteArray("Authorization"), ("JWT " + token).toUtf8()); diff --git a/selfdrive/ui/qt/util.cc b/selfdrive/ui/qt/util.cc index 93bb2b0e79..1661c6c5d0 100644 --- a/selfdrive/ui/qt/util.cc +++ b/selfdrive/ui/qt/util.cc @@ -8,12 +8,21 @@ #include "selfdrive/common/swaglog.h" #include "selfdrive/hardware/hw.h" +QString getVersion() { + static QString version = QString::fromStdString(Params().get("Version")); + return version; +} + QString getBrand() { return Params().getBool("Passive") ? "dashcam" : "openpilot"; } QString getBrandVersion() { - return getBrand() + " v" + QString::fromStdString(Params().get("Version")).left(14).trimmed(); + return getBrand() + " v" + getVersion().left(14).trimmed(); +} + +QString getUserAgent() { + return "openpilot-" + getVersion(); } std::optional getDongleId() { diff --git a/selfdrive/ui/qt/util.h b/selfdrive/ui/qt/util.h index ad28b51166..588dac704f 100644 --- a/selfdrive/ui/qt/util.h +++ b/selfdrive/ui/qt/util.h @@ -9,8 +9,10 @@ #include #include +QString getVersion(); QString getBrand(); QString getBrandVersion(); +QString getUserAgent(); std::optional getDongleId(); void configFont(QPainter &p, const QString &family, int size, const QString &style); void clearLayout(QLayout* layout);