feat: store parsed results fully in heap

This commit is contained in:
Kevin J. 2024-09-08 21:41:17 +02:00
parent ad3edd850b
commit 678a44228f
3 changed files with 65 additions and 5 deletions

View File

@ -120,6 +120,36 @@ void fretres(void) {
frenod(header_tree); 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 readlin(char **buff, char **buff_lim) {
int diff = 0; int diff = 0;
if ((*buff) >= (*buff_lim)) { if ((*buff) >= (*buff_lim)) {
@ -180,8 +210,12 @@ _loop:
// header value // header value
diff = cursor_lim-cursor; 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; hentries[key].len = diff;
return 0; return 0;
@ -231,7 +265,12 @@ _loop1:
} }
diff = uri_lim-cursor; 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; titl->uri.len = diff;
cursor += diff; cursor += diff;
@ -248,7 +287,12 @@ _loop2:
// ver // ver
diff = cursor_lim-cursor; diff = cursor_lim-cursor;
titl->ver.er = cursor; e = strndup(cursor, diff);
if (!e) {
return -1;
}
titl->ver.er = e;
titl->ver.len = diff; titl->ver.len = diff;
cursor += diff; cursor += diff;
@ -272,7 +316,12 @@ int parestitl(char *offset, int len, struct httitleres *titl) {
} }
diff = ver_lim-cursor; 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; titl->ver.len = diff;
cursor += diff; cursor += diff;
@ -318,7 +367,12 @@ _loop2:
// status text // status text
diff = cursor_lim-cursor; diff = cursor_lim-cursor;
titl->stxt.er = cursor; e = strndup(cursor, diff);
if (!e) {
return -1;
}
titl->stxt.er = e;
titl->stxt.len = diff; titl->stxt.len = diff;
cursor += diff; cursor += diff;

View File

@ -167,6 +167,8 @@ struct httpares {
int initres(void); int initres(void);
void fretres(void); void fretres(void);
void frepareq(struct httpareq *req);
void frepares(struct httpares *res);
int readlin(char **buff, char **buff_lim); int readlin(char **buff, char **buff_lim);
int parshfield(char *offset, int len, /* out */ struct point *hentries); int parshfield(char *offset, int len, /* out */ struct point *hentries);

4
test.c
View File

@ -72,6 +72,8 @@ int main(void) {
fprintf(stdout, "-------------------------------------\n"); fprintf(stdout, "-------------------------------------\n");
printfpareq(req); printfpareq(req);
fprintf(stdout, "-------------------------------------\n"); fprintf(stdout, "-------------------------------------\n");
frepareq(req);
} }
fprintf(stdout, "xxxRESxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n"); fprintf(stdout, "xxxRESxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n");
for (char *ch = strtok(reses, "|"); ch != NULL; ch = strtok(NULL, "|")) { for (char *ch = strtok(reses, "|"); ch != NULL; ch = strtok(NULL, "|")) {
@ -79,6 +81,8 @@ int main(void) {
fprintf(stdout, "-------------------------------------\n"); fprintf(stdout, "-------------------------------------\n");
printfpares(res); printfpares(res);
fprintf(stdout, "-------------------------------------\n"); fprintf(stdout, "-------------------------------------\n");
frepares(res);
} }
fprintf(stdout, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n"); fprintf(stdout, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n");