119c15fecSAndrew Lindesay/*
254312619SAndrew Lindesay * Copyright 2017-2018, Andrew Lindesay <apl@lindesay.co.nz>.
319c15fecSAndrew Lindesay * All rights reserved. Distributed under the terms of the MIT License.
419c15fecSAndrew Lindesay */
519c15fecSAndrew Lindesay
619c15fecSAndrew Lindesay#include "ServerSettings.h"
719c15fecSAndrew Lindesay
819c15fecSAndrew Lindesay#include <stdio.h>
919c15fecSAndrew Lindesay#include <stdlib.h>
10f1fafe31SAugustin Cavalier#include <pthread.h>
1119c15fecSAndrew Lindesay
1219c15fecSAndrew Lindesay#include <AppFileInfo.h>
1319c15fecSAndrew Lindesay#include <Application.h>
1454312619SAndrew Lindesay#include <Autolock.h>
1554312619SAndrew Lindesay#include <NetworkInterface.h>
1654312619SAndrew Lindesay#include <NetworkRoster.h>
1719c15fecSAndrew Lindesay#include <Roster.h>
1819c15fecSAndrew Lindesay#include <Url.h>
1919c15fecSAndrew Lindesay
2019c15fecSAndrew Lindesay
2119c15fecSAndrew Lindesay#define BASEURL_DEFAULT "https://depot.haiku-os.org"
2219c15fecSAndrew Lindesay#define USERAGENT_FALLBACK_VERSION "0.0.0"
2319c15fecSAndrew Lindesay
2419c15fecSAndrew Lindesay
250c1bbfe5SAndrew LindesayBUrl ServerSettings::sBaseUrl = BUrl(BASEURL_DEFAULT);
260c1bbfe5SAndrew LindesayBString ServerSettings::sUserAgent = BString();
270c1bbfe5SAndrew Lindesaypthread_once_t ServerSettings::sUserAgentInitOnce = PTHREAD_ONCE_INIT;
283094fef3SAndrew Lindesaybool ServerSettings::sPreferCache = false;
293094fef3SAndrew Lindesaybool ServerSettings::sDropCache = false;
303094fef3SAndrew Lindesaybool ServerSettings::sForceNoNetwork = false;
3154312619SAndrew Lindesaybool ServerSettings::sClientTooOld = false;
3254312619SAndrew LindesayBLocker ServerSettings::sLock;
3319c15fecSAndrew Lindesay
3419c15fecSAndrew Lindesay
3519c15fecSAndrew Lindesaystatus_t
360c1bbfe5SAndrew LindesayServerSettings::SetBaseUrl(const BUrl& value)
3719c15fecSAndrew Lindesay{
380c1bbfe5SAndrew Lindesay	if (!value.IsValid()) {
3919c15fecSAndrew Lindesay		fprintf(stderr, "the url is not valid\n");
4019c15fecSAndrew Lindesay		return B_BAD_VALUE;
4119c15fecSAndrew Lindesay	}
4219c15fecSAndrew Lindesay
430c1bbfe5SAndrew Lindesay	if (value.Protocol() != "http" && value.Protocol() != "https") {
4419c15fecSAndrew Lindesay		fprintf(stderr, "the url protocol must be 'http' or 'https'\n");
4519c15fecSAndrew Lindesay		return B_BAD_VALUE;
4619c15fecSAndrew Lindesay	}
4719c15fecSAndrew Lindesay
480c1bbfe5SAndrew Lindesay	sBaseUrl = value;
4919c15fecSAndrew Lindesay
5019c15fecSAndrew Lindesay	return B_OK;
5119c15fecSAndrew Lindesay}
5219c15fecSAndrew Lindesay
5319c15fecSAndrew Lindesay
540c1bbfe5SAndrew LindesayBUrl
5519c15fecSAndrew LindesayServerSettings::CreateFullUrl(const BString urlPathComponents)
5619c15fecSAndrew Lindesay{
570c1bbfe5SAndrew Lindesay	return BUrl(sBaseUrl, urlPathComponents);
5819c15fecSAndrew Lindesay}
5919c15fecSAndrew Lindesay
6019c15fecSAndrew Lindesay
6119c15fecSAndrew Lindesayconst BString
6219c15fecSAndrew LindesayServerSettings::GetUserAgent()
6319c15fecSAndrew Lindesay{
640c1bbfe5SAndrew Lindesay	if (sUserAgent.IsEmpty())
650c1bbfe5SAndrew Lindesay		pthread_once(&sUserAgentInitOnce, &ServerSettings::_InitUserAgent);
6619c15fecSAndrew Lindesay
670c1bbfe5SAndrew Lindesay	return sUserAgent;
680c1bbfe5SAndrew Lindesay}
6919c15fecSAndrew Lindesay
700c1bbfe5SAndrew Lindesay
71d9e65badSJessica Hamiltonvoid
720c1bbfe5SAndrew LindesayServerSettings::_InitUserAgent()
730c1bbfe5SAndrew Lindesay{
740c1bbfe5SAndrew Lindesay	sUserAgent.SetTo("HaikuDepot/");
750c1bbfe5SAndrew Lindesay	sUserAgent.Append(_GetUserAgentVersionString());
7619c15fecSAndrew Lindesay}
7719c15fecSAndrew Lindesay
7819c15fecSAndrew Lindesay
7919c15fecSAndrew Lindesayconst BString
8019c15fecSAndrew LindesayServerSettings::_GetUserAgentVersionString()
8119c15fecSAndrew Lindesay{
8219c15fecSAndrew Lindesay	app_info info;
8319c15fecSAndrew Lindesay
8419c15fecSAndrew Lindesay	if (be_app->GetAppInfo(&info) != B_OK) {
8519c15fecSAndrew Lindesay		fprintf(stderr, "Unable to get the application info\n");
8619c15fecSAndrew Lindesay		be_app->Quit();
8719c15fecSAndrew Lindesay		return BString(USERAGENT_FALLBACK_VERSION);
8819c15fecSAndrew Lindesay	}
8919c15fecSAndrew Lindesay
9019c15fecSAndrew Lindesay	BFile file(&info.ref, B_READ_ONLY);
9119c15fecSAndrew Lindesay
9219c15fecSAndrew Lindesay	if (file.InitCheck() != B_OK) {
9319c15fecSAndrew Lindesay		fprintf(stderr, "Unable to access the application info file\n");
9419c15fecSAndrew Lindesay		be_app->Quit();
9519c15fecSAndrew Lindesay		return BString(USERAGENT_FALLBACK_VERSION);
9619c15fecSAndrew Lindesay	}
9719c15fecSAndrew Lindesay
9819c15fecSAndrew Lindesay	BAppFileInfo appFileInfo(&file);
9919c15fecSAndrew Lindesay	version_info versionInfo;
10019c15fecSAndrew Lindesay
10119c15fecSAndrew Lindesay	if (appFileInfo.GetVersionInfo(
10219c15fecSAndrew Lindesay		&versionInfo, B_APP_VERSION_KIND) != B_OK) {
10319c15fecSAndrew Lindesay		fprintf(stderr, "Unable to establish the application version\n");
10419c15fecSAndrew Lindesay		be_app->Quit();
10519c15fecSAndrew Lindesay		return BString(USERAGENT_FALLBACK_VERSION);
10619c15fecSAndrew Lindesay	}
10719c15fecSAndrew Lindesay
10819c15fecSAndrew Lindesay	BString result;
10919c15fecSAndrew Lindesay	result.SetToFormat("%" B_PRId32 ".%" B_PRId32 ".%" B_PRId32,
11019c15fecSAndrew Lindesay		versionInfo.major, versionInfo.middle, versionInfo.minor);
11119c15fecSAndrew Lindesay	return result;
11219c15fecSAndrew Lindesay}
11319c15fecSAndrew Lindesay
1143094fef3SAndrew Lindesay
11519c15fecSAndrew Lindesayvoid
11619c15fecSAndrew LindesayServerSettings::AugmentHeaders(BHttpHeaders& headers)
11719c15fecSAndrew Lindesay{
11819c15fecSAndrew Lindesay	headers.AddHeader("User-Agent", GetUserAgent());
11919c15fecSAndrew Lindesay}
12019c15fecSAndrew Lindesay
12119c15fecSAndrew Lindesay
1223094fef3SAndrew Lindesaybool
1233094fef3SAndrew LindesayServerSettings::PreferCache()
1243094fef3SAndrew Lindesay{
1253094fef3SAndrew Lindesay	return sPreferCache;
1263094fef3SAndrew Lindesay}
1273094fef3SAndrew Lindesay
1283094fef3SAndrew Lindesay
1293094fef3SAndrew Lindesayvoid
1303094fef3SAndrew LindesayServerSettings::SetPreferCache(bool value)
1313094fef3SAndrew Lindesay{
1323094fef3SAndrew Lindesay	sPreferCache = value;
1333094fef3SAndrew Lindesay}
1343094fef3SAndrew Lindesay
1353094fef3SAndrew Lindesay
1363094fef3SAndrew Lindesaybool
1373094fef3SAndrew LindesayServerSettings::DropCache()
1383094fef3SAndrew Lindesay{
1393094fef3SAndrew Lindesay	return sDropCache;
1403094fef3SAndrew Lindesay}
1413094fef3SAndrew Lindesay
1423094fef3SAndrew Lindesay
1433094fef3SAndrew Lindesayvoid
1443094fef3SAndrew LindesayServerSettings::SetDropCache(bool value)
1453094fef3SAndrew Lindesay{
1463094fef3SAndrew Lindesay	sDropCache = value;
1473094fef3SAndrew Lindesay}
1483094fef3SAndrew Lindesay
1493094fef3SAndrew Lindesay
1503094fef3SAndrew Lindesaybool
1513094fef3SAndrew LindesayServerSettings::ForceNoNetwork()
1523094fef3SAndrew Lindesay{
1533094fef3SAndrew Lindesay	return sForceNoNetwork;
1543094fef3SAndrew Lindesay}
1553094fef3SAndrew Lindesay
1563094fef3SAndrew Lindesay
1573094fef3SAndrew Lindesayvoid
1583094fef3SAndrew LindesayServerSettings::SetForceNoNetwork(bool value)
1593094fef3SAndrew Lindesay{
1603094fef3SAndrew Lindesay	sForceNoNetwork = value;
16154312619SAndrew Lindesay}
16254312619SAndrew Lindesay
16354312619SAndrew Lindesay
16454312619SAndrew Lindesaybool
16554312619SAndrew LindesayServerSettings::IsClientTooOld()
16654312619SAndrew Lindesay{
16754312619SAndrew Lindesay	BAutolock locker(&sLock);
16854312619SAndrew Lindesay	return sClientTooOld;
16954312619SAndrew Lindesay}
17054312619SAndrew Lindesay
17154312619SAndrew Lindesay
17254312619SAndrew Lindesayvoid
17354312619SAndrew LindesayServerSettings::SetClientTooOld()
17454312619SAndrew Lindesay{
17554312619SAndrew Lindesay	BAutolock locker(&sLock);
17654312619SAndrew Lindesay	sClientTooOld = true;
177f1fafe31SAugustin Cavalier}
178