Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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_OPAQUE] = ':',
  418. [URI_PASSWORD] = ':',
  419. [URI_PORT] = ':',
  420. [URI_QUERY] = '?',
  421. [URI_FRAGMENT] = '#',
  422. };
  423. char prefix;
  424. size_t used = 0;
  425. unsigned int field;
  426. /* Ensure buffer is NUL-terminated */
  427. if ( len )
  428. buf[0] = '\0';
  429. /* Special-case NULL URI */
  430. if ( ! uri )
  431. return 0;
  432. /* Generate fields */
  433. for ( field = 0 ; field < URI_FIELDS ; field++ ) {
  434. /* Skip non-existent fields */
  435. if ( ! uri_field ( uri, field ) )
  436. continue;
  437. /* Prefix this field, if applicable */
  438. prefix = prefixes[field];
  439. if ( ( field == URI_HOST ) && ( uri->user != NULL ) )
  440. prefix = '@';
  441. if ( prefix ) {
  442. used += ssnprintf ( ( buf + used ), ( len - used ),
  443. "%c", prefix );
  444. }
  445. /* Encode this field */
  446. used += uri_encode_string ( field, uri_field ( uri, field ),
  447. ( buf + used ), ( len - used ) );
  448. /* Suffix this field, if applicable */
  449. if ( ( field == URI_SCHEME ) && ( ! uri->opaque ) ) {
  450. used += ssnprintf ( ( buf + used ), ( len - used ),
  451. "://" );
  452. }
  453. }
  454. if ( len ) {
  455. DBGC ( uri, "URI formatted" );
  456. uri_dump ( uri );
  457. DBGC ( uri, " to \"%s%s\"\n", buf,
  458. ( ( used > len ) ? "<TRUNCATED>" : "" ) );
  459. }
  460. return used;
  461. }
  462. /**
  463. * Format URI
  464. *
  465. * @v uri URI
  466. * @ret string URI string, or NULL on failure
  467. *
  468. * The caller is responsible for eventually freeing the allocated
  469. * memory.
  470. */
  471. char * format_uri_alloc ( const struct uri *uri ) {
  472. size_t len;
  473. char *string;
  474. len = ( format_uri ( uri, NULL, 0 ) + 1 /* NUL */ );
  475. string = malloc ( len );
  476. if ( string )
  477. format_uri ( uri, string, len );
  478. return string;
  479. }
  480. /**
  481. * Copy URI fields
  482. *
  483. * @v src Source URI
  484. * @v dest Destination URI, or NULL to calculate length
  485. * @ret len Length of raw URI
  486. */
  487. static size_t uri_copy_fields ( const struct uri *src, struct uri *dest ) {
  488. size_t len = sizeof ( *dest );
  489. char *out = ( ( void * ) dest + len );
  490. unsigned int field;
  491. size_t field_len;
  492. /* Copy existent fields */
  493. for ( field = 0 ; field < URI_FIELDS ; field++ ) {
  494. /* Skip non-existent fields */
  495. if ( ! uri_field ( src, field ) )
  496. continue;
  497. /* Calculate field length */
  498. field_len = ( strlen ( uri_field ( src, field ) )
  499. + 1 /* NUL */ );
  500. len += field_len;
  501. /* Copy field, if applicable */
  502. if ( dest ) {
  503. memcpy ( out, uri_field ( src, field ), field_len );
  504. uri_field ( dest, field ) = out;
  505. out += field_len;
  506. }
  507. }
  508. return len;
  509. }
  510. /**
  511. * Duplicate URI
  512. *
  513. * @v uri URI
  514. * @ret uri Duplicate URI
  515. *
  516. * Creates a modifiable copy of a URI.
  517. */
  518. struct uri * uri_dup ( const struct uri *uri ) {
  519. struct uri *dup;
  520. size_t len;
  521. /* Allocate new URI */
  522. len = uri_copy_fields ( uri, NULL );
  523. dup = zalloc ( len );
  524. if ( ! dup )
  525. return NULL;
  526. ref_init ( &dup->refcnt, uri_free );
  527. /* Copy fields */
  528. uri_copy_fields ( uri, dup );
  529. /* Copy parameters */
  530. dup->params = params_get ( uri->params );
  531. DBGC ( uri, "URI duplicated" );
  532. uri_dump ( uri );
  533. DBGC ( uri, "\n" );
  534. return dup;
  535. }
  536. /**
  537. * Resolve base+relative path
  538. *
  539. * @v base_uri Base path
  540. * @v relative_uri Relative path
  541. * @ret resolved_uri Resolved path, or NULL on failure
  542. *
  543. * Takes a base path (e.g. "/var/lib/tftpboot/vmlinuz" and a relative
  544. * path (e.g. "initrd.gz") and produces a new path
  545. * (e.g. "/var/lib/tftpboot/initrd.gz"). Note that any non-directory
  546. * portion of the base path will automatically be stripped; this
  547. * matches the semantics used when resolving the path component of
  548. * URIs.
  549. */
  550. char * resolve_path ( const char *base_path,
  551. const char *relative_path ) {
  552. char *base_copy;
  553. char *base_tmp;
  554. char *resolved;
  555. /* If relative path is absolute, just re-use it */
  556. if ( relative_path[0] == '/' )
  557. return strdup ( relative_path );
  558. /* Create modifiable copy of path for dirname() */
  559. base_copy = strdup ( base_path );
  560. if ( ! base_copy )
  561. return NULL;
  562. /* Strip filename portion of base path */
  563. base_tmp = dirname ( base_copy );
  564. /* Process "./" and "../" elements */
  565. while ( *relative_path == '.' ) {
  566. relative_path++;
  567. if ( *relative_path == 0 ) {
  568. /* Do nothing */
  569. } else if ( *relative_path == '/' ) {
  570. relative_path++;
  571. } else if ( *relative_path == '.' ) {
  572. relative_path++;
  573. if ( *relative_path == 0 ) {
  574. base_tmp = dirname ( base_tmp );
  575. } else if ( *relative_path == '/' ) {
  576. base_tmp = dirname ( base_tmp );
  577. relative_path++;
  578. } else {
  579. relative_path -= 2;
  580. break;
  581. }
  582. } else {
  583. relative_path--;
  584. break;
  585. }
  586. }
  587. /* Create and return new path */
  588. if ( asprintf ( &resolved, "%s%s%s", base_tmp,
  589. ( ( base_tmp[ strlen ( base_tmp ) - 1 ] == '/' ) ?
  590. "" : "/" ), relative_path ) < 0 )
  591. resolved = NULL;
  592. free ( base_copy );
  593. return resolved;
  594. }
  595. /**
  596. * Resolve base+relative URI
  597. *
  598. * @v base_uri Base URI, or NULL
  599. * @v relative_uri Relative URI
  600. * @ret resolved_uri Resolved URI, or NULL on failure
  601. *
  602. * Takes a base URI (e.g. "http://ipxe.org/kernels/vmlinuz" and a
  603. * relative URI (e.g. "../initrds/initrd.gz") and produces a new URI
  604. * (e.g. "http://ipxe.org/initrds/initrd.gz").
  605. */
  606. struct uri * resolve_uri ( const struct uri *base_uri,
  607. struct uri *relative_uri ) {
  608. struct uri tmp_uri;
  609. char *tmp_path = NULL;
  610. struct uri *new_uri;
  611. /* If relative URI is absolute, just re-use it */
  612. if ( uri_is_absolute ( relative_uri ) || ( ! base_uri ) )
  613. return uri_get ( relative_uri );
  614. /* Mangle URI */
  615. memcpy ( &tmp_uri, base_uri, sizeof ( tmp_uri ) );
  616. if ( relative_uri->path ) {
  617. tmp_path = resolve_path ( ( base_uri->path ?
  618. base_uri->path : "/" ),
  619. relative_uri->path );
  620. tmp_uri.path = tmp_path;
  621. tmp_uri.query = relative_uri->query;
  622. tmp_uri.fragment = relative_uri->fragment;
  623. tmp_uri.params = relative_uri->params;
  624. } else if ( relative_uri->query ) {
  625. tmp_uri.query = relative_uri->query;
  626. tmp_uri.fragment = relative_uri->fragment;
  627. tmp_uri.params = relative_uri->params;
  628. } else if ( relative_uri->fragment ) {
  629. tmp_uri.fragment = relative_uri->fragment;
  630. tmp_uri.params = relative_uri->params;
  631. } else if ( relative_uri->params ) {
  632. tmp_uri.params = relative_uri->params;
  633. }
  634. /* Create demangled URI */
  635. new_uri = uri_dup ( &tmp_uri );
  636. free ( tmp_path );
  637. return new_uri;
  638. }
  639. /**
  640. * Construct TFTP URI from server address and filename
  641. *
  642. * @v sa_server Server address
  643. * @v filename Filename
  644. * @ret uri URI, or NULL on failure
  645. */
  646. static struct uri * tftp_uri ( struct sockaddr *sa_server,
  647. const char *filename ) {
  648. struct sockaddr_tcpip *st_server =
  649. ( ( struct sockaddr_tcpip * ) sa_server );
  650. char buf[ 6 /* "65535" + NUL */ ];
  651. char *path;
  652. struct uri tmp;
  653. struct uri *uri = NULL;
  654. /* Initialise TFTP URI */
  655. memset ( &tmp, 0, sizeof ( tmp ) );
  656. tmp.scheme = "tftp";
  657. /* Construct TFTP server address */
  658. tmp.host = sock_ntoa ( sa_server );
  659. if ( ! tmp.host )
  660. goto err_host;
  661. /* Construct TFTP server port, if applicable */
  662. if ( st_server->st_port ) {
  663. snprintf ( buf, sizeof ( buf ), "%d",
  664. ntohs ( st_server->st_port ) );
  665. tmp.port = buf;
  666. }
  667. /* Construct TFTP path */
  668. if ( asprintf ( &path, "/%s", filename ) < 0 )
  669. goto err_path;
  670. tmp.path = path;
  671. /* Demangle URI */
  672. uri = uri_dup ( &tmp );
  673. if ( ! uri )
  674. goto err_uri;
  675. err_uri:
  676. free ( path );
  677. err_path:
  678. err_host:
  679. return uri;
  680. }
  681. /**
  682. * Construct URI from server address and filename
  683. *
  684. * @v sa_server Server address
  685. * @v filename Filename
  686. * @ret uri URI, or NULL on failure
  687. *
  688. * PXE TFTP filenames specified via the DHCP next-server field often
  689. * contain characters such as ':' or '#' which would confuse the
  690. * generic URI parser. We provide a mechanism for directly
  691. * constructing a TFTP URI from the next-server and filename.
  692. */
  693. struct uri * pxe_uri ( struct sockaddr *sa_server, const char *filename ) {
  694. struct uri *uri;
  695. /* Fail if filename is empty */
  696. if ( ! ( filename && filename[0] ) )
  697. return NULL;
  698. /* If filename is a hierarchical absolute URI, then use that
  699. * URI. (We accept only hierarchical absolute URIs, since PXE
  700. * filenames sometimes start with DOS drive letters such as
  701. * "C:\", which get misinterpreted as opaque absolute URIs.)
  702. */
  703. uri = parse_uri ( filename );
  704. if ( uri && uri_is_absolute ( uri ) && ( ! uri->opaque ) )
  705. return uri;
  706. uri_put ( uri );
  707. /* Otherwise, construct a TFTP URI directly */
  708. return tftp_uri ( sa_server, filename );
  709. }