mirror of https://github.com/ipxe/ipxe.git
Made parse_url do more of the processing, to avoid duplicating parts of
nic.c in http.cpull/1/head
parent
17bb10479d
commit
63482e4fe9
|
@ -1,42 +1,51 @@
|
||||||
#include "string.h"
|
#include "string.h"
|
||||||
|
#include "resolv.h"
|
||||||
|
#include "etherboot.h" /* for arptable */
|
||||||
#include "url.h"
|
#include "url.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Parse a URL string into its constituent parts.
|
* Parse a URL and deduce a struct protocol *, a struct sockaddr_in
|
||||||
|
* and a char *filename.
|
||||||
*
|
*
|
||||||
* We accept URLs of the form
|
* We accept URLs of the form
|
||||||
*
|
*
|
||||||
* [protocol://[host][:port]/]path/to/file
|
* [protocol://[host][:port]/]path/to/file
|
||||||
*
|
*
|
||||||
* The URL string will be modified by having NULs inserted after
|
* Returns 1 for success, 0 for failure (e.g. unknown protocol).
|
||||||
* "protocol", "host" and "port". The original URL can be
|
|
||||||
* reconstructed by calling unparse_url.
|
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
void parse_url ( struct url_info *info, char *url ) {
|
int parse_url ( char *url, struct protocol **proto,
|
||||||
|
struct sockaddr_in *server, char **filename ) {
|
||||||
char *p;
|
char *p;
|
||||||
|
char *protocol = NULL;
|
||||||
|
char *host = NULL;
|
||||||
|
char *port = NULL;
|
||||||
|
int rc = 0;
|
||||||
|
|
||||||
DBG ( "URL parsing \"%s\"\n", url );
|
DBG ( "URL parsing \"%s\"\n", url );
|
||||||
|
|
||||||
/* Zero the structure */
|
/* If no protocol is present, the whole URL will be a filename */
|
||||||
memset ( info, 0, sizeof ( *info ) );
|
*filename = url;
|
||||||
|
|
||||||
/* Search for a protocol delimiter */
|
/* Search for a protocol delimiter. If found, parse out the
|
||||||
|
* host and port parts of the URL, inserting NULs to terminate
|
||||||
|
* the different sections.
|
||||||
|
*/
|
||||||
for ( p = url ; *p ; p++ ) {
|
for ( p = url ; *p ; p++ ) {
|
||||||
if ( memcmp ( p, "://", 3 ) != 0 )
|
if ( memcmp ( p, "://", 3 ) != 0 )
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
/* URL has an explicit protocol */
|
/* URL has an explicit protocol */
|
||||||
info->protocol = url;
|
|
||||||
*p = '\0';
|
*p = '\0';
|
||||||
p += 3;
|
p += 3;
|
||||||
info->host = p;
|
protocol = url;
|
||||||
|
host = p;
|
||||||
|
|
||||||
/* Search for port or file delimiter */
|
/* Search for port and file delimiters */
|
||||||
for ( ; *p ; p++ ) {
|
for ( ; *p ; p++ ) {
|
||||||
if ( *p == ':' ) {
|
if ( *p == ':' ) {
|
||||||
*p = '\0';
|
*p = '\0';
|
||||||
info->port = p + 1;
|
port = p + 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if ( *p == '/' ) {
|
if ( *p == '/' ) {
|
||||||
|
@ -44,35 +53,46 @@ void parse_url ( struct url_info *info, char *url ) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
info->file = p;
|
*filename = p;
|
||||||
DBG ( "URL protocol \"%s\" host \"%s\" port \"%s\" "
|
|
||||||
"file \"%s\"\n", info->protocol, info->host,
|
break;
|
||||||
info->port ? info->port : "(NONE)", info->file );
|
}
|
||||||
return;
|
DBG ( "URL protocol \"%s\" host \"%s\" port \"%s\" file \"%s\"\n",
|
||||||
|
protocol ? protocol : "(default)", host ? host : "(default)",
|
||||||
|
port ? port : "(default)", *filename );
|
||||||
|
|
||||||
|
/* Identify the protocol */
|
||||||
|
*proto = identify_protocol ( protocol );
|
||||||
|
if ( ! *proto ) {
|
||||||
|
DBG ( "URL unknown protocol \"%s\"\n",
|
||||||
|
protocol ? protocol : "(default)" );
|
||||||
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* URL has no explicit protocol; is just a filename */
|
/* Identify the host */
|
||||||
info->file = url;
|
server->sin_addr = arptable[ARP_SERVER].ipaddr;
|
||||||
DBG ( "URL file \"%s\"\n", info->file );
|
if ( host && host[0] ) {
|
||||||
}
|
if ( ! resolv ( &server->sin_addr, host ) ) {
|
||||||
|
DBG ( "URL unknown host \"%s\"\n", host );
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/* Identify the port */
|
||||||
* Restore a parsed URL to its original pristine form.
|
server->sin_port = (*proto)->default_port;
|
||||||
*
|
if ( port && port[0] ) {
|
||||||
*/
|
server->sin_port = strtoul ( port, NULL, 10 );
|
||||||
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] = ':';
|
|
||||||
DBG ( "URL reconstructed \"%s\"\n", info->protocol );
|
rc = 1;
|
||||||
return info->protocol;
|
|
||||||
} else {
|
out:
|
||||||
/* URL had no protocol; was just a filename */
|
/* Fill back in the original URL */
|
||||||
DBG ( "URL reconstructed \"%s\"\n", info->file );
|
if ( protocol ) {
|
||||||
return info->file;
|
(*filename)[-1] = '/';
|
||||||
|
if ( port )
|
||||||
|
port[-1] = ':';
|
||||||
|
host[-3] = ':';
|
||||||
}
|
}
|
||||||
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,22 +1,10 @@
|
||||||
#ifndef URL_H
|
#ifndef URL_H
|
||||||
#define URL_H
|
#define URL_H
|
||||||
|
|
||||||
/*
|
#include "proto.h"
|
||||||
* Information parsed from a URL string. "char *" pointers will point
|
#include "in.h"
|
||||||
* to the start of the relevant portion of the original URL string,
|
|
||||||
* which will have been modified by inserting terminating NULs at the
|
|
||||||
* appropriate points. Use unparse_url() if you want to get back the
|
|
||||||
* original string.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
struct url_info {
|
|
||||||
char *protocol;
|
|
||||||
char *host;
|
|
||||||
char *port;
|
|
||||||
char *file;
|
|
||||||
};
|
|
||||||
|
|
||||||
extern void parse_url ( struct url_info *info, char *url );
|
extern int parse_url ( char *url, struct protocol **proto,
|
||||||
extern char * unparse_url ( struct url_info *info );
|
struct sockaddr_in *server, char **filename );
|
||||||
|
|
||||||
#endif /* URL_H */
|
#endif /* URL_H */
|
||||||
|
|
Loading…
Reference in New Issue