xref: /haiku/src/apps/haikudepot/server/ServerSettings.cpp (revision 6889394848e2dc9f41ff53b12141d572822ca0c6)
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::sUrlConnectionTraceLogging = false;
25 
26 
27 status_t
28 ServerSettings::SetBaseUrl(const BUrl& value)
29 {
30 	if (!value.IsValid()) {
31 		fprintf(stderr, "the url is not valid\n");
32 		return B_BAD_VALUE;
33 	}
34 
35 	if (value.Protocol() != "http" && value.Protocol() != "https") {
36 		fprintf(stderr, "the url protocol must be 'http' or 'https'\n");
37 		return B_BAD_VALUE;
38 	}
39 
40 	sBaseUrl = value;
41 
42 	return B_OK;
43 }
44 
45 
46 BUrl
47 ServerSettings::CreateFullUrl(const BString urlPathComponents)
48 {
49 	return BUrl(sBaseUrl, urlPathComponents);
50 }
51 
52 
53 const BString
54 ServerSettings::GetUserAgent()
55 {
56 	if (sUserAgent.IsEmpty())
57 		pthread_once(&sUserAgentInitOnce, &ServerSettings::_InitUserAgent);
58 
59 	return sUserAgent;
60 }
61 
62 
63 void
64 ServerSettings::_InitUserAgent()
65 {
66 	sUserAgent.SetTo("HaikuDepot/");
67 	sUserAgent.Append(_GetUserAgentVersionString());
68 }
69 
70 
71 void
72 ServerSettings::EnableUrlConnectionTraceLogging() {
73 	sUrlConnectionTraceLogging = true;
74 }
75 
76 bool
77 ServerSettings::UrlConnectionTraceLoggingEnabled() {
78 	return sUrlConnectionTraceLogging;
79 }
80 
81 
82 const BString
83 ServerSettings::_GetUserAgentVersionString()
84 {
85 	app_info info;
86 
87 	if (be_app->GetAppInfo(&info) != B_OK) {
88 		fprintf(stderr, "Unable to get the application info\n");
89 		be_app->Quit();
90 		return BString(USERAGENT_FALLBACK_VERSION);
91 	}
92 
93 	BFile file(&info.ref, B_READ_ONLY);
94 
95 	if (file.InitCheck() != B_OK) {
96 		fprintf(stderr, "Unable to access the application info file\n");
97 		be_app->Quit();
98 		return BString(USERAGENT_FALLBACK_VERSION);
99 	}
100 
101 	BAppFileInfo appFileInfo(&file);
102 	version_info versionInfo;
103 
104 	if (appFileInfo.GetVersionInfo(
105 		&versionInfo, B_APP_VERSION_KIND) != B_OK) {
106 		fprintf(stderr, "Unable to establish the application version\n");
107 		be_app->Quit();
108 		return BString(USERAGENT_FALLBACK_VERSION);
109 	}
110 
111 	BString result;
112 	result.SetToFormat("%" B_PRId32 ".%" B_PRId32 ".%" B_PRId32,
113 		versionInfo.major, versionInfo.middle, versionInfo.minor);
114 	return result;
115 }
116 
117 void
118 ServerSettings::AugmentHeaders(BHttpHeaders& headers)
119 {
120 	headers.AddHeader("User-Agent", GetUserAgent());
121 }
122 
123 
124