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.h 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #ifndef _IPXE_URI_H
  2. #define _IPXE_URI_H
  3. /** @file
  4. *
  5. * Uniform Resource Identifiers
  6. *
  7. */
  8. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  9. #include <stddef.h>
  10. #include <stdlib.h>
  11. #include <ipxe/refcnt.h>
  12. #include <ipxe/in.h>
  13. struct parameters;
  14. /** A Uniform Resource Identifier
  15. *
  16. * Terminology for this data structure is as per uri(7), except that
  17. * "path" is defined to include the leading '/' for an absolute path.
  18. *
  19. * Note that all fields within a URI are optional and may be NULL.
  20. *
  21. * The pointers to the various fields are packed together so they can
  22. * be accessed in array fashion in some places in uri.c where doing so
  23. * saves significant code size.
  24. *
  25. * Some examples are probably helpful:
  26. *
  27. * http://www.ipxe.org/wiki :
  28. *
  29. * scheme = "http", host = "www.ipxe.org", path = "/wiki"
  30. *
  31. * /var/lib/tftpboot :
  32. *
  33. * path = "/var/lib/tftpboot"
  34. *
  35. * mailto:bob@nowhere.com :
  36. *
  37. * scheme = "mailto", opaque = "bob@nowhere.com"
  38. *
  39. * ftp://joe:secret@insecure.org:8081/hidden/path/to?what=is#this
  40. *
  41. * scheme = "ftp", user = "joe", password = "secret",
  42. * host = "insecure.org", port = "8081", path = "/hidden/path/to",
  43. * query = "what=is", fragment = "this"
  44. */
  45. struct uri {
  46. /** Reference count */
  47. struct refcnt refcnt;
  48. /** Scheme */
  49. const char *scheme;
  50. /** Opaque part */
  51. const char *opaque;
  52. /** User name */
  53. const char *user;
  54. /** Password */
  55. const char *password;
  56. /** Host name */
  57. const char *host;
  58. /** Port number */
  59. const char *port;
  60. /** Path */
  61. const char *path;
  62. /** Query */
  63. const char *query;
  64. /** Fragment */
  65. const char *fragment;
  66. /** Form parameters */
  67. struct parameters *params;
  68. } __attribute__ (( packed ));
  69. /**
  70. * Access URI field
  71. *
  72. * @v uri URI
  73. * @v field URI field index
  74. * @ret field URI field (as an lvalue)
  75. */
  76. #define uri_field( uri, field ) (&uri->scheme)[field]
  77. /**
  78. * Calculate index of a URI field
  79. *
  80. * @v name URI field name
  81. * @ret field URI field index
  82. */
  83. #define URI_FIELD( name ) \
  84. ( ( offsetof ( struct uri, name ) - \
  85. offsetof ( struct uri, scheme ) ) / sizeof ( void * ) )
  86. /** URI fields */
  87. enum uri_fields {
  88. URI_SCHEME = URI_FIELD ( scheme ),
  89. URI_OPAQUE = URI_FIELD ( opaque ),
  90. URI_USER = URI_FIELD ( user ),
  91. URI_PASSWORD = URI_FIELD ( password ),
  92. URI_HOST = URI_FIELD ( host ),
  93. URI_PORT = URI_FIELD ( port ),
  94. URI_PATH = URI_FIELD ( path ),
  95. URI_QUERY = URI_FIELD ( query ),
  96. URI_FRAGMENT = URI_FIELD ( fragment ),
  97. URI_FIELDS
  98. };
  99. /**
  100. * URI is an absolute URI
  101. *
  102. * @v uri URI
  103. * @ret is_absolute URI is absolute
  104. *
  105. * An absolute URI begins with a scheme, e.g. "http:" or "mailto:".
  106. * Note that this is a separate concept from a URI with an absolute
  107. * path.
  108. */
  109. static inline int uri_is_absolute ( const struct uri *uri ) {
  110. return ( uri->scheme != NULL );
  111. }
  112. /**
  113. * URI has an opaque part
  114. *
  115. * @v uri URI
  116. * @ret has_opaque URI has an opaque part
  117. */
  118. static inline int uri_has_opaque ( const struct uri *uri ) {
  119. return ( uri->opaque && ( uri->opaque[0] != '\0' ) );
  120. }
  121. /**
  122. * URI has a path
  123. *
  124. * @v uri URI
  125. * @ret has_path URI has a path
  126. */
  127. static inline int uri_has_path ( const struct uri *uri ) {
  128. return ( uri->path && ( uri->path[0] != '\0' ) );
  129. }
  130. /**
  131. * URI has an absolute path
  132. *
  133. * @v uri URI
  134. * @ret has_absolute_path URI has an absolute path
  135. *
  136. * An absolute path begins with a '/'. Note that this is a separate
  137. * concept from an absolute URI. Note also that a URI may not have a
  138. * path at all.
  139. */
  140. static inline int uri_has_absolute_path ( const struct uri *uri ) {
  141. return ( uri->path && ( uri->path[0] == '/' ) );
  142. }
  143. /**
  144. * URI has a relative path
  145. *
  146. * @v uri URI
  147. * @ret has_relative_path URI has a relative path
  148. *
  149. * A relative path begins with something other than a '/'. Note that
  150. * this is a separate concept from a relative URI. Note also that a
  151. * URI may not have a path at all.
  152. */
  153. static inline int uri_has_relative_path ( const struct uri *uri ) {
  154. return ( uri->path && ( uri->path[0] != '/' ) );
  155. }
  156. /**
  157. * Increment URI reference count
  158. *
  159. * @v uri URI, or NULL
  160. * @ret uri URI as passed in
  161. */
  162. static inline __attribute__ (( always_inline )) struct uri *
  163. uri_get ( struct uri *uri ) {
  164. ref_get ( &uri->refcnt );
  165. return uri;
  166. }
  167. /**
  168. * Decrement URI reference count
  169. *
  170. * @v uri URI, or NULL
  171. */
  172. static inline __attribute__ (( always_inline )) void
  173. uri_put ( struct uri *uri ) {
  174. ref_put ( &uri->refcnt );
  175. }
  176. extern struct uri *cwuri;
  177. extern size_t uri_encode ( const char *string, unsigned int field,
  178. char *buf, ssize_t len );
  179. extern struct uri * parse_uri ( const char *uri_string );
  180. extern size_t format_uri ( const struct uri *uri, char *buf, size_t len );
  181. extern char * format_uri_alloc ( const struct uri *uri );
  182. extern unsigned int uri_port ( const struct uri *uri,
  183. unsigned int default_port );
  184. extern struct uri * uri_dup ( const struct uri *uri );
  185. extern char * resolve_path ( const char *base_path,
  186. const char *relative_path );
  187. extern struct uri * resolve_uri ( const struct uri *base_uri,
  188. struct uri *relative_uri );
  189. extern struct uri * tftp_uri ( struct in_addr next_server, unsigned int port,
  190. const char *filename );
  191. extern void churi ( struct uri *uri );
  192. #endif /* _IPXE_URI_H */