[ADD] Start using JSON

This commit is contained in:
lda 2024-02-04 21:25:57 +01:00
parent 906f155395
commit c6bf347b7f
2 changed files with 34 additions and 5 deletions

View file

@ -189,7 +189,6 @@ void http_transfer_send(http_transfer_t *t)
size_t line_i;
size_t offset = 0;
char *key, *value;
t->rep_header = utils_new_hashmap();
for (line_i = 0; true; line_i++)
{
@ -265,6 +264,8 @@ void http_transfer_send(http_transfer_t *t)
}
/* TODO */
t->reply = buffer + offset;
t->rep_length = size - offset;
}
}
@ -279,9 +280,13 @@ char * http_get_reply_header(http_transfer_t *trans, char *key)
return utils_hashmap_get(trans->rep_header, key);
}
void * http_get_reply_data(http_transfer_t *, size_t *)
void * http_get_reply_data(http_transfer_t *trans, size_t *size)
{
return NULL;
if (!trans || !trans->completed) return NULL;
*size = trans->rep_length;
return trans->reply;
}
void http_transfer_free(http_transfer_t *)

View file

@ -12,6 +12,7 @@
#include <unistd.h>
#include <string.h>
#include <utils.h>
#include <http.h>
void ui_chat_manager(ui_t *ui, ui_state_info_t *state, jevent e)
@ -22,13 +23,36 @@ void ui_chat_manager(ui_t *ui, ui_state_info_t *state, jevent e)
}
if (e.type == JINPUT_VALIDATED)
{
/* Just tests whenever we can do delegation */
const char *string = jinput_value(state->focus);
char *key, *value;
void *data;
size_t size;
http_transfer_t *trans;
hashmap_t *json;
hashmap_t *homeserver;
trans = http_transfer_create(HTTP_GET, "freetards.xyz", (char*)string);
http_transfer_add_header(trans, "User-Agent", "Mastrix/1.0 (fx-CG)");
trans = http_transfer_create(
HTTP_GET, "matrix.org", "/.well-known/matrix/client"
);
http_transfer_add_header(trans, "User-Agent", "Mastrix/1.0 (fx-CG50)");
http_transfer_send(trans);
data = http_get_reply_data(trans, &size);
json = utils_json_parse(data, size);
homeserver = utils_json_as_object(
utils_hashmap_get(json, "m.homeserver")
);
dclear(C_GREEN);
dprint(1, 1, C_WHITE, "%s",
utils_json_as_string(
utils_hashmap_get(homeserver, "base_url")
)
);
dupdate();
getkey();
http_transfer_free(trans);
}
}