Compare commits

..

No commits in common. "main" and "dev" have entirely different histories.
main ... dev

7 changed files with 109 additions and 388 deletions

2
.gitignore vendored
View File

@ -1,3 +1,3 @@
*.o *.o
compile_commands.json compile_commands.json
test tinyparser

View File

@ -1,26 +1,14 @@
CC=gcc CC=gcc
LD=ld
CFLAGS=-g3 CFLAGS=-g3
CFILES=parslib.c strings.c CFILES=parslib.c strings.c
TEST_FILES=test.c CFILES_STREECMP=streecmp/streecmp.c
OUTPUT=parslib.o OUTPUT=parslib
TOUTPUT=test
LOUTPUT=parslib.final.o
all: $(FILES) all:
$(CC) -c $(CFLAGS) -o parslib.o parslib.c $(CC) $(CFLAGS) $(CFILES) $(CFILES_STREECMP) -o $(OUTPUT)
$(CC) -c $(CFLAGS) -o strings.o strings.c
$(CC) -c $(CFLAGS) -o streecmp/streecmp.o streecmp/streecmp.c
$(LD) -relocatable -o $(LOUTPUT) parslib.o strings.o streecmp/streecmp.o
test: all
$(CC) $(CFLAGS) $(TEST_FILES) parslib.o strings.o streecmp/streecmp.o -o $(TOUTPUT)
run_test:
./$(TOUTPUT)
run: run:
./$(OUTPUT) ./$(OUTPUT)
clean: clean:
rm -rf *.o $(TOUTPUT) $(OUTPUT) $(LOUTPUT) rm -rf $(OUTPUT)

8
README
View File

@ -27,10 +27,14 @@ TECHNICALS
LEAKS LEAKS
LEAKS file contains the most recent valgrind LEAKS file contains the most recent valgrind memory leaks
memory leaks dump ran on a test program. dump ran on main.c
LEAKS.TEST.[id] files respectively contain the most recent
valgrind memory leaks dump ran on a test with an id=[id]
TODO TODO
* implement tests
* integrations with tinyproxy..? * integrations with tinyproxy..?

338
parslib.c
View File

