diff options
author | Marvin Borner | 2020-11-18 22:12:40 +0100 |
---|---|---|
committer | Marvin Borner | 2020-11-18 22:12:40 +0100 |
commit | be1fa7dfe5d98cf636b8f54ea80152f47006203b (patch) | |
tree | bed590b3e77de39bbb55f3d81d01b02661746129 /apps | |
parent | a5a04ef3de6ad3f81d37a04fede23eb3b4b348b1 (diff) |
HTTP and browser stuff
Diffstat (limited to 'apps')
-rw-r--r-- | apps/browser.c | 40 |
1 files changed, 22 insertions, 18 deletions
diff --git a/apps/browser.c b/apps/browser.c index 68a61c1..de8481c 100644 --- a/apps/browser.c +++ b/apps/browser.c @@ -11,18 +11,9 @@ #include <str.h> static struct element *root; +static struct element *code_label; static struct element *output; -char *http_get(char *url) -{ - char *query = malloc(27 + strlen(url)); // TODO: Dynamic http length etc - query[0] = '\0'; - strcat(query, "GET / HTTP/1.1\r\nHost: "); - strcat(query, url); - strcat(query, "\r\n\r\n"); - return query; -} - // Temporary: Will be moved to libnet char **dns_split(char *url, char **buf) { @@ -39,30 +30,43 @@ void on_submit(void *event, struct element *box) { (void)event; char *url = ((struct element_text_input *)box->data)->text; - char *query = http_get(url); + + char *path = strchr(url, '/'); + if (path) { + path[0] = '\0'; + path++; + } + char *query = http_query_get(url, path ? path : "/"); + char *dns[2]; dns_split(url, dns); struct element_text_box *l = output->data; + struct element_label *c = code_label->data; struct socket *socket = net_open(S_TCP); if (socket && net_connect(socket, dns_request(dns[0], dns[1]), 80)) { net_send(socket, query, strlen(query)); - u8 buf[4096] = { 0 }; + char buf[4096] = { 0 }; net_receive(socket, buf, 4096); - l->text = (char *)buf; - gui_sync(root, output); + l->text = http_data(buf); + c->text = http_code(buf); } else { - print("Couldn't connect!\n"); + l->text = strdup("Can't connect to server."); + c->text = strdup("000"); } + gui_sync(root, output); + gui_sync(root, code_label); /* net_close(socket); */ // TODO: Fix net close before FIN/ACK got ACK'ed } int main() { - root = gui_init("browser", 600, 400, COLOR_BG); + // TODO: Dynamic element positioning + root = gui_init("browser", 640, 400, COLOR_BG); + code_label = gui_add_label(root, 2, 2, FONT_24, "000", COLOR_WHITE, COLOR_BLACK); struct element *text_input = - gui_add_text_input(root, 10, 10, 580, FONT_24, COLOR_WHITE, COLOR_BLACK); - output = gui_add_text_box(root, 10, 50, 580, 340, FONT_24, "Enter URL and press Enter :)", + gui_add_text_input(root, 40, 2, 598, FONT_24, COLOR_WHITE, COLOR_BLACK); + output = gui_add_text_box(root, 2, 28, 636, 370, FONT_16, "Enter URL and press Enter :)", COLOR_WHITE, COLOR_BLACK); text_input->event.on_submit = on_submit; |