HaikuDepot: Test some web app features

* Invoke "getPkg" JSON API, doesn't work yet.
 * Download a package icon in HVIF, works nicely.
This commit is contained in:
Stephan Aßmus
2014-03-26 23:02:48 +01:00
parent 8d6d2daa3f
commit 4846299ab9
2 changed files with 122 additions and 8 deletions
+110 -5
View File
@@ -7,6 +7,10 @@
#include <stdio.h> #include <stdio.h>
#include <mail_encoding.h>
#include <File.h>
#include <HttpHeaders.h>
#include <HttpRequest.h> #include <HttpRequest.h>
#include <Url.h> #include <Url.h>
#include <UrlContext.h> #include <UrlContext.h>
@@ -15,6 +19,13 @@
class ProtocolListener : public BUrlProtocolListener { class ProtocolListener : public BUrlProtocolListener {
public:
ProtocolListener::ProtocolListener()
:
fDownloadIO(NULL)
{
}
virtual void ConnectionOpened(BUrlRequest* caller) virtual void ConnectionOpened(BUrlRequest* caller)
{ {
printf("ConnectionOpened(%p)\n", caller); printf("ConnectionOpened(%p)\n", caller);
@@ -39,6 +50,9 @@ class ProtocolListener : public BUrlProtocolListener {
ssize_t size) ssize_t size)
{ {
printf("DataReceived(%p): %ld bytes\n", caller, size); printf("DataReceived(%p): %ld bytes\n", caller, size);
if (fDownloadIO != NULL)
fDownloadIO->Write(data, size);
} }
virtual void DownloadProgress(BUrlRequest* caller, ssize_t bytesReceived, virtual void DownloadProgress(BUrlRequest* caller, ssize_t bytesReceived,
@@ -64,12 +78,22 @@ class ProtocolListener : public BUrlProtocolListener {
{ {
printf("DebugMessage(%p): %s\n", caller, text); 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"); 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; ProtocolListener listener;
BUrlContext context; BUrlContext 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); 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(); thread_id thread = request.Run();
wait_for_thread(thread, NULL); 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 int
main(int argc, char* argv[]) 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; return 0;
} }
+10 -1
View File
@@ -7,14 +7,23 @@
#include <Application.h> #include <Application.h>
#include <String.h>
class ConnectionTest : public BApplication { class ConnectionTest : public BApplication {
public: public:
ConnectionTest(); ConnectionTest(const char* username,
const char* password);
virtual ~ConnectionTest(); virtual ~ConnectionTest();
virtual void ReadyToRun(); virtual void ReadyToRun();
private:
void _TestGetPackage();
void _TestDownloadIcon();
private:
BString fUsername;
BString fPassword;
}; };