@ -10,19 +10,19 @@ extern char *headers;
static struct nod *method_tree = NULL; static struct nod *method_tree = NULL;
static struct nod *header_tree = NULL; static struct nod *header_tree = NULL;
int stoin(char *str, int len, int *out) { static int stoin(char *str, int len, int *out) {
int ret = 0; int ret = 0;
int place = 1; int place = 1;
for (char *chr = str+len-1; chr >= str; chr--) { for (char *chr = str+len; chr >= str; chr--) {
if (chr == str) { if (chr == str) {
if (*chr == '+') { if (*ch == '+') {
goto _proceed; goto _proceed;
} }
if (*chr == '-') { if (*ch == '-') {
goto _proceed; goto _proceed;
} }
} }
if (*chr >= '0' && *chr <= '9') { if (*chr >= '0' && *ch <= '9') {
goto _proceed; goto _proceed;
} }
@ -44,43 +44,21 @@ _proceed:
} }
*out = ret; *out = ret;
return 0;
} }
void printfpareq(struct httpareq *req) { void printfpars(struct httpars *pars) {
fprintf(stderr, "\tstats:\n" fprintf(stderr, "\tstats:\n"
"\t\tmethod\t: %d\n" "\t\tmethod\t: %d\n"
"\t\turi\t: %.*s\n" "\t\turi\t: %.*s\n"
"\t\tver\t: %.*s\n", "\t\tver\t: %.*s\n",
req->titl.method, pars->titl.method,
req->titl.uri.len, req->titl.uri.er, pars->titl.uri.len, pars->titl.uri.er,
req->titl.ver.len, req->titl.ver.er pars->titl.ver.len, pars->titl.ver.er
); );
fprintf(stdout, "\theaders:\n"); fprintf(stdout, "\theaders:\n");
for (int i = 0; i < header_count; i++) { for (int i = 0; i < header_count; i++) {
struct point *pnt = &req->hentries[i]; struct point *pnt = &pars->hentries[i];
if (!pnt->er) {
continue;
}
fprintf(stdout, "\t\t%d\t: %.*s\n", i, pnt->len, pnt->er);
}
}
void printfpares(struct httpares *res) {
fprintf(stderr, "\tstats:\n"
"\t\tver\t: %.*s\n"
"\t\tcode\t: %d\n"
"\t\tstext\t: %.*s\n",
res->titl.ver.len, res->titl.ver.er,
res->titl.code,
res->titl.stxt.len, res->titl.stxt.er
);
fprintf(stdout, "\theaders:\n");
for (int i = 0; i < header_count; i++) {
struct point *pnt = &res->hentries[i];
if (!pnt->er) { if (!pnt->er) {
continue; continue;
} }
@ -102,12 +80,12 @@ int initres(void) {
return -1; return -1;
} }
ret = gentree(header_tree, headers, NULL, LOOSE_CHECK); ret = gentree(header_tree, headers, NULL);
if (ret < 0) { if (ret < 0) {
return -1; return -1;
} }
ret = gentree(method_tree, methods, NULL, LOOSE_CHECK); ret = gentree(method_tree, methods, NULL);
if (ret < 0) { if (ret < 0) {
return -1; return -1;
} }
@ -116,44 +94,8 @@ int initres(void) {
} }
void fretres(void) { void fretres(void) {
frenod(method_tree); free(method_tree);
frenod(header_tree); free(header_tree);
}
void frepareq(struct httpareq *req) {
struct httitlereq *titl = &req->titl;
free(titl->uri.er);
titl->uri.er = NULL;
free(titl->ver.er);
titl->uri.er = NULL;
for (int i = 0; i < header_count; i++) {
struct point *e = &req->hentries[i];
if (!e->er) {
continue;
}
free(e->er);
e->er = NULL;
}
}
void frepares(struct httpares *res) {
struct httitleres *titl = &res->titl;
free(titl->ver.er);
titl->ver.er = NULL;
free(titl->stxt.er);
titl->stxt.er = NULL;
for (int i = 0; i < header_count; i++) {
struct point *e = &res->hentries[i];
if (!e->er) {
continue;
}
free(e->er);
e->er = NULL;
}
} }
int readlin(char **buff, char **buff_lim) { int readlin(char **buff, char **buff_lim) {
@ -192,9 +134,9 @@ int parshfield(char *offset, int len, struct point *hentries) {
} }
diff = htitle_lim-cursor; diff = htitle_lim-cursor;
ret = streencmp(header_tree, cursor, diff, LOOSE_CHECK); ret = streencmp(header_tree, cursor, diff);
if (!ret) { if (!ret) {
return 0; // skip it return -1;
} }
key = ret; key = ret;
@ -216,139 +158,13 @@ _loop:
// header value // header value
diff = cursor_lim-cursor; diff = cursor_lim-cursor;
char *e = strndup(cursor, diff);
if (!e) {
return -1;
}
hentries[key].er = e; hentries[key].er = cursor;
hentries[key].len = diff; hentries[key].len = diff;
return 0; return 0;
} }
int pahostinfo(char *offset, int len, struct hostinfo *info) {
int diff = 0;
int usedefault = 0;
char *delim = strchr(offset, ':');
if (!delim) {
delim = offset+len;
usedefault = 1;
}
diff = delim-offset;
info->hostname = strndup(offset, diff);
if (!info->hostname) {
goto _err;
}
info->hostname_len = diff;
if (usedefault) {
diff = strlen(DEFAULT_HTTP);
info->service = strndup(DEFAULT_HTTP, diff);
if (!info->service) {
goto _err_service;
}
info->service_len = diff;
} else {
char *service_offset = delim+1;
char *end = offset+len;
diff = end-service_offset;
info->service = strndup(service_offset, diff);
if (!info->service) {
goto _err_service;
}
info->service_len = diff;
}
return 0;
_err_service:
free(info->hostname);
_err:
return -1;
}
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(cursor, ' ');
if (!method_lim) {
return -1;
}
if (method_lim > cursor_lim) {
return -1;
}
diff = method_lim-cursor;
ret = streencmp(method_tree, cursor, diff, LOOSE_CHECK);
if (ret == 0) {
return -1;
}
titl->method = ret;
cursor += diff;
// white space
_loop1:
if (cursor > cursor_lim) {
return -1;
}
if (*cursor == ' ') {
cursor++;
goto _loop1;
}
// uri
char *uri_lim = strchr(cursor, ' ');
if (!uri_lim) {
return -1;
}
if (uri_lim > cursor_lim) {
return -1;
}
diff = uri_lim-cursor;
char *e = strndup(cursor, diff);
if (!e) {
return -1;
}
titl->uri.er = e;
titl->uri.len = diff;
cursor += diff;
// white space
_loop2:
if (cursor > cursor_lim) {
return -1;
}
if (*cursor == ' ') {
cursor++;
goto _loop2;
}
// ver
diff = cursor_lim-cursor;
e = strndup(cursor, diff);
if (!e) {
return -1;
}
titl->ver.er = e;
titl->ver.len = diff;
cursor += diff;
return 0;
}
int parestitl(char *offset, int len, struct httitleres *titl) { int parestitl(char *offset, int len, struct httitleres *titl) {
int ret = 0; int ret = 0;
int diff = 0; int diff = 0;
@ -365,12 +181,7 @@ int parestitl(char *offset, int len, struct httitleres *titl) {
} }
diff = ver_lim-cursor; diff = ver_lim-cursor;
char *e = strndup(cursor, diff); titl->ver.er = cursor;
if (!e) {
return -1;
}
titl->ver.er = e;
titl->ver.len = diff; titl->ver.len = diff;
cursor += diff; cursor += diff;
@ -416,12 +227,7 @@ _loop2:
// status text // status text
diff = cursor_lim-cursor; diff = cursor_lim-cursor;
e = strndup(cursor, diff); titl->stxt.er = cursor;
if (!e) {
return -1;
}
titl->stxt.er = e;
titl->stxt.len = diff; titl->stxt.len = diff;
cursor += diff; cursor += diff;
@ -429,13 +235,82 @@ _loop2:
return 0; return 0;
} }
int pareq(char *buff, struct httpareq *req) { 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(cursor, ' ');
if (!method_lim) {
return -1;
}
if (method_lim > cursor_lim) {
return -1;
}
diff = method_lim-cursor;
ret = streencmp(method_tree, cursor, diff);
if (ret == 0) {
return -1;
}
titl->method = ret;
cursor += diff;
// white space
_loop1:
if (cursor > cursor_lim) {
return -1;
}
if (*cursor == ' ') {
cursor++;
goto _loop1;
}
// uri
char *uri_lim = strchr(cursor, ' ');
if (!uri_lim) {
return -1;
}
if (uri_lim > cursor_lim) {
return -1;
}
diff = uri_lim-cursor;
titl->uri.er = cursor;
titl->uri.len = diff;
cursor += diff;
// white space
_loop2:
if (cursor > cursor_lim) {
return -1;
}
if (*cursor == ' ') {
cursor++;
goto _loop2;
}
// ver
diff = cursor_lim-cursor;
titl->ver.er = cursor;
titl->ver.len = diff;
cursor += diff;
return 0;
}
int parsme(char *buff, struct httpars *pars) {
int ret; int ret;
char *buff_lim = buff+strlen(buff); char *buff_lim = buff+strlen(buff);
char *title_offset = buff; char *title_offset = buff;
int title_len = readlin(&buff, &buff_lim); int title_len = readlin(&buff, &buff_lim);
if ((ret = pareqtitl(title_offset, title_len, &req->titl)) < 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;
} }
@ -452,7 +327,7 @@ int pareq(char *buff, struct httpareq *req) {
if ((ret = parshfield(header_offset, if ((ret = parshfield(header_offset,
header_len, header_len,
req->hentries)) < 0) { pars->hentries)) < 0) {
fprintf(stderr, "Failed parsing header\n"); fprintf(stderr, "Failed parsing header\n");
return -1; return -1;
} }
@ -461,34 +336,3 @@ int pareq(char *buff, struct httpareq *req) {
return 0; return 0;
} }
int pares(char *buff, struct httpares *res) {
int ret;
char *buff_lim = buff+strlen(buff);
char *title_offset = buff;
int title_len = readlin(&buff, &buff_lim);
if ((ret = parestitl(title_offset, title_len, &res->titl)) < 0) {
fprintf(stderr, "Failed parsing title\n");
return -1;
}
for (int bound = 0; bound < MAX_BOUND; bound++) {
char *header_offset = buff;
int header_len = readlin(&buff, &buff_lim);
char *header_limit = header_offset+header_len;
// IF END OF MESSAGE
if (!header_len) {
break;
}
if ((ret = parshfield(header_offset,
header_len,
res->hentries)) < 0) {
fprintf(stderr, "Failed parsing header\n");
return -1;
}
}
return 0;
}

