xref: /haiku/src/apps/haikudepot/server/ServerSettings.cpp (revision 21258e2674226d6aa732321b6f8494841895af5f)
1 /*
2  * Copyright 2017-2020, Andrew Lindesay <apl@lindesay.co.nz>.
3  * All rights reserved. Distributed under the terms of the MIT License.
4  */
5 
6 #include "ServerSettings.h"
7 
8 #include <stdlib.h>
9 #include <pthread.h>
10 
11 #include <AppFileInfo.h>
12 #include <Application.h>
13 #include <Autolock.h>
14 #include <NetworkInterface.h>
15 #include <NetworkRoster.h>
16 #include <Roster.h>
17 #include <Url.h>
18 
19 #include "Logger.h"
20 
21 
22 #define BASEURL_DEFAULT "https://depot.haiku-os.org"
23 #define USERAGENT_FALLBACK_VERSION "0.0.0"
24 
25 
26 BUrl ServerSettings::sBaseUrl = BUrl(BASEURL_DEFAULT);
27 BString ServerSettings::sUserAgent = BString();
28 pthread_once_t ServerSettings::sUserAgentInitOnce = PTHREAD_ONCE_INIT;
29 bool ServerSettings::sPreferCache = false;
30 bool ServerSettings::sDropCache = false;
31 bool ServerSettings::sForceNoNetwork = false;
32 bool ServerSettings::sClientTooOld = false;
33 BLocker ServerSettings::sLock;
34 
35 
36 status_t
37 ServerSettings::SetBaseUrl(const BUrl& value)
38 {
39 	if (!value.IsValid()) {
40 		HDERROR("the url is not valid");
41 		return B_BAD_VALUE;
42 	}
43 
44 	if (value.Protocol() != "http" && value.Protocol() != "https") {
45 		HDERROR("the url protocol must be 'http' or 'https'");
46 		return B_BAD_VALUE;
47 	}
48 
49 	sBaseUrl = value;
50 
51 	return B_OK;
52 }
53 
54 
55 BUrl
56 ServerSettings::CreateFullUrl(const BString urlPathComponents)
57 {
58 	return BUrl(sBaseUrl, urlPathComponents);
59 }
60 
61 
62 const BString
63 ServerSettings::GetUserAgent()
64 {
65 	if (sUserAgent.IsEmpty())
66 		pthread_once(&sUserAgentInitOnce, &ServerSettings::_InitUserAgent);
67 
68 	return sUserAgent;
69 }
70 
71 
72 void
73 ServerSettings::_InitUserAgent()
74 {
75 	sUserAgent.SetTo("HaikuDepot/");
76 	sUserAgent.Append(_GetUserAgentVersionString());
77 }
78 
79 
80 const BString
81 ServerSettings::_GetUserAgentVersionString()
82 {
83 	app_info info;
84 
85 	if (be_app->GetAppInfo(&info) != B_OK) {
86 		HDERROR("Unable to get the application info");
87 		be_app->Quit();
88 		return BString(USERAGENT_FALLBACK_VERSION);
89 	}
90 
91 	BFile file(&info.ref, B_READ_ONLY);
92 
93 	if (file.InitCheck() != B_OK) {
94 		HDERROR("Unable to access the application info file");
95 		be_app->Quit();
96 		return BString(USERAGENT_FALLBACK_VERSION);
97 	}
98 
99 	BAppFileInfo appFileInfo(&file);
100 	version_info versionInfo;
101 
102 	if (appFileInfo.GetVersionInfo(
103 			&versionInfo, B_APP_VERSION_KIND) != B_OK) {
104 		HDERROR("Unable to establish the application version");
105 		be_app->Quit();
106 		return BString(USERAGENT_FALLBACK_VERSION);
107 	}
108 
109 	BString result;
110 	result.SetToFormat("%" B_PRId32 ".%" B_PRId32 ".%" B_PRId32,
111 		versionInfo.major, versionInfo.middle, versionInfo.minor);
112 	return result;
113 }
114 
115 
116 void
117 ServerSettings::AugmentHeaders(BHttpHeaders& headers)
118 {
119 	headers.AddHeader("User-Agent", GetUserAgent());
120 }
121 
122 
123 bool
124 ServerSettings::PreferCache()
125 {
126 	return sPreferCache;
127 }
128 
129 
130 void
131 ServerSettings::SetPreferCache(bool value)
132 {
133 	sPreferCache = value;
134 }
135 
136 
137 bool
138 ServerSettings::DropCache()
139 {
140 	return sDropCache;
141 }
142 
143 
144 void
145 ServerSettings::SetDropCache(bool value)
146 {
147 	sDropCache = value;
148 }
149 
150 
151 bool
152 ServerSettings::ForceNoNetwork()
153 {
154 	return sForceNoNetwork;
155 }
156 
157 
158 void
159 ServerSettings::SetForceNoNetwork(bool value)
160 {
161 	sForceNoNetwork = value;
162 }
163 
164 
165 bool
166 ServerSettings::IsClientTooOld()
167 {
168 	BAutolock locker(&sLock);
169 	return sClientTooOld;
170 }
171 
172 
173 void
174 ServerSettings::SetClientTooOld()
175 {
176 	BAutolock locker(&sLock);
177 	sClientTooOld = true;
178 }
179