aboutsummaryrefslogtreecommitdiff
path: root/apps/browser.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/browser.c')
-rw-r--r--apps/browser.c65
1 files changed, 63 insertions, 2 deletions
diff --git a/apps/browser.c b/apps/browser.c
index bdc7b14..179acb1 100644
--- a/apps/browser.c
+++ b/apps/browser.c
@@ -9,6 +9,7 @@
#include <net.h>
#include <print.h>
#include <str.h>
+#include <xml.h>
#define WIDTH 640
#define HEIGHT 400
@@ -45,6 +46,62 @@ u32 status_color(char *http_code)
return c;
}
+void parse(void *data, u32 len, char **out)
+{
+ struct xml_token tokens[128];
+
+ struct xml parser;
+ xml_init(&parser);
+ void *buffer = data;
+ len = strlen(data);
+ enum xml_error err = xml_parse(&parser, buffer, len, tokens, 128);
+ printf("%s\n", data);
+ if (err != XML_SUCCESS) {
+ printf("ERROR %d\n", err);
+ return;
+ }
+
+ *out[0] = '\0';
+
+ u32 pos = 0;
+ u32 indent = 0;
+ char name[16] = { 0 };
+ for (u32 i = 0; i < parser.ntokens; i++) {
+ const struct xml_token *token = tokens + i;
+ name[0] = '\0';
+ switch (token->type) {
+ case XML_START_TAG:
+ for (u32 j = 0; j < indent; j++)
+ strcat(*out, "\t");
+ indent++;
+ memcpy(&name, (u8 *)buffer + token->start_pos,
+ token->end_pos - token->start_pos);
+ name[token->end_pos - token->start_pos] = '\0';
+ printf("%s\n", name);
+ print("START TAG\n");
+ strcat(*out, name);
+ strcat(*out, "\n");
+ break;
+ case XML_END_TAG:
+ indent--;
+ for (u32 j = 0; j < indent; j++)
+ strcat(*out, "\t");
+ memcpy(&name, (u8 *)buffer + token->start_pos,
+ token->end_pos - token->start_pos);
+ name[token->end_pos - token->start_pos] = '\0';
+ print("END TAG\n");
+ strcat(*out, name);
+ strcat(*out, "/\n");
+ break;
+ default:
+ break;
+ }
+
+ i += token->size;
+ }
+ printf("%s\n", *out);
+}
+
void on_submit(void *event, struct element *box)
{
(void)event;
@@ -66,11 +123,13 @@ void on_submit(void *event, struct element *box)
struct element_label *c = code_label->data;
struct socket *socket = net_open(S_TCP);
- if (socket && net_connect(socket, ip, 80)) {
+ if (socket && net_connect(socket, ip, 8000)) {
net_send(socket, query, strlen(query));
char buf[4096] = { 0 };
+ char parsed[4096] = { 0 };
net_receive(socket, buf, 4096);
- l->text = http_data(buf);
+ parse(http_data(buf), 4096, (char **)&parsed);
+ l->text = http_data(parsed);
c->text = http_code(buf);
c->color_fg = status_color(c->text);
} else {
@@ -90,8 +149,10 @@ int main()
code_label = gui_add_label(root, 0, 0, FONT_24, "000", COLOR_BLACK, COLOR_WHITE);
struct element *text_input =
gui_add_text_input(root, LABEL_WIDTH, 0, 100, FONT_24, COLOR_WHITE, COLOR_BLACK);
+ memcpy(((struct element_text_input *)text_input->data)->text, strdup("127.0.0.1"), 10);
output = gui_add_text_box(root, 0, FONT_HEIGHT + 2, 100, 100, FONT_16,
"Enter URL and press Enter :)", COLOR_WHITE, COLOR_BLACK);
+ gui_sync(root, text_input);
text_input->event.on_submit = on_submit;