[PATCH setup] Add bitness to user-agent string

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[PATCH setup] Add bitness to user-agent string

Jon TURNEY
We have no idea about the proportion of 32-bit and 64-bit Cygwin installs.

Add a 'Win32', 'WoW64' or 'Win64' token to the user-agent string to report
bitness.

Future work: it might be useful to report the OS version as well

Signed-off-by: Jon Turney <[hidden email]>
---
 nio-ie5.cc | 27 +++++++++++++++++++++++++--
 1 file changed, 25 insertions(+), 2 deletions(-)

diff --git a/nio-ie5.cc b/nio-ie5.cc
index 24d2c13..c8f65a9 100644
--- a/nio-ie5.cc
+++ b/nio-ie5.cc
@@ -31,11 +31,34 @@
 #include "getopt++/StringOption.h"
 
 static StringOption UserAgent ("", '\0', "user-agent", "User agent string for HTTP requests");
-static std::string default_useragent = std::string("Cygwin-Setup/") + setup_version;
 
 static HINTERNET internet_direct = 0;
 static HINTERNET internet_preconfig = 0;
 
+const std::string &
+determine_default_useragent(void)
+{
+  static std::string default_useragent;
+
+  if (!default_useragent.empty())
+    return default_useragent;
+
+  std::string token = "Unknown";
+#ifdef __x86_64__
+  token = "Win64";
+#else
+  typedef BOOL (WINAPI *PFNISWOW64PROCESS)(HANDLE, PBOOL);
+  PFNISWOW64PROCESS pfnIsWow64Process = (PFNISWOW64PROCESS)GetProcAddress(GetModuleHandle("kernel32"), "IsWow64Process");
+  if (pfnIsWow64Process) {
+    BOOL bIsWow64 = FALSE;
+    if (pfnIsWow64Process(GetCurrentProcess(), &bIsWow64))
+      token = bIsWow64 ? "WoW64" : "Win32";
+  }
+#endif
+  default_useragent = std::string("Cygwin-Setup/") + setup_version + " (" + token + ")";
+  return default_useragent;
+}
+
 NetIO_IE5::NetIO_IE5 (char const *_url, bool direct, bool cachable):
 NetIO (_url)
 {
@@ -51,7 +74,7 @@ NetIO (_url)
     {
       InternetAttemptConnect (0);
 
-      const char *lpszAgent = default_useragent.c_str();
+      const char *lpszAgent = determine_default_useragent().c_str();
       if (UserAgent.isPresent())
         {
           const std::string &user_agent = UserAgent;
--
2.14.2

Reply | Threaded
Open this post in threaded view
|

[PATCH setup] Add OS version to user-agent string

Jon TURNEY
Signed-off-by: Jon Turney <[hidden email]>
---
 nio-ie5.cc | 12 ++++++++----
 win32.h    |  2 ++
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/nio-ie5.cc b/nio-ie5.cc
index c8f65a9..a649233 100644
--- a/nio-ie5.cc
+++ b/nio-ie5.cc
@@ -29,6 +29,7 @@
 #include "LogSingleton.h"
 #include "setup_version.h"
 #include "getopt++/StringOption.h"
+#include <sstream>
 
 static StringOption UserAgent ("", '\0', "user-agent", "User agent string for HTTP requests");
 
@@ -43,19 +44,22 @@ determine_default_useragent(void)
   if (!default_useragent.empty())
     return default_useragent;
 
-  std::string token = "Unknown";
+  std::stringstream os;
+  os << "Windows NT " << OSMajorVersion() << "." << OSMinorVersion() << "." << OSBuildNumber();
+
+  std::string bitness = "Unknown";
 #ifdef __x86_64__
-  token = "Win64";
+  bitness = "Win64";
 #else
   typedef BOOL (WINAPI *PFNISWOW64PROCESS)(HANDLE, PBOOL);
   PFNISWOW64PROCESS pfnIsWow64Process = (PFNISWOW64PROCESS)GetProcAddress(GetModuleHandle("kernel32"), "IsWow64Process");
   if (pfnIsWow64Process) {
     BOOL bIsWow64 = FALSE;
     if (pfnIsWow64Process(GetCurrentProcess(), &bIsWow64))
-      token = bIsWow64 ? "WoW64" : "Win32";
+      bitness = bIsWow64 ? "WoW64" : "Win32";
   }
 #endif
-  default_useragent = std::string("Cygwin-Setup/") + setup_version + " (" + token + ")";
+  default_useragent = std::string("Cygwin-Setup/") + setup_version + " (" + os.str() + ";" + bitness + ")";
   return default_useragent;
 }
 
diff --git a/win32.h b/win32.h
index fd4c80c..c866790 100644
--- a/win32.h
+++ b/win32.h
@@ -166,6 +166,7 @@ class VersionInfo
      VersionInfo ();
      DWORD major () const { return v.dwMajorVersion; }
      DWORD minor () const { return v.dwMinorVersion; }
+     DWORD buildNumber () const { return v.dwBuildNumber; }
   private:
      OSVERSIONINFO v;
 };
@@ -174,6 +175,7 @@ VersionInfo& GetVer ();
 
 #define OSMajorVersion() (GetVer ().major ())
 #define OSMinorVersion() (GetVer ().minor ())
+#define OSBuildNumber() (GetVer ().buildNumber ())
 
 static inline void
 GetDlgItemRect (HWND h, int item, LPRECT r)
--
2.14.2