2 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * Uniform Resource Identifiers
29 #include <gpxe/vsprintf.h>
33 * Dump URI for debugging
37 static void dump_uri ( struct uri
*uri
) {
41 DBG ( " scheme \"%s\"", uri
->scheme
);
43 DBG ( " opaque \"%s\"", uri
->opaque
);
45 DBG ( " user \"%s\"", uri
->user
);
47 DBG ( " password \"%s\"", uri
->password
);
49 DBG ( " host \"%s\"", uri
->host
);
51 DBG ( " port \"%s\"", uri
->port
);
53 DBG ( " path \"%s\"", uri
->path
);
55 DBG ( " query \"%s\"", uri
->query
);
57 DBG ( " fragment \"%s\"", uri
->fragment
);
63 * @v uri_string URI as a string
66 * Splits a URI into its component parts. The return URI structure is
67 * dynamically allocated and must eventually be freed by calling
70 struct uri
* parse_uri ( const char *uri_string
) {
75 char *authority
= NULL
;
78 /* Allocate space for URI struct and a copy of the string */
79 raw_len
= ( strlen ( uri_string
) + 1 /* NUL */ );
80 uri
= zalloc ( sizeof ( *uri
) + raw_len
);
83 raw
= ( ( ( char * ) uri
) + sizeof ( *uri
) );
85 /* Zero URI struct and copy in the raw string */
86 memcpy ( raw
, uri_string
, raw_len
);
88 /* Start by chopping off the fragment, if it exists */
89 if ( ( tmp
= strchr ( raw
, '#' ) ) ) {
94 /* Identify absolute/relative URI */
95 if ( ( tmp
= strchr ( raw
, ':' ) ) ) {
96 /* Absolute URI: identify hierarchical/opaque */
100 /* Absolute URI with hierarchical part */
103 /* Absolute URI with opaque part */
111 /* If we don't have a path (i.e. we have an absolute URI with
112 * an opaque portion, we're already finished processing
117 /* Chop off the query, if it exists */
118 if ( ( tmp
= strchr ( path
, '?' ) ) ) {
123 /* Identify net/absolute/relative path */
124 if ( strncmp ( path
, "//", 2 ) == 0 ) {
125 /* Net path. If this is terminated by the first '/'
126 * of an absolute path, then we have no space for a
127 * terminator after the authority field, so shuffle
128 * the authority down by one byte, overwriting one of
131 authority
= ( path
+ 2 );
132 if ( ( tmp
= strchr ( authority
, '/' ) ) ) {
135 memmove ( ( authority
- 1 ), authority
,
136 ( tmp
- authority
) );
141 /* Absolute/relative path */
145 /* Split authority into user[:password] and host[:port] portions */
146 if ( ( tmp
= strchr ( authority
, '@' ) ) ) {
147 /* Has user[:password] */
150 uri
->user
= authority
;
151 if ( ( tmp
= strchr ( authority
, ':' ) ) ) {
157 /* No user:password */
158 uri
->host
= authority
;
161 /* Split host into host[:port] */
162 if ( ( tmp
= strchr ( uri
->host
, ':' ) ) ) {
168 DBG ( "URI \"%s\" split into", uri_string
);
178 * @v uri URI, or NULL
179 * @v default_port Default port to use if none specified in URI
182 unsigned int uri_port ( struct uri
*uri
, unsigned int default_port
) {
183 if ( ( ! uri
) || ( ! uri
->port
) )
185 return ( strtoul ( uri
->port
, NULL
, 0 ) );
191 * @v buf Buffer to fill with URI string
192 * @v size Size of buffer
193 * @v uri URI to write into buffer, or NULL
194 * @ret len Length of URI string
196 int unparse_uri ( char *buf
, size_t size
, struct uri
*uri
) {
199 DBG ( "URI unparsing" );
203 /* Special-case NULL URI */
210 /* Special-case opaque URIs */
212 return ssnprintf ( ( buf
+ used
), ( size
- used
),
213 "%s:%s", uri
->scheme
, uri
->opaque
);
218 used
+= ssnprintf ( ( buf
+ used
), ( size
- used
),
219 "%s://", uri
->scheme
);
222 /* [user[:password]@]host[:port] */
225 used
+= ssnprintf ( ( buf
+ used
), ( size
- used
),
227 if ( uri
->password
) {
228 used
+= ssnprintf ( ( buf
+ used
),
230 ":%s", uri
->password
);
232 used
+= ssnprintf ( ( buf
+ used
), ( size
- used
),
235 used
+= ssnprintf ( ( buf
+ used
), ( size
- used
), "%s",
238 used
+= ssnprintf ( ( buf
+ used
), ( size
- used
),
245 used
+= ssnprintf ( ( buf
+ used
), ( size
- used
),
251 used
+= ssnprintf ( ( buf
+ used
), ( size
- used
),
256 if ( uri
->fragment
) {
257 used
+= ssnprintf ( ( buf
+ used
), ( size
- used
),
258 "#%s", uri
->fragment
);
268 * @ret uri Duplicate URI
270 * Creates a modifiable copy of a URI.
272 struct uri
* uri_dup ( struct uri
*uri
) {
273 size_t len
= ( unparse_uri ( NULL
, 0, uri
) + 1 );
276 unparse_uri ( buf
, len
, uri
);
277 return parse_uri ( buf
);
281 * Resolve base+relative path
283 * @v base_uri Base path
284 * @v relative_uri Relative path
285 * @ret resolved_uri Resolved path
287 * Takes a base path (e.g. "/var/lib/tftpboot/vmlinuz" and a relative
288 * path (e.g. "initrd.gz") and produces a new path
289 * (e.g. "/var/lib/tftpboot/initrd.gz"). Note that any non-directory
290 * portion of the base path will automatically be stripped; this
291 * matches the semantics used when resolving the path component of
294 char * resolve_path ( const char *base_path
,
295 const char *relative_path
) {
296 size_t base_len
= ( strlen ( base_path
) + 1 );
297 char base_path_copy
[base_len
];
298 char *base_tmp
= base_path_copy
;
301 /* If relative path is absolute, just re-use it */
302 if ( relative_path
[0] == '/' )
303 return strdup ( relative_path
);
305 /* Create modifiable copy of path for dirname() */
306 memcpy ( base_tmp
, base_path
, base_len
);
307 base_tmp
= dirname ( base_tmp
);
309 /* Process "./" and "../" elements */
310 while ( *relative_path
== '.' ) {
312 if ( *relative_path
== 0 ) {
314 } else if ( *relative_path
== '/' ) {
316 } else if ( *relative_path
== '.' ) {
318 if ( *relative_path
== 0 ) {
319 base_tmp
= dirname ( base_tmp
);
320 } else if ( *relative_path
== '/' ) {
321 base_tmp
= dirname ( base_tmp
);
333 /* Create and return new path */
334 if ( asprintf ( &resolved
, "%s%s%s", base_tmp
,
335 ( ( base_tmp
[ strlen ( base_tmp
) - 1 ] == '/' ) ?
336 "" : "/" ), relative_path
) < 0 )
343 * Resolve base+relative URI
345 * @v base_uri Base URI, or NULL
346 * @v relative_uri Relative URI
347 * @ret resolved_uri Resolved URI
349 * Takes a base URI (e.g. "http://etherboot.org/kernels/vmlinuz" and a
350 * relative URI (e.g. "../initrds/initrd.gz") and produces a new URI
351 * (e.g. "http://etherboot.org/initrds/initrd.gz").
353 struct uri
* resolve_uri ( struct uri
*base_uri
,
354 struct uri
*relative_uri
) {
356 char *tmp_path
= NULL
;
359 /* If relative URI is absolute, just re-use it */
360 if ( uri_is_absolute ( relative_uri
) || ( ! base_uri
) )
361 return uri_get ( relative_uri
);
364 memcpy ( &tmp_uri
, base_uri
, sizeof ( tmp_uri
) );
365 if ( relative_uri
->path
) {
366 tmp_path
= resolve_path ( ( base_uri
->path
?
367 base_uri
->path
: "/" ),
368 relative_uri
->path
);
369 tmp_uri
.path
= tmp_path
;
370 tmp_uri
.query
= relative_uri
->query
;
371 tmp_uri
.fragment
= relative_uri
->fragment
;
372 } else if ( relative_uri
->query
) {
373 tmp_uri
.query
= relative_uri
->query
;
374 tmp_uri
.fragment
= relative_uri
->fragment
;
375 } else if ( relative_uri
->fragment
) {
376 tmp_uri
.fragment
= relative_uri
->fragment
;
379 /* Create demangled URI */
380 new_uri
= uri_dup ( &tmp_uri
);