david/ipxe
Archived
1
0

Add concept of "current working URI".

This commit is contained in:
Michael Brown 2007-06-11 23:54:51 +01:00
parent 2d4c72b762
commit 15dae1e042
3 changed files with 61 additions and 5 deletions

42
src/core/cwuri.c Normal file
View File

@ -0,0 +1,42 @@
/*
* Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
*
* 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 <stddef.h>
#include <gpxe/uri.h>
/** @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 );
}

View File

@ -35,6 +35,8 @@
* @v uri URI * @v uri URI
*/ */
static void dump_uri ( struct uri *uri ) { static void dump_uri ( struct uri *uri ) {
if ( ! uri )
return;
if ( uri->scheme ) if ( uri->scheme )
DBG ( " scheme \"%s\"", uri->scheme ); DBG ( " scheme \"%s\"", uri->scheme );
if ( uri->opaque ) if ( uri->opaque )
@ -174,12 +176,14 @@ struct uri * parse_uri ( const char *uri_string ) {
/** /**
* Get port from URI * Get port from URI
* *
* @v uri URI * @v uri URI, or NULL
* @v default_port Default port to use if none specified in URI * @v default_port Default port to use if none specified in URI
* @ret port Port * @ret port Port
*/ */
unsigned int uri_port ( struct uri *uri, unsigned int default_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 buf Buffer to fill with URI string
* @v size Size of buffer * @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 * @ret len Length of URI string
*/ */
int unparse_uri ( char *buf, size_t size, struct uri *uri ) { 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 ); dump_uri ( uri );
DBG ( "\n" ); DBG ( "\n" );
/* Special-case NULL URI */
if ( ! uri ) {
if ( size )
buf[0] = '\0';
return 0;
}
/* Special-case opaque URIs */ /* Special-case opaque URIs */
if ( uri->opaque ) { if ( uri->opaque ) {
return ssnprintf ( ( buf + used ), ( size - used ), return ssnprintf ( ( buf + used ), ( size - used ),
@ -332,7 +343,7 @@ char * resolve_path ( const char *base_path,
/** /**
* Resolve base+relative URI * Resolve base+relative URI
* *
* @v base_uri Base URI * @v base_uri Base URI, or NULL
* @v relative_uri Relative URI * @v relative_uri Relative URI
* @ret resolved_uri Resolved URI * @ret resolved_uri Resolved URI
* *
@ -347,7 +358,7 @@ struct uri * resolve_uri ( struct uri *base_uri,
struct uri *new_uri; struct uri *new_uri;
/* If relative URI is absolute, just re-use it */ /* 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 ); return uri_get ( relative_uri );
/* Mangle URI */ /* Mangle URI */

View File

@ -124,6 +124,8 @@ uri_put ( struct uri *uri ) {
ref_put ( &uri->refcnt ); ref_put ( &uri->refcnt );
} }
extern struct uri *cwuri;
extern struct uri * parse_uri ( const char *uri_string ); extern struct uri * parse_uri ( const char *uri_string );
extern unsigned int uri_port ( struct uri *uri, unsigned int default_port ); extern unsigned int uri_port ( struct uri *uri, unsigned int default_port );
extern int unparse_uri ( char *buf, size_t size, struct uri *uri ); 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 ); const char *relative_path );
extern struct uri * resolve_uri ( struct uri *base_uri, extern struct uri * resolve_uri ( struct uri *base_uri,
struct uri *relative_uri ); struct uri *relative_uri );
extern void churi ( struct uri *uri );
#endif /* _GPXE_URI_H */ #endif /* _GPXE_URI_H */