Next: Implementing An HTTP GET Command Function, Previous: Implementing the HTTP Client Header---Includes---Defines, Up: Steps in Implementing an Insecure HTTP Client [Index]
The main routine is invoked with a URL of the form
http://www.server.com/path/to/document.html
You need to separate the host and the path using a utility routine parse_url,
shown in Listing 1-2.
/**
* Accept a well-formed URL (e.g. http://www.company.com/index.html) and return
* pointers to the host part and the path part. Note that this function
* modifies the uri itself as well. It returns 0 on success, -1 if the URL is
* found to be malformed in any way.
*/
int parse_url( char *uri, char **host, char **path )
{
char *pos;
pos = strstr( uri, "//" );
if ( !pos )
{
return -1;
}
*host = pos + 2;
pos = strchr( *host, '/' );
if ( !pos )
{
*path = NULL;
}
else
{
*pos = '\0';
*path = pos + 1;
}
return 0;
}
Listing 1.2: "http.c" parse_url
You scan through the URL, looking for the delimiters ‘//’ and ‘/’ and replace
them with null-terminators so that the caller can treat them as C strings. The
calling function passes in two pointers to pointers; these should be null when
the function starts and will be modified to point into the ‘uri’ string, which
came from argv.