From 6f627a88aa793caaab22e9deddf538353fde64b6 Mon Sep 17 00:00:00 2001 From: nezbednik Date: Fri, 21 Jul 2023 09:47:59 +0200 Subject: [PATCH 1/2] Add jansson.h + for-loop hack for header skip on http req --- Makefile | 4 ++-- source/main.c | 28 +++++++++++++++++++++++++++- source/sandia | 2 +- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 47bc760..8a93863 100644 --- a/Makefile +++ b/Makefile @@ -33,13 +33,13 @@ LDFLAGS = -g $(MACHDEP) -Wl,-Map,$(notdir $@).map #--------------------------------------------------------------------------------- # any extra libraries we wish to link with the project #--------------------------------------------------------------------------------- -LIBS := -lwiiuse -lbte -logc -lm +LIBS := -lwiiuse -lbte -logc -lm -ljansson #--------------------------------------------------------------------------------- # list of directories containing libraries, this must be the top level containing # include and lib #--------------------------------------------------------------------------------- -LIBDIRS := +LIBDIRS := $(PORTLIBS) #--------------------------------------------------------------------------------- # no real need to edit anything past this point unless you need to add additional diff --git a/source/main.c b/source/main.c index 95a5b22..a30fc18 100644 --- a/source/main.c +++ b/source/main.c @@ -4,7 +4,10 @@ #include #include +#include + #include "sandia/sandia.h" +#include // Uncomment if this is a development build //#include "debug.h" @@ -82,7 +85,30 @@ int main(int argc, char **argv) { sandia req = sandia_create("libreshop.donut.eu.org", 80); sandia_response res = sandia_get_request(&req, "/"); - printf("\n%s\n", res.body); + bool lastWasNewline = false; + int resBodyLength = strlen(res.body); + for (int i = 0; i < resBodyLength; i++) { + if (res.body[i] == '\r') continue; + + if (res.body[i] == '\n') { + if (lastWasNewline) { + printf("Headers end at: %i\n", i); + + int rBL = resBodyLength - i; + char* body = malloc(rBL); + for (int j = 0; j < rBL; j++) { + body[j] = res.body[j + i + 1]; + } + + printf("\"%s\"\n", body); + + break; + } + else lastWasNewline = true; + } + else lastWasNewline = false; + } + printf("\n"); sandia_close(&req); } else { logprint(0, "Network configuration failed :(\n"); diff --git a/source/sandia b/source/sandia index 9b383cd..62ea4d0 160000 --- a/source/sandia +++ b/source/sandia @@ -1 +1 @@ -Subproject commit 9b383cdda825ab4ba2767102b06cbd5dc734dae9 +Subproject commit 62ea4d024fda8663463ddc20c13eec5e1c08b2de -- 2.30.2 From 27cdc9cc0b943a4a32c6b137fe1fe22cea76d527 Mon Sep 17 00:00:00 2001 From: nezbednik Date: Fri, 21 Jul 2023 15:39:29 +0200 Subject: [PATCH 2/2] JSON parsing example --- source/main.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/source/main.c b/source/main.c index a30fc18..ac52100 100644 --- a/source/main.c +++ b/source/main.c @@ -92,15 +92,24 @@ int main(int argc, char **argv) { if (res.body[i] == '\n') { if (lastWasNewline) { - printf("Headers end at: %i\n", i); - int rBL = resBodyLength - i; char* body = malloc(rBL); for (int j = 0; j < rBL; j++) { body[j] = res.body[j + i + 1]; } - printf("\"%s\"\n", body); + json_error_t error; + json_t *root = json_loads(body, 0, &error); + if (!root || !json_is_array(root)) { + printf("failed decoding\n"); + } + else { + printf("%s\n", body); + for (int j = 0; j < json_array_size(root); j++) { + json_t *data = json_array_get(root, j); + printf("%s\n", json_string_value(data)); + } + } break; } -- 2.30.2