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