dynhds.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. };
  48. /**
  49. * Init for use on first time or after a reset.
  50. * Allow `max_entries` headers to be added, 0 for unlimited.
  51. * Allow size of all name and values added to not exceed `max_strs_size``
  52. */
  53. void Curl_dynhds_init(struct dynhds *dynhds, size_t max_entries,
  54. size_t max_strs_size);
  55. /**
  56. * Frees all data held in `dynhds`, but not the struct itself.
  57. */
  58. void Curl_dynhds_free(struct dynhds *dynhds);
  59. /**
  60. * Reset `dyndns` to the initial init state. May keep allocations
  61. * around.
  62. */
  63. void Curl_dynhds_reset(struct dynhds *dynhds);
  64. /**
  65. * Return the number of header entries.
  66. */
  67. size_t Curl_dynhds_count(struct dynhds *dynhds);
  68. /**
  69. * Return the n-th header entry or NULL if it does not exist.
  70. */
  71. struct dynhds_entry *Curl_dynhds_getn(struct dynhds *dynhds, size_t n);
  72. /**
  73. * Return the 1st header entry of the name or NULL if none exists.
  74. */
  75. struct dynhds_entry *Curl_dynhds_get(struct dynhds *dynhds,
  76. const char *name, size_t namelen);
  77. struct dynhds_entry *Curl_dynhds_cget(struct dynhds *dynhds, const char *name);
  78. /**
  79. * Return TRUE iff one or more headers with the given name exist.
  80. */
  81. bool Curl_dynhds_contains(struct dynhds *dynhds,
  82. const char *name, size_t namelen);
  83. bool Curl_dynhds_ccontains(struct dynhds *dynhds, const char *name);
  84. /**
  85. * Return how often the given name appears in `dynhds`.
  86. * Names are case-insensitive.
  87. */
  88. size_t Curl_dynhds_count_name(struct dynhds *dynhds,
  89. const char *name, size_t namelen);
  90. /**
  91. * Return how often the given 0-terminated name appears in `dynhds`.
  92. * Names are case-insensitive.
  93. */
  94. size_t Curl_dynhds_ccount_name(struct dynhds *dynhds, const char *name);
  95. /**
  96. * Add a header, name + value, to `dynhds` at the end. Does *not*
  97. * check for duplicate names.
  98. */
  99. CURLcode Curl_dynhds_add(struct dynhds *dynhds,
  100. const char *name, size_t namelen,
  101. const char *value, size_t valuelen);
  102. /**
  103. * Add a header, c-string name + value, to `dynhds` at the end.
  104. */
  105. CURLcode Curl_dynhds_cadd(struct dynhds *dynhds,
  106. const char *name, const char *value);
  107. /**
  108. * Remove all entries with the given name.
  109. * Returns number of entries removed.
  110. */
  111. size_t Curl_dynhds_remove(struct dynhds *dynhds,
  112. const char *name, size_t namelen);
  113. size_t Curl_dynhds_cremove(struct dynhds *dynhds, const char *name);
  114. /**
  115. * Set the give header name and value, replacing any entries with
  116. * the same name. The header is added at the end of all (remaining)
  117. * entries.
  118. */
  119. CURLcode Curl_dynhds_set(struct dynhds *dynhds,
  120. const char *name, size_t namelen,
  121. const char *value, size_t valuelen);
  122. CURLcode Curl_dynhds_cset(struct dynhds *dynhds,
  123. const char *name, const char *value);
  124. /**
  125. * Add a single header from a HTTP/1.1 formatted line at the end. Line
  126. * may contain a delimiting \r\n or just \n. And characters after
  127. * that will be ignored.
  128. */
  129. CURLcode Curl_dynhds_h1_cadd_line(struct dynhds *dynhds, const char *line);
  130. /**
  131. * Add the headers to the given `dynbuf` in HTTP/1.1 format with
  132. * cr+lf line endings. Will NOT output a last empty line.
  133. */
  134. CURLcode Curl_dynhds_h1_dprint(struct dynhds *dynhds, struct dynbuf *dbuf);
  135. #endif /* HEADER_CURL_DYNHDS_H */