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
10FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
11FILE_SECBOOT ( PERMITTED );
12
13#include <stddef.h>
14#include <stdlib.h>
15#include <ipxe/refcnt.h>
16#include <ipxe/in.h>
17
18struct 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 */
65struct 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 */
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 */
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 */
136static 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 */
146static 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 */
156static 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 */
170static 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 */
184static 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 */
194static inline __attribute__ (( always_inline )) struct uri *
195uri_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 */
205static inline __attribute__ (( always_inline )) void
206uri_put ( struct uri *uri ) {
207 ref_put ( &uri->refcnt );
208}
209
210extern struct uri *cwuri;
211
212extern size_t uri_decode ( const char *encoded, void *buf, size_t len );
213extern size_t uri_encode ( unsigned int field, const void *raw, size_t raw_len,
214 char *buf, ssize_t len );
215extern size_t uri_encode_string ( unsigned int field, const char *string,
216 char *buf, ssize_t len );
217extern struct uri * parse_uri ( const char *uri_string );
218extern size_t format_uri ( const struct uri *uri, char *buf, size_t len );
219extern char * format_uri_alloc ( const struct uri *uri );
220extern unsigned int uri_port ( const struct uri *uri,
221 unsigned int default_port );
222extern struct uri * uri_dup ( const struct uri *uri );
223extern char * resolve_path ( const char *base_path,
224 const char *relative_path );
225extern struct uri * resolve_uri ( const struct uri *base_uri,
226 struct uri *relative_uri );
227extern struct uri * pxe_uri ( struct sockaddr *sa_server,
228 const char *filename );
229extern void churi ( struct uri *uri );
230
231#endif /* _IPXE_URI_H */
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
u8 port
Port number.
Definition CIB_PRM.h:3
__be32 raw[7]
Definition CIB_PRM.h:0
signed long ssize_t
Definition stdint.h:7
static size_t raw_len
Definition base16.h:54
struct uri * cwuri
Current working URI.
Definition cwuri.c:39
ring len
Length.
Definition dwmac.h:226
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:926
#define __attribute__(x)
Definition compiler.h:10
static struct dynamic_item password
Definition login_ui.c:37
Reference counting.
#define ref_get(refcnt)
Get additional reference to object.
Definition refcnt.h:93
#define ref_put(refcnt)
Drop reference to object.
Definition refcnt.h:107
A request parameter list.
Definition params.h:17
Generalized socket address structure.
Definition socket.h:97
A Uniform Resource Identifier.
Definition uri.h:65
const char * epath
Path (with original URI encoding)
Definition uri.h:83
const char * path
Path (after URI decoding)
Definition uri.h:81
const char * user
User name.
Definition uri.h:73
struct parameters * params
Request parameters.
Definition uri.h:89
const char * host
Host name.
Definition uri.h:77
const char * password
Password.
Definition uri.h:75
struct refcnt refcnt
Reference count.
Definition uri.h:67
const char * efragment
Fragment (with original URI encoding)
Definition uri.h:87
const char * scheme
Scheme.
Definition uri.h:69
const char * equery
Query (with original URI encoding)
Definition uri.h:85
const char * port
Port number.
Definition uri.h:79
const char * opaque
Opaque part.
Definition uri.h:71
static int uri_is_absolute(const struct uri *uri)
URI is an absolute URI.
Definition uri.h:136
size_t uri_decode(const char *encoded, void *buf, size_t len)
Decode URI field.
Definition uri.c:54
uri_fields
URI fields.
Definition uri.h:112
@ URI_PATH
Definition uri.h:119
@ URI_FIELDS
Definition uri.h:123
@ URI_EFRAGMENT
Definition uri.h:122
@ URI_EPATH
Definition uri.h:120
@ URI_SCHEME
Definition uri.h:113
@ URI_PORT
Definition uri.h:118
@ URI_EQUERY
Definition uri.h:121
@ URI_USER
Definition uri.h:115
@ URI_HOST
Definition uri.h:117
@ URI_OPAQUE
Definition uri.h:114
@ URI_PASSWORD
Definition uri.h:116
size_t format_uri(const struct uri *uri, char *buf, size_t len)
Format URI.
Definition uri.c:473
static int uri_has_relative_path(const struct uri *uri)
URI has a relative path.
Definition uri.h:184
static struct uri * uri_get(struct uri *uri)
Increment URI reference count.
Definition uri.h:195
static int uri_has_absolute_path(const struct uri *uri)
URI has an absolute path.
Definition uri.h:170
struct uri * uri_dup(const struct uri *uri)
Duplicate URI.
Definition uri.c:596
char * resolve_path(const char *base_path, const char *relative_path)
Resolve base+relative path.
Definition uri.c:634
unsigned int uri_port(const struct uri *uri, unsigned int default_port)
Get port from URI.
Definition uri.c:457
static int uri_has_path(const struct uri *uri)
URI has a path.
Definition uri.h:156
size_t uri_encode_string(unsigned int field, const char *string, char *buf, ssize_t len)
Encode URI field string.
Definition uri.c:236
struct uri * parse_uri(const char *uri_string)
Parse URI.
Definition uri.c:297
static int uri_has_opaque(const struct uri *uri)
URI has an opaque part.
Definition uri.h:146
static void uri_put(struct uri *uri)
Decrement URI reference count.
Definition uri.h:206
struct uri * resolve_uri(const struct uri *base_uri, struct uri *relative_uri)
Resolve base+relative URI.
Definition uri.c:696
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 URI_FIELD(name)
Calculate index of a URI field.
Definition uri.h:107
void churi(struct uri *uri)
Change working URI.
Definition cwuri.c:46
char * format_uri_alloc(const struct uri *uri)
Format URI.
Definition uri.c:542
struct uri * pxe_uri(struct sockaddr *sa_server, const char *filename)
Construct URI from server address and filename.
Definition uri.c:810