fix: edit docs and re-design struct use
This commit is contained in:
parent
a20c833796
commit
84055b8c90
17
README
17
README
|
@ -8,6 +8,22 @@
|
||||||
RES: github.com/tinyproxy/tinyproxy
|
RES: github.com/tinyproxy/tinyproxy
|
||||||
RES: github.com/nginx/nginx
|
RES: github.com/nginx/nginx
|
||||||
|
|
||||||
|
TECHNICALS
|
||||||
|
|
||||||
|
int initres(void);
|
||||||
|
void fretres(void);
|
||||||
|
int readlin(char **buff, char **buff_lim);
|
||||||
|
int parshfield(char **offset, int len, /* out */ struct point *hentries);
|
||||||
|
int parstitle(char *offset, int len, /* out */ struct httitle *titl);
|
||||||
|
int parsme(char **buff, /* out */ struct httpars *pars);
|
||||||
|
|
||||||
|
Library is designed to follow source input unaltering.
|
||||||
|
Which means, that it does not directly alter the input
|
||||||
|
data or strings while storing the information of all
|
||||||
|
parsed results on the stack or as pointers to specific
|
||||||
|
parts of the source data. This allows to provide it
|
||||||
|
with input that resides both within the stack or heap.
|
||||||
|
|
||||||
LEAKS
|
LEAKS
|
||||||
|
|
||||||
LEAKS file contains the most recent valgrind memory leaks
|
LEAKS file contains the most recent valgrind memory leaks
|
||||||
|
@ -18,7 +34,6 @@ LEAKS
|
||||||
|
|
||||||
TODO
|
TODO
|
||||||
|
|
||||||
* headers table needs length fields
|
|
||||||
* implement tests
|
* implement tests
|
||||||
* integrations with tinyproxy..?
|
* integrations with tinyproxy..?
|
||||||
|
|
||||||
|
|
BIN
tinyparser
BIN
tinyparser
Binary file not shown.
182
tinyparser.c
182
tinyparser.c
|
@ -4,38 +4,83 @@
|
||||||
#include "tinyparser.h"
|
#include "tinyparser.h"
|
||||||
#include "streecmp/streecmp.h"
|
#include "streecmp/streecmp.h"
|
||||||
|
|
||||||
extern char *headers;
|
|
||||||
extern char *methods;
|
extern char *methods;
|
||||||
|
extern char *headers;
|
||||||
|
|
||||||
int method = 0;
|
static struct nod *method_tree = NULL;
|
||||||
char *uri = NULL;
|
static struct nod *header_tree = NULL;
|
||||||
char *ver = NULL;
|
|
||||||
int uri_len = 0;
|
|
||||||
int ver_len = 0;
|
|
||||||
|
|
||||||
struct nod *header_tree = NULL;
|
static void debug_stats(struct httpars *pars) {
|
||||||
struct nod *method_tree = NULL;
|
fprintf(stderr, "\tstats:\n"
|
||||||
struct point *header_table = NULL;
|
"\t\tmethod\t: %d\n"
|
||||||
|
"\t\turi\t: %.*s\n"
|
||||||
|
"\t\tver\t: %.*s\n",
|
||||||
|
pars->titl.method,
|
||||||
|
pars->titl.uri.len, pars->titl.uri.er,
|
||||||
|
pars->titl.ver.len, pars->titl.ver.er
|
||||||
|
);
|
||||||
|
|
||||||
int read_line(char **buffer, char **buffer_limit) {
|
fprintf(stdout, "\theaders:\n");
|
||||||
|
for (int i = 0; i < header_count; i++) {
|
||||||
|
struct point *pnt = &pars->hentries[i];
|
||||||
|
if (!pnt->er) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf(stdout, "\t\t%d\t: %.*s\n", i, pnt->len, pnt->er);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int initres(void) {
|
||||||
|
int ret = 0;
|
||||||
|
|
||||||
|
header_tree = allocnod();
|
||||||
|
if (!header_tree) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
method_tree = allocnod();
|
||||||
|
if (!method_tree) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = gentree(header_tree, headers, NULL);
|
||||||
|
if (ret < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = gentree(method_tree, methods, NULL);
|
||||||
|
if (ret < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void fretres(void) {
|
||||||
|
free(method_tree);
|
||||||
|
free(header_tree);
|
||||||
|
}
|
||||||
|
|
||||||
|
int read_line(char **buff, char **buff_lim) {
|
||||||
int diff = 0;
|
int diff = 0;
|
||||||
if ((*buffer) >= (*buffer_limit)) {
|
if ((*buff) >= (*buff_lim)) {
|
||||||
return diff;
|
return diff;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
char *match = strstr(*buffer, "\r\n");
|
char *match = strstr(*buff, "\r\n");
|
||||||
if (match) {
|
if (match) {
|
||||||
ret = match-(*buffer);
|
ret = match-(*buff);
|
||||||
*buffer += 2;
|
*buff += 2;
|
||||||
} else {
|
} else {
|
||||||
ret = (*buffer_limit)-(*buffer);
|
ret = (*buff_lim)-(*buff);
|
||||||
}
|
}
|
||||||
*buffer += ret;
|
*buff += ret;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int parse_header(char *offset, int len) {
|
int parshfield(char *offset, int len, struct point *hentries) {
|
||||||
int key = 0;
|
int key = 0;
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
int diff = 0;
|
int diff = 0;
|
||||||
|
@ -78,13 +123,13 @@ _loop:
|
||||||
// header value
|
// header value
|
||||||
diff = cursor_lim-cursor;
|
diff = cursor_lim-cursor;
|
||||||
|
|
||||||
header_table[key].er = cursor;
|
hentries[key].er = cursor;
|
||||||
header_table[key].len = diff;
|
hentries[key].len = diff;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int parse_title(char *offset, int len) {
|
int parstitle(char *offset, int len, struct httitle *titl) {
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
int diff = 0;
|
int diff = 0;
|
||||||
char *cursor = offset;
|
char *cursor = offset;
|
||||||
|
@ -105,7 +150,7 @@ int parse_title(char *offset, int len) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
method = ret;
|
titl->method = ret;
|
||||||
cursor += diff;
|
cursor += diff;
|
||||||
|
|
||||||
// white space
|
// white space
|
||||||
|
@ -128,8 +173,8 @@ _loop1:
|
||||||
}
|
}
|
||||||
|
|
||||||
diff = uri_lim-cursor;
|
diff = uri_lim-cursor;
|
||||||
uri = cursor;
|
titl->uri.er = cursor;
|
||||||
uri_len = diff;
|
titl->uri.len = diff;
|
||||||
|
|
||||||
cursor += diff;
|
cursor += diff;
|
||||||
|
|
||||||
|
@ -145,28 +190,28 @@ _loop2:
|
||||||
|
|
||||||
// ver
|
// ver
|
||||||
diff = cursor_lim-cursor;
|
diff = cursor_lim-cursor;
|
||||||
ver = cursor;
|
titl->ver.er = cursor;
|
||||||
ver_len = diff;
|
titl->ver.len = diff;
|
||||||
|
|
||||||
cursor += diff;
|
cursor += diff;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int parse_request(char *buffer) {
|
int parsme(char *buff, struct httpars *pars) {
|
||||||
int ret;
|
int ret;
|
||||||
char *buffer_limit = buffer+strlen(buffer);
|
char *buff_lim = buff+strlen(buff);
|
||||||
|
|
||||||
char *title_offset = buffer;
|
char *title_offset = buff;
|
||||||
int title_len = read_line(&buffer, &buffer_limit);
|
int title_len = read_line(&buff, &buff_lim);
|
||||||
if ((ret = parse_title(title_offset, title_len)) < 0) {
|
if ((ret = parstitle(title_offset, title_len, &pars->titl)) < 0) {
|
||||||
fprintf(stderr, "Failed parsing title\n");
|
fprintf(stderr, "Failed parsing title\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int bound = 0; bound < MAX_BOUND; bound++) {
|
for (int bound = 0; bound < MAX_BOUND; bound++) {
|
||||||
char *header_offset = buffer;
|
char *header_offset = buff;
|
||||||
int header_len = read_line(&buffer, &buffer_limit);
|
int header_len = read_line(&buff, &buff_lim);
|
||||||
char *header_limit = header_offset+header_len;
|
char *header_limit = header_offset+header_len;
|
||||||
|
|
||||||
// IF END OF MESSAGE
|
// IF END OF MESSAGE
|
||||||
|
@ -174,7 +219,9 @@ int parse_request(char *buffer) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((ret = parse_header(header_offset, header_len)) < 0) {
|
if ((ret = parshfield(header_offset,
|
||||||
|
header_len,
|
||||||
|
pars->hentries)) < 0) {
|
||||||
fprintf(stderr, "Failed parsing header\n");
|
fprintf(stderr, "Failed parsing header\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -183,72 +230,3 @@ int parse_request(char *buffer) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void debug_stats(void) {
|
|
||||||
fprintf(stderr, "\tstats:\n"
|
|
||||||
"\t\tmethod\t: %d\n"
|
|
||||||
"\t\turi\t: %.*s\n"
|
|
||||||
"\t\tver\t: %.*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\t: %.*s\n", i, pnt->len, pnt->er);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(void) {
|
|
||||||
int ret;
|
|
||||||
char *str = strdup(TEST_ONE);
|
|
||||||
|
|
||||||
header_table = (struct point *) calloc(header_count, sizeof(struct point));
|
|
||||||
if (!header_table) {
|
|
||||||
fprintf(stderr, "Not enough dynamic memory\n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
header_tree = allocnod();
|
|
||||||
if (!header_tree) {
|
|
||||||
fprintf(stderr, "Not enough dynamic memory\n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
method_tree = allocnod();
|
|
||||||
if (!method_tree) {
|
|
||||||
fprintf(stderr, "Not enough dynamic memory\n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = gentree(header_tree, headers, NULL);
|
|
||||||
if (ret < 0) {
|
|
||||||
fprintf(stderr, "Failed generating the header comparison tree\n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = gentree(method_tree, methods, NULL);
|
|
||||||
if (ret < 0) {
|
|
||||||
fprintf(stderr, "Failed generating the header comparison tree\n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = parse_request(str);
|
|
||||||
if (ret < 0) {
|
|
||||||
fprintf(stderr, "Failed parsing request\n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
fprintf(stdout, "++Successfully parsed data++\n");
|
|
||||||
debug_stats();
|
|
||||||
|
|
||||||
frenod(method_tree);
|
|
||||||
frenod(header_tree);
|
|
||||||
free(header_table);
|
|
||||||
free(str);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
20
tinyparser.h
20
tinyparser.h
|
@ -11,8 +11,6 @@
|
||||||
"\r\n"\
|
"\r\n"\
|
||||||
"{\"key\": \"kefjoiawejfojgorgjbosejrgo\"}"\
|
"{\"key\": \"kefjoiawejfojgorgjbosejrgo\"}"\
|
||||||
|
|
||||||
// ENUMS
|
|
||||||
|
|
||||||
// SRC:https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
|
// SRC:https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
|
||||||
enum methods {
|
enum methods {
|
||||||
method_get = 1,
|
method_get = 1,
|
||||||
|
@ -145,4 +143,22 @@ struct point {
|
||||||
int len;
|
int len;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct httitle {
|
||||||
|
int method;
|
||||||
|
struct point uri;
|
||||||
|
struct point ver;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct httpars {
|
||||||
|
struct httitle titl;
|
||||||
|
struct point hentries[header_count];
|
||||||
|
};
|
||||||
|
|
||||||
|
int initres(void);
|
||||||
|
void fretres(void);
|
||||||
|
int readlin(char **buff, char **buff_lim);
|
||||||
|
int parshfield(char *offset, int len, /* out */ struct point *hentries);
|
||||||
|
int parstitle(char *offset, int len, /* out */ struct httitle *titl);
|
||||||
|
int parsme(char *buff, /* out */ struct httpars *pars);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue
Block a user