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