diff options
author | Marvin Borner | 2020-11-24 22:28:40 +0100 |
---|---|---|
committer | Marvin Borner | 2020-11-24 22:28:40 +0100 |
commit | 0bc77c8c0dbc1737e2909ff5a2d5e5af48b298f7 (patch) | |
tree | 106d144f4ac653f111d3942b4c30e9d67861982e /apps | |
parent | 9257facb17bf74eb0eb1babfc59ff3549eb66242 (diff) |
Started HTML rendering
Diffstat (limited to 'apps')
-rw-r--r-- | apps/browser.c | 87 |
1 files changed, 5 insertions, 82 deletions
diff --git a/apps/browser.c b/apps/browser.c index 83292e9..160b626 100644 --- a/apps/browser.c +++ b/apps/browser.c @@ -53,80 +53,6 @@ void print_indent(char *buf, u32 n) strcat(buf, "\t"); } -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); - out[0] = '\0'; - enum xml_error err = xml_parse(&parser, buffer, len, tokens, 128); - - if (err != XML_SUCCESS) { - printf("\nXML parse error: %d\n", err); - return; - } - - 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: - memcpy(&name, (u8 *)buffer + token->start_pos, - token->end_pos - token->start_pos); - name[token->end_pos - token->start_pos] = '\0'; - if (html_self_closing(name)) - print_indent(out, indent); - else - print_indent(out, indent++); - strcat(out, name); - strcat(out, "\n"); - break; - case XML_END_TAG: - print_indent(out, --indent); - memcpy(&name, (u8 *)buffer + token->start_pos, - token->end_pos - token->start_pos); - name[token->end_pos - token->start_pos] = '\0'; - strcat(out, name); - strcat(out, "/\n"); - break; - case XML_CHARACTER: - if (token->end_pos == token->start_pos + 2) { - const char *ptr = (char *)buffer + token->start_pos; - - if (ptr[0] == '\r' && ptr[1] == '\n') - continue; - } - memcpy(&name, (u8 *)buffer + token->start_pos, - token->end_pos - token->start_pos); - name[token->end_pos - token->start_pos] = '\0'; - char *clean_name = name; - for (u32 j = 0; j < strlen(name); j++) { - if (name[j] == ' ' || name[j] == '\n' || name[j] == '\r' || - name[j] == '\t') { - clean_name++; - } else { - break; - } - } - if (!strlen(clean_name)) - break; - print_indent(out, indent++); - strcat(out, clean_name); - strcat(out, "\n"); - indent--; - break; - default: - break; - } - - i += token->size; - } -} - void on_submit(void *event, struct element *box) { (void)event; @@ -152,22 +78,20 @@ void on_submit(void *event, struct element *box) ip = dns_request(url); } - 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, ip, port, NET_TIMEOUT)) { net_send(socket, query, strlen(query)); char buf[4096] = { 0 }; - char parsed[4096] = { 0 }; - if (!net_receive(socket, buf, 4096, NET_TIMEOUT)) + if (!net_receive(socket, buf, 4096, NET_TIMEOUT) || + !html_render(output, http_data(buf), 4096)) return; - parse(http_data(buf), 4096, parsed); - l->text = parsed[0] ? parsed : http_data(buf); + c->text = http_code(buf); c->color_fg = status_color(c->text); } else { - l->text = strdup("Can't connect to server."); + /* l->text = strdup("Can't connect to server."); */ c->text = strdup("000"); c->color_fg = COLOR_RED; } @@ -183,8 +107,7 @@ 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); - output = gui_add_text_box(root, 0, FONT_HEIGHT + 2, 100, 100, FONT_16, - "Enter URL and press Enter :)", COLOR_WHITE, COLOR_BLACK); + output = gui_add_container(root, 0, FONT_HEIGHT + 2, 100, 100, COLOR_GREEN); text_input->event.on_submit = on_submit; |