Skip to content

Commit

Permalink
HaikuDepot: desktop app can now have server url specified
Browse files Browse the repository at this point in the history
* Enables us to test to other HD servers

Completes Task #12216
  • Loading branch information
andponlin authored and kallisti5 committed Jul 19, 2015
1 parent 874bb4e commit 93407f0
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 13 deletions.
94 changes: 84 additions & 10 deletions src/apps/haikudepot/model/WebAppInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
#include "PackageInfo.h"


#define CODE_REPOSITORY_DEFAULT "haikuports"
#define BASEURL_DEFAULT "https://depot.haiku-os.org"


class JsonBuilder {
public:
JsonBuilder()
Expand Down Expand Up @@ -269,6 +273,9 @@ enum {
};


BString WebAppInterface::fBaseUrl = BString(BASEURL_DEFAULT);


WebAppInterface::WebAppInterface()
:
fLanguage("en")
Expand Down Expand Up @@ -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)
{
Expand All @@ -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()
Expand All @@ -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")
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand Down Expand Up @@ -625,15 +687,27 @@ 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
{
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;
Expand All @@ -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);

Expand Down
3 changes: 3 additions & 0 deletions src/apps/haikudepot/model/WebAppInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class WebAppInterface {
const BString& Username() const
{ return fUsername; }

static status_t SetBaseUrl(const BString& url);
void SetPreferredLanguage(const BString& language);
void SetArchitecture(const BString& architecture);

Expand Down Expand Up @@ -100,11 +101,13 @@ class WebAppInterface {
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;
Expand Down
23 changes: 20 additions & 3 deletions src/apps/haikudepot/ui/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ++;
}
}
}

Expand Down

0 comments on commit 93407f0

Please sign in to comment.