From 0f0bf366c97ed5d5f496d9bb81b56222d120ae49 Mon Sep 17 00:00:00 2001
From: Adrien Beraud <adrien.beraud@savoirfairelinux.com>
Date: Thu, 26 Sep 2024 16:08:34 -0400
Subject: [PATCH] namedirectory: add platform/arch to user-agent

Add basic info about the platform similar to most http clients,
to allow basic statistics about name registration and make debugging easier.

Change-Id: Id6f928f628b7606ed3cb27ffee8c2410725e0cf1
---
 src/jamidht/namedirectory.cpp |  3 ++-
 src/string_utils.h            | 36 +++++++++++++++++++++++++++++++++++
 2 files changed, 38 insertions(+), 1 deletion(-)

diff --git a/src/jamidht/namedirectory.cpp b/src/jamidht/namedirectory.cpp
index edf093819..ffb384f32 100644
--- a/src/jamidht/namedirectory.cpp
+++ b/src/jamidht/namedirectory.cpp
@@ -146,7 +146,8 @@ NameDirectory::instance(const std::string& serverUrl, std::shared_ptr<dht::Logge
 void
 NameDirectory::setHeaderFields(Request& request)
 {
-    request.set_header_field(restinio::http_field_t::user_agent, "JamiDHT");
+    request.set_header_field(restinio::http_field_t::user_agent, fmt::format("Jami ({}/{})",
+        jami::platform(), jami::arch()));
     request.set_header_field(restinio::http_field_t::accept, "*/*");
     request.set_header_field(restinio::http_field_t::content_type, "application/json");
 }
diff --git a/src/string_utils.h b/src/string_utils.h
index 9ca821728..ab73d3aad 100644
--- a/src/string_utils.h
+++ b/src/string_utils.h
@@ -45,6 +45,42 @@ bool_to_str(bool b) noexcept
     return b ? TRUE_STR : FALSE_STR;
 }
 
+constexpr inline std::string_view
+platform() {
+    using namespace std::literals;
+#if defined(__ANDROID__)
+    return "android"sv;
+#elif defined(__linux__)
+    return "linux"sv;
+#elif defined(__APPLE__)
+#    if TARGET_OS_IPHONE
+    return "ios"sv;
+#    else
+    return "macos"sv;
+#    endif
+#elif defined(_WIN32)
+    return "windows"sv;
+#else
+    return "unknown"sv;
+#endif
+}
+
+constexpr inline std::string_view
+arch() {
+    using namespace std::literals;
+#if defined(__x86_64__) || defined(_M_X64)
+    return "x86_64"sv;
+#elif defined(__i386__) || defined(_M_IX86)
+    return "x86"sv;
+#elif defined(__aarch64__)
+    return "arm64"sv;
+#elif defined(__arm__)
+    return "arm"sv;
+#else
+    return "unknown"sv;
+#endif
+}
+
 std::string to_string(double value);
 
 #ifdef _WIN32
-- 
GitLab