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.

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