From 7525e8af775a1d039d08df6ba066fe83295c7193 Mon Sep 17 00:00:00 2001 From: 0xdeadbeer Date: Wed, 4 Sep 2024 12:16:40 +0200 Subject: [PATCH] tmp: start working on parsing response messages --- parslib.c | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++++- parslib.h | 17 +++++++-- 2 files changed, 122 insertions(+), 5 deletions(-) diff --git a/parslib.c b/parslib.c index ac138de..e7e64b3 100644 --- a/parslib.c +++ b/parslib.c @@ -10,6 +10,42 @@ extern char *headers; static struct nod *method_tree = NULL; static struct nod *header_tree = NULL; +static int stoin(char *str, int len, int *out) { + int ret = 0; + int place = 1; + for (char *chr = str+len; chr >= str; chr--) { + if (chr == str) { + if (*ch == '+') { + goto _proceed; + } + if (*ch == '-') { + goto _proceed; + } + } + if (*chr >= '0' && *ch <= '9') { + goto _proceed; + } + + return -1; + +_proceed: + + if (*chr == '-') { + ret = -ret; + continue; + } + if (*chr == '+') { + continue; + } + + int number = *chr-'0'; + ret += number * place; + place *= 10; + } + + *out = ret; +} + void printfpars(struct httpars *pars) { fprintf(stderr, "\tstats:\n" "\t\tmethod\t: %d\n" @@ -129,14 +165,84 @@ _loop: return 0; } -int parstitle(char *offset, int len, struct httitle *titl) { +int parestitl(char *offset, int len, struct httitleres *titl) { + int ret = 0; + int diff = 0; + char *cursor = offset; + char *cursor_lim = cursor+len; + + // ver + char *ver_lim = strchr(cursor, ' '); + if (!ver_lim) { + return -1; + } + if (ver_lim > cursor_lim) { + return -1; + } + + diff = ver_lim-cursor; + titl->ver.er = cursor; + titl->ver.len = diff; + + cursor += diff; + + // white space +_loop1: + if (cursor > cursor_lim) { + return -1; + } + if (*cursor == ' ') { + cursor++; + goto _loop1; + } + + // code + char *code_lim = strchr(cursor, ' '); + if (!code_lim) { + return -1; + } + if (code_lim > cursor_lim) { + return -1; + } + + diff = code_lim-cursor; + int code; + ret = stoin(cursor, diff, &code); + if (ret < 0) { + return -1; + } + + titl->code = code; + cursor += diff; + + // white space +_loop2: + if (cursor > cursor_lim) { + return -1; + } + if (*cursor == ' ') { + cursor++; + goto _loop2; + } + + // status text + diff = cursor_lim-cursor; + titl->stxt.er = cursor; + titl->stxt.len = diff; + + cursor += diff; + + return 0; +} + +int pareqtitl(char *offset, int len, struct httitlereq *titl) { int ret = 0; int diff = 0; char *cursor = offset; char *cursor_lim = cursor+len; // method - char *method_lim = strchr(offset, ' '); + char *method_lim = strchr(cursor, ' '); if (!method_lim) { return -1; } diff --git a/parslib.h b/parslib.h index c041030..c2ef075 100644 --- a/parslib.h +++ b/parslib.h @@ -143,17 +143,28 @@ struct point { int len; }; -struct httitle { +struct httitlereq { int method; struct point uri; struct point ver; }; -struct httpars { - struct httitle titl; +struct httitleres { + struct point ver; + int code; + struct point stxt; +}; + +struct httpareq { + struct httitlereq titl; struct point hentries[header_count]; }; +struct httpares { + struct httitleres titl; + struct point hentries[header_count]; +} + int initres(void); void fretres(void); int readlin(char **buff, char **buff_lim);