From 93407f0c870981d98a4dafbc16ef5e25b01c52a7 Mon Sep 17 00:00:00 2001 From: Andrew Lindesay Date: Sat, 18 Jul 2015 22:24:49 +1200 Subject: [PATCH] HaikuDepot: desktop app can now have server url specified * Enables us to test to other HD servers Completes Task #12216 --- src/apps/haikudepot/model/WebAppInterface.cpp | 94 +++++++++++++++++-- src/apps/haikudepot/model/WebAppInterface.h | 3 + src/apps/haikudepot/ui/App.cpp | 23 ++++- 3 files changed, 107 insertions(+), 13 deletions(-) diff --git a/src/apps/haikudepot/model/WebAppInterface.cpp b/src/apps/haikudepot/model/WebAppInterface.cpp index 22a0a43db3..e8d51373a5 100644 --- a/src/apps/haikudepot/model/WebAppInterface.cpp +++ b/src/apps/haikudepot/model/WebAppInterface.cpp @@ -21,6 +21,10 @@ #include "PackageInfo.h" +#define CODE_REPOSITORY_DEFAULT "haikuports" +#define BASEURL_DEFAULT "https://depot.haiku-os.org" + + class JsonBuilder { public: JsonBuilder() @@ -269,6 +273,9 @@ enum { }; +BString WebAppInterface::fBaseUrl = BString(BASEURL_DEFAULT); + + WebAppInterface::WebAppInterface() : fLanguage("en") @@ -313,6 +320,54 @@ WebAppInterface::SetAuthorization(const BString& username, } +static bool +arguments_is_url_valid(const BString& value) +{ + if (value.Length() < 8) { + fprintf(stderr,"the url is less than 8 characters in length\n"); + return false; + } + + int32 schemeEnd = value.FindFirst("://"); + + if (schemeEnd == B_ERROR) { + fprintf(stderr,"the url does not contain the '://' string\n"); + return false; + } + + 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; + } + + if (value.Length()-1 == value.FindLast("/")) { + fprintf(stderr,"the url should be be terminated with a '/'\n"); + return false; + } + + return true; +} + + +/*! This method will set the web app base URL, returning a status to + indicate if the URL was acceptable. + \return B_OK if the base URL was valid and B_BAD_VALUE if not. + */ + +status_t +WebAppInterface::SetBaseUrl(const BString& url) { + if (!arguments_is_url_valid(url)) + return B_BAD_VALUE; + + fBaseUrl.SetTo(url); + + return B_OK; +} + + void WebAppInterface::SetPreferredLanguage(const BString& language) { @@ -333,6 +388,7 @@ WebAppInterface::RetrievePackageInfo(const BString& packageName, .AddValue("name", packageName) .AddValue("architectureCode", architecture) .AddValue("naturalLanguageCode", fLanguage) + .AddValue("repositoryCode", CODE_REPOSITORY_DEFAULT) .AddValue("versionType", "NONE") .EndObject() .EndArray() @@ -358,6 +414,9 @@ WebAppInterface::RetrieveBulkPackageInfo(const StringList& packageNames, .AddArray("architectureCodes") .AddStrings(packageArchitectures) .EndArray() + .AddArray("repositoryCodes") + .AddItem(CODE_REPOSITORY_DEFAULT) + .EndArray() .AddValue("naturalLanguageCode", fLanguage) .AddValue("versionType", "LATEST") .AddArray("filter") @@ -378,15 +437,16 @@ status_t WebAppInterface::RetrievePackageIcon(const BString& packageName, BDataIO* stream) { - BString urlString = "https://depot.haiku-os.org/pkgicon/"; - urlString << packageName << ".hvif"; + BString urlString = _FormFullUrl(BString("/pkgicon/") << packageName + << ".hvif"); + bool isSecure = 0 == urlString.FindFirst("https://"); BUrl url(urlString); ProtocolListener listener; listener.SetDownloadIO(stream); - BHttpRequest request(url, true, "HTTP", &listener); + BHttpRequest request(url, isSecure, "HTTP", &listener); request.SetMethod(B_HTTP_GET); thread_id thread = request.Run(); @@ -446,6 +506,7 @@ WebAppInterface::RetrieveUserRating(const BString& packageName, .AddValue("pkgVersionMicro", version.Micro(), true) .AddValue("pkgVersionPreRelease", version.PreRelease(), true) .AddValue("pkgVersionRevision", (int)version.Revision()) + .AddValue("repositoryCode", CODE_REPOSITORY_DEFAULT) .EndObject() .EndArray() .End(); @@ -473,6 +534,7 @@ WebAppInterface::CreateUserRating(const BString& packageName, .AddValue("rating", rating) .AddValue("userRatingStabilityCode", stability, true) .AddValue("comment", comment) + .AddValue("repositoryCode", CODE_REPOSITORY_DEFAULT) .AddValue("naturalLanguageCode", languageCode) .EndObject() .EndArray() @@ -520,9 +582,9 @@ status_t WebAppInterface::RetrieveScreenshot(const BString& code, int32 width, int32 height, BDataIO* stream) { - BString urlString = "https://depot.haiku-os.org/pkgscreenshot/"; - urlString << code << ".png" - << "?tw=" << width << "&th=" << height; + BString urlString = _FormFullUrl(BString("/pkgscreenshot/") << code + << ".png" << "?tw=" << width << "&th=" << height); + bool isSecure = 0 == urlString.FindFirst("https://"); BUrl url(urlString); @@ -532,7 +594,7 @@ WebAppInterface::RetrieveScreenshot(const BString& code, BHttpHeaders headers; headers.AddHeader("User-Agent", "X-HDS-Client"); - BHttpRequest request(url, true, "HTTP", &listener); + BHttpRequest request(url, isSecure, "HTTP", &listener); request.SetMethod(B_HTTP_GET); request.SetHeaders(headers); @@ -625,6 +687,18 @@ WebAppInterface::AuthenticateUser(const BString& nickName, // #pragma mark - private +BString +WebAppInterface::_FormFullUrl(const BString& suffix) const +{ + if (fBaseUrl.IsEmpty()) { + fprintf(stderr,"illegal state - missing web app base url\n"); + exit(EXIT_FAILURE); + } + + return BString(fBaseUrl) << suffix; +} + + status_t WebAppInterface::_SendJsonRequest(const char* domain, BString jsonString, uint32 flags, BMessage& reply) const @@ -632,8 +706,8 @@ WebAppInterface::_SendJsonRequest(const char* domain, BString jsonString, if ((flags & ENABLE_DEBUG) != 0) printf("_SendJsonRequest(%s)\n", jsonString.String()); - BString urlString("https://depot.haiku-os.org/api/v1/"); - urlString << domain; + BString urlString = _FormFullUrl(BString("/api/v1/") << domain); + bool isSecure = 0 == urlString.FindFirst("https://"); BUrl url(urlString); ProtocolListener listener; @@ -643,7 +717,7 @@ WebAppInterface::_SendJsonRequest(const char* domain, BString jsonString, headers.AddHeader("Content-Type", "application/json"); headers.AddHeader("User-Agent", "X-HDS-Client"); - BHttpRequest request(url, true, "HTTP", &listener, &context); + BHttpRequest request(url, isSecure, "HTTP", &listener, &context); request.SetMethod(B_HTTP_POST); request.SetHeaders(headers); diff --git a/src/apps/haikudepot/model/WebAppInterface.h b/src/apps/haikudepot/model/WebAppInterface.h index 23018b904f..8cf8779830 100644 --- a/src/apps/haikudepot/model/WebAppInterface.h +++ b/src/apps/haikudepot/model/WebAppInterface.h @@ -33,6 +33,7 @@ public: const BString& Username() const { return fUsername; } + static status_t SetBaseUrl(const BString& url); void SetPreferredLanguage(const BString& language); void SetArchitecture(const BString& architecture); @@ -100,11 +101,13 @@ public: BMessage& message); private: + BString _FormFullUrl(const BString& suffix) const; status_t _SendJsonRequest(const char* domain, BString jsonString, uint32 flags, BMessage& reply) const; private: + static BString fBaseUrl; BString fUsername; BString fPassword; BString fLanguage; diff --git a/src/apps/haikudepot/ui/App.cpp b/src/apps/haikudepot/ui/App.cpp index 65b145c809..95a1a85f38 100644 --- a/src/apps/haikudepot/ui/App.cpp +++ b/src/apps/haikudepot/ui/App.cpp @@ -118,9 +118,26 @@ App::RefsReceived(BMessage* message) void App::ArgvReceived(int32 argc, char* argv[]) { - for (int i = 1; i < argc; i++) { - BEntry entry(argv[i], true); - _Open(entry); + for (int i = 1; i < argc;) { + if (0 == strcmp("--webappbaseurl", argv[i])) { + if (i == argc-1) { + fprintf(stderr,"unexpected end of arguments; missing web app base url\n"); + Quit(); + } + + if (B_OK != WebAppInterface::SetBaseUrl(argv[i+1])) { + fprintf(stderr,"malformed web app base url; %s\n", argv[i+1]); + Quit(); + } + else + fprintf(stderr,"did configure the web base url; %s\n",argv[i+1]); + + i += 2; + } else { + BEntry entry(argv[i], true); + _Open(entry); + i ++; + } } }