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
;
27 ServerSettings::SetBaseUrl(const BUrl
& value
)
29 if (!value
.IsValid()) {
30 fprintf(stderr
, "the url is not valid\n");
34 if (value
.Protocol() != "http" && value
.Protocol() != "https") {
35 fprintf(stderr
, "the url protocol must be 'http' or 'https'\n");
46 ServerSettings::CreateFullUrl(const BString urlPathComponents
)
48 return BUrl(sBaseUrl
, urlPathComponents
);
53 ServerSettings::GetUserAgent()
55 if (sUserAgent
.IsEmpty())
56 pthread_once(&sUserAgentInitOnce
, &ServerSettings::_InitUserAgent
);
63 ServerSettings::_InitUserAgent()
65 sUserAgent
.SetTo("HaikuDepot/");
66 sUserAgent
.Append(_GetUserAgentVersionString());
71 ServerSettings::_GetUserAgentVersionString()
75 if (be_app
->GetAppInfo(&info
) != B_OK
) {
76 fprintf(stderr
, "Unable to get the application info\n");
78 return BString(USERAGENT_FALLBACK_VERSION
);
81 BFile
file(&info
.ref
, B_READ_ONLY
);
83 if (file
.InitCheck() != B_OK
) {
84 fprintf(stderr
, "Unable to access the application info file\n");
86 return BString(USERAGENT_FALLBACK_VERSION
);
89 BAppFileInfo
appFileInfo(&file
);
90 version_info versionInfo
;
92 if (appFileInfo
.GetVersionInfo(
93 &versionInfo
, B_APP_VERSION_KIND
) != B_OK
) {
94 fprintf(stderr
, "Unable to establish the application version\n");
96 return BString(USERAGENT_FALLBACK_VERSION
);
100 result
.SetToFormat("%" B_PRId32
".%" B_PRId32
".%" B_PRId32
,
101 versionInfo
.major
, versionInfo
.middle
, versionInfo
.minor
);
106 ServerSettings::AugmentHeaders(BHttpHeaders
& headers
)
108 headers
.AddHeader("User-Agent", GetUserAgent());