forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_agent.cpp
150 lines (117 loc) · 3.32 KB
/
user_agent.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Aseprite
// Copyright (C) 2019-2020 Igara Studio S.A.
// Copyright (C) 2001-2017 David Capello
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "updater/user_agent.h"
#include "ver/info.h"
#include <string>
#include <sstream>
#if _WIN32 // Windows
#include <windows.h>
extern "C" BOOL IsWow64();
extern "C" const char* WineVersion();
#elif __APPLE__ // Mac OS X
void getMacOSXVersion(int& major, int& minor, int& patch);
#else // Unix-like system
#include "base/trim_string.h"
#include "cfg/cfg.h"
#include <cstring>
#include <sys/utsname.h>
#endif
namespace updater {
std::string getUserAgent()
{
std::stringstream userAgent;
// App name and version
userAgent << get_app_name() << "/" << get_app_version() << " (";
#if _WIN32
// ----------------------------------------------------------------------
// Windows
OSVERSIONINFOEX osv;
osv.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
::GetVersionEx((OSVERSIONINFO*)&osv);
userAgent << "Windows";
switch (osv.wProductType) {
case VER_NT_DOMAIN_CONTROLLER:
case VER_NT_SERVER:
userAgent << " Server";
break;
case VER_NT_WORKSTATION:
userAgent << " NT";
break;
}
userAgent << " " << osv.dwMajorVersion << "." << osv.dwMinorVersion;
if (osv.wServicePackMajor > 0)
userAgent << " SP" << osv.wServicePackMajor;
if (IsWow64())
userAgent << "; WOW64";
if (const char* wineVer = WineVersion())
userAgent << "; Wine " << wineVer;
#elif __APPLE__
// ----------------------------------------------------------------------
// Mac OS X
int major, minor, patch;
getMacOSXVersion(major, minor, patch);
userAgent << "Mac OS X " << major << "." << minor << "." << patch;
#else
// ----------------------------------------------------------------------
// Unix like
cfg::CfgFile file;
auto isQuote = [](int c) {
return
c == '"' ||
c == '\'' ||
std::isspace(c);
};
auto getValue = [&file, &isQuote](const char* varName) -> std::string {
std::string result;
const char* value = file.getValue("", varName, nullptr);
if (value && std::strlen(value) > 0)
base::trim_string(value, result, isQuote);
return result;
};
// Read information from /etc/os-release
file.load("/etc/os-release");
std::string name = getValue("PRETTY_NAME");
if (!name.empty()) {
userAgent << name;
}
else {
name = getValue("NAME");
std::string version = getValue("VERSION");
if (!name.empty() && !version.empty()) {
userAgent << name << " " << version;
}
else {
// Read information from /etc/lsb-release
file.load("/etc/lsb-release");
name = getValue("DISTRIB_DESCRIPTION");
if (!name.empty()) {
userAgent << name;
}
else {
name = getValue("DISTRIB_ID");
version = getValue("DISTRIB_RELEASE");
if (!name.empty() &&
!version.empty()) {
userAgent << name << " " << version;
}
else {
// Last resource, use uname() function
struct utsname utsn;
uname(&utsn);
userAgent << utsn.sysname << " " << utsn.release;
}
}
}
}
#endif
userAgent << ")";
return userAgent.str();
}
} // namespace updater