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

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