First commit; added functions for retrieving different parts of a URL

master
Aadhavan Srinivasan 2 years ago
commit 833e1027cc

49
url.c

@ -0,0 +1,49 @@
#include "url.h"
#include <string.h>
#include <stdlib.h>
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;
}

@ -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);
Loading…
Cancel
Save