tmp: start working on parsing response messages

This commit is contained in:
0xdeadbeer 2024-09-04 12:16:40 +02:00
parent 630b6f2cbb
commit 7525e8af77
2 changed files with 122 additions and 5 deletions

110
parslib.c
View File

@ -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;
}

View File

@ -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);