From 4846299ab97e83a9b16be2b95bf64a84640c1445 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20A=C3=9Fmus?= Date: Wed, 26 Mar 2014 22:53:44 +0100 Subject: [PATCH] HaikuDepot: Test some web app features * Invoke "getPkg" JSON API, doesn't work yet. * Download a package icon in HVIF, works nicely. --- src/apps/haikudepot/ConnectionTest.cpp | 119 +++++++++++++++++++++++-- src/apps/haikudepot/ConnectionTest.h | 11 ++- 2 files changed, 122 insertions(+), 8 deletions(-) diff --git a/src/apps/haikudepot/ConnectionTest.cpp b/src/apps/haikudepot/ConnectionTest.cpp index 9c95fa8f79..717d3267b6 100644 --- a/src/apps/haikudepot/ConnectionTest.cpp +++ b/src/apps/haikudepot/ConnectionTest.cpp @@ -7,6 +7,10 @@ #include +#include + +#include +#include #include #include #include @@ -15,6 +19,13 @@ class ProtocolListener : public BUrlProtocolListener { +public: + ProtocolListener::ProtocolListener() + : + fDownloadIO(NULL) + { + } + virtual void ConnectionOpened(BUrlRequest* caller) { printf("ConnectionOpened(%p)\n", caller); @@ -39,6 +50,9 @@ class ProtocolListener : public BUrlProtocolListener { ssize_t size) { printf("DataReceived(%p): %ld bytes\n", caller, size); + + if (fDownloadIO != NULL) + fDownloadIO->Write(data, size); } virtual void DownloadProgress(BUrlRequest* caller, ssize_t bytesReceived, @@ -64,12 +78,22 @@ class ProtocolListener : public BUrlProtocolListener { { printf("DebugMessage(%p): %s\n", caller, text); } + + void SetDownloadIO(BDataIO* downloadIO) + { + fDownloadIO = downloadIO; + } + +private: + BDataIO* fDownloadIO; }; -ConnectionTest::ConnectionTest() +ConnectionTest::ConnectionTest(const char* username, const char* password) : - BApplication("application/x-vnd.Haiku-HaikuDepot-ConnectionTest") + BApplication("application/x-vnd.Haiku-HaikuDepot-ConnectionTest"), + fUsername(username), + fPassword(password) { } @@ -84,25 +108,106 @@ ConnectionTest::ReadyToRun() { printf("Connecting...\n"); - BUrl url("https://depot.haiku-os.org"); + _TestDownloadIcon(); + + PostMessage(B_QUIT_REQUESTED, this); +} + + +void +ConnectionTest::_TestGetPackage() +{ + BUrl url("https://depot.haiku-os.org/api/v1/pkg"); ProtocolListener listener; BUrlContext context; - - BHttpRequest request(url, true, "HTTP", &listener, &context); + BHttpHeaders headers; + // Authentication + if (!fUsername.IsEmpty() && !fPassword.IsEmpty()) { + BString plain; + plain << fUsername << ':' << fPassword; + + char base64[1024]; + ssize_t base64Size = encode_base64(base64, plain, plain.Length(), + false); + + if (base64Size > 0 && base64Size <= (ssize_t)sizeof(base64)) { + base64[base64Size] = '\0'; + + BString authorization("Basic "); + authorization << base64; + + headers.AddHeader("Authorization", authorization); + } + } + // Content-Type + headers.AddHeader("Content-Type", "application/json"); + + BHttpRequest request(url, true, "HTTP", &listener, &context); + request.SetMethod(B_HTTP_POST); + request.SetHeaders(headers); + + BMallocIO* data = new BMallocIO(); + + BString jsonString + = "{" + "\"jsonrpc\":\"2.0\"," + "\"id\":4143431," + "\"method\":\"getPkg\"," + "\"params\":[{" + "\"name\":\"apr\"," + "\"architectureCode\":\"x86\"," + "\"versionType\":\"NONE\"" + "}]" + "}" + ; + + data->WriteAt(0, jsonString.String(), jsonString.Length()); + data->Seek(0, SEEK_SET); + request.AdoptInputData(data); thread_id thread = request.Run(); wait_for_thread(thread, NULL); +} - PostMessage(B_QUIT_REQUESTED, this); + +void +ConnectionTest::_TestDownloadIcon() +{ + BUrl url("https://depot.haiku-os.org/pkgicon/wonderbrush_x86_gcc2.hvif"); + + BFile file("/boot/home/Desktop/wonderbrush.hvif", + B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE); + + ProtocolListener listener; + listener.SetDownloadIO(&file); + + // Content-Type + BHttpRequest request(url, true, "HTTP", &listener); + request.SetMethod(B_HTTP_GET); + + thread_id thread = request.Run(); + wait_for_thread(thread, NULL); } int main(int argc, char* argv[]) { - ConnectionTest().Run(); + const char* username = ""; + const char* password = ""; + + for (int i = 1; i < argc; i++) { + if (strcmp("-u", argv[i]) == 0 && i < argc - 1) + username = argv[++i]; + else if (strcmp("-p", argv[i]) == 0 && i < argc - 1) + password = argv[++i]; + } + + printf("Using username: %s, password: %s\n", username, password); + + ConnectionTest(username, password).Run(); return 0; } diff --git a/src/apps/haikudepot/ConnectionTest.h b/src/apps/haikudepot/ConnectionTest.h index e07f6d82c3..24c745407e 100644 --- a/src/apps/haikudepot/ConnectionTest.h +++ b/src/apps/haikudepot/ConnectionTest.h @@ -7,14 +7,23 @@ #include +#include class ConnectionTest : public BApplication { public: - ConnectionTest(); + ConnectionTest(const char* username, + const char* password); virtual ~ConnectionTest(); virtual void ReadyToRun(); + +private: + void _TestGetPackage(); + void _TestDownloadIcon(); +private: + BString fUsername; + BString fPassword; };