diff --git a/src/core/cwuri.c b/src/core/cwuri.c new file mode 100644 index 00000000..cf5e90f8 --- /dev/null +++ b/src/core/cwuri.c @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2007 Michael Brown . + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include +#include + +/** @file + * + * Current working URI + * + * Somewhat analogous to the current working directory in a POSIX + * system. + */ + +/** Current working URI */ +struct uri *cwuri = NULL; + +/** + * Change working URI + * + * @v uri New working URI + */ +void churi ( struct uri *uri ) { + if ( cwuri ) + uri_put ( cwuri ); + cwuri = uri_get ( uri ); +} diff --git a/src/core/uri.c b/src/core/uri.c index a793c457..8cb855a6 100644 --- a/src/core/uri.c +++ b/src/core/uri.c @@ -35,6 +35,8 @@ * @v uri URI */ static void dump_uri ( struct uri *uri ) { + if ( ! uri ) + return; if ( uri->scheme ) DBG ( " scheme \"%s\"", uri->scheme ); if ( uri->opaque ) @@ -174,12 +176,14 @@ struct uri * parse_uri ( const char *uri_string ) { /** * Get port from URI * - * @v uri URI + * @v uri URI, or NULL * @v default_port Default port to use if none specified in URI * @ret port Port */ unsigned int uri_port ( struct uri *uri, unsigned int default_port ) { - return ( uri->port ? strtoul ( uri->port, NULL, 0 ) : default_port ); + if ( ( ! uri ) || ( ! uri->port ) ) + return default_port; + return ( strtoul ( uri->port, NULL, 0 ) ); } /** @@ -187,7 +191,7 @@ unsigned int uri_port ( struct uri *uri, unsigned int default_port ) { * * @v buf Buffer to fill with URI string * @v size Size of buffer - * @v uri URI to write into buffer + * @v uri URI to write into buffer, or NULL * @ret len Length of URI string */ int unparse_uri ( char *buf, size_t size, struct uri *uri ) { @@ -197,6 +201,13 @@ int unparse_uri ( char *buf, size_t size, struct uri *uri ) { dump_uri ( uri ); DBG ( "\n" ); + /* Special-case NULL URI */ + if ( ! uri ) { + if ( size ) + buf[0] = '\0'; + return 0; + } + /* Special-case opaque URIs */ if ( uri->opaque ) { return ssnprintf ( ( buf + used ), ( size - used ), @@ -332,7 +343,7 @@ char * resolve_path ( const char *base_path, /** * Resolve base+relative URI * - * @v base_uri Base URI + * @v base_uri Base URI, or NULL * @v relative_uri Relative URI * @ret resolved_uri Resolved URI * @@ -347,7 +358,7 @@ struct uri * resolve_uri ( struct uri *base_uri, struct uri *new_uri; /* If relative URI is absolute, just re-use it */ - if ( uri_is_absolute ( relative_uri ) ) + if ( uri_is_absolute ( relative_uri ) || ( ! base_uri ) ) return uri_get ( relative_uri ); /* Mangle URI */ diff --git a/src/include/gpxe/uri.h b/src/include/gpxe/uri.h index f6162382..b1389316 100644 --- a/src/include/gpxe/uri.h +++ b/src/include/gpxe/uri.h @@ -124,6 +124,8 @@ uri_put ( struct uri *uri ) { ref_put ( &uri->refcnt ); } +extern struct uri *cwuri; + extern struct uri * parse_uri ( const char *uri_string ); extern unsigned int uri_port ( struct uri *uri, unsigned int default_port ); extern int unparse_uri ( char *buf, size_t size, struct uri *uri ); @@ -132,5 +134,6 @@ extern char * resolve_path ( const char *base_path, const char *relative_path ); extern struct uri * resolve_uri ( struct uri *base_uri, struct uri *relative_uri ); +extern void churi ( struct uri *uri ); #endif /* _GPXE_URI_H */