1 /* 2 * Copyright 2017-2021, 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 <stdlib.h> 9 #include <pthread.h> 10 11 #include <Application.h> 12 #include <Autolock.h> 13 #include <NetworkInterface.h> 14 #include <NetworkRoster.h> 15 #include <Roster.h> 16 #include <Url.h> 17 18 #include "AppUtils.h" 19 #include "Logger.h" 20 21 22 #define BASEURL_DEFAULT "https://depot.haiku-os.org" 23 #define USERAGENT_FALLBACK_VERSION "0.0.0" 24 25 26 BUrl ServerSettings::sBaseUrl = BUrl(BASEURL_DEFAULT); 27 BString ServerSettings::sUserAgent = BString(); 28 pthread_once_t ServerSettings::sUserAgentInitOnce = PTHREAD_ONCE_INIT; 29 bool ServerSettings::sPreferCache = false; 30 bool ServerSettings::sDropCache = false; 31 bool ServerSettings::sForceNoNetwork = false; 32 bool ServerSettings::sClientTooOld = false; 33 BLocker ServerSettings::sLock; 34 35 36 status_t SetBaseUrl(const BUrl & value)37ServerSettings::SetBaseUrl(const BUrl& value) 38 { 39 if (!value.IsValid()) { 40 HDERROR("the url is not valid"); 41 return B_BAD_VALUE; 42 } 43 44 if (value.Protocol() != "http" && value.Protocol() != "https") { 45 HDERROR("the url protocol must be 'http' or 'https'"); 46 return B_BAD_VALUE; 47 } 48 49 sBaseUrl = value; 50 51 return B_OK; 52 } 53 54 55 BUrl CreateFullUrl(const BString urlPathComponents)56ServerSettings::CreateFullUrl(const BString urlPathComponents) 57 { 58 return BUrl(sBaseUrl, urlPathComponents); 59 } 60 61 62 const BString GetUserAgent()63ServerSettings::GetUserAgent() 64 { 65 if (sUserAgent.IsEmpty()) 66 pthread_once(&sUserAgentInitOnce, &ServerSettings::_InitUserAgent); 67 68 return sUserAgent; 69 } 70 71 72 void _InitUserAgent()73ServerSettings::_InitUserAgent() 74 { 75 sUserAgent.SetTo("HaikuDepot/"); 76 sUserAgent.Append(_GetUserAgentVersionString()); 77 } 78 79 80 const BString _GetUserAgentVersionString()81ServerSettings::_GetUserAgentVersionString() 82 { 83 BString result; 84 if (AppUtils::GetAppVersionString(result) != B_OK) { 85 be_app->Quit(); 86 return BString(USERAGENT_FALLBACK_VERSION); 87 } 88 return result; 89 } 90 91 92 void AugmentHeaders(BHttpHeaders & headers)93ServerSettings::AugmentHeaders(BHttpHeaders& headers) 94 { 95 headers.AddHeader("User-Agent", GetUserAgent()); 96 } 97 98 99 bool PreferCache()100ServerSettings::PreferCache() 101 { 102 return sPreferCache; 103 } 104 105 106 void SetPreferCache(bool value)107ServerSettings::SetPreferCache(bool value) 108 { 109 sPreferCache = value; 110 } 111 112 113 bool DropCache()114ServerSettings::DropCache() 115 { 116 return sDropCache; 117 } 118 119 120 void SetDropCache(bool value)121ServerSettings::SetDropCache(bool value) 122 { 123 sDropCache = value; 124 } 125 126 127 bool ForceNoNetwork()128ServerSettings::ForceNoNetwork() 129 { 130 return sForceNoNetwork; 131 } 132 133 134 void SetForceNoNetwork(bool value)135ServerSettings::SetForceNoNetwork(bool value) 136 { 137 sForceNoNetwork = value; 138 } 139 140 141 bool IsClientTooOld()142ServerSettings::IsClientTooOld() 143 { 144 BAutolock locker(&sLock); 145 return sClientTooOld; 146 } 147 148 149 void SetClientTooOld()150ServerSettings::SetClientTooOld() 151 { 152 BAutolock locker(&sLock); 153 sClientTooOld = true; 154 } 155