選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

uri.h 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #ifndef _GPXE_URI_H
  2. #define _GPXE_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 <gpxe/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.etherboot.org/wiki :
  26. *
  27. * scheme = "http", host = "www.etherboot.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 ( struct uri *uri ) {
  103. return ( uri->scheme != NULL );
  104. }
  105. /**
  106. * URI has an absolute path
  107. *
  108. * @v uri URI
  109. * @ret has_absolute_path URI has an absolute path
  110. *
  111. * An absolute path begins with a '/'. Note that this is a separate
  112. * concept from an absolute URI. Note also that a URI may not have a
  113. * path at all.
  114. */
  115. static inline int uri_has_absolute_path ( struct uri *uri ) {
  116. return ( uri->path && ( uri->path[0] == '/' ) );
  117. }
  118. /**
  119. * URI has a relative path
  120. *
  121. * @v uri URI
  122. * @ret has_relative_path URI has a relative path
  123. *
  124. * A relative path begins with something other than a '/'. Note that
  125. * this is a separate concept from a relative URI. Note also that a
  126. * URI may not have a path at all.
  127. */
  128. static inline int uri_has_relative_path ( struct uri *uri ) {
  129. return ( uri->path && ( uri->path[0] != '/' ) );
  130. }
  131. /**
  132. * Increment URI reference count
  133. *
  134. * @v uri URI, or NULL
  135. * @ret uri URI as passed in
  136. */
  137. static inline __attribute__ (( always_inline )) struct uri *
  138. uri_get ( struct uri *uri ) {
  139. ref_get ( &uri->refcnt );
  140. return uri;
  141. }
  142. /**
  143. * Decrement URI reference count
  144. *
  145. * @v uri URI, or NULL
  146. */
  147. static inline __attribute__ (( always_inline )) void
  148. uri_put ( struct uri *uri ) {
  149. ref_put ( &uri->refcnt );
  150. }
  151. extern struct uri *cwuri;
  152. extern struct uri * parse_uri ( const char *uri_string );
  153. extern unsigned int uri_port ( struct uri *uri, unsigned int default_port );
  154. extern int unparse_uri ( char *buf, size_t size, struct uri *uri,
  155. unsigned int fields );
  156. extern struct uri * uri_dup ( struct uri *uri );
  157. extern char * resolve_path ( const char *base_path,
  158. const char *relative_path );
  159. extern struct uri * resolve_uri ( struct uri *base_uri,
  160. struct uri *relative_uri );
  161. extern void churi ( struct uri *uri );
  162. extern size_t uri_encode ( const char *raw_string, char *buf, ssize_t len,
  163. int field );
  164. extern size_t uri_decode ( const char *encoded_string, char *buf, ssize_t len );
  165. #endif /* _GPXE_URI_H */