You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

uri.c 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. /*
  2. * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  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.
  8. *
  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.
  13. *
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. /** @file
  25. *
  26. * Uniform Resource Identifiers
  27. *
  28. */
  29. #include <stdint.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <libgen.h>
  33. #include <ctype.h>
  34. #include <ipxe/vsprintf.h>
  35. #include <ipxe/params.h>
  36. #include <ipxe/tcpip.h>
  37. #include <ipxe/uri.h>
  38. /**
  39. * Decode URI field
  40. *
  41. * @v encoded Encoded field
  42. * @v buf Data buffer
  43. * @v len Length
  44. * @ret len Length of data
  45. *
  46. * URI decoding can never increase the length of a string; we can
  47. * therefore safely decode in place.
  48. */
  49. size_t uri_decode ( const char *encoded, void *buf, size_t len ) {
  50. uint8_t *out = buf;
  51. unsigned int count = 0;
  52. char hexbuf[3];
  53. char *hexbuf_end;
  54. char c;
  55. char decoded;
  56. unsigned int skip;
  57. /* Copy string, decoding escaped characters as necessary */
  58. while ( ( c = *(encoded++) ) ) {
  59. if ( c == '%' ) {
  60. snprintf ( hexbuf, sizeof ( hexbuf ), "%s", encoded );
  61. decoded = strtoul ( hexbuf, &hexbuf_end, 16 );
  62. skip = ( hexbuf_end - hexbuf );
  63. encoded += skip;
  64. if ( skip )
  65. c = decoded;
  66. }
  67. if ( count < len )
  68. out[count] = c;
  69. count++;
  70. }
  71. return count;
  72. }
  73. /**
  74. * Decode URI field in-place
  75. *
  76. * @v uri URI
  77. * @v field URI field index
  78. */
  79. static void uri_decode_inplace ( struct uri *uri, unsigned int field ) {
  80. const char *encoded = uri_field ( uri, field );
  81. char *decoded = ( ( char * ) encoded );
  82. size_t len;
  83. /* Do nothing if field is not present */
  84. if ( ! encoded )
  85. return;
  86. /* Decode field in place */
  87. len = uri_decode ( encoded, decoded, strlen ( encoded ) );
  88. /* Terminate decoded string */
  89. decoded[len] = '\0';
  90. }
  91. /**
  92. * Check if character should be escaped within a URI field
  93. *
  94. * @v c Character
  95. * @v field URI field index
  96. * @ret escaped Character should be escaped
  97. */
  98. static int uri_character_escaped ( char c, unsigned int field ) {
  99. /* Non-printing characters and whitespace should always be
  100. * escaped, since they cannot sensibly be displayed as part of
  101. * a coherent URL string. (This test also catches control
  102. * characters such as CR and LF, which could affect the
  103. * operation of line-based protocols such as HTTP.)
  104. *
  105. * We should also escape characters which would alter the
  106. * interpretation of the URL if not escaped, i.e. characters
  107. * which have significance to the URL parser. We should not
  108. * blindly escape all such characters, because this would lead
  109. * to some very strange-looking URLs (e.g. if we were to
  110. * always escape '/' as "%2F" even within the URI path).
  111. *
  112. * We do not need to be perfect. Our primary role is as a
  113. * consumer of URIs rather than a producer; the main situation
  114. * in which we produce a URI string is for display to a human
  115. * user, who can probably tolerate some variance from the
  116. * formal specification. The only situation in which we
  117. * currently produce a URI string to be consumed by a computer
  118. * is when constructing an HTTP request URI, which contains
  119. * only the path and query fields.
  120. *
  121. * We can therefore sacrifice some correctness for the sake of
  122. * code size. For example, colons within the URI host should
  123. * be escaped unless they form part of an IPv6 literal
  124. * address; doing this correctly would require the URI
  125. * formatter to be aware of whether or not the URI host
  126. * contained an IPv4 address, an IPv6 address, or a host name.
  127. * We choose to simplify and never escape colons within the
  128. * URI host field: in the event of a pathological hostname
  129. * containing colons, this could potentially produce a URI
  130. * string which could not be reparsed.
  131. *
  132. * After excluding non-printing characters, whitespace, and
  133. * '%', the full set of characters with significance to the
  134. * URL parser is "/#:@?". We choose for each URI field which
  135. * of these require escaping in our use cases.
  136. *
  137. * For the scheme field (equivalently, if field is zero), we
  138. * escape anything that has significance not just for our URI
  139. * parser but for any other URI parsers (e.g. HTTP query
  140. * string parsers, which care about '=' and '&').
  141. */
  142. static const char *escaped[URI_FIELDS] = {
  143. /* Scheme or default: escape everything */
  144. [URI_SCHEME] = "/#:@?=&",
  145. /* Opaque part: escape characters which would affect
  146. * the reparsing of the URI, allowing everything else
  147. * (e.g. ':', which will appear in iSCSI URIs).
  148. */
  149. [URI_OPAQUE] = "#",
  150. /* User name: escape everything */
  151. [URI_USER] = "/#:@?",
  152. /* Password: escape everything */
  153. [URI_PASSWORD] = "/#:@?",
  154. /* Host name: escape everything except ':', which may
  155. * appear as part of an IPv6 literal address.
  156. */
  157. [URI_HOST] = "/#@?",
  158. /* Port number: escape everything */
  159. [URI_PORT] = "/#:@?",
  160. /* Path: escape everything except '/', which usually
  161. * appears within paths.
  162. */
  163. [URI_PATH] = "#:@?",
  164. /* Query: escape everything except '/', which
  165. * sometimes appears within queries.
  166. */
  167. [URI_QUERY] = "#:@?",
  168. /* Fragment: escape everything */
  169. [URI_FRAGMENT] = "/#:@?",
  170. };
  171. return ( /* Always escape non-printing characters and whitespace */
  172. ( ! isprint ( c ) ) || ( c == ' ' ) ||
  173. /* Always escape '%' */
  174. ( c == '%' ) ||
  175. /* Escape field-specific characters */
  176. strchr ( escaped[field], c ) );
  177. }
  178. /**
  179. * Encode URI field
  180. *
  181. * @v field URI field index
  182. * @v raw Raw data
  183. * @v raw_len Length of raw data
  184. * @v buf Buffer
  185. * @v len Length of buffer
  186. * @ret len Length of encoded string (excluding NUL)
  187. */
  188. size_t uri_encode ( unsigned int field, const void *raw, size_t raw_len,
  189. char *buf, ssize_t len ) {
  190. const uint8_t *raw_bytes = ( ( const uint8_t * ) raw );
  191. ssize_t remaining = len;
  192. size_t used;
  193. char c;
  194. /* Ensure encoded string is NUL-terminated even if empty */
  195. if ( len > 0 )
  196. buf[0] = '\0';
  197. /* Copy string, escaping as necessary */
  198. while ( raw_len-- ) {
  199. c = *(raw_bytes++);
  200. if ( uri_character_escaped ( c, field ) ) {
  201. used = ssnprintf ( buf, remaining, "%%%02X", c );
  202. } else {
  203. used = ssnprintf ( buf, remaining, "%c", c );
  204. }
  205. buf += used;
  206. remaining -= used;
  207. }
  208. return ( len - remaining );
  209. }
  210. /**
  211. * Encode URI field string
  212. *
  213. * @v field URI field index
  214. * @v string String
  215. * @v buf Buffer
  216. * @v len Length of buffer
  217. * @ret len Length of encoded string (excluding NUL)
  218. */
  219. size_t uri_encode_string ( unsigned int field, const char *string,
  220. char *buf, ssize_t len ) {
  221. return uri_encode ( field, string, strlen ( string ), buf, len );
  222. }
  223. /**
  224. * Dump URI for debugging
  225. *
  226. * @v uri URI
  227. */
  228. static void uri_dump ( const struct uri *uri ) {
  229. if ( ! uri )
  230. return;
  231. if ( uri->scheme )
  232. DBGC ( uri, " scheme \"%s\"", uri->scheme );
  233. if ( uri->opaque )
  234. DBGC ( uri, " opaque \"%s\"", uri->opaque );
  235. if ( uri->user )
  236. DBGC ( uri, " user \"%s\"", uri->user );
  237. if ( uri->password )
  238. DBGC ( uri, " password \"%s\"", uri->password );
  239. if ( uri->host )
  240. DBGC ( uri, " host \"%s\"", uri->host );
  241. if ( uri->port )
  242. DBGC ( uri, " port \"%s\"", uri->port );
  243. if ( uri->path )
  244. DBGC ( uri, " path \"%s\"", uri->path );
  245. if ( uri->query )
  246. DBGC ( uri, " query \"%s\"", uri->query );
  247. if ( uri->fragment )
  248. DBGC ( uri, " fragment \"%s\"", uri->fragment );
  249. if ( uri->params )
  250. DBGC ( uri, " params \"%s\"", uri->params->name );
  251. }
  252. /**
  253. * Free URI
  254. *
  255. * @v refcnt Reference count
  256. */
  257. static void uri_free ( struct refcnt *refcnt ) {
  258. struct uri *uri = container_of ( refcnt, struct uri, refcnt );
  259. params_put ( uri->params );
  260. free ( uri );
  261. }
  262. /**
  263. * Parse URI
  264. *
  265. * @v uri_string URI as a string
  266. * @ret uri URI
  267. *
  268. * Splits a URI into its component parts. The return URI structure is
  269. * dynamically allocated and must eventually be freed by calling
  270. * uri_put().
  271. */
  272. struct uri * parse_uri ( const char *uri_string ) {
  273. struct uri *uri;
  274. struct parameters *params;
  275. char *raw;
  276. char *tmp;
  277. char *path;
  278. char *authority;
  279. size_t raw_len;
  280. unsigned int field;
  281. /* Allocate space for URI struct and a copy of the string */
  282. raw_len = ( strlen ( uri_string ) + 1 /* NUL */ );
  283. uri = zalloc ( sizeof ( *uri ) + raw_len );
  284. if ( ! uri )
  285. return NULL;
  286. ref_init ( &uri->refcnt, uri_free );
  287. raw = ( ( ( void * ) uri ) + sizeof ( *uri ) );
  288. /* Copy in the raw string */
  289. memcpy ( raw, uri_string, raw_len );
  290. /* Identify the parameter list, if present */
  291. if ( ( tmp = strstr ( raw, "##params" ) ) ) {
  292. *tmp = '\0';
  293. tmp += 8 /* "##params" */;
  294. params = find_parameters ( *tmp ? ( tmp + 1 ) : NULL );
  295. if ( params ) {
  296. uri->params = claim_parameters ( params );
  297. } else {
  298. /* Ignore non-existent submission blocks */
  299. }
  300. }
  301. /* Chop off the fragment, if it exists */
  302. if ( ( tmp = strchr ( raw, '#' ) ) ) {
  303. *(tmp++) = '\0';
  304. uri->fragment = tmp;
  305. }
  306. /* Identify absolute/relative URI */
  307. if ( ( tmp = strchr ( raw, ':' ) ) ) {
  308. /* Absolute URI: identify hierarchical/opaque */
  309. uri->scheme = raw;
  310. *(tmp++) = '\0';
  311. if ( *tmp == '/' ) {
  312. /* Absolute URI with hierarchical part */
  313. path = tmp;
  314. } else {
  315. /* Absolute URI with opaque part */
  316. uri->opaque = tmp;
  317. path = NULL;
  318. }
  319. } else {
  320. /* Relative URI */
  321. path = raw;
  322. }
  323. /* If we don't have a path (i.e. we have an absolute URI with
  324. * an opaque portion, we're already finished processing
  325. */
  326. if ( ! path )
  327. goto done;
  328. /* Chop off the query, if it exists */
  329. if ( ( tmp = strchr ( path, '?' ) ) ) {
  330. *(tmp++) = '\0';
  331. uri->query = tmp;
  332. }
  333. /* If we have no path remaining, then we're already finished
  334. * processing.
  335. */
  336. if ( ! path[0] )
  337. goto done;
  338. /* Identify net/absolute/relative path */
  339. if ( uri->scheme && ( strncmp ( path, "//", 2 ) == 0 ) ) {
  340. /* Net path. If this is terminated by the first '/'
  341. * of an absolute path, then we have no space for a
  342. * terminator after the authority field, so shuffle
  343. * the authority down by one byte, overwriting one of
  344. * the two slashes.
  345. */
  346. authority = ( path + 2 );
  347. if ( ( tmp = strchr ( authority, '/' ) ) ) {
  348. /* Shuffle down */
  349. uri->path = tmp;
  350. memmove ( ( authority - 1 ), authority,
  351. ( tmp - authority ) );
  352. authority--;
  353. *(--tmp) = '\0';
  354. }
  355. } else {
  356. /* Absolute/relative path */
  357. uri->path = path;
  358. authority = NULL;
  359. }
  360. /* If we don't have an authority (i.e. we have a non-net
  361. * path), we're already finished processing
  362. */
  363. if ( ! authority )
  364. goto done;
  365. /* Split authority into user[:password] and host[:port] portions */
  366. if ( ( tmp = strchr ( authority, '@' ) ) ) {
  367. /* Has user[:password] */
  368. *(tmp++) = '\0';
  369. uri->host = tmp;
  370. uri->user = authority;
  371. if ( ( tmp = strchr ( authority, ':' ) ) ) {
  372. /* Has password */
  373. *(tmp++) = '\0';
  374. uri->password = tmp;
  375. }
  376. } else {
  377. /* No user:password */
  378. uri->host = authority;
  379. }
  380. /* Split host into host[:port] */
  381. if ( ( uri->host[ strlen ( uri->host ) - 1 ] != ']' ) &&
  382. ( tmp = strrchr ( uri->host, ':' ) ) ) {
  383. *(tmp++) = '\0';
  384. uri->port = tmp;
  385. }
  386. done:
  387. /* Decode fields in-place */
  388. for ( field = 0 ; field < URI_FIELDS ; field++ )
  389. uri_decode_inplace ( uri, field );
  390. DBGC ( uri, "URI parsed \"%s\" to", uri_string );
  391. uri_dump ( uri );
  392. DBGC ( uri, "\n" );
  393. return uri;
  394. }
  395. /**
  396. * Get port from URI
  397. *
  398. * @v uri URI, or NULL
  399. * @v default_port Default port to use if none specified in URI
  400. * @ret port Port
  401. */
  402. unsigned int uri_port ( const struct uri *uri, unsigned int default_port ) {
  403. if ( ( ! uri ) || ( ! uri->port ) )
  404. return default_port;
  405. return ( strtoul ( uri->port, NULL, 0 ) );
  406. }
  407. /**
  408. * Format URI
  409. *
  410. * @v uri URI
  411. * @v buf Buffer to fill with URI string
  412. * @v size Size of buffer
  413. * @ret len Length of URI string
  414. */
  415. size_t format_uri ( const struct uri *uri, char *buf, size_t len ) {
  416. static const char prefixes[URI_FIELDS] = {
  417. [URI_PASSWORD] = ':',
  418. [URI_PORT] = ':',
  419. [URI_QUERY] = '?',
  420. [URI_FRAGMENT] = '#',
  421. };
  422. char prefix;
  423. size_t used = 0;
  424. unsigned int field;
  425. /* Ensure buffer is NUL-terminated */
  426. if ( len )
  427. buf[0] = '\0';
  428. /* Special-case NULL URI */
  429. if ( ! uri )
  430. return 0;
  431. /* Generate fields */
  432. for ( field = 0 ; field < URI_FIELDS ; field++ ) {
  433. /* Skip non-existent fields */
  434. if ( ! uri_field ( uri, field ) )
  435. continue;
  436. /* Prefix this field, if applicable */
  437. prefix = prefixes[field];
  438. if ( ( field == URI_HOST ) && ( uri->user != NULL ) )
  439. prefix = '@';
  440. if ( prefix ) {
  441. used += ssnprintf ( ( buf + used ), ( len - used ),
  442. "%c", prefix );
  443. }
  444. /* Encode this field */
  445. used += uri_encode_string ( field, uri_field ( uri, field ),
  446. ( buf + used ), ( len - used ) );
  447. /* Suffix this field, if applicable */
  448. if ( field == URI_SCHEME ) {
  449. used += ssnprintf ( ( buf + used ), ( len - used ),
  450. ":%s", ( uri->host ? "//" : "" ) );
  451. }
  452. }
  453. if ( len ) {
  454. DBGC ( uri, "URI formatted" );
  455. uri_dump ( uri );
  456. DBGC ( uri, " to \"%s%s\"\n", buf,
  457. ( ( used > len ) ? "<TRUNCATED>" : "" ) );
  458. }
  459. return used;
  460. }
  461. /**
  462. * Format URI
  463. *
  464. * @v uri URI
  465. * @ret string URI string, or NULL on failure
  466. *
  467. * The caller is responsible for eventually freeing the allocated
  468. * memory.
  469. */
  470. char * format_uri_alloc ( const struct uri *uri ) {
  471. size_t len;
  472. char *string;
  473. len = ( format_uri ( uri, NULL, 0 ) + 1 /* NUL */ );
  474. string = malloc ( len );
  475. if ( string )
  476. format_uri ( uri, string, len );
  477. return string;
  478. }
  479. /**
  480. * Copy URI fields
  481. *
  482. * @v src Source URI
  483. * @v dest Destination URI, or NULL to calculate length
  484. * @ret len Length of raw URI
  485. */
  486. static size_t uri_copy_fields ( const struct uri *src, struct uri *dest ) {
  487. size_t len = sizeof ( *dest );
  488. char *out = ( ( void * ) dest + len );
  489. unsigned int field;
  490. size_t field_len;
  491. /* Copy existent fields */
  492. for ( field = 0 ; field < URI_FIELDS ; field++ ) {
  493. /* Skip non-existent fields */
  494. if ( ! uri_field ( src, field ) )
  495. continue;
  496. /* Calculate field length */
  497. field_len = ( strlen ( uri_field ( src, field ) )
  498. + 1 /* NUL */ );
  499. len += field_len;
  500. /* Copy field, if applicable */
  501. if ( dest ) {
  502. memcpy ( out, uri_field ( src, field ), field_len );
  503. uri_field ( dest, field ) = out;
  504. out += field_len;
  505. }
  506. }
  507. return len;
  508. }
  509. /**
  510. * Duplicate URI
  511. *
  512. * @v uri URI
  513. * @ret uri Duplicate URI
  514. *
  515. * Creates a modifiable copy of a URI.
  516. */
  517. struct uri * uri_dup ( const struct uri *uri ) {
  518. struct uri *dup;
  519. size_t len;
  520. /* Allocate new URI */
  521. len = uri_copy_fields ( uri, NULL );
  522. dup = zalloc ( len );
  523. if ( ! dup )
  524. return NULL;
  525. ref_init ( &dup->refcnt, uri_free );
  526. /* Copy fields */
  527. uri_copy_fields ( uri, dup );
  528. /* Copy parameters */
  529. dup->params = params_get ( uri->params );
  530. DBGC ( uri, "URI duplicated" );
  531. uri_dump ( uri );
  532. DBGC ( uri, "\n" );
  533. return dup;
  534. }
  535. /**
  536. * Resolve base+relative path
  537. *
  538. * @v base_uri Base path
  539. * @v relative_uri Relative path
  540. * @ret resolved_uri Resolved path, or NULL on failure
  541. *
  542. * Takes a base path (e.g. "/var/lib/tftpboot/vmlinuz" and a relative
  543. * path (e.g. "initrd.gz") and produces a new path
  544. * (e.g. "/var/lib/tftpboot/initrd.gz"). Note that any non-directory
  545. * portion of the base path will automatically be stripped; this
  546. * matches the semantics used when resolving the path component of
  547. * URIs.
  548. */
  549. char * resolve_path ( const char *base_path,
  550. const char *relative_path ) {
  551. char *base_copy;
  552. char *base_tmp;
  553. char *resolved;
  554. /* If relative path is absolute, just re-use it */
  555. if ( relative_path[0] == '/' )
  556. return strdup ( relative_path );
  557. /* Create modifiable copy of path for dirname() */
  558. base_copy = strdup ( base_path );
  559. if ( ! base_copy )
  560. return NULL;
  561. /* Strip filename portion of base path */
  562. base_tmp = dirname ( base_copy );
  563. /* Process "./" and "../" elements */
  564. while ( *relative_path == '.' ) {
  565. relative_path++;
  566. if ( *relative_path == 0 ) {
  567. /* Do nothing */
  568. } else if ( *relative_path == '/' ) {
  569. relative_path++;
  570. } else if ( *relative_path == '.' ) {
  571. relative_path++;
  572. if ( *relative_path == 0 ) {
  573. base_tmp = dirname ( base_tmp );
  574. } else if ( *relative_path == '/' ) {
  575. base_tmp = dirname ( base_tmp );
  576. relative_path++;
  577. } else {
  578. relative_path -= 2;
  579. break;
  580. }
  581. } else {
  582. relative_path--;
  583. break;
  584. }
  585. }
  586. /* Create and return new path */
  587. if ( asprintf ( &resolved, "%s%s%s", base_tmp,
  588. ( ( base_tmp[ strlen ( base_tmp ) - 1 ] == '/' ) ?
  589. "" : "/" ), relative_path ) < 0 )
  590. resolved = NULL;
  591. free ( base_copy );
  592. return resolved;
  593. }
  594. /**
  595. * Resolve base+relative URI
  596. *
  597. * @v base_uri Base URI, or NULL
  598. * @v relative_uri Relative URI
  599. * @ret resolved_uri Resolved URI, or NULL on failure
  600. *
  601. * Takes a base URI (e.g. "http://ipxe.org/kernels/vmlinuz" and a
  602. * relative URI (e.g. "../initrds/initrd.gz") and produces a new URI
  603. * (e.g. "http://ipxe.org/initrds/initrd.gz").
  604. */
  605. struct uri * resolve_uri ( const struct uri *base_uri,
  606. struct uri *relative_uri ) {
  607. struct uri tmp_uri;
  608. char *tmp_path = NULL;
  609. struct uri *new_uri;
  610. /* If relative URI is absolute, just re-use it */
  611. if ( uri_is_absolute ( relative_uri ) || ( ! base_uri ) )
  612. return uri_get ( relative_uri );
  613. /* Mangle URI */
  614. memcpy ( &tmp_uri, base_uri, sizeof ( tmp_uri ) );
  615. if ( relative_uri->path ) {
  616. tmp_path = resolve_path ( ( base_uri->path ?
  617. base_uri->path : "/" ),
  618. relative_uri->path );
  619. tmp_uri.path = tmp_path;
  620. tmp_uri.query = relative_uri->query;
  621. tmp_uri.fragment = relative_uri->fragment;
  622. tmp_uri.params = relative_uri->params;
  623. } else if ( relative_uri->query ) {
  624. tmp_uri.query = relative_uri->query;
  625. tmp_uri.fragment = relative_uri->fragment;
  626. tmp_uri.params = relative_uri->params;
  627. } else if ( relative_uri->fragment ) {
  628. tmp_uri.fragment = relative_uri->fragment;
  629. tmp_uri.params = relative_uri->params;
  630. } else if ( relative_uri->params ) {
  631. tmp_uri.params = relative_uri->params;
  632. }
  633. /* Create demangled URI */
  634. new_uri = uri_dup ( &tmp_uri );
  635. free ( tmp_path );
  636. return new_uri;
  637. }
  638. /**
  639. * Construct TFTP URI from server address and filename
  640. *
  641. * @v sa_server Server address
  642. * @v filename Filename
  643. * @ret uri URI, or NULL on failure
  644. */
  645. static struct uri * tftp_uri ( struct sockaddr *sa_server,
  646. const char *filename ) {
  647. struct sockaddr_tcpip *st_server =
  648. ( ( struct sockaddr_tcpip * ) sa_server );
  649. char buf[ 6 /* "65535" + NUL */ ];
  650. char *path;
  651. struct uri tmp;
  652. struct uri *uri = NULL;
  653. /* Initialise TFTP URI */
  654. memset ( &tmp, 0, sizeof ( tmp ) );
  655. tmp.scheme = "tftp";
  656. /* Construct TFTP server address */
  657. tmp.host = sock_ntoa ( sa_server );
  658. if ( ! tmp.host )
  659. goto err_host;
  660. /* Construct TFTP server port, if applicable */
  661. if ( st_server->st_port ) {
  662. snprintf ( buf, sizeof ( buf ), "%d",
  663. ntohs ( st_server->st_port ) );
  664. tmp.port = buf;
  665. }
  666. /* Construct TFTP path */
  667. if ( asprintf ( &path, "/%s", filename ) < 0 )
  668. goto err_path;
  669. tmp.path = path;
  670. /* Demangle URI */
  671. uri = uri_dup ( &tmp );
  672. if ( ! uri )
  673. goto err_uri;
  674. err_uri:
  675. free ( path );
  676. err_path:
  677. err_host:
  678. return uri;
  679. }
  680. /**
  681. * Construct URI from server address and filename
  682. *
  683. * @v sa_server Server address
  684. * @v filename Filename
  685. * @ret uri URI, or NULL on failure
  686. *
  687. * PXE TFTP filenames specified via the DHCP next-server field often
  688. * contain characters such as ':' or '#' which would confuse the
  689. * generic URI parser. We provide a mechanism for directly
  690. * constructing a TFTP URI from the next-server and filename.
  691. */
  692. struct uri * pxe_uri ( struct sockaddr *sa_server, const char *filename ) {
  693. struct uri *uri;
  694. /* Fail if filename is empty */
  695. if ( ! ( filename && filename[0] ) )
  696. return NULL;
  697. /* If filename is a hierarchical absolute URI, then use that
  698. * URI. (We accept only hierarchical absolute URIs, since PXE
  699. * filenames sometimes start with DOS drive letters such as
  700. * "C:\", which get misinterpreted as opaque absolute URIs.)
  701. */
  702. uri = parse_uri ( filename );
  703. if ( uri && uri_is_absolute ( uri ) && ( ! uri->opaque ) )
  704. return uri;
  705. uri_put ( uri );
  706. /* Otherwise, construct a TFTP URI directly */
  707. return tftp_uri ( sa_server, filename );
  708. }