dynhds.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #ifndef HEADER_CURL_DYNHDS_H
  2. #define HEADER_CURL_DYNHDS_H
  3. /***************************************************************************
  4. * _ _ ____ _
  5. * Project ___| | | | _ \| |
  6. * / __| | | | |_) | |
  7. * | (__| |_| | _ <| |___
  8. * \___|\___/|_| \_\_____|
  9. *
  10. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  11. *
  12. * This software is licensed as described in the file COPYING, which
  13. * you should have received as part of this distribution. The terms
  14. * are also available at https://curl.se/docs/copyright.html.
  15. *
  16. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  17. * copies of the Software, and permit persons to whom the Software is
  18. * furnished to do so, under the terms of the COPYING file.
  19. *
  20. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  21. * KIND, either express or implied.
  22. *
  23. * SPDX-License-Identifier: curl
  24. *
  25. ***************************************************************************/
  26. #include "curl_setup.h"
  27. #include <curl/curl.h>
  28. #include "dynbuf.h"
  29. struct dynbuf;
  30. /**
  31. * A single header entry.
  32. * `name` and `value` are non-NULL and always NUL terminated.
  33. */
  34. struct dynhds_entry {
  35. char *name;
  36. char *value;
  37. size_t namelen;
  38. size_t valuelen;
  39. };
  40. struct dynhds {
  41. struct dynhds_entry **hds;
  42. size_t hds_len; /* number of entries in hds */
  43. size_t hds_allc; /* size of hds allocation */
  44. size_t max_entries; /* size limit number of entries */
  45. size_t strs_len; /* length of all strings */
  46. size_t max_strs_size; /* max length of all strings */
  47. int opts;
  48. };
  49. #define DYNHDS_OPT_NONE (0)
  50. #define DYNHDS_OPT_LOWERCASE (1 << 0)
  51. /**
  52. * Init for use on first time or after a reset.
  53. * Allow `max_entries` headers to be added, 0 for unlimited.
  54. * Allow size of all name and values added to not exceed `max_strs_size``
  55. */
  56. void Curl_dynhds_init(struct dynhds *dynhds, size_t max_entries,
  57. size_t max_strs_size);
  58. /**
  59. * Frees all data held in `dynhds`, but not the struct itself.
  60. */
  61. void Curl_dynhds_free(struct dynhds *dynhds);
  62. /**
  63. * Reset `dyndns` to the initial init state. May keep allocations
  64. * around.
  65. */
  66. void Curl_dynhds_reset(struct dynhds *dynhds);
  67. /**
  68. * Return the number of header entries.
  69. */
  70. size_t Curl_dynhds_count(struct dynhds *dynhds);
  71. /**
  72. * Set the options to use, replacing any existing ones.
  73. * This will not have an effect on already existing headers.
  74. */
  75. void Curl_dynhds_set_opts(struct dynhds *dynhds, int opts);
  76. /**
  77. * Return the n-th header entry or NULL if it does not exist.
  78. */
  79. struct dynhds_entry *Curl_dynhds_getn(struct dynhds *dynhds, size_t n);
  80. /**
  81. * Return the 1st header entry of the name or NULL if none exists.
  82. */
  83. struct dynhds_entry *Curl_dynhds_get(struct dynhds *dynhds,
  84. const char *name, size_t namelen);
  85. struct dynhds_entry *Curl_dynhds_cget(struct dynhds *dynhds, const char *name);
  86. /**
  87. * Return TRUE iff one or more headers with the given name exist.
  88. */
  89. bool Curl_dynhds_contains(struct dynhds *dynhds,
  90. const char *name, size_t namelen);
  91. bool Curl_dynhds_ccontains(struct dynhds *dynhds, const char *name);
  92. /**
  93. * Return how often the given name appears in `dynhds`.
  94. * Names are case-insensitive.
  95. */
  96. size_t Curl_dynhds_count_name(struct dynhds *dynhds,
  97. const char *name, size_t namelen);
  98. /**
  99. * Return how often the given 0-terminated name appears in `dynhds`.
  100. * Names are case-insensitive.
  101. */
  102. size_t Curl_dynhds_ccount_name(struct dynhds *dynhds, const char *name);
  103. /**
  104. * Add a header, name + value, to `dynhds` at the end. Does *not*
  105. * check for duplicate names.
  106. */
  107. CURLcode Curl_dynhds_add(struct dynhds *dynhds,
  108. const char *name, size_t namelen,
  109. const char *value, size_t valuelen);
  110. /**
  111. * Add a header, c-string name + value, to `dynhds` at the end.
  112. */
  113. CURLcode Curl_dynhds_cadd(struct dynhds *dynhds,
  114. const char *name, const char *value);
  115. /**
  116. * Remove all entries with the given name.
  117. * Returns number of entries removed.
  118. */
  119. size_t Curl_dynhds_remove(struct dynhds *dynhds,
  120. const char *name, size_t namelen);
  121. size_t Curl_dynhds_cremove(struct dynhds *dynhds, const char *name);
  122. /**
  123. * Set the give header name and value, replacing any entries with
  124. * the same name. The header is added at the end of all (remaining)
  125. * entries.
  126. */
  127. CURLcode Curl_dynhds_set(struct dynhds *dynhds,
  128. const char *name, size_t namelen,
  129. const char *value, size_t valuelen);
  130. CURLcode Curl_dynhds_cset(struct dynhds *dynhds,
  131. const char *name, const char *value);
  132. /**
  133. * Add a single header from a HTTP/1.1 formatted line at the end. Line
  134. * may contain a delimiting \r\n or just \n. Any characters after
  135. * that will be ignored.
  136. */
  137. CURLcode Curl_dynhds_h1_cadd_line(struct dynhds *dynhds, const char *line);
  138. /**
  139. * Add a single header from a HTTP/1.1 formatted line at the end. Line
  140. * may contain a delimiting \r\n or just \n. Any characters after
  141. * that will be ignored.
  142. */
  143. CURLcode Curl_dynhds_h1_add_line(struct dynhds *dynhds,
  144. const char *line, size_t line_len);
  145. /**
  146. * Add the headers to the given `dynbuf` in HTTP/1.1 format with
  147. * cr+lf line endings. Will NOT output a last empty line.
  148. */
  149. CURLcode Curl_dynhds_h1_dprint(struct dynhds *dynhds, struct dynbuf *dbuf);
  150. #endif /* HEADER_CURL_DYNHDS_H */