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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #ifndef _GPXE_URI_H
  2. #define _GPXE_URI_H
  3. /** @file
  4. *
  5. * Uniform Resource Identifiers
  6. *
  7. */
  8. #include <stddef.h>
  9. #include <stdlib.h>
  10. #include <gpxe/refcnt.h>
  11. /** A Uniform Resource Identifier
  12. *
  13. * Terminology for this data structure is as per uri(7), except that
  14. * "path" is defined to include the leading '/' for an absolute path.
  15. *
  16. * Note that all fields within a URI are optional and may be NULL.
  17. *
  18. * Some examples are probably helpful:
  19. *
  20. * http://www.etherboot.org/wiki :
  21. *
  22. * scheme = "http", host = "www.etherboot.org", path = "/wiki"
  23. *
  24. * /var/lib/tftpboot :
  25. *
  26. * path = "/var/lib/tftpboot"
  27. *
  28. * mailto:bob@nowhere.com :
  29. *
  30. * scheme = "mailto", opaque = "bob@nowhere.com"
  31. *
  32. * ftp://joe:secret@insecure.org:8081/hidden/path/to?what=is#this
  33. *
  34. * scheme = "ftp", user = "joe", password = "secret",
  35. * host = "insecure.org", port = "8081", path = "/hidden/path/to",
  36. * query = "what=is", fragment = "this"
  37. */
  38. struct uri {
  39. /** Reference count */
  40. struct refcnt refcnt;
  41. /** Scheme */
  42. const char *scheme;
  43. /** Opaque part */
  44. const char *opaque;
  45. /** User name */
  46. const char *user;
  47. /** Password */
  48. const char *password;
  49. /** Host name */
  50. const char *host;
  51. /** Port number */
  52. const char *port;
  53. /** Path */
  54. const char *path;
  55. /** Query */
  56. const char *query;
  57. /** Fragment */
  58. const char *fragment;
  59. };
  60. /**
  61. * URI is an absolute URI
  62. *
  63. * @v uri URI
  64. * @ret is_absolute URI is absolute
  65. *
  66. * An absolute URI begins with a scheme, e.g. "http:" or "mailto:".
  67. * Note that this is a separate concept from a URI with an absolute
  68. * path.
  69. */
  70. static inline int uri_is_absolute ( struct uri *uri ) {
  71. return ( uri->scheme != NULL );
  72. }
  73. /**
  74. * URI has an absolute path
  75. *
  76. * @v uri URI
  77. * @ret has_absolute_path URI has an absolute path
  78. *
  79. * An absolute path begins with a '/'. Note that this is a separate
  80. * concept from an absolute URI. Note also that a URI may not have a
  81. * path at all.
  82. */
  83. static inline int uri_has_absolute_path ( struct uri *uri ) {
  84. return ( uri->path && ( uri->path[0] == '/' ) );
  85. }
  86. /**
  87. * URI has a relative path
  88. *
  89. * @v uri URI
  90. * @ret has_relative_path URI has a relative path
  91. *
  92. * A relative path begins with something other than a '/'. Note that
  93. * this is a separate concept from a relative URI. Note also that a
  94. * URI may not have a path at all.
  95. */
  96. static inline int uri_has_relative_path ( struct uri *uri ) {
  97. return ( uri->path && ( uri->path[0] != '/' ) );
  98. }
  99. /**
  100. * Increment URI reference count
  101. *
  102. * @v uri URI, or NULL
  103. * @ret uri URI as passed in
  104. */
  105. static inline __attribute__ (( always_inline )) struct uri *
  106. uri_get ( struct uri *uri ) {
  107. ref_get ( &uri->refcnt );
  108. return uri;
  109. }
  110. /**
  111. * Decrement URI reference count
  112. *
  113. * @v uri URI, or NULL
  114. */
  115. static inline __attribute__ (( always_inline )) void
  116. uri_put ( struct uri *uri ) {
  117. ref_put ( &uri->refcnt );
  118. }
  119. extern struct uri *cwuri;
  120. extern struct uri * parse_uri ( const char *uri_string );
  121. extern unsigned int uri_port ( struct uri *uri, unsigned int default_port );
  122. extern int unparse_uri ( char *buf, size_t size, struct uri *uri );
  123. extern struct uri * uri_dup ( struct uri *uri );
  124. extern char * resolve_path ( const char *base_path,
  125. const char *relative_path );
  126. extern struct uri * resolve_uri ( struct uri *base_uri,
  127. struct uri *relative_uri );
  128. extern void churi ( struct uri *uri );
  129. extern size_t uri_encode ( const char *raw_string, char *buf, size_t len );
  130. extern size_t uri_decode ( const char *encoded_string, char *buf, size_t len );
  131. #endif /* _GPXE_URI_H */