dynhds.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #include "dynhds.h"
  26. #include "strcase.h"
  27. /* The last 3 #include files should be in this order */
  28. #include "curl_printf.h"
  29. #include "curl_memory.h"
  30. #include "memdebug.h"
  31. static struct dynhds_entry *
  32. entry_new(const char *name, size_t namelen,
  33. const char *value, size_t valuelen, int opts)
  34. {
  35. struct dynhds_entry *e;
  36. char *p;
  37. DEBUGASSERT(name);
  38. DEBUGASSERT(value);
  39. e = calloc(1, sizeof(*e) + namelen + valuelen + 2);
  40. if(!e)
  41. return NULL;
  42. e->name = p = ((char *)e) + sizeof(*e);
  43. memcpy(p, name, namelen);
  44. e->namelen = namelen;
  45. e->value = p += namelen + 1; /* leave a \0 at the end of name */
  46. memcpy(p, value, valuelen);
  47. e->valuelen = valuelen;
  48. if(opts & DYNHDS_OPT_LOWERCASE)
  49. Curl_strntolower(e->name, e->name, e->namelen);
  50. return e;
  51. }
  52. static struct dynhds_entry *
  53. entry_append(struct dynhds_entry *e,
  54. const char *value, size_t valuelen)
  55. {
  56. struct dynhds_entry *e2;
  57. size_t valuelen2 = e->valuelen + 1 + valuelen;
  58. char *p;
  59. DEBUGASSERT(value);
  60. e2 = calloc(1, sizeof(*e) + e->namelen + valuelen2 + 2);
  61. if(!e2)
  62. return NULL;
  63. e2->name = p = ((char *)e2) + sizeof(*e2);
  64. memcpy(p, e->name, e->namelen);
  65. e2->namelen = e->namelen;
  66. e2->value = p += e->namelen + 1; /* leave a \0 at the end of name */
  67. memcpy(p, e->value, e->valuelen);
  68. p += e->valuelen;
  69. p[0] = ' ';
  70. memcpy(p + 1, value, valuelen);
  71. e2->valuelen = valuelen2;
  72. return e2;
  73. }
  74. static void entry_free(struct dynhds_entry *e)
  75. {
  76. free(e);
  77. }
  78. void Curl_dynhds_init(struct dynhds *dynhds, size_t max_entries,
  79. size_t max_strs_size)
  80. {
  81. DEBUGASSERT(dynhds);
  82. DEBUGASSERT(max_strs_size);
  83. dynhds->hds = NULL;
  84. dynhds->hds_len = dynhds->hds_allc = dynhds->strs_len = 0;
  85. dynhds->max_entries = max_entries;
  86. dynhds->max_strs_size = max_strs_size;
  87. dynhds->opts = 0;
  88. }
  89. void Curl_dynhds_free(struct dynhds *dynhds)
  90. {
  91. DEBUGASSERT(dynhds);
  92. if(dynhds->hds && dynhds->hds_len) {
  93. size_t i;
  94. DEBUGASSERT(dynhds->hds);
  95. for(i = 0; i < dynhds->hds_len; ++i) {
  96. entry_free(dynhds->hds[i]);
  97. }
  98. }
  99. Curl_safefree(dynhds->hds);
  100. dynhds->hds_len = dynhds->hds_allc = dynhds->strs_len = 0;
  101. }
  102. void Curl_dynhds_reset(struct dynhds *dynhds)
  103. {
  104. DEBUGASSERT(dynhds);
  105. if(dynhds->hds_len) {
  106. size_t i;
  107. DEBUGASSERT(dynhds->hds);
  108. for(i = 0; i < dynhds->hds_len; ++i) {
  109. entry_free(dynhds->hds[i]);
  110. dynhds->hds[i] = NULL;
  111. }
  112. }
  113. dynhds->hds_len = dynhds->strs_len = 0;
  114. }
  115. size_t Curl_dynhds_count(struct dynhds *dynhds)
  116. {
  117. return dynhds->hds_len;
  118. }
  119. void Curl_dynhds_set_opts(struct dynhds *dynhds, int opts)
  120. {
  121. dynhds->opts = opts;
  122. }
  123. struct dynhds_entry *Curl_dynhds_getn(struct dynhds *dynhds, size_t n)
  124. {
  125. DEBUGASSERT(dynhds);
  126. return (n < dynhds->hds_len)? dynhds->hds[n] : NULL;
  127. }
  128. struct dynhds_entry *Curl_dynhds_get(struct dynhds *dynhds, const char *name,
  129. size_t namelen)
  130. {
  131. size_t i;
  132. for(i = 0; i < dynhds->hds_len; ++i) {
  133. if(dynhds->hds[i]->namelen == namelen &&
  134. strncasecompare(dynhds->hds[i]->name, name, namelen)) {
  135. return dynhds->hds[i];
  136. }
  137. }
  138. return NULL;
  139. }
  140. struct dynhds_entry *Curl_dynhds_cget(struct dynhds *dynhds, const char *name)
  141. {
  142. return Curl_dynhds_get(dynhds, name, strlen(name));
  143. }
  144. CURLcode Curl_dynhds_add(struct dynhds *dynhds,
  145. const char *name, size_t namelen,
  146. const char *value, size_t valuelen)
  147. {
  148. struct dynhds_entry *entry = NULL;
  149. CURLcode result = CURLE_OUT_OF_MEMORY;
  150. DEBUGASSERT(dynhds);
  151. if(dynhds->max_entries && dynhds->hds_len >= dynhds->max_entries)
  152. return CURLE_OUT_OF_MEMORY;
  153. if(dynhds->strs_len + namelen + valuelen > dynhds->max_strs_size)
  154. return CURLE_OUT_OF_MEMORY;
  155. entry = entry_new(name, namelen, value, valuelen, dynhds->opts);
  156. if(!entry)
  157. goto out;
  158. if(dynhds->hds_len + 1 >= dynhds->hds_allc) {
  159. size_t nallc = dynhds->hds_len + 16;
  160. struct dynhds_entry **nhds;
  161. if(dynhds->max_entries && nallc > dynhds->max_entries)
  162. nallc = dynhds->max_entries;
  163. nhds = calloc(nallc, sizeof(struct dynhds_entry *));
  164. if(!nhds)
  165. goto out;
  166. if(dynhds->hds) {
  167. memcpy(nhds, dynhds->hds,
  168. dynhds->hds_len * sizeof(struct dynhds_entry *));
  169. Curl_safefree(dynhds->hds);
  170. }
  171. dynhds->hds = nhds;
  172. dynhds->hds_allc = nallc;
  173. }
  174. dynhds->hds[dynhds->hds_len++] = entry;
  175. entry = NULL;
  176. dynhds->strs_len += namelen + valuelen;
  177. result = CURLE_OK;
  178. out:
  179. if(entry)
  180. entry_free(entry);
  181. return result;
  182. }
  183. CURLcode Curl_dynhds_cadd(struct dynhds *dynhds,
  184. const char *name, const char *value)
  185. {
  186. return Curl_dynhds_add(dynhds, name, strlen(name), value, strlen(value));
  187. }
  188. CURLcode Curl_dynhds_h1_add_line(struct dynhds *dynhds,
  189. const char *line, size_t line_len)
  190. {
  191. const char *p;
  192. const char *name;
  193. size_t namelen;
  194. const char *value;
  195. size_t valuelen, i;
  196. if(!line || !line_len)
  197. return CURLE_OK;
  198. if((line[0] == ' ') || (line[0] == '\t')) {
  199. struct dynhds_entry *e, *e2;
  200. /* header continuation, yikes! */
  201. if(!dynhds->hds_len)
  202. return CURLE_BAD_FUNCTION_ARGUMENT;
  203. while(line_len && ISBLANK(line[0])) {
  204. ++line;
  205. --line_len;
  206. }
  207. if(!line_len)
  208. return CURLE_BAD_FUNCTION_ARGUMENT;
  209. e = dynhds->hds[dynhds->hds_len-1];
  210. e2 = entry_append(e, line, line_len);
  211. if(!e2)
  212. return CURLE_OUT_OF_MEMORY;
  213. dynhds->hds[dynhds->hds_len-1] = e2;
  214. entry_free(e);
  215. return CURLE_OK;
  216. }
  217. else {
  218. p = memchr(line, ':', line_len);
  219. if(!p)
  220. return CURLE_BAD_FUNCTION_ARGUMENT;
  221. name = line;
  222. namelen = p - line;
  223. p++; /* move past the colon */
  224. for(i = namelen + 1; i < line_len; ++i, ++p) {
  225. if(!ISBLANK(*p))
  226. break;
  227. }
  228. value = p;
  229. valuelen = line_len - i;
  230. p = memchr(value, '\r', valuelen);
  231. if(!p)
  232. p = memchr(value, '\n', valuelen);
  233. if(p)
  234. valuelen = (size_t)(p - value);
  235. return Curl_dynhds_add(dynhds, name, namelen, value, valuelen);
  236. }
  237. }
  238. CURLcode Curl_dynhds_h1_cadd_line(struct dynhds *dynhds, const char *line)
  239. {
  240. return Curl_dynhds_h1_add_line(dynhds, line, line? strlen(line) : 0);
  241. }
  242. #ifdef DEBUGBUILD
  243. /* used by unit2602.c */
  244. bool Curl_dynhds_contains(struct dynhds *dynhds,
  245. const char *name, size_t namelen)
  246. {
  247. return !!Curl_dynhds_get(dynhds, name, namelen);
  248. }
  249. bool Curl_dynhds_ccontains(struct dynhds *dynhds, const char *name)
  250. {
  251. return Curl_dynhds_contains(dynhds, name, strlen(name));
  252. }
  253. size_t Curl_dynhds_count_name(struct dynhds *dynhds,
  254. const char *name, size_t namelen)
  255. {
  256. size_t n = 0;
  257. if(dynhds->hds_len) {
  258. size_t i;
  259. for(i = 0; i < dynhds->hds_len; ++i) {
  260. if((namelen == dynhds->hds[i]->namelen) &&
  261. strncasecompare(name, dynhds->hds[i]->name, namelen))
  262. ++n;
  263. }
  264. }
  265. return n;
  266. }
  267. size_t Curl_dynhds_ccount_name(struct dynhds *dynhds, const char *name)
  268. {
  269. return Curl_dynhds_count_name(dynhds, name, strlen(name));
  270. }
  271. CURLcode Curl_dynhds_set(struct dynhds *dynhds,
  272. const char *name, size_t namelen,
  273. const char *value, size_t valuelen)
  274. {
  275. Curl_dynhds_remove(dynhds, name, namelen);
  276. return Curl_dynhds_add(dynhds, name, namelen, value, valuelen);
  277. }
  278. size_t Curl_dynhds_remove(struct dynhds *dynhds,
  279. const char *name, size_t namelen)
  280. {
  281. size_t n = 0;
  282. if(dynhds->hds_len) {
  283. size_t i, len;
  284. for(i = 0; i < dynhds->hds_len; ++i) {
  285. if((namelen == dynhds->hds[i]->namelen) &&
  286. strncasecompare(name, dynhds->hds[i]->name, namelen)) {
  287. ++n;
  288. --dynhds->hds_len;
  289. dynhds->strs_len -= (dynhds->hds[i]->namelen +
  290. dynhds->hds[i]->valuelen);
  291. entry_free(dynhds->hds[i]);
  292. len = dynhds->hds_len - i; /* remaining entries */
  293. if(len) {
  294. memmove(&dynhds->hds[i], &dynhds->hds[i + 1],
  295. len * sizeof(dynhds->hds[i]));
  296. }
  297. --i; /* do this index again */
  298. }
  299. }
  300. }
  301. return n;
  302. }
  303. size_t Curl_dynhds_cremove(struct dynhds *dynhds, const char *name)
  304. {
  305. return Curl_dynhds_remove(dynhds, name, strlen(name));
  306. }
  307. #endif
  308. CURLcode Curl_dynhds_h1_dprint(struct dynhds *dynhds, struct dynbuf *dbuf)
  309. {
  310. CURLcode result = CURLE_OK;
  311. size_t i;
  312. if(!dynhds->hds_len)
  313. return result;
  314. for(i = 0; i < dynhds->hds_len; ++i) {
  315. result = Curl_dyn_addf(dbuf, "%.*s: %.*s\r\n",
  316. (int)dynhds->hds[i]->namelen, dynhds->hds[i]->name,
  317. (int)dynhds->hds[i]->valuelen, dynhds->hds[i]->value);
  318. if(result)
  319. break;
  320. }
  321. return result;
  322. }