Continued working on url_manipulation library

master
Aadhavan Srinivasan 2 years ago
parent 5ff532ecd9
commit 7c17fcadcf

@ -1,5 +1,8 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
#define DEFAULT_PATH "/index.html"
char* url_to_proto(char* url) { char* url_to_proto(char* url) {
size_t length = strlen(url); size_t length = strlen(url);
@ -22,20 +25,49 @@ char* url_to_hostname(char* url) {
size_t length = strlen(url); size_t length = strlen(url);
char* protocol = url_to_proto(url); char* protocol = url_to_proto(url);
char* temp_substring = malloc(sizeof(char) * 256);
char* to_return = malloc(sizeof(char) * 256); char* to_return = malloc(sizeof(char) * 256);
/* This is a very roundabout way of saying that, starting from a certain point in the /* 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 URL (length of protocol string + 3 (':','/','/'), we are copying the rest of the string into another
substring. */ substring. */
strncpy(to_return,url+(strlen(protocol) + 3),(length - (strlen(protocol) + 3)));
strncpy(temp_substring,url+(strlen(protocol) + 3),(length - (strlen(protocol) + 3)));
/* Now we need to substring _this_ substring, by returning only the part before the next slash. */
int sentinel = 0;
for (int i=0;i<strlen(temp_substring);i++) {
if (*(temp_substring+(sizeof(char) * i)) == '/') {
sentinel = i;
}
}
strncpy(to_return,temp_substring,sentinel);
free(temp_substring);
return to_return;
} }
char* url_to_path(char* url) { char* url_to_path(char* url) {
/* Assumption: The path starts from the third slash to the end of the string. */ /* Assumption: The path starts from the third slash to the end of the string. */
int str_len = strlen(url);
int sentinel = 0;
int num_slashes = 0;
char* to_return = malloc(sizeof(char) * 512);
for (int i=0;i<strlen(url);i++) {
if (*(url + (sizeof(char) * i)) == '/') {
num_slashes++;
sentinel = i;
}
if (num_slashes == 3) {
break;
}
}
if (sentinel == 0) {
to_return = DEFAULT_PATH;
}
/* Copy all bytes of the string, starting from the third slash */
strncpy(to_return, url+(sizeof(char) * sentinel), strlen(url)-(sizeof(char) * sentinel));
return to_return;
} }

@ -10,3 +10,7 @@ Example - 'https://example.org' would return 'example.org'. */
char* url_to_hostname(char* url); char* url_to_hostname(char* url);
/* url_to_path - Returns the path portion of a URL.
Example: 'https://example.org/index.html' will return '/index.html'
Any empty path will also return '/index.html' */
char* url_to_path(char* url);

Loading…
Cancel
Save