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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. FILE_LICENCE ( GPL2_OR_LATER );
  20. /** @file
  21. *
  22. * Uniform Resource Identifiers
  23. *
  24. */
  25. #include <stdint.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <libgen.h>
  29. #include <ctype.h>
  30. #include <ipxe/vsprintf.h>
  31. #include <ipxe/uri.h>
  32. /**
  33. * Dump URI for debugging
  34. *
  35. * @v uri URI
  36. */
  37. static void dump_uri ( struct uri *uri ) {
  38. if ( ! uri )
  39. return;
  40. if ( uri->scheme )
  41. DBG ( " scheme \"%s\"", uri->scheme );
  42. if ( uri->opaque )
  43. DBG ( " opaque \"%s\"", uri->opaque );
  44. if ( uri->user )
  45. DBG ( " user \"%s\"", uri->user );
  46. if ( uri->password )
  47. DBG ( " password \"%s\"", uri->password );
  48. if ( uri->host )
  49. DBG ( " host \"%s\"", uri->host );
  50. if ( uri->port )
  51. DBG ( " port \"%s\"", uri->port );
  52. if ( uri->path )
  53. DBG ( " path \"%s\"", uri->path );
  54. if ( uri->query )
  55. DBG ( " query \"%s\"", uri->query );
  56. if ( uri->fragment )
  57. DBG ( " fragment \"%s\"", uri->fragment );
  58. }
  59. /**
  60. * Parse URI
  61. *
  62. * @v uri_string URI as a string
  63. * @ret uri URI
  64. *
  65. * Splits a URI into its component parts. The return URI structure is
  66. * dynamically allocated and must eventually be freed by calling
  67. * uri_put().
  68. */
  69. struct uri * parse_uri ( const char *uri_string ) {
  70. struct uri *uri;
  71. char *raw;
  72. char *tmp;
  73. char *path;
  74. char *authority;
  75. int i;
  76. size_t raw_len;
  77. /* Allocate space for URI struct and a copy of the string */
  78. raw_len = ( strlen ( uri_string ) + 1 /* NUL */ );
  79. uri = zalloc ( sizeof ( *uri ) + raw_len );
  80. if ( ! uri )
  81. return NULL;
  82. raw = ( ( ( char * ) uri ) + sizeof ( *uri ) );
  83. /* Copy in the raw string */
  84. memcpy ( raw, uri_string, raw_len );
  85. /* Start by chopping off the fragment, if it exists */
  86. if ( ( tmp = strchr ( raw, '#' ) ) ) {
  87. *(tmp++) = '\0';
  88. uri->fragment = tmp;
  89. }
  90. /* Identify absolute/relative URI. We ignore schemes that are
  91. * apparently only a single character long, since otherwise we
  92. * misinterpret a DOS-style path name ("C:\path\to\file") as a
  93. * URI with scheme="C",opaque="\path\to\file".
  94. */
  95. if ( ( tmp = strchr ( raw, ':' ) ) && ( tmp > ( raw + 1 ) ) ) {
  96. /* Absolute URI: identify hierarchical/opaque */
  97. uri->scheme = raw;
  98. *(tmp++) = '\0';
  99. if ( *tmp == '/' ) {
  100. /* Absolute URI with hierarchical part */
  101. path = tmp;
  102. } else {
  103. /* Absolute URI with opaque part */
  104. uri->opaque = tmp;
  105. path = NULL;
  106. }
  107. } else {
  108. /* Relative URI */
  109. path = raw;
  110. }
  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
  113. */
  114. if ( ! path )
  115. goto done;
  116. /* Chop off the query, if it exists */
  117. if ( ( tmp = strchr ( path, '?' ) ) ) {
  118. *(tmp++) = '\0';
  119. uri->query = tmp;
  120. }
  121. /* Identify net/absolute/relative path */
  122. if ( strncmp ( path, "//", 2 ) == 0 ) {
  123. /* Net path. If this is terminated by the first '/'
  124. * of an absolute path, then we have no space for a
  125. * terminator after the authority field, so shuffle
  126. * the authority down by one byte, overwriting one of
  127. * the two slashes.
  128. */
  129. authority = ( path + 2 );
  130. if ( ( tmp = strchr ( authority, '/' ) ) ) {
  131. /* Shuffle down */
  132. uri->path = tmp;
  133. memmove ( ( authority - 1 ), authority,
  134. ( tmp - authority ) );
  135. authority--;
  136. *(--tmp) = '\0';
  137. }
  138. } else {
  139. /* Absolute/relative path */
  140. uri->path = path;
  141. authority = NULL;
  142. }
  143. /* If we don't have an authority (i.e. we have a non-net
  144. * path), we're already finished processing
  145. */
  146. if ( ! authority )
  147. goto done;
  148. /* Split authority into user[:password] and host[:port] portions */
  149. if ( ( tmp = strchr ( authority, '@' ) ) ) {
  150. /* Has user[:password] */
  151. *(tmp++) = '\0';
  152. uri->host = tmp;
  153. uri->user = authority;
  154. if ( ( tmp = strchr ( authority, ':' ) ) ) {
  155. /* Has password */
  156. *(tmp++) = '\0';
  157. uri->password = tmp;
  158. }
  159. } else {
  160. /* No user:password */
  161. uri->host = authority;
  162. }
  163. /* Split host into host[:port] */
  164. if ( ( tmp = strchr ( uri->host, ':' ) ) ) {
  165. *(tmp++) = '\0';
  166. uri->port = tmp;
  167. }
  168. /* Decode fields that should be decoded */
  169. for ( i = URI_FIRST_FIELD; i <= URI_LAST_FIELD; i++ ) {
  170. const char *field = uri_get_field ( uri, i );
  171. if ( field && ( URI_ENCODED & ( 1 << i ) ) )
  172. uri_decode ( field, ( char * ) field,
  173. strlen ( field ) + 1 /* NUL */ );
  174. }
  175. done:
  176. DBG ( "URI \"%s\" split into", uri_string );
  177. dump_uri ( uri );
  178. DBG ( "\n" );
  179. return uri;
  180. }
  181. /**
  182. * Get port from URI
  183. *
  184. * @v uri URI, or NULL
  185. * @v default_port Default port to use if none specified in URI
  186. * @ret port Port
  187. */
  188. unsigned int uri_port ( struct uri *uri, unsigned int default_port ) {
  189. if ( ( ! uri ) || ( ! uri->port ) )
  190. return default_port;
  191. return ( strtoul ( uri->port, NULL, 0 ) );
  192. }
  193. /**
  194. * Unparse URI
  195. *
  196. * @v buf Buffer to fill with URI string
  197. * @v size Size of buffer
  198. * @v uri URI to write into buffer, or NULL
  199. * @v fields Bitmask of fields to include in URI string, or URI_ALL
  200. * @ret len Length of URI string
  201. */
  202. int unparse_uri ( char *buf, size_t size, struct uri *uri,
  203. unsigned int fields ) {
  204. /* List of characters that typically go before certain fields */
  205. static char separators[] = { /* scheme */ 0, /* opaque */ ':',
  206. /* user */ 0, /* password */ ':',
  207. /* host */ '@', /* port */ ':',
  208. /* path */ 0, /* query */ '?',
  209. /* fragment */ '#' };
  210. int used = 0;
  211. int i;
  212. DBG ( "URI unparsing" );
  213. dump_uri ( uri );
  214. DBG ( "\n" );
  215. /* Ensure buffer is NUL-terminated */
  216. if ( size )
  217. buf[0] = '\0';
  218. /* Special-case NULL URI */
  219. if ( ! uri )
  220. return 0;
  221. /* Iterate through requested fields */
  222. for ( i = URI_FIRST_FIELD; i <= URI_LAST_FIELD; i++ ) {
  223. const char *field = uri_get_field ( uri, i );
  224. char sep = separators[i];
  225. /* Ensure `fields' only contains bits for fields that exist */
  226. if ( ! field )
  227. fields &= ~( 1 << i );
  228. /* Store this field if we were asked to */
  229. if ( fields & ( 1 << i ) ) {
  230. /* Print :// if we're non-opaque and had a scheme */
  231. if ( ( fields & URI_SCHEME_BIT ) &&
  232. ( i > URI_OPAQUE ) ) {
  233. used += ssnprintf ( buf + used, size - used,
  234. "://" );
  235. /* Only print :// once */
  236. fields &= ~URI_SCHEME_BIT;
  237. }
  238. /* Only print separator if an earlier field exists */
  239. if ( sep && ( fields & ( ( 1 << i ) - 1 ) ) )
  240. used += ssnprintf ( buf + used, size - used,
  241. "%c", sep );
  242. /* Print contents of field, possibly encoded */
  243. if ( URI_ENCODED & ( 1 << i ) )
  244. used += uri_encode ( field, buf + used,
  245. size - used, i );
  246. else
  247. used += ssnprintf ( buf + used, size - used,
  248. "%s", field );
  249. }
  250. }
  251. return used;
  252. }
  253. /**
  254. * Duplicate URI
  255. *
  256. * @v uri URI
  257. * @ret uri Duplicate URI
  258. *
  259. * Creates a modifiable copy of a URI.
  260. */
  261. struct uri * uri_dup ( struct uri *uri ) {
  262. size_t len = ( unparse_uri ( NULL, 0, uri, URI_ALL ) + 1 );
  263. char buf[len];
  264. unparse_uri ( buf, len, uri, URI_ALL );
  265. return parse_uri ( buf );
  266. }
  267. /**
  268. * Resolve base+relative path
  269. *
  270. * @v base_uri Base path
  271. * @v relative_uri Relative path
  272. * @ret resolved_uri Resolved path
  273. *
  274. * Takes a base path (e.g. "/var/lib/tftpboot/vmlinuz" and a relative
  275. * path (e.g. "initrd.gz") and produces a new path
  276. * (e.g. "/var/lib/tftpboot/initrd.gz"). Note that any non-directory
  277. * portion of the base path will automatically be stripped; this
  278. * matches the semantics used when resolving the path component of
  279. * URIs.
  280. */
  281. char * resolve_path ( const char *base_path,
  282. const char *relative_path ) {
  283. size_t base_len = ( strlen ( base_path ) + 1 );
  284. char base_path_copy[base_len];
  285. char *base_tmp = base_path_copy;
  286. char *resolved;
  287. /* If relative path is absolute, just re-use it */
  288. if ( relative_path[0] == '/' )
  289. return strdup ( relative_path );
  290. /* Create modifiable copy of path for dirname() */
  291. memcpy ( base_tmp, base_path, base_len );
  292. base_tmp = dirname ( base_tmp );
  293. /* Process "./" and "../" elements */
  294. while ( *relative_path == '.' ) {
  295. relative_path++;
  296. if ( *relative_path == 0 ) {
  297. /* Do nothing */
  298. } else if ( *relative_path == '/' ) {
  299. relative_path++;
  300. } else if ( *relative_path == '.' ) {
  301. relative_path++;
  302. if ( *relative_path == 0 ) {
  303. base_tmp = dirname ( base_tmp );
  304. } else if ( *relative_path == '/' ) {
  305. base_tmp = dirname ( base_tmp );
  306. relative_path++;
  307. } else {
  308. relative_path -= 2;
  309. break;
  310. }
  311. } else {
  312. relative_path--;
  313. break;
  314. }
  315. }
  316. /* Create and return new path */
  317. if ( asprintf ( &resolved, "%s%s%s", base_tmp,
  318. ( ( base_tmp[ strlen ( base_tmp ) - 1 ] == '/' ) ?
  319. "" : "/" ), relative_path ) < 0 )
  320. return NULL;
  321. return resolved;
  322. }
  323. /**
  324. * Resolve base+relative URI
  325. *
  326. * @v base_uri Base URI, or NULL
  327. * @v relative_uri Relative URI
  328. * @ret resolved_uri Resolved URI
  329. *
  330. * Takes a base URI (e.g. "http://ipxe.org/kernels/vmlinuz" and a
  331. * relative URI (e.g. "../initrds/initrd.gz") and produces a new URI
  332. * (e.g. "http://ipxe.org/initrds/initrd.gz").
  333. */
  334. struct uri * resolve_uri ( struct uri *base_uri,
  335. struct uri *relative_uri ) {
  336. struct uri tmp_uri;
  337. char *tmp_path = NULL;
  338. struct uri *new_uri;
  339. /* If relative URI is absolute, just re-use it */
  340. if ( uri_is_absolute ( relative_uri ) || ( ! base_uri ) )
  341. return uri_get ( relative_uri );
  342. /* Mangle URI */
  343. memcpy ( &tmp_uri, base_uri, sizeof ( tmp_uri ) );
  344. if ( relative_uri->path ) {
  345. tmp_path = resolve_path ( ( base_uri->path ?
  346. base_uri->path : "/" ),
  347. relative_uri->path );
  348. tmp_uri.path = tmp_path;
  349. tmp_uri.query = relative_uri->query;
  350. tmp_uri.fragment = relative_uri->fragment;
  351. } else if ( relative_uri->query ) {
  352. tmp_uri.query = relative_uri->query;
  353. tmp_uri.fragment = relative_uri->fragment;
  354. } else if ( relative_uri->fragment ) {
  355. tmp_uri.fragment = relative_uri->fragment;
  356. }
  357. /* Create demangled URI */
  358. new_uri = uri_dup ( &tmp_uri );
  359. free ( tmp_path );
  360. return new_uri;
  361. }
  362. /**
  363. * Test for unreserved URI characters
  364. *
  365. * @v c Character to test
  366. * @v field Field of URI in which character lies
  367. * @ret is_unreserved Character is an unreserved character
  368. */
  369. static int is_unreserved_uri_char ( int c, int field ) {
  370. /* According to RFC3986, the unreserved character set is
  371. *
  372. * A-Z a-z 0-9 - _ . ~
  373. *
  374. * but we also pass & ; = in queries, / in paths,
  375. * and everything in opaques
  376. */
  377. int ok = ( isupper ( c ) || islower ( c ) || isdigit ( c ) ||
  378. ( c == '-' ) || ( c == '_' ) ||
  379. ( c == '.' ) || ( c == '~' ) );
  380. if ( field == URI_QUERY )
  381. ok = ok || ( c == ';' ) || ( c == '&' ) || ( c == '=' );
  382. if ( field == URI_PATH )
  383. ok = ok || ( c == '/' );
  384. if ( field == URI_OPAQUE )
  385. ok = 1;
  386. return ok;
  387. }
  388. /**
  389. * URI-encode string
  390. *
  391. * @v raw_string String to be URI-encoded
  392. * @v buf Buffer to contain encoded string
  393. * @v len Length of buffer
  394. * @v field Field of URI in which string lies
  395. * @ret len Length of encoded string (excluding NUL)
  396. */
  397. size_t uri_encode ( const char *raw_string, char *buf, ssize_t len,
  398. int field ) {
  399. ssize_t remaining = len;
  400. size_t used;
  401. unsigned char c;
  402. if ( len > 0 )
  403. buf[0] = '\0';
  404. while ( ( c = *(raw_string++) ) ) {
  405. if ( is_unreserved_uri_char ( c, field ) ) {
  406. used = ssnprintf ( buf, remaining, "%c", c );
  407. } else {
  408. used = ssnprintf ( buf, remaining, "%%%02X", c );
  409. }
  410. buf += used;
  411. remaining -= used;
  412. }
  413. return ( len - remaining );
  414. }
  415. /**
  416. * Decode URI-encoded string
  417. *
  418. * @v encoded_string URI-encoded string
  419. * @v buf Buffer to contain decoded string
  420. * @v len Length of buffer
  421. * @ret len Length of decoded string (excluding NUL)
  422. *
  423. * This function may be used in-place, with @a buf the same as
  424. * @a encoded_string.
  425. */
  426. size_t uri_decode ( const char *encoded_string, char *buf, ssize_t len ) {
  427. ssize_t remaining;
  428. char hexbuf[3];
  429. char *hexbuf_end;
  430. unsigned char c;
  431. for ( remaining = len; *encoded_string; remaining-- ) {
  432. if ( *encoded_string == '%' ) {
  433. encoded_string++;
  434. snprintf ( hexbuf, sizeof ( hexbuf ), "%s",
  435. encoded_string );
  436. c = strtoul ( hexbuf, &hexbuf_end, 16 );
  437. encoded_string += ( hexbuf_end - hexbuf );
  438. } else {
  439. c = *(encoded_string++);
  440. }
  441. if ( remaining > 1 )
  442. *buf++ = c;
  443. }
  444. if ( len )
  445. *buf = 0;
  446. return ( len - remaining );
  447. }