fix: header parsing
This commit is contained in:
parent
7b8bb91e2f
commit
b33e45ff35
1
README
1
README
|
@ -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..?
|
||||||
|
|
||||||
|
|
BIN
tinyparser
BIN
tinyparser
Binary file not shown.
68
tinyparser.c
68
tinyparser.c
|
@ -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);
|
||||||
|
|
|
@ -140,4 +140,9 @@ enum headers {
|
||||||
header_count
|
header_count
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct point {
|
||||||
|
char *er;
|
||||||
|
int len;
|
||||||
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue
Block a user