xref: /haiku/src/apps/haikudepot/server/ServerSettings.cpp (revision 3995592cdf304335132305e27c40cbb0b1ac46e3)
1 /*
2  * Copyright 2017, 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 
11 #include <AppFileInfo.h>
12 #include <Application.h>
13 #include <Roster.h>
14 #include <Url.h>
15 
16 
17 #define BASEURL_DEFAULT "https://depot.haiku-os.org"
18 #define USERAGENT_FALLBACK_VERSION "0.0.0"
19 
20 
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;
27 
28 
29 status_t
30 ServerSettings::SetBaseUrl(const BUrl& value)
31 {
32 	if (!value.IsValid()) {
33 		fprintf(stderr, "the url is not valid\n");
34 		return B_BAD_VALUE;
35 	}
36 
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;
40 	}
41 
42 	sBaseUrl = value;
43 
44 	return B_OK;
45 }
46 
47 
48 BUrl
49 ServerSettings::CreateFullUrl(const BString urlPathComponents)
50 {
51 	return BUrl(sBaseUrl, urlPathComponents);
52 }
53 
54 
55 const BString
56 ServerSettings::GetUserAgent()
57 {
58 	if (sUserAgent.IsEmpty())
59 		pthread_once(&sUserAgentInitOnce, &ServerSettings::_InitUserAgent);
60 
61 	return sUserAgent;
62 }
63 
64 
65 void
66 ServerSettings::_InitUserAgent()
67 {
68 	sUserAgent.SetTo("HaikuDepot/");
69 	sUserAgent.Append(_GetUserAgentVersionString());
70 }
71 
72 
73 const BString
74 ServerSettings::_GetUserAgentVersionString()
75 {
76 	app_info info;
77 
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);
82 	}
83 
84 	BFile file(&info.ref, B_READ_ONLY);
85 
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);
90 	}
91 
92 	BAppFileInfo appFileInfo(&file);
93 	version_info versionInfo;
94 
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);
100 	}
101 
102 	BString result;
103 	result.SetToFormat("%" B_PRId32 ".%" B_PRId32 ".%" B_PRId32,
104 		versionInfo.major, versionInfo.middle, versionInfo.minor);
105 	return result;
106 }
107 
108 
109 void
110 ServerSettings::AugmentHeaders(BHttpHeaders& headers)
111 {
112 	headers.AddHeader("User-Agent", GetUserAgent());
113 }
114 
115 
116 bool
117 ServerSettings::PreferCache()
118 {
119 	return sPreferCache;
120 }
121 
122 
123 void
124 ServerSettings::SetPreferCache(bool value)
125 {
126 	sPreferCache = value;
127 }
128 
129 
130 bool
131 ServerSettings::DropCache()
132 {
133 	return sDropCache;
134 }
135 
136 
137 void
138 ServerSettings::SetDropCache(bool value)
139 {
140 	sDropCache = value;
141 }
142 
143 
144 bool
145 ServerSettings::ForceNoNetwork()
146 {
147 	return sForceNoNetwork;
148 }
149 
150 
151 void
152 ServerSettings::SetForceNoNetwork(bool value)
153 {
154 	sForceNoNetwork = value;
155 }