fix: header parsing

This commit is contained in:
0xdeadbeer 2024-09-03 21:51:27 +02:00
parent 7b8bb91e2f
commit b33e45ff35
4 changed files with 54 additions and 20 deletions

1
README
View File

@ -19,7 +19,6 @@ LEAKS
TODO TODO
* headers table needs length fields * headers table needs length fields
* refactor header parsing
* implement tests * implement tests
* integrations with tinyproxy..? * integrations with tinyproxy..?

Binary file not shown.

View File

@ -15,7 +15,7 @@ int ver_len = 0;
struct nod *header_tree = NULL; struct nod *header_tree = NULL;
struct nod *method_tree = NULL; struct nod *method_tree = NULL;
char **header_table = NULL; struct point *header_table = NULL;
int read_line(char **buffer, char **buffer_limit) { int read_line(char **buffer, char **buffer_limit) {
int diff = 0; int diff = 0;
@ -36,26 +36,41 @@ int read_line(char **buffer, char **buffer_limit) {
} }
int parse_header(char *offset, int len) { int parse_header(char *offset, int len) {
int cursor = 0; int key = 0;
int ret = 0; int ret = 0;
char *header_limit = offset+len; int diff = 0;
char *sep = strchr(offset, ':');
if (!sep) { char *cursor = offset;
char *cursor_lim = offset+len;
// header title
char *htitle_lim = strchr(offset, ':');
if (!htitle_lim) {
return -1;
}
if (htitle_lim > cursor_lim) {
return -1; return -1;
} }
if (sep > header_limit) { diff = htitle_lim-cursor;
ret = streencmp(header_tree, cursor, diff);
if (!ret) {
return -1; return -1;
} }
int htitle_len = sep-offset; key = ret;
ret = streencmp(header_tree, offset, htitle_len); cursor += diff;
if (ret == 0) {
return -1; // white space and seperators
while (*cursor == ':' || *cursor == ' ') {
cursor++;
} }
char *hvalue = sep+1; // header value
header_table[ret] = hvalue; diff = cursor_lim-cursor;
header_table[key].er = cursor;
header_table[key].len = diff;
return 0; return 0;
} }
@ -143,11 +158,30 @@ int parse_request(char *buffer) {
return 0; return 0;
} }
void debug_stats(void) {
fprintf(stderr, "\tstats:\n"
"\t\tmethod: %d\n"
"\t\turi : %.*s\n"
"\t\tver : %.*s\n",
method, uri_len, uri, ver_len, ver
);
fprintf(stdout, "\theaders:\n");
for (int i = 0; i < header_count; i++) {
struct point *pnt = &header_table[i];
if (!pnt->er) {
continue;
}
fprintf(stdout, "\t\t%d: %.*s\n", i, pnt->len, pnt->er);
}
}
int main(void) { int main(void) {
int ret; int ret;
char *str = strdup(TEST_ONE); char *str = strdup(TEST_ONE);
header_table = (char **) calloc(header_count, sizeof(char *)); header_table = (struct point *) calloc(header_count, sizeof(struct point));
if (!header_table) { if (!header_table) {
fprintf(stderr, "Not enough dynamic memory\n"); fprintf(stderr, "Not enough dynamic memory\n");
return -1; return -1;
@ -183,12 +217,8 @@ int main(void) {
return -1; return -1;
} }
fprintf(stderr, "[+] Finished parsing request\n" fprintf(stdout, "++Successfully parsed data++\n");
" method: %d\n" debug_stats();
" uri : %.*s\n"
" ver : %.*s\n",
method, uri_len, uri, ver_len, ver
);
frenod(method_tree); frenod(method_tree);
frenod(header_tree); frenod(header_tree);

View File

@ -140,4 +140,9 @@ enum headers {
header_count header_count
}; };
struct point {
char *er;
int len;
};
#endif #endif