Added helper library for URL manipulation functions
parent
1ec2fd1097
commit
7d3ef3a6c9
@ -0,0 +1,37 @@
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
char* url_to_proto(char* url) {
|
||||
size_t length = strlen(url);
|
||||
int sentinel = 0;
|
||||
char* to_return = malloc(sizeof(char) * 10);
|
||||
for (int i=0;i<length;i++) {
|
||||
if (*(url + (sizeof(char) * i)) == ':') { /* If the character is ':' ...*/
|
||||
sentinel = i; /* ...Then we stop scanning the string */
|
||||
}
|
||||
}
|
||||
strncpy(to_return,url,sentinel);
|
||||
return to_return;
|
||||
}
|
||||
|
||||
char* url_to_hostname(char* url) {
|
||||
/* This function relies on a hack-y assumption: that, given a string
|
||||
that represents a URL (e.g. https://www.example.com), the host portion
|
||||
of the address is the length of the address minus (length of protocol + 3 ("://").
|
||||
In this case that would be 23 - [5 (h,t,t,p,s) + 3 = 8]. */
|
||||
|
||||
size_t length = strlen(url);
|
||||
char* protocol = url_to_proto(url);
|
||||
|
||||
char* to_return = malloc(sizeof(char) * 256);
|
||||
|
||||
|
||||
/* This is a very roundabout way of saying that, starting from a certain point in the
|
||||
URL (length of protocol string + 3), we are copying the rest of the string into another
|
||||
substring. */
|
||||
strncpy(to_return,url+(strlen(protocol) + 3),(length - (strlen(protocol) + 3)));
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,12 @@
|
||||
#include <string.h>
|
||||
|
||||
/* url_to_proto - This takes in a URL, and returns the protocol.
|
||||
Example - 'https://example.org' would return 'https'. */
|
||||
|
||||
char* url_to_proto(char* url);
|
||||
|
||||
/* url_to_hostname - This takes in a URL, and returns the hostname.
|
||||
Example - 'https://example.org' would return 'example.org'. */
|
||||
|
||||
char* url_to_hostname(char* url);
|
||||
|
Loading…
Reference in New Issue