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

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