2005-04-28 12:12:11 +00:00
|
|
|
#include "string.h"
|
2005-05-01 19:10:04 +00:00
|
|
|
#include "resolv.h"
|
|
|
|
#include "etherboot.h" /* for arptable */
|
2005-04-28 12:12:11 +00:00
|
|
|
#include "url.h"
|
|
|
|
|
|
|
|
/*
|
2005-05-01 19:10:04 +00:00
|
|
|
* Parse a URL and deduce a struct protocol *, a struct sockaddr_in
|
|
|
|
* and a char *filename.
|
2005-04-28 12:12:11 +00:00
|
|
|
*
|
2005-04-28 13:20:02 +00:00
|
|
|
* We accept URLs of the form
|
2005-04-28 12:12:11 +00:00
|
|
|
*
|
2005-04-28 13:20:02 +00:00
|
|
|
* [protocol://[host][:port]/]path/to/file
|
|
|
|
*
|
2005-05-01 19:10:04 +00:00
|
|
|
* Returns 1 for success, 0 for failure (e.g. unknown protocol).
|
2005-04-28 12:12:11 +00:00
|
|
|
*
|
|
|
|
*/
|
2005-05-01 19:10:04 +00:00
|
|
|
int parse_url ( char *url, struct protocol **proto,
|
|
|
|
struct sockaddr_in *server, char **filename ) {
|
2005-04-28 13:20:02 +00:00
|
|
|
char *p;
|
2005-05-01 19:10:04 +00:00
|
|
|
char *protocol = NULL;
|
|
|
|
char *host = NULL;
|
|
|
|
char *port = NULL;
|
|
|
|
int rc = 0;
|
2005-04-28 12:12:11 +00:00
|
|
|
|
2005-04-30 13:24:26 +00:00
|
|
|
DBG ( "URL parsing \"%s\"\n", url );
|
|
|
|
|
2005-05-01 19:10:04 +00:00
|
|
|
/* If no protocol is present, the whole URL will be a filename */
|
|
|
|
*filename = url;
|
2005-04-28 12:12:11 +00:00
|
|
|
|
2005-05-01 19:10:04 +00:00
|
|
|
/* Search for a protocol delimiter. If found, parse out the
|
|
|
|
* host and port parts of the URL, inserting NULs to terminate
|
|
|
|
* the different sections.
|
|
|
|
*/
|
2005-04-28 13:20:02 +00:00
|
|
|
for ( p = url ; *p ; p++ ) {
|
|
|
|
if ( memcmp ( p, "://", 3 ) != 0 )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* URL has an explicit protocol */
|
|
|
|
*p = '\0';
|
|
|
|
p += 3;
|
2005-05-01 19:10:04 +00:00
|
|
|
protocol = url;
|
|
|
|
host = p;
|
2005-04-28 13:20:02 +00:00
|
|
|
|
2005-05-01 19:10:04 +00:00
|
|
|
/* Search for port and file delimiters */
|
2005-04-28 13:20:02 +00:00
|
|
|
for ( ; *p ; p++ ) {
|
|
|
|
if ( *p == ':' ) {
|
|
|
|
*p = '\0';
|
2005-05-01 19:10:04 +00:00
|
|
|
port = p + 1;
|
2005-04-28 13:20:02 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( *p == '/' ) {
|
|
|
|
*(p++) = '\0';
|
2005-04-28 12:12:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2005-05-01 19:10:04 +00:00
|
|
|
*filename = p;
|
|
|
|
|
|
|
|
break;
|
2005-04-28 12:12:11 +00:00
|
|
|
}
|
2005-05-01 19:10:04 +00:00
|
|
|
DBG ( "URL protocol \"%s\" host \"%s\" port \"%s\" file \"%s\"\n",
|
|
|
|
protocol ? protocol : "(default)", host ? host : "(default)",
|
|
|
|
port ? port : "(default)", *filename );
|
2005-04-28 12:12:11 +00:00
|
|
|
|
2005-05-01 19:10:04 +00:00
|
|
|
/* Identify the protocol */
|
|
|
|
*proto = identify_protocol ( protocol );
|
|
|
|
if ( ! *proto ) {
|
|
|
|
DBG ( "URL unknown protocol \"%s\"\n",
|
|
|
|
protocol ? protocol : "(default)" );
|
|
|
|
goto out;
|
|
|
|
}
|
2005-04-28 12:12:11 +00:00
|
|
|
|
2005-05-01 19:10:04 +00:00
|
|
|
/* Identify the host */
|
|
|
|
server->sin_addr = arptable[ARP_SERVER].ipaddr;
|
|
|
|
if ( host && host[0] ) {
|
|
|
|
if ( ! resolv ( &server->sin_addr, host ) ) {
|
|
|
|
DBG ( "URL unknown host \"%s\"\n", host );
|
|
|
|
goto out;
|
2005-04-28 13:20:02 +00:00
|
|
|
}
|
|
|
|
}
|
2005-05-01 19:10:04 +00:00
|
|
|
|
|
|
|
/* Identify the port */
|
|
|
|
server->sin_port = (*proto)->default_port;
|
|
|
|
if ( port && port[0] ) {
|
|
|
|
server->sin_port = strtoul ( port, NULL, 10 );
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = 1;
|
|
|
|
|
|
|
|
out:
|
|
|
|
/* Fill back in the original URL */
|
|
|
|
if ( protocol ) {
|
|
|
|
(*filename)[-1] = '/';
|
|
|
|
if ( port )
|
|
|
|
port[-1] = ':';
|
|
|
|
host[-3] = ':';
|
|
|
|
}
|
|
|
|
return rc;
|
2005-04-28 12:12:11 +00:00
|
|
|
}
|