From 7eb898a6f4ca429416ba2ab07d93564f137dd8e2 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Mon, 1 Jan 2024 18:47:20 +0200 Subject: [browser-cli] Proper command line and URI parsing --- browser-cli.c | 39 +++++++++++++++++++++++++++++++-------- browser.c | 1 - util.c | 8 ++++++++ util.h | 1 + 4 files changed, 40 insertions(+), 9 deletions(-) diff --git a/browser-cli.c b/browser-cli.c index 8b720c2..64d5a73 100644 --- a/browser-cli.c +++ b/browser-cli.c @@ -94,6 +94,15 @@ int hostLen(char* start) { return count; } +char* findBeginningOfPath(char* uri) { + char* startPath = strchr(uri, '/'); + while (startPath != uri && startPath != NULL) { + if (*(startPath - 1) == '.') startPath--; + else break; + } + return startPath; +} + int handleCLI(sds *host, sds *port, sds *uri, const sds page) { // Get a line char line[1024]; @@ -107,8 +116,10 @@ int handleCLI(sds *host, sds *port, sds *uri, const sds page) { // Number or URL if (line[0] != ':') { + sds newURI; + // Index of anchor - if (strchr(line, '@') == NULL) { + if (isNumber(line)) { int gotoIndex = 0; sscanf(line, "%d", &gotoIndex); @@ -117,23 +128,35 @@ int handleCLI(sds *host, sds *port, sds *uri, const sds page) { return 0; } - if (*uri != NULL) sdsfree(*uri); char* start = strchr(page + anchorsIndecies[gotoIndex], '(') + 1; - *uri = sdsnewlen(start, strchr(start, ')') - start); + newURI = sdsnewlen(start, strchr(start, ')') - start); } // New address else { - if (*uri != NULL) sdsfree(*uri); - *uri = sdsnewlen(line, strlen(line)-1 /* skip newline */); + newURI = sdsnewlen(line, strlen(line)-1); // skip newline + } + + char* startPath = findBeginningOfPath(newURI); + + // Handle relative URLs + if (startPath == newURI) { + newURI = sdscatsds(sdsnewlen(*uri, findBeginningOfPath(*uri) - *uri), newURI); + startPath = findBeginningOfPath(newURI); } - char* startPath = strchr(*uri, '/'); - char* startHost = strchr(*uri, '@'); - char* startPort = strchr(*uri, ':'); + if (*uri != NULL) sdsfree(*uri); + *uri = newURI; + + char* startHost = strchr(newURI, '@'); + char* startPort = strchr(newURI, ':'); + + // Update host if (startHost < startPath) { if (host != NULL) sdsfree(*host); *host = sdsnewlen(startHost + 1, hostLen(startHost + 1)); } + + // Update port if (startPort < startPath) { if (port != NULL) sdsfree(*port); *port = sdsnewlen(startPort + 1, portLen(startPort + 1)); diff --git a/browser.c b/browser.c index 4625c78..7268831 100644 --- a/browser.c +++ b/browser.c @@ -89,7 +89,6 @@ int main(int argc, char* argv[]) { * Handle user input */ stopProgram = handleCLI(&host, &port, &uri, page); - printf("%s %s %s\n", host, port, uri); sdsfree(page); } diff --git a/util.c b/util.c index ca793ce..5f847fa 100644 --- a/util.c +++ b/util.c @@ -161,3 +161,11 @@ void shiftLeft(char* str, size_t size, size_t shift) { str++; } } + +int isNumber(char* str) { + while (*str >= ' ') { + if (*str < '0' || *str > '9') return 0; + str++; + } + return 1; +} diff --git a/util.h b/util.h index 8fa59c5..e632578 100644 --- a/util.h +++ b/util.h @@ -23,5 +23,6 @@ sds gsub_getm(sds str, const regex_t *regex, const char* repl, int* *matches, in int digits(int num); int streq(const char* first, const char* second); void shiftLeft(char* str, size_t size, size_t shift); +int isNumber(char* str); #endif -- cgit v1.2.3