Cryptography Reference
In-Depth Information
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.
Listing 1-2: “http.c” parse_url
/**
* 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;
}
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. Notice
that the calling function passes in two pointers to pointers; these should be
null when the function starts and will be modifi ed to point into the uri string,
which came from argv .
The main routine that coordinates all of this is shown in Listing 1-3.
Listing 1-3: “http.c” main
#define HTTP_PORT 80
/**
* Simple command-line HTTP client.
Search WWH ::




Custom Search