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

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