gnunet_uri_lib.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * Copyright (C) 2016 Jack Engqvist Johansson
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. #ifndef GNUNET_URI_LIB_H
  23. #define GNUNET_URI_LIB_H
  24. /**
  25. * The struct where the parsed values will be stored:
  26. *
  27. * scheme ":" [ "//" ] [ username ":" password "@" ] host [ ":" port ] [ "/" ] [ path ] [ "?" query ] [ "#" fragment ]
  28. *
  29. * Note: to make sure that no strings are copied, the first slash "/" in the
  30. * path will be used to null terminate the hostname if no port is supplied.
  31. */
  32. struct GNUNET_Uri
  33. {
  34. char *scheme; /* scheme, without ":" and "//" */
  35. char *username; /* username, default: NULL */
  36. char *password; /* password, default: NULL */
  37. char *host; /* hostname or IP address */
  38. int port; /* port, default: 0 */
  39. char *path; /* path, without leading "/", default: NULL */
  40. char *query; /* query, default: NULL */
  41. char *fragment; /* fragment, default: NULL */
  42. };
  43. /* A struct to hold the query string parameter values. */
  44. struct GNUNET_UriParam
  45. {
  46. char *key;
  47. char *val;
  48. };
  49. /**
  50. * Parse a URL to a struct.
  51. *
  52. * The URL string should be in one of the following formats:
  53. *
  54. * Absolute URL:
  55. * scheme ":" [ "//" ] [ username ":" password "@" ] host [ ":" port ] [ "/" ] [ path ] [ "?" query ] [ "#" fragment ]
  56. *
  57. * Relative URL:
  58. * path [ "?" query ] [ "#" fragment ]
  59. *
  60. * The following parts will be parsed to the corresponding struct member.
  61. *
  62. * *url: a pointer to the struct where to store the parsed values.
  63. * *url_str: a pointer to the url to be parsed (null terminated). The string
  64. * will be modified.
  65. *
  66. * Returns 0 on success, otherwise -1.
  67. */
  68. int
  69. GNUNET_uri_parse (struct GNUNET_Uri *url,
  70. char *url_str);
  71. /**
  72. * Split a path into several strings.
  73. *
  74. * No data is copied, the slashed are used as null terminators and then
  75. * pointers to each path part will be stored in **parts. Double slashes will be
  76. * treated as one.
  77. *
  78. * *path: the path to split. The string will be modified.
  79. * **parts: a pointer to an array of (char *) where to store the result.
  80. * max_parts: max number of parts to parse.
  81. *
  82. * Returns the number of parsed items. -1 on error.
  83. */
  84. int
  85. GNUNET_uri_split_path (char *path,
  86. char **parts,
  87. int max_parts);
  88. /**
  89. * Parse a query string into a key/value struct.
  90. *
  91. * The query string should be a null terminated string of parameters separated by
  92. * a delimiter. Each parameter are checked for the equal sign character. If it
  93. * appears in the parameter, it will be used as a null terminator and the part
  94. * that comes after it will be the value of the parameter.
  95. *
  96. * No data are copied, the equal sign and delimiters are used as null
  97. * terminators and then pointers to each parameter key and value will be stored
  98. * in the yuarel_param struct.
  99. *
  100. * *query: the query string to parse. The string will be modified.
  101. * delimiter: the character that separates the key/value pairs from each other.
  102. * *params: an array of (struct yuarel_param) where to store the result.
  103. * max_values: max number of parameters to parse.
  104. *
  105. * Returns the number of parsed items. -1 on error.
  106. */
  107. int
  108. GNUNET_uri_parse_query (char *query,
  109. char delimiter,
  110. struct GNUNET_UriParam *params,
  111. int max_params);
  112. #endif /* GNUNET_URI_LIB_H */