From 678a44228fe0c5b24b2d1b96576da0e44211c364 Mon Sep 17 00:00:00 2001 From: Kevin Jerebica Date: Sun, 8 Sep 2024 21:41:17 +0200 Subject: [PATCH] feat: store parsed results fully in heap --- parslib.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++----- parslib.h | 2 ++ test.c | 4 ++++ 3 files changed, 65 insertions(+), 5 deletions(-) diff --git a/parslib.c b/parslib.c index a714406..dc3cb59 100644 --- a/parslib.c +++ b/parslib.c @@ -120,6 +120,36 @@ void fretres(void) { frenod(header_tree); } +void frepareq(struct httpareq *req) { + struct httitlereq *titl = &req->titl; + free(titl->uri.er); + free(titl->ver.er); + + for (int i = 0; i < header_count; i++) { + struct point *e = &req->hentries[i]; + if (!e->er) { + continue; + } + + free(e->er); + } +} + +void frepares(struct httpares *res) { + struct httitleres *titl = &res->titl; + free(titl->ver.er); + free(titl->stxt.er); + + for (int i = 0; i < header_count; i++) { + struct point *e = &res->hentries[i]; + if (!e->er) { + continue; + } + + free(e->er); + } +} + int readlin(char **buff, char **buff_lim) { int diff = 0; if ((*buff) >= (*buff_lim)) { @@ -180,8 +210,12 @@ _loop: // header value diff = cursor_lim-cursor; + char *e = strndup(cursor, diff); + if (!e) { + return -1; + } - hentries[key].er = cursor; + hentries[key].er = e; hentries[key].len = diff; return 0; @@ -231,7 +265,12 @@ _loop1: } diff = uri_lim-cursor; - titl->uri.er = cursor; + char *e = strndup(cursor, diff); + if (!e) { + return -1; + } + + titl->uri.er = e; titl->uri.len = diff; cursor += diff; @@ -248,7 +287,12 @@ _loop2: // ver diff = cursor_lim-cursor; - titl->ver.er = cursor; + e = strndup(cursor, diff); + if (!e) { + return -1; + } + + titl->ver.er = e; titl->ver.len = diff; cursor += diff; @@ -272,7 +316,12 @@ int parestitl(char *offset, int len, struct httitleres *titl) { } diff = ver_lim-cursor; - titl->ver.er = cursor; + char *e = strndup(cursor, diff); + if (!e) { + return -1; + } + + titl->ver.er = e; titl->ver.len = diff; cursor += diff; @@ -318,7 +367,12 @@ _loop2: // status text diff = cursor_lim-cursor; - titl->stxt.er = cursor; + e = strndup(cursor, diff); + if (!e) { + return -1; + } + + titl->stxt.er = e; titl->stxt.len = diff; cursor += diff; diff --git a/parslib.h b/parslib.h index 77bca95..e0556c1 100644 --- a/parslib.h +++ b/parslib.h @@ -167,6 +167,8 @@ struct httpares { int initres(void); void fretres(void); +void frepareq(struct httpareq *req); +void frepares(struct httpares *res); int readlin(char **buff, char **buff_lim); int parshfield(char *offset, int len, /* out */ struct point *hentries); diff --git a/test.c b/test.c index 5e5e3ff..c97e6b1 100644 --- a/test.c +++ b/test.c @@ -72,6 +72,8 @@ int main(void) { fprintf(stdout, "-------------------------------------\n"); printfpareq(req); fprintf(stdout, "-------------------------------------\n"); + + frepareq(req); } fprintf(stdout, "xxxRESxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n"); for (char *ch = strtok(reses, "|"); ch != NULL; ch = strtok(NULL, "|")) { @@ -79,6 +81,8 @@ int main(void) { fprintf(stdout, "-------------------------------------\n"); printfpares(res); fprintf(stdout, "-------------------------------------\n"); + + frepares(res); } fprintf(stdout, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n");