Include system info in the HTTP user agent on Windows
authorSfan5 <sfan5@live.de>
Sun, 19 Jan 2014 13:32:03 +0000 (14:32 +0100)
committerSfan5 <sfan5@live.de>
Thu, 23 Jan 2014 21:27:13 +0000 (22:27 +0100)
src/httpfetch.cpp
src/porting.cpp
src/porting.h

index 599507562db4e9bee34d43deb2c04f702813a045..2eca363d7bb9a34bb415a431cc2df00ddedd8d30 100644 (file)
@@ -18,16 +18,13 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 */
 
 #include "socket.h" // for select()
-#include "porting.h" // for sleep_ms()
+#include "porting.h" // for sleep_ms(), get_sysinfo()
 #include "httpfetch.h"
 #include <iostream>
 #include <sstream>
 #include <list>
 #include <map>
 #include <errno.h>
-#ifndef _WIN32
-#include <sys/utsname.h>
-#endif
 #include "jthread/jevent.h"
 #include "config.h"
 #include "exceptions.h"
@@ -50,15 +47,7 @@ HTTPFetchRequest::HTTPFetchRequest()
        timeout = g_settings->getS32("curl_timeout");
        connect_timeout = timeout * 5;
        
-       useragent = std::string("Minetest/") + minetest_version_hash + " ";
-#ifdef _WIN32
-       useragent += "(Windows)";
-#else
-       struct utsname osinfo;
-       uname(&osinfo);
-       useragent += std::string("(") + osinfo.sysname + "/"
-                       + osinfo.release + " " + osinfo.machine + ")";
-#endif
+       useragent = std::string("Minetest/") + minetest_version_hash + " (" + porting::get_sysinfo() + ")";
 }
 
 
index a080a44e5bb5cbae1944003fad47779f1f1ff61e..b0a1843eb0b89d1fa17a8627c917d2eff0948d2e 100644 (file)
@@ -23,15 +23,18 @@ with this program; if not, write to the Free Software Foundation, Inc.,
        See comments in porting.h
 */
 
-#if defined(linux)
-       #include <unistd.h>
-#elif defined(__APPLE__)
-       #include <unistd.h>
+#if defined(__APPLE__)
        #include <mach-o/dyld.h>
+       #include "CoreFoundation/CoreFoundation.h"
 #elif defined(__FreeBSD__)
-       #include <unistd.h>
        #include <sys/types.h>
        #include <sys/sysctl.h>
+#elif defined(_WIN32)
+       #include <algorithm>
+#endif
+#if !defined(_WIN32)
+       #include <unistd.h>
+       #include <sys/utsname.h>
 #endif
 
 #include "porting.h"
@@ -42,10 +45,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "util/string.h"
 #include <list>
 
-#ifdef __APPLE__
-       #include "CoreFoundation/CoreFoundation.h"
-#endif
-
 namespace porting
 {
 
@@ -284,6 +283,42 @@ bool detectMSVCBuildDir(char *c_path)
        return (removeStringEnd(path, ends) != "");
 }
 
+std::string get_sysinfo()
+{
+#ifdef _WIN32
+       OSVERSIONINFO osvi;
+       std::ostringstream oss;
+       std::string tmp;
+       ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
+       osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
+       GetVersionEx(&osvi);
+       tmp = osvi.szCSDVersion;
+       std::replace(tmp.begin(), tmp.end(), ' ', '_');
+
+       oss << "Windows/" << osvi.dwMajorVersion << "."
+               << osvi.dwMinorVersion;
+       if(osvi.szCSDVersion[0])
+               oss << "-" << tmp;
+       oss << " ";
+       #ifdef _WIN64
+       oss << "x86_64";
+       #else
+       BOOL is64 = FALSE;
+       if(IsWow64Process(GetCurrentProcess(), &is64) && is64)
+               oss << "x86_64"; // 32-bit app on 64-bit OS
+       else
+               oss << "x86";
+       #endif
+
+       return oss.str();
+#else
+       struct utsname osinfo;
+       uname(&osinfo);
+       return std::string(osinfo.sysname) + "/"
+               + osinfo.release + " " + osinfo.machine;
+#endif
+}
+
 void initializePaths()
 {
 #if RUN_IN_PLACE
index ea7f31b137f3dda6aaf93d07215878198aa9d99f..7cd4cf031f446bf9c433e14aa5eca9b210ffc468 100644 (file)
@@ -148,6 +148,12 @@ bool threadBindToProcessor(threadid_t tid, int pnumber);
 */
 bool threadSetPriority(threadid_t tid, int prio);
 
+/*
+       Return system information
+       e.g. "Linux/3.12.7 x86_64"
+*/
+std::string get_sysinfo();
+
 /*
        Resolution is 10-20ms.
        Remember to check for overflows.