Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

uri.h 5.3KB

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