From a61f0364d8b4119819e7b13c8a10875baa66bf42 Mon Sep 17 00:00:00 2001
From: Andrew Lindesay <apl@lindesay.co.nz>
Date: Sat, 18 Jul 2015 22:24:49 +1200
Subject: [PATCH] 12216 haikudepot desktop app can now have server url
specified and force repository code to "haikuports"
---
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 22a0a43db..e8d5137 100644
a
|
b
|
|
21 | 21 | #include "PackageInfo.h" |
22 | 22 | |
23 | 23 | |
| 24 | #define CODE_REPOSITORY_DEFAULT "haikuports" |
| 25 | #define BASEURL_DEFAULT "https://depot.haiku-os.org" |
| 26 | |
| 27 | |
24 | 28 | class JsonBuilder { |
25 | 29 | public: |
26 | 30 | JsonBuilder() |
… |
… |
enum {
|
269 | 273 | }; |
270 | 274 | |
271 | 275 | |
| 276 | BString WebAppInterface::fBaseUrl = BString(BASEURL_DEFAULT); |
| 277 | |
| 278 | |
272 | 279 | WebAppInterface::WebAppInterface() |
273 | 280 | : |
274 | 281 | fLanguage("en") |
… |
… |
WebAppInterface::SetAuthorization(const BString& username,
|
313 | 320 | } |
314 | 321 | |
315 | 322 | |
| 323 | static bool |
| 324 | arguments_is_url_valid(const BString& value) |
| 325 | { |
| 326 | if (value.Length() < 8) { |
| 327 | fprintf(stderr,"the url is less than 8 characters in length\n"); |
| 328 | return false; |
| 329 | } |
| 330 | |
| 331 | int32 schemeEnd = value.FindFirst("://"); |
| 332 | |
| 333 | if (schemeEnd == B_ERROR) { |
| 334 | fprintf(stderr,"the url does not contain the '://' string\n"); |
| 335 | return false; |
| 336 | } |
| 337 | |
| 338 | BString scheme; |
| 339 | value.CopyInto(scheme, 0, schemeEnd); |
| 340 | |
| 341 | if (scheme != "http" && scheme != "https") { |
| 342 | fprintf(stderr,"the url scheme should be 'http' or 'https'\n"); |
| 343 | return false; |
| 344 | } |
| 345 | |
| 346 | if (value.Length()-1 == value.FindLast("/")) { |
| 347 | fprintf(stderr,"the url should be be terminated with a '/'\n"); |
| 348 | return false; |
| 349 | } |
| 350 | |
| 351 | return true; |
| 352 | } |
| 353 | |
| 354 | |
| 355 | /*! This method will set the web app base URL, returning a status to |
| 356 | indicate if the URL was acceptable. |
| 357 | \return B_OK if the base URL was valid and B_BAD_VALUE if not. |
| 358 | */ |
| 359 | |
| 360 | status_t |
| 361 | WebAppInterface::SetBaseUrl(const BString& url) { |
| 362 | if (!arguments_is_url_valid(url)) |
| 363 | return B_BAD_VALUE; |
| 364 | |
| 365 | fBaseUrl.SetTo(url); |
| 366 | |
| 367 | return B_OK; |
| 368 | } |
| 369 | |
| 370 | |
316 | 371 | void |
317 | 372 | WebAppInterface::SetPreferredLanguage(const BString& language) |
318 | 373 | { |
… |
… |
WebAppInterface::RetrievePackageInfo(const BString& packageName,
|
333 | 388 | .AddValue("name", packageName) |
334 | 389 | .AddValue("architectureCode", architecture) |
335 | 390 | .AddValue("naturalLanguageCode", fLanguage) |
| 391 | .AddValue("repositoryCode", CODE_REPOSITORY_DEFAULT) |
336 | 392 | .AddValue("versionType", "NONE") |
337 | 393 | .EndObject() |
338 | 394 | .EndArray() |
… |
… |
WebAppInterface::RetrieveBulkPackageInfo(const StringList& packageNames,
|
358 | 414 | .AddArray("architectureCodes") |
359 | 415 | .AddStrings(packageArchitectures) |
360 | 416 | .EndArray() |
| 417 | .AddArray("repositoryCodes") |
| 418 | .AddItem(CODE_REPOSITORY_DEFAULT) |
| 419 | .EndArray() |
361 | 420 | .AddValue("naturalLanguageCode", fLanguage) |
362 | 421 | .AddValue("versionType", "LATEST") |
363 | 422 | .AddArray("filter") |
… |
… |
status_t
|
378 | 437 | WebAppInterface::RetrievePackageIcon(const BString& packageName, |
379 | 438 | BDataIO* stream) |
380 | 439 | { |
381 | | BString urlString = "https://depot.haiku-os.org/pkgicon/"; |
382 | | urlString << packageName << ".hvif"; |
| 440 | BString urlString = _FormFullUrl(BString("/pkgicon/") << packageName |
| 441 | << ".hvif"); |
| 442 | bool isSecure = 0 == urlString.FindFirst("https://"); |
383 | 443 | |
384 | 444 | BUrl url(urlString); |
385 | 445 | |
386 | 446 | ProtocolListener listener; |
387 | 447 | listener.SetDownloadIO(stream); |
388 | 448 | |
389 | | BHttpRequest request(url, true, "HTTP", &listener); |
| 449 | BHttpRequest request(url, isSecure, "HTTP", &listener); |
390 | 450 | request.SetMethod(B_HTTP_GET); |
391 | 451 | |
392 | 452 | thread_id thread = request.Run(); |
… |
… |
WebAppInterface::RetrieveUserRating(const BString& packageName,
|
446 | 506 | .AddValue("pkgVersionMicro", version.Micro(), true) |
447 | 507 | .AddValue("pkgVersionPreRelease", version.PreRelease(), true) |
448 | 508 | .AddValue("pkgVersionRevision", (int)version.Revision()) |
| 509 | .AddValue("repositoryCode", CODE_REPOSITORY_DEFAULT) |
449 | 510 | .EndObject() |
450 | 511 | .EndArray() |
451 | 512 | .End(); |
… |
… |
WebAppInterface::CreateUserRating(const BString& packageName,
|
473 | 534 | .AddValue("rating", rating) |
474 | 535 | .AddValue("userRatingStabilityCode", stability, true) |
475 | 536 | .AddValue("comment", comment) |
| 537 | .AddValue("repositoryCode", CODE_REPOSITORY_DEFAULT) |
476 | 538 | .AddValue("naturalLanguageCode", languageCode) |
477 | 539 | .EndObject() |
478 | 540 | .EndArray() |
… |
… |
status_t
|
520 | 582 | WebAppInterface::RetrieveScreenshot(const BString& code, |
521 | 583 | int32 width, int32 height, BDataIO* stream) |
522 | 584 | { |
523 | | BString urlString = "https://depot.haiku-os.org/pkgscreenshot/"; |
524 | | urlString << code << ".png" |
525 | | << "?tw=" << width << "&th=" << height; |
| 585 | BString urlString = _FormFullUrl(BString("/pkgscreenshot/") << code |
| 586 | << ".png" << "?tw=" << width << "&th=" << height); |
| 587 | bool isSecure = 0 == urlString.FindFirst("https://"); |
526 | 588 | |
527 | 589 | BUrl url(urlString); |
528 | 590 | |
… |
… |
WebAppInterface::RetrieveScreenshot(const BString& code,
|
532 | 594 | BHttpHeaders headers; |
533 | 595 | headers.AddHeader("User-Agent", "X-HDS-Client"); |
534 | 596 | |
535 | | BHttpRequest request(url, true, "HTTP", &listener); |
| 597 | BHttpRequest request(url, isSecure, "HTTP", &listener); |
536 | 598 | request.SetMethod(B_HTTP_GET); |
537 | 599 | request.SetHeaders(headers); |
538 | 600 | |
… |
… |
WebAppInterface::AuthenticateUser(const BString& nickName,
|
625 | 687 | // #pragma mark - private |
626 | 688 | |
627 | 689 | |
| 690 | BString |
| 691 | WebAppInterface::_FormFullUrl(const BString& suffix) const |
| 692 | { |
| 693 | if (fBaseUrl.IsEmpty()) { |
| 694 | fprintf(stderr,"illegal state - missing web app base url\n"); |
| 695 | exit(EXIT_FAILURE); |
| 696 | } |
| 697 | |
| 698 | return BString(fBaseUrl) << suffix; |
| 699 | } |
| 700 | |
| 701 | |
628 | 702 | status_t |
629 | 703 | WebAppInterface::_SendJsonRequest(const char* domain, BString jsonString, |
630 | 704 | uint32 flags, BMessage& reply) const |
… |
… |
WebAppInterface::_SendJsonRequest(const char* domain, BString jsonString,
|
632 | 706 | if ((flags & ENABLE_DEBUG) != 0) |
633 | 707 | printf("_SendJsonRequest(%s)\n", jsonString.String()); |
634 | 708 | |
635 | | BString urlString("https://depot.haiku-os.org/api/v1/"); |
636 | | urlString << domain; |
| 709 | BString urlString = _FormFullUrl(BString("/api/v1/") << domain); |
| 710 | bool isSecure = 0 == urlString.FindFirst("https://"); |
637 | 711 | BUrl url(urlString); |
638 | 712 | |
639 | 713 | ProtocolListener listener; |
… |
… |
WebAppInterface::_SendJsonRequest(const char* domain, BString jsonString,
|
643 | 717 | headers.AddHeader("Content-Type", "application/json"); |
644 | 718 | headers.AddHeader("User-Agent", "X-HDS-Client"); |
645 | 719 | |
646 | | BHttpRequest request(url, true, "HTTP", &listener, &context); |
| 720 | BHttpRequest request(url, isSecure, "HTTP", &listener, &context); |
647 | 721 | request.SetMethod(B_HTTP_POST); |
648 | 722 | request.SetHeaders(headers); |
649 | 723 | |
diff --git a/src/apps/haikudepot/model/WebAppInterface.h b/src/apps/haikudepot/model/WebAppInterface.h
index 23018b9..8cf8779 100644
a
|
b
|
public:
|
33 | 33 | const BString& Username() const |
34 | 34 | { return fUsername; } |
35 | 35 | |
| 36 | static status_t SetBaseUrl(const BString& url); |
36 | 37 | void SetPreferredLanguage(const BString& language); |
37 | 38 | void SetArchitecture(const BString& architecture); |
38 | 39 | |
… |
… |
public:
|
100 | 101 | BMessage& message); |
101 | 102 | |
102 | 103 | private: |
| 104 | BString _FormFullUrl(const BString& suffix) const; |
103 | 105 | status_t _SendJsonRequest(const char* domain, |
104 | 106 | BString jsonString, uint32 flags, |
105 | 107 | BMessage& reply) const; |
106 | 108 | |
107 | 109 | private: |
| 110 | static BString fBaseUrl; |
108 | 111 | BString fUsername; |
109 | 112 | BString fPassword; |
110 | 113 | BString fLanguage; |
diff --git a/src/apps/haikudepot/ui/App.cpp b/src/apps/haikudepot/ui/App.cpp
index 65b145c..95a1a85 100644
a
|
b
|
App::RefsReceived(BMessage* message)
|
118 | 118 | void |
119 | 119 | App::ArgvReceived(int32 argc, char* argv[]) |
120 | 120 | { |
121 | | for (int i = 1; i < argc; i++) { |
122 | | BEntry entry(argv[i], true); |
123 | | _Open(entry); |
| 121 | for (int i = 1; i < argc;) { |
| 122 | if (0 == strcmp("--webappbaseurl", argv[i])) { |
| 123 | if (i == argc-1) { |
| 124 | fprintf(stderr,"unexpected end of arguments; missing web app base url\n"); |
| 125 | Quit(); |
| 126 | } |
| 127 | |
| 128 | if (B_OK != WebAppInterface::SetBaseUrl(argv[i+1])) { |
| 129 | fprintf(stderr,"malformed web app base url; %s\n", argv[i+1]); |
| 130 | Quit(); |
| 131 | } |
| 132 | else |
| 133 | fprintf(stderr,"did configure the web base url; %s\n",argv[i+1]); |
| 134 | |
| 135 | i += 2; |
| 136 | } else { |
| 137 | BEntry entry(argv[i], true); |
| 138 | _Open(entry); |
| 139 | i ++; |
| 140 | } |
124 | 141 | } |
125 | 142 | } |
126 | 143 | |