View File

@ -9,9 +9,7 @@
"User-Agent: curl/8.9.1\r\n"\ "User-Agent: curl/8.9.1\r\n"\
"Accept: */*\r\n"\ "Accept: */*\r\n"\
"\r\n"\ "\r\n"\
"{\"key\": \"kefjoiawejfojgorgjbosejrgo\"}" "{\"key\": \"kefjoiawejfojgorgjbosejrgo\"}"\
#define DEFAULT_HTTP "80"
#define DEFAULT_HTTPS "443"
// 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 {
@ -145,13 +143,6 @@ struct point {
int len; int len;
}; };
struct hostinfo {
char *hostname;
char *service;
int hostname_len;
int service_len;
};
struct httitlereq { struct httitlereq {
int method; int method;
struct point uri; struct point uri;
@ -172,24 +163,14 @@ struct httpareq {
struct httpares { struct httpares {
struct httitleres titl; struct httitleres titl;
struct point hentries[header_count]; struct point hentries[header_count];
}; }
int stoin(char *str, int len, int *out);
int initres(void); int initres(void);
void fretres(void); void fretres(void);
void frepareq(struct httpareq *req);
void frepares(struct httpares *res);
int readlin(char **buff, char **buff_lim); int readlin(char **buff, char **buff_lim);
int parshfield(char *offset, int len, /* out */ struct point *hentries); int parshfield(char *offset, int len, /* out */ struct point *hentries);
int parstitle(char *offset, int len, /* out */ struct httitle *titl);
int pahostinfo(char *offset, int len, /* out */ struct hostinfo *info); int parsme(char *buff, /* out */ struct httpars *pars);
int pareqtitl(char *offset, int len, struct httitlereq *titl); void printfpars(struct httpars *pars);
int parestitl(char *offset, int len, struct httitleres *titl);
int pareq(char *buff, /* out */ struct httpareq *req);
int pares(char *buff, /* out */ struct httpares *res);
void printfpareq(struct httpareq *req);
void printfpares(struct httpares *res);
#endif #endif

@ -1 +1 @@
Subproject commit 29d80f211dde476f138f984fd7199bb6b1365034 Subproject commit ca1293aa8f123adb91d80f687933ea5c596c114a

96
test.c
View File

@ -1,96 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "parslib.h"
char *req_test_data = {
"GET / HTTP/1.1\r\n"
"Host: archive.0xdeadbeer.xyz\r\n"
"User-Agent: curl/8.9.1\r\n"
"Accept: */*\r\n"
"\r\n"
"{\"key\": \"kefjoiawejfojgorgjbosejrgo\"}|"
"GET / HTTP/1.1\r\n"
"Host: google.com\r\n"
"User-Agent: curl/8.9.1\r\n"
"Accept: */*\r\n|"
};
char *res_test_data = {
"HTTP/1.1 301 Moved Permanently\r\n"
"Location: http://www.google.com/\r\n"
"Content-Type: text/html; charset=UTF-8\r\n"
"Content-Security-Policy-Report-Only: object-src 'none';base-uri"
"'self';script-src 'nonce-7hRaJZVpUAa6E54ys6qpuA' 'strict-dynamic'"
"'report-sample' 'unsafe-eval' 'unsafe-inline' https:"
"http:;report-uri https://csp.withgoogle.com/csp/gws/other-hp\r\n"
"Date: Wed, 04 Sep 2024 17:52:02 GMT\r\n"
"Expires: Fri, 04 Oct 2024 17:52:02 GMT\r\n"
"Cache-Control: public, max-age=2592000\r\n"
"Server: gws\r\n"
"Content-Length: 219\r\n"
"X-XSS-Protection: 0\r\n"
"X-Frame-Options: SAMEORIGIN\r\n|"
};
int main(void) {
int ret = 0;
char *reqs = strdup(req_test_data);
if (!reqs) {
fprintf(stderr, "Not enough dynamic memory\n");
return -1;
}
char *reses = strdup(res_test_data);
if (!reses) {
fprintf(stderr, "Not enough dynamic memory\n");
return -1;
}
struct httpareq *req =
(struct httpareq *) calloc(1, sizeof(struct httpareq));
if (!req) {
fprintf(stderr, "Not enough dynamic memory\n");
return -1;
}
struct httpares *res =
(struct httpares *) calloc(1, sizeof(struct httpares));
if (!res) {
fprintf(stderr, "Not enough dynamic memory\n");
return -1;
}
ret = initres();
if (ret < 0) {
fprintf(stderr, "Failed initializing parser\n");
return -1;
}
fprintf(stdout, "xxxREQxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n");
for (char *ch = strtok(reqs, "|"); ch != NULL; ch = strtok(NULL, "|")) {
ret = pareq(ch, req);
fprintf(stdout, "-------------------------------------\n");
printfpareq(req);
fprintf(stdout, "-------------------------------------\n");
frepareq(req);
}
fprintf(stdout, "xxxRESxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n");
for (char *ch = strtok(reses, "|"); ch != NULL; ch = strtok(NULL, "|")) {
ret = pares(ch, res);
fprintf(stdout, "-------------------------------------\n");
printfpares(res);
fprintf(stdout, "-------------------------------------\n");
frepares(res);
}
fprintf(stdout, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n");
fretres();
free(res);
free(req);
free(reses);
free(reqs);
return 0;
}