Skip to content

Commit cd7e837

Browse files
committedJan 23, 2014
Include system info in the HTTP user agent on Windows
1 parent 1b5b6fe commit cd7e837

File tree

3 files changed

+52
-22
lines changed

3 files changed

+52
-22
lines changed
 

‎src/httpfetch.cpp

+2-13
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,13 @@ with this program; if not, write to the Free Software Foundation, Inc.,
1818
*/
1919

2020
#include "socket.h" // for select()
21-
#include "porting.h" // for sleep_ms()
21+
#include "porting.h" // for sleep_ms(), get_sysinfo()
2222
#include "httpfetch.h"
2323
#include <iostream>
2424
#include <sstream>
2525
#include <list>
2626
#include <map>
2727
#include <errno.h>
28-
#ifndef _WIN32
29-
#include <sys/utsname.h>
30-
#endif
3128
#include "jthread/jevent.h"
3229
#include "config.h"
3330
#include "exceptions.h"
@@ -50,15 +47,7 @@ HTTPFetchRequest::HTTPFetchRequest()
5047
timeout = g_settings->getS32("curl_timeout");
5148
connect_timeout = timeout * 5;
5249

53-
useragent = std::string("Minetest/") + minetest_version_hash + " ";
54-
#ifdef _WIN32
55-
useragent += "(Windows)";
56-
#else
57-
struct utsname osinfo;
58-
uname(&osinfo);
59-
useragent += std::string("(") + osinfo.sysname + "/"
60-
+ osinfo.release + " " + osinfo.machine + ")";
61-
#endif
50+
useragent = std::string("Minetest/") + minetest_version_hash + " (" + porting::get_sysinfo() + ")";
6251
}
6352

6453

‎src/porting.cpp

+44-9
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,18 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2323
See comments in porting.h
2424
*/
2525

26-
#if defined(linux)
27-
#include <unistd.h>
28-
#elif defined(__APPLE__)
29-
#include <unistd.h>
26+
#if defined(__APPLE__)
3027
#include <mach-o/dyld.h>
28+
#include "CoreFoundation/CoreFoundation.h"
3129
#elif defined(__FreeBSD__)
32-
#include <unistd.h>
3330
#include <sys/types.h>
3431
#include <sys/sysctl.h>
32+
#elif defined(_WIN32)
33+
#include <algorithm>
34+
#endif
35+
#if !defined(_WIN32)
36+
#include <unistd.h>
37+
#include <sys/utsname.h>
3538
#endif
3639

3740
#include "porting.h"
@@ -42,10 +45,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
4245
#include "util/string.h"
4346
#include <list>
4447

45-
#ifdef __APPLE__
46-
#include "CoreFoundation/CoreFoundation.h"
47-
#endif
48-
4948
namespace porting
5049
{
5150

@@ -284,6 +283,42 @@ bool detectMSVCBuildDir(char *c_path)
284283
return (removeStringEnd(path, ends) != "");
285284
}
286285

286+
std::string get_sysinfo()
287+
{
288+
#ifdef _WIN32
289+
OSVERSIONINFO osvi;
290+
std::ostringstream oss;
291+
std::string tmp;
292+
ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
293+
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
294+
GetVersionEx(&osvi);
295+
tmp = osvi.szCSDVersion;
296+
std::replace(tmp.begin(), tmp.end(), ' ', '_');
297+
298+
oss << "Windows/" << osvi.dwMajorVersion << "."
299+
<< osvi.dwMinorVersion;
300+
if(osvi.szCSDVersion[0])
301+
oss << "-" << tmp;
302+
oss << " ";
303+
#ifdef _WIN64
304+
oss << "x86_64";
305+
#else
306+
BOOL is64 = FALSE;
307+
if(IsWow64Process(GetCurrentProcess(), &is64) && is64)
308+
oss << "x86_64"; // 32-bit app on 64-bit OS
309+
else
310+
oss << "x86";
311+
#endif
312+
313+
return oss.str();
314+
#else
315+
struct utsname osinfo;
316+
uname(&osinfo);
317+
return std::string(osinfo.sysname) + "/"
318+
+ osinfo.release + " " + osinfo.machine;
319+
#endif
320+
}
321+
287322
void initializePaths()
288323
{
289324
#if RUN_IN_PLACE

‎src/porting.h

+6
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,12 @@ bool threadBindToProcessor(threadid_t tid, int pnumber);
148148
*/
149149
bool threadSetPriority(threadid_t tid, int prio);
150150

151+
/*
152+
Return system information
153+
e.g. "Linux/3.12.7 x86_64"
154+
*/
155+
std::string get_sysinfo();
156+
151157
/*
152158
Resolution is 10-20ms.
153159
Remember to check for overflows.

0 commit comments

Comments
 (0)
Please sign in to comment.