2
0

dynhds.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. #ifdef UNITTESTS
  87. /* used by unit2602.c */
  88. /**
  89. * Return TRUE iff one or more headers with the given name exist.
  90. */
  91. bool Curl_dynhds_contains(struct dynhds *dynhds,
  92. const char *name, size_t namelen);
  93. bool Curl_dynhds_ccontains(struct dynhds *dynhds, const char *name);
  94. /**
  95. * Return how often the given name appears in `dynhds`.
  96. * Names are case-insensitive.
  97. */
  98. size_t Curl_dynhds_count_name(struct dynhds *dynhds,
  99. const char *name, size_t namelen);
  100. /**
  101. * Return how often the given 0-terminated name appears in `dynhds`.
  102. * Names are case-insensitive.
  103. */
  104. size_t Curl_dynhds_ccount_name(struct dynhds *dynhds, const char *name);
  105. /**
  106. * Remove all entries with the given name.
  107. * Returns number of entries removed.
  108. */
  109. size_t Curl_dynhds_remove(struct dynhds *dynhds,
  110. const char *name, size_t namelen);
  111. size_t Curl_dynhds_cremove(struct dynhds *dynhds, const char *name);
  112. /**
  113. * Set the give header name and value, replacing any entries with
  114. * the same name. The header is added at the end of all (remaining)
  115. * entries.
  116. */
  117. CURLcode Curl_dynhds_set(struct dynhds *dynhds,
  118. const char *name, size_t namelen,
  119. const char *value, size_t valuelen);
  120. #endif
  121. CURLcode Curl_dynhds_cset(struct dynhds *dynhds,
  122. const char *name, const char *value);
  123. /**
  124. * Add a header, name + value, to `dynhds` at the end. Does *not*
  125. * check for duplicate names.
  126. */
  127. CURLcode Curl_dynhds_add(struct dynhds *dynhds,
  128. const char *name, size_t namelen,
  129. const char *value, size_t valuelen);
  130. /**
  131. * Add a header, c-string name + value, to `dynhds` at the end.
  132. */
  133. CURLcode Curl_dynhds_cadd(struct dynhds *dynhds,
  134. const char *name, const char *value);
  135. /**
  136. * Add a single header from an HTTP/1.1 formatted line at the end. Line
  137. * may contain a delimiting \r\n or just \n. Any characters after
  138. * that will be ignored.
  139. */
  140. CURLcode Curl_dynhds_h1_cadd_line(struct dynhds *dynhds, const char *line);
  141. /**
  142. * Add a single header from an HTTP/1.1 formatted line at the end. Line
  143. * may contain a delimiting \r\n or just \n. Any characters after
  144. * that will be ignored.
  145. */
  146. CURLcode Curl_dynhds_h1_add_line(struct dynhds *dynhds,
  147. const char *line, size_t line_len);
  148. /**
  149. * Add the headers to the given `dynbuf` in HTTP/1.1 format with
  150. * cr+lf line endings. Will NOT output a last empty line.
  151. */
  152. CURLcode Curl_dynhds_h1_dprint(struct dynhds *dynhds, struct dynbuf *dbuf);
  153. #ifdef USE_NGHTTP2
  154. #include <stdint.h>
  155. #include <nghttp2/nghttp2.h>
  156. nghttp2_nv *Curl_dynhds_to_nva(struct dynhds *dynhds, size_t *pcount);
  157. #endif /* USE_NGHTTP2 */
  158. #endif /* HEADER_CURL_DYNHDS_H */