您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  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 ( 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. /* Decode fields in-place */
  387. for ( field = 0 ; field < URI_FIELDS ; field++ )
  388. uri_decode_inplace ( uri, field );
  389. done:
  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_PATH] = '/',
  421. [URI_QUERY] = '?',
  422. [URI_FRAGMENT] = '#',
  423. };
  424. char prefix;
  425. size_t used = 0;
  426. unsigned int field;
  427. /* Ensure buffer is NUL-terminated */
  428. if ( len )
  429. buf[0] = '\0';
  430. /* Special-case NULL URI */
  431. if ( ! uri )
  432. return 0;
  433. /* Generate fields */
  434. for ( field = 0 ; field < URI_FIELDS ; field++ ) {
  435. /* Skip non-existent fields */
  436. if ( ! uri_field ( uri, field ) )
  437. continue;
  438. /* Prefix this field, if applicable */
  439. prefix = prefixes[field];
  440. if ( ( field == URI_HOST ) && ( uri->user != NULL ) )
  441. prefix = '@';
  442. if ( ( field == URI_PATH ) && ( uri->path[0] == '/' ) )
  443. prefix = '\0';
  444. if ( prefix ) {
  445. used += ssnprintf ( ( buf + used ), ( len - used ),
  446. "%c", prefix );
  447. }
  448. /* Encode this field */
  449. used += uri_encode_string ( field, uri_field ( uri, field ),
  450. ( buf + used ), ( len - used ) );
  451. /* Suffix this field, if applicable */
  452. if ( ( field == URI_SCHEME ) && ( ! uri->opaque ) ) {
  453. used += ssnprintf ( ( buf + used ), ( len - used ),
  454. "://" );
  455. }
  456. }
  457. if ( len ) {
  458. DBGC ( uri, "URI formatted" );
  459. uri_dump ( uri );
  460. DBGC ( uri, " to \"%s%s\"\n", buf,
  461. ( ( used > len ) ? "<TRUNCATED>" : "" ) );
  462. }
  463. return used;
  464. }
  465. /**
  466. * Format URI
  467. *
  468. * @v uri URI
  469. * @ret string URI string, or NULL on failure
  470. *
  471. * The caller is responsible for eventually freeing the allocated
  472. * memory.
  473. */
  474. char * format_uri_alloc ( const struct uri *uri ) {
  475. size_t len;
  476. char *string;
  477. len = ( format_uri ( uri, NULL, 0 ) + 1 /* NUL */ );
  478. string = malloc ( len );
  479. if ( string )
  480. format_uri ( uri, string, len );
  481. return string;
  482. }
  483. /**
  484. * Copy URI fields
  485. *
  486. * @v src Source URI
  487. * @v dest Destination URI, or NULL to calculate length
  488. * @ret len Length of raw URI
  489. */
  490. static size_t uri_copy_fields ( const struct uri *src, struct uri *dest ) {
  491. size_t len = sizeof ( *dest );
  492. char *out = ( ( void * ) dest + len );
  493. unsigned int field;
  494. size_t field_len;
  495. /* Copy existent fields */
  496. for ( field = 0 ; field < URI_FIELDS ; field++ ) {
  497. /* Skip non-existent fields */
  498. if ( ! uri_field ( src, field ) )
  499. continue;
  500. /* Calculate field length */
  501. field_len = ( strlen ( uri_field ( src, field ) )
  502. + 1 /* NUL */ );
  503. len += field_len;
  504. /* Copy field, if applicable */
  505. if ( dest ) {
  506. memcpy ( out, uri_field ( src, field ), field_len );
  507. uri_field ( dest, field ) = out;
  508. out += field_len;
  509. }
  510. }
  511. return len;
  512. }
  513. /**
  514. * Duplicate URI
  515. *
  516. * @v uri URI
  517. * @ret uri Duplicate URI
  518. *
  519. * Creates a modifiable copy of a URI.
  520. */
  521. struct uri * uri_dup ( const struct uri *uri ) {
  522. struct uri *dup;
  523. size_t len;
  524. /* Allocate new URI */
  525. len = uri_copy_fields ( uri, NULL );
  526. dup = zalloc ( len );
  527. if ( ! dup )
  528. return NULL;
  529. ref_init ( &dup->refcnt, uri_free );
  530. /* Copy fields */
  531. uri_copy_fields ( uri, dup );
  532. /* Copy parameters */
  533. dup->params = params_get ( uri->params );
  534. DBGC ( uri, "URI duplicated" );
  535. uri_dump ( uri );
  536. DBGC ( uri, "\n" );
  537. return dup;
  538. }
  539. /**
  540. * Resolve base+relative path
  541. *
  542. * @v base_uri Base path
  543. * @v relative_uri Relative path
  544. * @ret resolved_uri Resolved path
  545. *
  546. * Takes a base path (e.g. "/var/lib/tftpboot/vmlinuz" and a relative
  547. * path (e.g. "initrd.gz") and produces a new path
  548. * (e.g. "/var/lib/tftpboot/initrd.gz"). Note that any non-directory
  549. * portion of the base path will automatically be stripped; this
  550. * matches the semantics used when resolving the path component of
  551. * URIs.
  552. */
  553. char * resolve_path ( const char *base_path,
  554. const char *relative_path ) {
  555. size_t base_len = ( strlen ( base_path ) + 1 );
  556. char base_path_copy[base_len];
  557. char *base_tmp = base_path_copy;
  558. char *resolved;
  559. /* If relative path is absolute, just re-use it */
  560. if ( relative_path[0] == '/' )
  561. return strdup ( relative_path );
  562. /* Create modifiable copy of path for dirname() */
  563. memcpy ( base_tmp, base_path, base_len );
  564. base_tmp = dirname ( base_tmp );
  565. /* Process "./" and "../" elements */
  566. while ( *relative_path == '.' ) {
  567. relative_path++;
  568. if ( *relative_path == 0 ) {
  569. /* Do nothing */
  570. } else if ( *relative_path == '/' ) {
  571. relative_path++;
  572. } else if ( *relative_path == '.' ) {
  573. relative_path++;
  574. if ( *relative_path == 0 ) {
  575. base_tmp = dirname ( base_tmp );
  576. } else if ( *relative_path == '/' ) {
  577. base_tmp = dirname ( base_tmp );
  578. relative_path++;
  579. } else {
  580. relative_path -= 2;
  581. break;
  582. }
  583. } else {
  584. relative_path--;
  585. break;
  586. }
  587. }
  588. /* Create and return new path */
  589. if ( asprintf ( &resolved, "%s%s%s", base_tmp,
  590. ( ( base_tmp[ strlen ( base_tmp ) - 1 ] == '/' ) ?
  591. "" : "/" ), relative_path ) < 0 )
  592. return NULL;
  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
  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 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. * PXE TFTP filenames specified via the DHCP next-server field often
  647. * contain characters such as ':' or '#' which would confuse the
  648. * generic URI parser. We provide a mechanism for directly
  649. * constructing a TFTP URI from the next-server and filename.
  650. */
  651. struct uri * pxe_uri ( struct sockaddr *sa_server, const char *filename ) {
  652. char buf[ 6 /* "65535" + NUL */ ];
  653. struct sockaddr_tcpip *st_server =
  654. ( ( struct sockaddr_tcpip * ) sa_server );
  655. struct uri tmp;
  656. struct uri *uri;
  657. /* Fail if filename is empty */
  658. if ( ! ( filename && filename[0] ) )
  659. return NULL;
  660. /* If filename is a hierarchical absolute URI, then use that
  661. * URI. (We accept only hierarchical absolute URIs, since PXE
  662. * filenames sometimes start with DOS drive letters such as
  663. * "C:\", which get misinterpreted as opaque absolute URIs.)
  664. */
  665. uri = parse_uri ( filename );
  666. if ( uri && uri_is_absolute ( uri ) && ( ! uri->opaque ) )
  667. return uri;
  668. uri_put ( uri );
  669. /* Otherwise, construct a TFTP URI directly */
  670. memset ( &tmp, 0, sizeof ( tmp ) );
  671. tmp.scheme = "tftp";
  672. tmp.host = sock_ntoa ( sa_server );
  673. if ( ! tmp.host )
  674. return NULL;
  675. if ( st_server->st_port ) {
  676. snprintf ( buf, sizeof ( buf ), "%d",
  677. ntohs ( st_server->st_port ) );
  678. tmp.port = buf;
  679. }
  680. tmp.path = filename;
  681. uri = uri_dup ( &tmp );
  682. return uri;
  683. }