May 2019
Intermediate to advanced
478 pages
11h 21m
English
We will write a C function to parse a given URL.
The function takes as input a URL, and it returns as output the hostname, the port number, and the document path. To avoid needing to do manual memory management, the outputs are returned as pointers to specific parts of the input URL. The input URL is modified with terminating null pointers as required.
Our function begins by printing the input URL. This is useful for debugging. The code for that is as follows:
/*web_get.c excerpt*/void parse_url(char *url, char **hostname, char **port, char** path) { printf("URL: %s\n", url);
The function then attempts to find :// in the URL. If found, it reads in the first part of the URL as a protocol. Our program only supports HTTP. If the ...
Read now
Unlock full access