commit 833e1027ccaa32688e5be6f85f2f45811ab3f4e9 Author: Aadhavan Srinivasan Date: Wed May 17 11:59:33 2023 -0500 First commit; added functions for retrieving different parts of a URL diff --git a/url.c b/url.c new file mode 100644 index 0000000..e542ba5 --- /dev/null +++ b/url.c @@ -0,0 +1,49 @@ +#include "url.h" +#include +#include + +struct URL_s { + char* schema; + char* hostname; + char* port; + char* filepath; +}; + +char* get_schema(URL* url) { + return url->schema; +} + +char* get_hostname(URL* url) { + return url->hostname; +} + +char* get_port(URL* url) { + return url->port; +} + +char* get_filepath(URL* url) { + return url->filepath; +} + + +URL* new_url(char* url_str) { + char* url_dup = strdup(url_str); + URL* url = malloc(sizeof(struct URL_s)); + + url->schema = strtok(url_dup,":"); + + url->hostname = strtok(NULL,"/"); + + if (strcmp(url->schema,"http") == 0) { + url->port = "80"; + } else { + url->port = "443"; + } + + url->filepath = strtok(NULL,"/"); + if (url->filepath == NULL) { + url->filepath = "/index.html"; + } + + return url; +} diff --git a/url.h b/url.h new file mode 100644 index 0000000..69abea2 --- /dev/null +++ b/url.h @@ -0,0 +1,9 @@ +typedef struct URL_s URL; + +URL* new_url(char* url); + +char* get_schema(URL* url); +char* get_hostname(URL* url); +char* get_port(URL* url); +char* get_filepath(URL* url); +