diff --git a/README b/README index b2199d8..8ec32db 100644 --- a/README +++ b/README @@ -19,7 +19,6 @@ LEAKS TODO * headers table needs length fields - * refactor header parsing * implement tests * integrations with tinyproxy..? diff --git a/tinyparser b/tinyparser index 87609c2..29d6abb 100755 Binary files a/tinyparser and b/tinyparser differ diff --git a/tinyparser.c b/tinyparser.c index e11afae..3a3df00 100644 --- a/tinyparser.c +++ b/tinyparser.c @@ -15,7 +15,7 @@ int ver_len = 0; struct nod *header_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 diff = 0; @@ -36,26 +36,41 @@ int read_line(char **buffer, char **buffer_limit) { } int parse_header(char *offset, int len) { - int cursor = 0; + int key = 0; int ret = 0; - char *header_limit = offset+len; - char *sep = strchr(offset, ':'); - if (!sep) { + int diff = 0; + + 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; } - if (sep > header_limit) { + diff = htitle_lim-cursor; + ret = streencmp(header_tree, cursor, diff); + if (!ret) { return -1; } - int htitle_len = sep-offset; - ret = streencmp(header_tree, offset, htitle_len); - if (ret == 0) { - return -1; + key = ret; + cursor += diff; + + // white space and seperators + while (*cursor == ':' || *cursor == ' ') { + cursor++; } - char *hvalue = sep+1; - header_table[ret] = hvalue; + // header value + diff = cursor_lim-cursor; + + header_table[key].er = cursor; + header_table[key].len = diff; return 0; } @@ -143,11 +158,30 @@ int parse_request(char *buffer) { 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 ret; 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) { fprintf(stderr, "Not enough dynamic memory\n"); return -1; @@ -183,12 +217,8 @@ int main(void) { return -1; } - fprintf(stderr, "[+] Finished parsing request\n" - " method: %d\n" - " uri : %.*s\n" - " ver : %.*s\n", - method, uri_len, uri, ver_len, ver - ); + fprintf(stdout, "++Successfully parsed data++\n"); + debug_stats(); frenod(method_tree); frenod(header_tree); diff --git a/tinyparser.h b/tinyparser.h index 0e54d21..e8d157d 100644 --- a/tinyparser.h +++ b/tinyparser.h @@ -140,4 +140,9 @@ enum headers { header_count }; +struct point { + char *er; + int len; +}; + #endif