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.4KB

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