vfs: check userland buffers before reading them.
[haiku.git] / src / apps / haikudepot / server / ServerSettings.cpp
blob043fee4b48d62ac32bc1522673731bb6fac3c988
1 /*
2 * Copyright 2017, Andrew Lindesay <apl@lindesay.co.nz>.
3 * All rights reserved. Distributed under the terms of the MIT License.
4 */
6 #include "ServerSettings.h"
8 #include <stdio.h>
9 #include <stdlib.h>
11 #include <AppFileInfo.h>
12 #include <Application.h>
13 #include <Roster.h>
14 #include <Url.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;
29 status_t
30 ServerSettings::SetBaseUrl(const BUrl& value)
32 if (!value.IsValid()) {
33 fprintf(stderr, "the url is not valid\n");
34 return B_BAD_VALUE;
37 if (value.Protocol() != "http" && value.Protocol() != "https") {
38 fprintf(stderr, "the url protocol must be 'http' or 'https'\n");
39 return B_BAD_VALUE;
42 sBaseUrl = value;
44 return B_OK;
48 BUrl
49 ServerSettings::CreateFullUrl(const BString urlPathComponents)
51 return BUrl(sBaseUrl, urlPathComponents);
55 const BString
56 ServerSettings::GetUserAgent()
58 if (sUserAgent.IsEmpty())
59 pthread_once(&sUserAgentInitOnce, &ServerSettings::_InitUserAgent);
61 return sUserAgent;
65 void
66 ServerSettings::_InitUserAgent()
68 sUserAgent.SetTo("HaikuDepot/");
69 sUserAgent.Append(_GetUserAgentVersionString());
73 const BString
74 ServerSettings::_GetUserAgentVersionString()
76 app_info info;
78 if (be_app->GetAppInfo(&info) != B_OK) {
79 fprintf(stderr, "Unable to get the application info\n");
80 be_app->Quit();
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");
88 be_app->Quit();
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");
98 be_app->Quit();
99 return BString(USERAGENT_FALLBACK_VERSION);
102 BString result;
103 result.SetToFormat("%" B_PRId32 ".%" B_PRId32 ".%" B_PRId32,
104 versionInfo.major, versionInfo.middle, versionInfo.minor);
105 return result;
109 void
110 ServerSettings::AugmentHeaders(BHttpHeaders& headers)
112 headers.AddHeader("User-Agent", GetUserAgent());
116 bool
117 ServerSettings::PreferCache()
119 return sPreferCache;
123 void
124 ServerSettings::SetPreferCache(bool value)
126 sPreferCache = value;
130 bool
131 ServerSettings::DropCache()
133 return sDropCache;
137 void
138 ServerSettings::SetDropCache(bool value)
140 sDropCache = value;
144 bool
145 ServerSettings::ForceNoNetwork()
147 return sForceNoNetwork;
151 void
152 ServerSettings::SetForceNoNetwork(bool value)
154 sForceNoNetwork = value;