2 * Copyright 2017, Andrew Lindesay <apl@lindesay.co.nz>.
3 * All rights reserved. Distributed under the terms of the MIT License.
6 #include "ServerSettings.h"
11 #include <AppFileInfo.h>
12 #include <Application.h>
17 #define BASEURL_DEFAULT "https://depot.haiku-os.org"
18 #define USERAGENT_FALLBACK_VERSION "0.0.0"
21 BUrl
ServerSettings::sBaseUrl
= BUrl(BASEURL_DEFAULT
);
22 BString
ServerSettings::sUserAgent
= BString();
23 pthread_once_t
ServerSettings::sUserAgentInitOnce
= PTHREAD_ONCE_INIT
;
24 bool ServerSettings::sPreferCache
= false;
25 bool ServerSettings::sDropCache
= false;
26 bool ServerSettings::sForceNoNetwork
= false;
30 ServerSettings::SetBaseUrl(const BUrl
& value
)
32 if (!value
.IsValid()) {
33 fprintf(stderr
, "the url is not valid\n");
37 if (value
.Protocol() != "http" && value
.Protocol() != "https") {
38 fprintf(stderr
, "the url protocol must be 'http' or 'https'\n");
49 ServerSettings::CreateFullUrl(const BString urlPathComponents
)
51 return BUrl(sBaseUrl
, urlPathComponents
);
56 ServerSettings::GetUserAgent()
58 if (sUserAgent
.IsEmpty())
59 pthread_once(&sUserAgentInitOnce
, &ServerSettings::_InitUserAgent
);
66 ServerSettings::_InitUserAgent()
68 sUserAgent
.SetTo("HaikuDepot/");
69 sUserAgent
.Append(_GetUserAgentVersionString());
74 ServerSettings::_GetUserAgentVersionString()
78 if (be_app
->GetAppInfo(&info
) != B_OK
) {
79 fprintf(stderr
, "Unable to get the application info\n");
81 return BString(USERAGENT_FALLBACK_VERSION
);
84 BFile
file(&info
.ref
, B_READ_ONLY
);
86 if (file
.InitCheck() != B_OK
) {
87 fprintf(stderr
, "Unable to access the application info file\n");
89 return BString(USERAGENT_FALLBACK_VERSION
);
92 BAppFileInfo
appFileInfo(&file
);
93 version_info versionInfo
;
95 if (appFileInfo
.GetVersionInfo(
96 &versionInfo
, B_APP_VERSION_KIND
) != B_OK
) {
97 fprintf(stderr
, "Unable to establish the application version\n");
99 return BString(USERAGENT_FALLBACK_VERSION
);
103 result
.SetToFormat("%" B_PRId32
".%" B_PRId32
".%" B_PRId32
,
104 versionInfo
.major
, versionInfo
.middle
, versionInfo
.minor
);
110 ServerSettings::AugmentHeaders(BHttpHeaders
& headers
)
112 headers
.AddHeader("User-Agent", GetUserAgent());
117 ServerSettings::PreferCache()
124 ServerSettings::SetPreferCache(bool value
)
126 sPreferCache
= value
;
131 ServerSettings::DropCache()
138 ServerSettings::SetDropCache(bool value
)
145 ServerSettings::ForceNoNetwork()
147 return sForceNoNetwork
;
152 ServerSettings::SetForceNoNetwork(bool value
)
154 sForceNoNetwork
= value
;