feat: add parser for host info

This commit is contained in:
Kevin J. 2024-09-09 20:55:21 +02:00
parent 678a44228f
commit 3e7e1faef7
3 changed files with 60 additions and 3 deletions

View File

@ -1,14 +1,17 @@
CC=gcc
LD=ld
CFLAGS=-g3
CFILES=parslib.c strings.c
TEST_FILES=test.c
OUTPUT=parslib.o
TOUTPUT=test
LOUTPUT=parslib.final.o
all: $(FILES)
$(CC) -c $(CFLAGS) -o parslib.o parslib.c
$(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)
@ -20,4 +23,4 @@ run:
./$(OUTPUT)
clean:
rm -rf *.o $(TOUTPUT)
rm -rf *.o $(TOUTPUT) $(OUTPUT) $(LOUTPUT)

View File

@ -10,7 +10,7 @@ extern char *headers;
static struct nod *method_tree = NULL;
static struct nod *header_tree = NULL;
static int stoin(char *str, int len, int *out) {
int stoin(char *str, int len, int *out) {
int ret = 0;
int place = 1;
for (char *chr = str+len-1; chr >= str; chr--) {
@ -221,6 +221,49 @@ _loop:
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;

View File

@ -9,7 +9,9 @@
"User-Agent: curl/8.9.1\r\n"\
"Accept: */*\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
enum methods {
@ -143,6 +145,13 @@ struct point {
int len;
};
struct hostinfo {
char *hostname;
char *service;
int hostname_len;
int service_len;
};
struct httitlereq {
int method;
struct point uri;
@ -165,6 +174,7 @@ struct httpares {
struct point hentries[header_count];
};
int stoin(char *str, int len, int *out);
int initres(void);
void fretres(void);
void frepareq(struct httpareq *req);
@ -172,6 +182,7 @@ void frepares(struct httpares *res);
int readlin(char **buff, char **buff_lim);
int parshfield(char *offset, int len, /* out */ struct point *hentries);
int pahostinfo(char *offset, int len, /* out */ struct hostinfo *info);
int pareqtitl(char *offset, int len, struct httitlereq *titl);
int parestitl(char *offset, int len, struct httitleres *titl);