From b8d10c6dc62727003c7d9533879987d521138630 Mon Sep 17 00:00:00 2001 From: Andrew Lindesay Date: Thu, 30 Jul 2015 22:21:46 +1200 Subject: [PATCH] HaikuDepot: Enhancements to the User-Agent header to include the version. Signed-off-by: Augustin Cavalier Fixes #12262. --- src/apps/haikudepot/model/WebAppInterface.cpp | 73 +++++++++++++++++-- src/apps/haikudepot/model/WebAppInterface.h | 4 + 2 files changed, 70 insertions(+), 7 deletions(-) diff --git a/src/apps/haikudepot/model/WebAppInterface.cpp b/src/apps/haikudepot/model/WebAppInterface.cpp index 78ed19e306..921e7dd767 100644 --- a/src/apps/haikudepot/model/WebAppInterface.cpp +++ b/src/apps/haikudepot/model/WebAppInterface.cpp @@ -7,22 +7,28 @@ #include +#include +#include +#include #include #include #include #include #include +#include #include #include #include #include +#include "AutoLocker.h" #include "List.h" #include "PackageInfo.h" #define CODE_REPOSITORY_DEFAULT "haikuports" #define BASEURL_DEFAULT "https://depot.haiku-os.org" +#define USERAGENT_FALLBACK_VERSION "0.0.0" class JsonBuilder { @@ -274,7 +280,8 @@ enum { BString WebAppInterface::fBaseUrl = BString(BASEURL_DEFAULT); - +BString WebAppInterface::fUserAgent = BString(); +BLocker WebAppInterface::fUserAgentLocker = BLocker(); WebAppInterface::WebAppInterface() : @@ -337,7 +344,7 @@ arguments_is_url_valid(const BString& value) BString scheme; value.CopyInto(scheme, 0, schemeEnd); - + if (scheme != "http" && scheme != "https") { fprintf(stderr, "the url scheme should be 'http' or 'https'\n"); return false; @@ -356,7 +363,7 @@ arguments_is_url_valid(const BString& value) indicate if the URL was acceptable. \return B_OK if the base URL was valid and B_BAD_VALUE if not. */ -status_t +status_t WebAppInterface::SetBaseUrl(const BString& url) { if (!arguments_is_url_valid(url)) @@ -368,6 +375,60 @@ WebAppInterface::SetBaseUrl(const BString& url) } +const BString +WebAppInterface::_GetUserAgentVersionString() +{ + app_info info; + + if (be_app->GetAppInfo(&info) != B_OK) { + fprintf(stderr, "Unable to get the application info\n"); + be_app->Quit(); + return BString(USERAGENT_FALLBACK_VERSION); + } + + BFile file(&info.ref, B_READ_ONLY); + + if (file.InitCheck() != B_OK) { + fprintf(stderr, "Unable to access the application info file\n"); + be_app->Quit(); + return BString(USERAGENT_FALLBACK_VERSION); + } + + BAppFileInfo appFileInfo(&file); + version_info versionInfo; + + if (appFileInfo.GetVersionInfo( + &versionInfo, B_APP_VERSION_KIND) != B_OK) { + fprintf(stderr, "Unable to establish the application version\n"); + be_app->Quit(); + return BString(USERAGENT_FALLBACK_VERSION); + } + + BString result; + result.SetToFormat("%ld.%ld.%ld", versionInfo.major, versionInfo.middle, + versionInfo.minor); + return result; +} + + +/*! This method will devise a suitable User-Agent header value that + can be transmitted with HTTP requests to the server in order + to identify this client. + */ +const BString +WebAppInterface::_GetUserAgent() +{ + AutoLocker lock(&fUserAgentLocker); + + if (fUserAgent.IsEmpty()) { + fUserAgent.SetTo("HaikuDepot/"); + fUserAgent.Append(_GetUserAgentVersionString()); + } + + return fUserAgent; +} + + void WebAppInterface::SetPreferredLanguage(const BString& language) { @@ -592,7 +653,7 @@ WebAppInterface::RetrieveScreenshot(const BString& code, listener.SetDownloadIO(stream); BHttpHeaders headers; - headers.AddHeader("User-Agent", "X-HDS-Client"); + headers.AddHeader("User-Agent", _GetUserAgent()); BHttpRequest request(url, isSecure, "HTTP", &listener); request.SetMethod(B_HTTP_GET); @@ -715,7 +776,7 @@ WebAppInterface::_SendJsonRequest(const char* domain, BString jsonString, BHttpHeaders headers; headers.AddHeader("Content-Type", "application/json"); - headers.AddHeader("User-Agent", "X-HDS-Client"); + headers.AddHeader("User-Agent", _GetUserAgent()); BHttpRequest request(url, isSecure, "HTTP", &listener, &context); request.SetMethod(B_HTTP_POST); @@ -764,5 +825,3 @@ WebAppInterface::_SendJsonRequest(const char* domain, BString jsonString, } return status; } - - diff --git a/src/apps/haikudepot/model/WebAppInterface.h b/src/apps/haikudepot/model/WebAppInterface.h index 8cf8779830..f5fffecabf 100644 --- a/src/apps/haikudepot/model/WebAppInterface.h +++ b/src/apps/haikudepot/model/WebAppInterface.h @@ -101,6 +101,8 @@ public: BMessage& message); private: + static const BString _GetUserAgentVersionString(); + static const BString _GetUserAgent(); BString _FormFullUrl(const BString& suffix) const; status_t _SendJsonRequest(const char* domain, BString jsonString, uint32 flags, @@ -108,6 +110,8 @@ private: private: static BString fBaseUrl; + static BString fUserAgent; + static BLocker fUserAgentLocker; BString fUsername; BString fPassword; BString fLanguage;