david/ipxe
Archived
1
0
This repository has been archived on 2020-12-06. You can view files and clone it, but cannot push or open issues or pull requests.
ipxe/src/core/url.c

79 lines
1.7 KiB
C
Raw Normal View History

#include "string.h"
#include "url.h"
/*
2005-04-28 15:20:02 +02:00
* Parse a URL string into its constituent parts.
*
2005-04-28 15:20:02 +02:00
* We accept URLs of the form
*
2005-04-28 15:20:02 +02:00
* [protocol://[host][:port]/]path/to/file
*
* The URL string will be modified by having NULs inserted after
* "protocol", "host" and "port". The original URL can be
* reconstructed by calling unparse_url.
*
*/
2005-04-28 15:20:02 +02:00
void parse_url ( struct url_info *info, char *url ) {
char *p;
2005-04-30 15:24:26 +02:00
DBG ( "URL parsing \"%s\"\n", url );
2005-04-28 15:20:02 +02:00
/* Zero the structure */
memset ( info, 0, sizeof ( *info ) );
2005-04-28 15:20:02 +02:00
/* Search for a protocol delimiter */
for ( p = url ; *p ; p++ ) {
if ( memcmp ( p, "://", 3 ) != 0 )
continue;
/* URL has an explicit protocol */
info->protocol = url;
*p = '\0';
p += 3;
info->host = p;
/* Search for port or file delimiter */
for ( ; *p ; p++ ) {
if ( *p == ':' ) {
*p = '\0';
info->port = p + 1;
continue;
}
if ( *p == '/' ) {
*(p++) = '\0';
break;
}
}
2005-04-28 15:20:02 +02:00
info->file = p;
2005-04-30 15:24:26 +02:00
DBG ( "URL protocol \"%s\" host \"%s\" port \"%s\" "
"file \"%s\"\n", info->protocol, info->host,
info->port ? info->port : "(NONE)", info->file );
2005-04-28 15:20:02 +02:00
return;
}
2005-04-28 15:20:02 +02:00
/* URL has no explicit protocol; is just a filename */
info->file = url;
2005-04-30 15:24:26 +02:00
DBG ( "URL file \"%s\"\n", info->file );
}
/*
2005-04-28 15:20:02 +02:00
* Restore a parsed URL to its original pristine form.
*
*/
2005-04-28 15:20:02 +02:00
char * unparse_url ( struct url_info *info ) {
if ( info->protocol ) {
/* URL had a protocol: fill in the deleted separators */
info->file[-1] = '/';
if ( info->port ) {
info->port[-1] = ':';
}
info->host[-3] = ':';
2005-04-30 15:24:26 +02:00
DBG ( "URL reconstructed \"%s\"\n", info->protocol );
2005-04-28 15:20:02 +02:00
return info->protocol;
} else {
/* URL had no protocol; was just a filename */
2005-04-30 15:24:26 +02:00
DBG ( "URL reconstructed \"%s\"\n", info->file );
2005-04-28 15:20:02 +02:00
return info->file;
}
}