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

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