iPXE
uri.h
Go to the documentation of this file.
1 #ifndef _IPXE_URI_H
2 #define _IPXE_URI_H
3 
4 /** @file
5  *
6  * Uniform Resource Identifiers
7  *
8  */
9 
10 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
11 FILE_SECBOOT ( PERMITTED );
12 
13 #include <stddef.h>
14 #include <stdlib.h>
15 #include <ipxe/refcnt.h>
16 #include <ipxe/in.h>
17 
18 struct parameters;
19 
20 /** A Uniform Resource Identifier
21  *
22  * Terminology for this data structure is as per uri(7), except that
23  * "path" is defined to include the leading '/' for an absolute path.
24  *
25  * Note that all fields within a URI are optional and may be NULL.
26  *
27  * The pointers to the various fields are packed together so they can
28  * be accessed in array fashion in some places in uri.c where doing so
29  * saves significant code size.
30  *
31  * Some examples are probably helpful:
32  *
33  * http://www.ipxe.org/wiki :
34  *
35  * scheme = "http", host = "www.ipxe.org", path = "/wiki"
36  *
37  * /var/lib/tftpboot :
38  *
39  * path = "/var/lib/tftpboot"
40  *
41  * mailto:bob@nowhere.com :
42  *
43  * scheme = "mailto", opaque = "bob@nowhere.com"
44  *
45  * ftp://joe:secret@insecure.org:8081/hidden/path/to?what=is#this
46  *
47  * scheme = "ftp", user = "joe", password = "secret",
48  * host = "insecure.org", port = "8081", path = "/hidden/path/to",
49  * query = "what=is", fragment = "this"
50  *
51  * The URI syntax includes a percent-encoding mechanism that can be
52  * used to represent characters that would otherwise not be possible,
53  * such as a '/' character within the password field. These encodings
54  * are decoded during the URI parsing stage, thereby allowing protocol
55  * implementations to consume the raw field values directly without
56  * further decoding.
57  *
58  * Some protocols (such as HTTP) communicate using URI-encoded values.
59  * For these protocols, the original encoded substring must be
60  * retained verbatim since the choice of whether or not to encode a
61  * particular character may have significance to the receiving
62  * application. We therefore retain the originally-encoded substrings
63  * for the path, query, and fragment fields.
64  */
65 struct uri {
66  /** Reference count */
67  struct refcnt refcnt;
68  /** Scheme */
69  const char *scheme;
70  /** Opaque part */
71  const char *opaque;
72  /** User name */
73  const char *user;
74  /** Password */
75  const char *password;
76  /** Host name */
77  const char *host;
78  /** Port number */
79  const char *port;
80  /** Path (after URI decoding) */
81  const char *path;
82  /** Path (with original URI encoding) */
83  const char *epath;
84  /** Query (with original URI encoding) */
85  const char *equery;
86  /** Fragment (with original URI encoding) */
87  const char *efragment;
88  /** Request parameters */
89  struct parameters *params;
90 } __attribute__ (( packed ));
91 
92 /**
93  * Access URI field
94  *
95  * @v uri URI
96  * @v field URI field index
97  * @ret field URI field (as an lvalue)
98  */
99 #define uri_field( uri, field ) (&uri->scheme)[field]
100 
101 /**
102  * Calculate index of a URI field
103  *
104  * @v name URI field name
105  * @ret field URI field index
106  */
107 #define URI_FIELD( name ) \
108  ( ( offsetof ( struct uri, name ) - \
109  offsetof ( struct uri, scheme ) ) / sizeof ( void * ) )
110 
111 /** URI fields */
113  URI_SCHEME = URI_FIELD ( scheme ),
114  URI_OPAQUE = URI_FIELD ( opaque ),
115  URI_USER = URI_FIELD ( user ),
117  URI_HOST = URI_FIELD ( host ),
119  URI_PATH = URI_FIELD ( path ),
120  URI_EPATH = URI_FIELD ( epath ),
121  URI_EQUERY = URI_FIELD ( equery ),
122  URI_EFRAGMENT = URI_FIELD ( efragment ),
124 };
125 
126 /**
127  * URI is an absolute URI
128  *
129  * @v uri URI
130  * @ret is_absolute URI is absolute
131  *
132  * An absolute URI begins with a scheme, e.g. "http:" or "mailto:".
133  * Note that this is a separate concept from a URI with an absolute
134  * path.
135  */
136 static inline int uri_is_absolute ( const struct uri *uri ) {
137  return ( uri->scheme != NULL );
138 }
139 
140 /**
141  * URI has an opaque part
142  *
143  * @v uri URI
144  * @ret has_opaque URI has an opaque part
145  */
146 static inline int uri_has_opaque ( const struct uri *uri ) {
147  return ( uri->opaque && ( uri->opaque[0] != '\0' ) );
148 }
149 
150 /**
151  * URI has a path
152  *
153  * @v uri URI
154  * @ret has_path URI has a path
155  */
156 static inline int uri_has_path ( const struct uri *uri ) {
157  return ( uri->path && ( uri->path[0] != '\0' ) );
158 }
159 
160 /**
161  * URI has an absolute path
162  *
163  * @v uri URI
164  * @ret has_absolute_path URI has an absolute path
165  *
166  * An absolute path begins with a '/'. Note that this is a separate
167  * concept from an absolute URI. Note also that a URI may not have a
168  * path at all.
169  */
170 static inline int uri_has_absolute_path ( const struct uri *uri ) {
171  return ( uri->path && ( uri->path[0] == '/' ) );
172 }
173 
174 /**
175  * URI has a relative path
176  *
177  * @v uri URI
178  * @ret has_relative_path URI has a relative path
179  *
180  * A relative path begins with something other than a '/'. Note that
181  * this is a separate concept from a relative URI. Note also that a
182  * URI may not have a path at all.
183  */
184 static inline int uri_has_relative_path ( const struct uri *uri ) {
185  return ( uri->path && ( uri->path[0] != '/' ) );
186 }
187 
188 /**
189  * Increment URI reference count
190  *
191  * @v uri URI, or NULL
192  * @ret uri URI as passed in
193  */
194 static inline __attribute__ (( always_inline )) struct uri *
195 uri_get ( struct uri *uri ) {
196  ref_get ( &uri->refcnt );
197  return uri;
198 }
199 
200 /**
201  * Decrement URI reference count
202  *
203  * @v uri URI, or NULL
204  */
205 static inline __attribute__ (( always_inline )) void
206 uri_put ( struct uri *uri ) {
207  ref_put ( &uri->refcnt );
208 }
209 
210 extern struct uri *cwuri;
211 
212 extern size_t uri_decode ( const char *encoded, void *buf, size_t len );
213 extern size_t uri_encode ( unsigned int field, const void *raw, size_t raw_len,
214  char *buf, ssize_t len );
215 extern size_t uri_encode_string ( unsigned int field, const char *string,
216  char *buf, ssize_t len );
217 extern struct uri * parse_uri ( const char *uri_string );
218 extern size_t format_uri ( const struct uri *uri, char *buf, size_t len );
219 extern char * format_uri_alloc ( const struct uri *uri );
220 extern unsigned int uri_port ( const struct uri *uri,
221  unsigned int default_port );
222 extern struct uri * uri_dup ( const struct uri *uri );
223 extern char * resolve_path ( const char *base_path,
224  const char *relative_path );
225 extern struct uri * resolve_uri ( const struct uri *base_uri,
226  struct uri *relative_uri );
227 extern struct uri * pxe_uri ( struct sockaddr *sa_server,
228  const char *filename );
229 extern void churi ( struct uri *uri );
230 
231 #endif /* _IPXE_URI_H */
Definition: uri.h:117
#define __attribute__(x)
Definition: compiler.h:10
const char * equery
Query (with original URI encoding)
Definition: uri.h:85
struct uri * cwuri
Current working URI.
Definition: cwuri.c:39
static void uri_put(struct uri *uri)
Decrement URI reference count.
Definition: uri.h:206
static struct uri * uri_get(struct uri *uri)
Increment URI reference count.
Definition: uri.h:195
static int uri_is_absolute(const struct uri *uri)
URI is an absolute URI.
Definition: uri.h:136
#define URI_FIELD(name)
Calculate index of a URI field.
Definition: uri.h:107
static int uri_has_path(const struct uri *uri)
URI has a path.
Definition: uri.h:156
static int uri_has_relative_path(const struct uri *uri)
URI has a relative path.
Definition: uri.h:184
A request parameter list.
Definition: params.h:17
struct uri * parse_uri(const char *uri_string)
Parse URI.
Definition: uri.c:297
Definition: uri.h:118
size_t uri_decode(const char *encoded, void *buf, size_t len)
Decode URI field.
Definition: uri.c:54
A reference counter.
Definition: refcnt.h:27
const char * port
Port number.
Definition: uri.h:79
struct uri * resolve_uri(const struct uri *base_uri, struct uri *relative_uri)
Resolve base+relative URI.
Definition: uri.c:695
const char * scheme
Scheme.
Definition: uri.h:69
Definition: uri.h:119
u8 port
Port number.
Definition: CIB_PRM.h:31
char * resolve_path(const char *base_path, const char *relative_path)
Resolve base+relative path.
Definition: uri.c:633
ring len
Length.
Definition: dwmac.h:231
const char * path
Path (after URI decoding)
Definition: uri.h:81
uri_fields
URI fields.
Definition: uri.h:112
struct parameters * params
Request parameters.
Definition: uri.h:89
static size_t raw_len
Definition: base16.h:54
Generalized socket address structure.
Definition: socket.h:97
#define ref_get(refcnt)
Get additional reference to object.
Definition: refcnt.h:93
size_t format_uri(const struct uri *uri, char *buf, size_t len)
Format URI.
Definition: uri.c:472
FILE_SECBOOT(PERMITTED)
unsigned int uri_port(const struct uri *uri, unsigned int default_port)
Get port from URI.
Definition: uri.c:456
Definition: uri.h:115
static struct dynamic_item password
Definition: login_ui.c:37
const char * host
Host name.
Definition: uri.h:77
void churi(struct uri *uri)
Change working URI.
Definition: cwuri.c:46
Definition: uri.h:120
size_t uri_encode_string(unsigned int field, const char *string, char *buf, ssize_t len)
Encode URI field string.
Definition: uri.c:236
const char * efragment
Fragment (with original URI encoding)
Definition: uri.h:87
static int uri_has_absolute_path(const struct uri *uri)
URI has an absolute path.
Definition: uri.h:170
const char * opaque
Opaque part.
Definition: uri.h:71
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
struct refcnt refcnt
Reference count.
Definition: uri.h:67
struct uri * uri_dup(const struct uri *uri)
Duplicate URI.
Definition: uri.c:595
Reference counting.
const char * password
Password.
Definition: uri.h:75
const char * epath
Path (with original URI encoding)
Definition: uri.h:83
__be32 raw[7]
Definition: CIB_PRM.h:28
const char * user
User name.
Definition: uri.h:73
A Uniform Resource Identifier.
Definition: uri.h:65
signed long ssize_t
Definition: stdint.h:7
struct uri * pxe_uri(struct sockaddr *sa_server, const char *filename)
Construct URI from server address and filename.
Definition: uri.c:809
char * format_uri_alloc(const struct uri *uri)
Format URI.
Definition: uri.c:541
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
static int uri_has_opaque(const struct uri *uri)
URI has an opaque part.
Definition: uri.h:146
size_t uri_encode(unsigned int field, const void *raw, size_t raw_len, char *buf, ssize_t len)
Encode URI field.
Definition: uri.c:201
#define ref_put(refcnt)
Drop reference to object.
Definition: refcnt.h:107