6 * Uniform Resource Identifiers
10 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
14 #include <ipxe/refcnt.h>
19 /** A Uniform Resource Identifier
21 * Terminology for this data structure is as per uri(7), except that
22 * "path" is defined to include the leading '/' for an absolute path.
24 * Note that all fields within a URI are optional and may be NULL.
26 * The pointers to the various fields are packed together so they can
27 * be accessed in array fashion in some places in uri.c where doing so
28 * saves significant code size.
30 * Some examples are probably helpful:
32 * http://www.ipxe.org/wiki :
34 * scheme = "http", host = "www.ipxe.org", path = "/wiki"
38 * path = "/var/lib/tftpboot"
40 * mailto:bob@nowhere.com :
42 * scheme = "mailto", opaque = "bob@nowhere.com"
44 * ftp://joe:secret@insecure.org:8081/hidden/path/to?what=is#this
46 * scheme = "ftp", user = "joe", password = "secret",
47 * host = "insecure.org", port = "8081", path = "/hidden/path/to",
48 * query = "what=is", fragment = "this"
51 /** Reference count */
71 /** Form parameters */
72 struct parameters *params;
73 } __attribute__ (( packed ));
79 * @v field URI field index
80 * @ret field URI field (as an lvalue)
82 #define uri_field( uri, field ) (&uri->scheme)[field]
85 * Calculate index of a URI field
87 * @v name URI field name
88 * @ret field URI field index
90 #define URI_FIELD( name ) \
91 ( ( offsetof ( struct uri, name ) - \
92 offsetof ( struct uri, scheme ) ) / sizeof ( void * ) )
96 URI_SCHEME = URI_FIELD ( scheme ),
97 URI_OPAQUE = URI_FIELD ( opaque ),
98 URI_USER = URI_FIELD ( user ),
99 URI_PASSWORD = URI_FIELD ( password ),
100 URI_HOST = URI_FIELD ( host ),
101 URI_PORT = URI_FIELD ( port ),
102 URI_PATH = URI_FIELD ( path ),
103 URI_QUERY = URI_FIELD ( query ),
104 URI_FRAGMENT = URI_FIELD ( fragment ),
109 * URI is an absolute URI
112 * @ret is_absolute URI is absolute
114 * An absolute URI begins with a scheme, e.g. "http:" or "mailto:".
115 * Note that this is a separate concept from a URI with an absolute
118 static inline int uri_is_absolute ( const struct uri *uri ) {
119 return ( uri->scheme != NULL );
123 * URI has an opaque part
126 * @ret has_opaque URI has an opaque part
128 static inline int uri_has_opaque ( const struct uri *uri ) {
129 return ( uri->opaque && ( uri->opaque[0] != '\0' ) );
136 * @ret has_path URI has a path
138 static inline int uri_has_path ( const struct uri *uri ) {
139 return ( uri->path && ( uri->path[0] != '\0' ) );
143 * URI has an absolute path
146 * @ret has_absolute_path URI has an absolute path
148 * An absolute path begins with a '/'. Note that this is a separate
149 * concept from an absolute URI. Note also that a URI may not have a
152 static inline int uri_has_absolute_path ( const struct uri *uri ) {
153 return ( uri->path && ( uri->path[0] == '/' ) );
157 * URI has a relative path
160 * @ret has_relative_path URI has a relative path
162 * A relative path begins with something other than a '/'. Note that
163 * this is a separate concept from a relative URI. Note also that a
164 * URI may not have a path at all.
166 static inline int uri_has_relative_path ( const struct uri *uri ) {
167 return ( uri->path && ( uri->path[0] != '/' ) );
171 * Increment URI reference count
173 * @v uri URI, or NULL
174 * @ret uri URI as passed in
176 static inline __attribute__ (( always_inline )) struct uri *
177 uri_get ( struct uri *uri ) {
178 ref_get ( &uri->refcnt );
183 * Decrement URI reference count
185 * @v uri URI, or NULL
187 static inline __attribute__ (( always_inline )) void
188 uri_put ( struct uri *uri ) {
189 ref_put ( &uri->refcnt );
192 extern struct uri *cwuri;
194 extern size_t uri_encode ( const char *string, unsigned int field,
195 char *buf, ssize_t len );
196 extern struct uri * parse_uri ( const char *uri_string );
197 extern size_t format_uri ( const struct uri *uri, char *buf, size_t len );
198 extern char * format_uri_alloc ( const struct uri *uri );
199 extern unsigned int uri_port ( const struct uri *uri,
200 unsigned int default_port );
201 extern struct uri * uri_dup ( const struct uri *uri );
202 extern char * resolve_path ( const char *base_path,
203 const char *relative_path );
204 extern struct uri * resolve_uri ( const struct uri *base_uri,
205 struct uri *relative_uri );
206 extern struct uri * tftp_uri ( struct in_addr next_server, unsigned int port,
207 const char *filename );
208 extern void churi ( struct uri *uri );
210 #endif /* _IPXE_URI_H */