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