5 * Parse a URL string into its constituent parts.
7 * We accept URLs of the form
9 * [protocol://[host][:port]/]path/to/file
11 * The URL string will be modified by having NULs inserted after
12 * "protocol", "host" and "port". The original URL can be
13 * reconstructed by calling unparse_url.
16 void parse_url ( struct url_info *info, char *url ) {
19 DBG ( "URL parsing \"%s\"\n", url );
21 /* Zero the structure */
22 memset ( info, 0, sizeof ( *info ) );
24 /* Search for a protocol delimiter */
25 for ( p = url ; *p ; p++ ) {
26 if ( memcmp ( p, "://", 3 ) != 0 )
29 /* URL has an explicit protocol */
35 /* Search for port or file delimiter */
48 DBG ( "URL protocol \"%s\" host \"%s\" port \"%s\" "
49 "file \"%s\"\n", info->protocol, info->host,
50 info->port ? info->port : "(NONE)", info->file );
54 /* URL has no explicit protocol; is just a filename */
56 DBG ( "URL file \"%s\"\n", info->file );
60 * Restore a parsed URL to its original pristine form.
63 char * unparse_url ( struct url_info *info ) {
64 if ( info->protocol ) {
65 /* URL had a protocol: fill in the deleted separators */
71 DBG ( "URL reconstructed \"%s\"\n", info->protocol );
72 return info->protocol;
74 /* URL had no protocol; was just a filename */
75 DBG ( "URL reconstructed \"%s\"\n", info->file );