uri.h

Go to the documentation of this file.
00001 #ifndef _GPXE_URI_H
00002 #define _GPXE_URI_H
00003 
00004 /** @file
00005  *
00006  * Uniform Resource Identifiers
00007  *
00008  */
00009 
00010 FILE_LICENCE ( GPL2_OR_LATER );
00011 
00012 #include <stddef.h>
00013 #include <stdlib.h>
00014 #include <gpxe/refcnt.h>
00015 
00016 /** A Uniform Resource Identifier
00017  *
00018  * Terminology for this data structure is as per uri(7), except that
00019  * "path" is defined to include the leading '/' for an absolute path.
00020  *
00021  * Note that all fields within a URI are optional and may be NULL.
00022  *
00023  * The pointers to the various fields are packed together so they can
00024  * be accessed in array fashion in some places in uri.c where doing so
00025  * saves significant code size.
00026  *
00027  * Some examples are probably helpful:
00028  *
00029  *   http://www.etherboot.org/wiki :
00030  *
00031  *   scheme = "http", host = "www.etherboot.org", path = "/wiki"
00032  *
00033  *   /var/lib/tftpboot :
00034  *
00035  *   path = "/var/lib/tftpboot"
00036  *
00037  *   mailto:bob@nowhere.com :
00038  *
00039  *   scheme = "mailto", opaque = "bob@nowhere.com"
00040  *
00041  *   ftp://joe:secret@insecure.org:8081/hidden/path/to?what=is#this
00042  *
00043  *   scheme = "ftp", user = "joe", password = "secret",
00044  *   host = "insecure.org", port = "8081", path = "/hidden/path/to",
00045  *   query = "what=is", fragment = "this"
00046  */
00047 struct uri {
00048         /** Reference count */
00049         struct refcnt refcnt;
00050         /** Scheme */
00051         const char *scheme;
00052         /** Opaque part */
00053         const char *opaque;
00054         /** User name */
00055         const char *user;
00056         /** Password */
00057         const char *password;
00058         /** Host name */
00059         const char *host;
00060         /** Port number */
00061         const char *port;
00062         /** Path */
00063         const char *path;
00064         /** Query */
00065         const char *query;
00066         /** Fragment */
00067         const char *fragment;
00068 } __attribute__ (( packed ));
00069 
00070 /** A field in a URI
00071  *
00072  * The order of the indices in this enumeration must match the order
00073  * of the fields in the URI structure.
00074  */
00075 enum {
00076         URI_SCHEME = 0,         URI_SCHEME_BIT = ( 1 << URI_SCHEME ),
00077         URI_OPAQUE = 1,         URI_OPAQUE_BIT = ( 1 << URI_OPAQUE ),
00078         URI_USER = 2,           URI_USER_BIT = ( 1 << URI_USER ),
00079         URI_PASSWORD = 3,       URI_PASSWORD_BIT = ( 1 << URI_PASSWORD ),
00080         URI_HOST = 4,           URI_HOST_BIT = ( 1 << URI_HOST ),
00081         URI_PORT = 5,           URI_PORT_BIT = ( 1 << URI_PORT ),
00082         URI_PATH = 6,           URI_PATH_BIT = ( 1 << URI_PATH ),
00083         URI_QUERY = 7,          URI_QUERY_BIT = ( 1 << URI_QUERY ),
00084         URI_FRAGMENT = 8,       URI_FRAGMENT_BIT = ( 1 << URI_FRAGMENT ),
00085 
00086         URI_FIRST_FIELD = URI_SCHEME,
00087         URI_LAST_FIELD = URI_FRAGMENT,
00088 };
00089 
00090 /** Extract field from URI */
00091 #define uri_get_field( uri, field )     (&uri->scheme)[field]
00092 
00093 /** All URI fields */
00094 #define URI_ALL         ( URI_SCHEME_BIT | URI_OPAQUE_BIT | URI_USER_BIT | \
00095                           URI_PASSWORD_BIT | URI_HOST_BIT | URI_PORT_BIT | \
00096                           URI_PATH_BIT | URI_QUERY_BIT | URI_FRAGMENT_BIT )
00097 
00098 /** URI fields that should be decoded on storage */
00099 #define URI_ENCODED     ( URI_USER_BIT | URI_PASSWORD_BIT | URI_HOST_BIT | \
00100                           URI_PATH_BIT | URI_QUERY_BIT | URI_FRAGMENT_BIT )
00101 
00102 /**
00103  * URI is an absolute URI
00104  *
00105  * @v uri                       URI
00106  * @ret is_absolute             URI is absolute
00107  *
00108  * An absolute URI begins with a scheme, e.g. "http:" or "mailto:".
00109  * Note that this is a separate concept from a URI with an absolute
00110  * path.
00111  */
00112 static inline int uri_is_absolute ( struct uri *uri ) {
00113         return ( uri->scheme != NULL );
00114 }
00115 
00116 /**
00117  * URI has an absolute path
00118  *
00119  * @v uri                       URI
00120  * @ret has_absolute_path       URI has an absolute path
00121  *
00122  * An absolute path begins with a '/'.  Note that this is a separate
00123  * concept from an absolute URI.  Note also that a URI may not have a
00124  * path at all.
00125  */
00126 static inline int uri_has_absolute_path ( struct uri *uri ) {
00127         return ( uri->path && ( uri->path[0] == '/' ) );
00128 }
00129 
00130 /**
00131  * URI has a relative path
00132  *
00133  * @v uri                       URI
00134  * @ret has_relative_path       URI has a relative path
00135  *
00136  * A relative path begins with something other than a '/'.  Note that
00137  * this is a separate concept from a relative URI.  Note also that a
00138  * URI may not have a path at all.
00139  */
00140 static inline int uri_has_relative_path ( struct uri *uri ) {
00141         return ( uri->path && ( uri->path[0] != '/' ) );
00142 }
00143 
00144 /**
00145  * Increment URI reference count
00146  *
00147  * @v uri               URI, or NULL
00148  * @ret uri             URI as passed in
00149  */
00150 static inline __attribute__ (( always_inline )) struct uri *
00151 uri_get ( struct uri *uri ) {
00152         ref_get ( &uri->refcnt );
00153         return uri;
00154 }
00155 
00156 /**
00157  * Decrement URI reference count
00158  *
00159  * @v uri               URI, or NULL
00160  */
00161 static inline __attribute__ (( always_inline )) void
00162 uri_put ( struct uri *uri ) {
00163         ref_put ( &uri->refcnt );
00164 }
00165 
00166 extern struct uri *cwuri;
00167 
00168 extern struct uri * parse_uri ( const char *uri_string );
00169 extern unsigned int uri_port ( struct uri *uri, unsigned int default_port );
00170 extern int unparse_uri ( char *buf, size_t size, struct uri *uri,
00171                          unsigned int fields );
00172 extern struct uri * uri_dup ( struct uri *uri );
00173 extern char * resolve_path ( const char *base_path,
00174                              const char *relative_path );
00175 extern struct uri * resolve_uri ( struct uri *base_uri,
00176                                   struct uri *relative_uri );
00177 extern void churi ( struct uri *uri );
00178 extern size_t uri_encode ( const char *raw_string, char *buf, ssize_t len,
00179                            int field );
00180 extern size_t uri_decode ( const char *encoded_string, char *buf, ssize_t len );
00181 
00182 #endif /* _GPXE_URI_H */

Generated on Tue Apr 6 20:01:09 2010 for gPXE by  doxygen 1.5.7.1