yuarel.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 INC_YUAREL_H
  23. #define INC_YUAREL_H
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. /**
  28. * The struct where the parsed values will be stored:
  29. *
  30. * scheme ":" [ "//" ] [ username ":" password "@" ] host [ ":" port ] [ "/" ] [ path ] [ "?" query ]
  31. *
  32. * Note: to make sure that no strings are copied, the first slash "/" in the
  33. * path will be used to null terminate the hostname if no port is supplied.
  34. */
  35. struct yuarel {
  36. char *scheme; /* scheme, without ":" and "//" */
  37. char *username; /* username, default: NULL */
  38. char *password; /* password, default: NULL */
  39. char *host; /* hostname or IP address */
  40. int port; /* port, default: 0 */
  41. char *path; /* path, without leading "/", default: NULL */
  42. char *query; /* query, default: NULL */
  43. char *fragment; /* fragment, default: NULL */
  44. };
  45. /* A struct to hold the query string parameter values. */
  46. struct yuarel_param {
  47. char *key;
  48. char *val;
  49. };
  50. /**
  51. * Parse a URL to a struct.
  52. *
  53. * The URL string should be in one of the following formats:
  54. *
  55. * Absolute URL:
  56. * scheme ":" [ "//" ] [ username ":" password "@" ] host [ ":" port ] [ "/" ] [ path ] [ "?" query ] [ "#" fragment ]
  57. *
  58. * Relative URL:
  59. * path [ "?" query ] [ "#" fragment ]
  60. *
  61. * The following parts will be parsed to the corresponding struct member.
  62. *
  63. * *url: a pointer to the struct where to store the parsed values.
  64. * *url_str: a pointer to the url to be parsed (null terminated).
  65. *
  66. * Returns 0 on success, otherwise -1.
  67. */
  68. extern int yuarel_parse(struct yuarel *url, char *url_str);
  69. /**
  70. * Split a path into several strings.
  71. *
  72. * No data is copied, the slashed are used as null terminators and then
  73. * pointers to each path part will be stored in **parts. Double slashes will be
  74. * treated as one.
  75. *
  76. * *path: the path to split.
  77. * **parts: a pointer to an array of (char *) where to store the result.
  78. * max_parts: max number of parts to parse.
  79. *
  80. * Returns the number of parsed items. -1 on error.
  81. */
  82. extern int yuarel_split_path(char *path, char **parts, int max_parts);
  83. /**
  84. * Parse a query string into a key/value struct.
  85. *
  86. * The query string should be a null terminated string of parameters separated by
  87. * a delimiter. Each parameter are checked for the equal sign character. If it
  88. * appears in the parameter, it will be used as a null terminator and the part
  89. * that comes after it will be the value of the parameter.
  90. *
  91. * No data are copied, the equal sign and delimiters are used as null
  92. * terminators and then pointers to each parameter key and value will be stored
  93. * in the yuarel_param struct.
  94. *
  95. * *query: the query string to parse.
  96. * delimiter: the character that separates the key/value pairs from eachother.
  97. * *params: an array of (struct yuarel_param) where to store the result.
  98. * max_values: max number of parameters to parse.
  99. *
  100. * Returns the number of parsed items. -1 on error.
  101. */
  102. extern int yuarel_parse_query(char *query, char delimiter, struct yuarel_param *params, int max_params);
  103. #ifdef __cplusplus
  104. }
  105. #endif
  106. #endif /* INC_YUAREL_H */