From 3e7e1faef7366413e0b9cfdafc3cca39c3986a40 Mon Sep 17 00:00:00 2001 From: Kevin Jerebica Date: Mon, 9 Sep 2024 20:55:21 +0200 Subject: [PATCH] feat: add parser for host info --- Makefile | 5 ++++- parslib.c | 45 ++++++++++++++++++++++++++++++++++++++++++++- parslib.h | 13 ++++++++++++- 3 files changed, 60 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index da7a49b..1852987 100644 --- a/Makefile +++ b/Makefile @@ -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) diff --git a/parslib.c b/parslib.c index dc3cb59..c6dedb1 100644 --- a/parslib.c +++ b/parslib.c @@ -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; diff --git a/parslib.h b/parslib.h index e0556c1..d6d3b87 100644 --- a/parslib.h +++ b/parslib.h @@ -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);