dynhds.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. #ifdef USE_NGHTTP2
  29. #include <stdint.h>
  30. #include <nghttp2/nghttp2.h>
  31. #endif /* USE_NGHTTP2 */
  32. #include "curl_printf.h"
  33. #include "curl_memory.h"
  34. #include "memdebug.h"
  35. static struct dynhds_entry *
  36. entry_new(const char *name, size_t namelen,
  37. const char *value, size_t valuelen, int opts)
  38. {
  39. struct dynhds_entry *e;
  40. char *p;
  41. DEBUGASSERT(name);
  42. DEBUGASSERT(value);
  43. e = calloc(1, sizeof(*e) + namelen + valuelen + 2);
  44. if(!e)
  45. return NULL;
  46. e->name = p = ((char *)e) + sizeof(*e);
  47. memcpy(p, name, namelen);
  48. e->namelen = namelen;
  49. e->value = p += namelen + 1; /* leave a \0 at the end of name */
  50. memcpy(p, value, valuelen);
  51. e->valuelen = valuelen;
  52. if(opts & DYNHDS_OPT_LOWERCASE)
  53. Curl_strntolower(e->name, e->name, e->namelen);
  54. return e;
  55. }
  56. static struct dynhds_entry *
  57. entry_append(struct dynhds_entry *e,
  58. const char *value, size_t valuelen)
  59. {
  60. struct dynhds_entry *e2;
  61. size_t valuelen2 = e->valuelen + 1 + valuelen;
  62. char *p;
  63. DEBUGASSERT(value);
  64. e2 = calloc(1, sizeof(*e) + e->namelen + valuelen2 + 2);
  65. if(!e2)
  66. return NULL;
  67. e2->name = p = ((char *)e2) + sizeof(*e2);
  68. memcpy(p, e->name, e->namelen);
  69. e2->namelen = e->namelen;
  70. e2->value = p += e->namelen + 1; /* leave a \0 at the end of name */
  71. memcpy(p, e->value, e->valuelen);
  72. p += e->valuelen;
  73. p[0] = ' ';
  74. memcpy(p + 1, value, valuelen);
  75. e2->valuelen = valuelen2;
  76. return e2;
  77. }
  78. static void entry_free(struct dynhds_entry *e)
  79. {
  80. free(e);
  81. }
  82. void Curl_dynhds_init(struct dynhds *dynhds, size_t max_entries,
  83. size_t max_strs_size)
  84. {
  85. DEBUGASSERT(dynhds);
  86. DEBUGASSERT(max_strs_size);
  87. dynhds->hds = NULL;
  88. dynhds->hds_len = dynhds->hds_allc = dynhds->strs_len = 0;
  89. dynhds->max_entries = max_entries;
  90. dynhds->max_strs_size = max_strs_size;
  91. dynhds->opts = 0;
  92. }
  93. void Curl_dynhds_free(struct dynhds *dynhds)
  94. {
  95. DEBUGASSERT(dynhds);
  96. if(dynhds->hds && dynhds->hds_len) {
  97. size_t i;
  98. DEBUGASSERT(dynhds->hds);
  99. for(i = 0; i < dynhds->hds_len; ++i) {
  100. entry_free(dynhds->hds[i]);
  101. }
  102. }
  103. Curl_safefree(dynhds->hds);
  104. dynhds->hds_len = dynhds->hds_allc = dynhds->strs_len = 0;
  105. }
  106. void Curl_dynhds_reset(struct dynhds *dynhds)
  107. {
  108. DEBUGASSERT(dynhds);
  109. if(dynhds->hds_len) {
  110. size_t i;
  111. DEBUGASSERT(dynhds->hds);
  112. for(i = 0; i < dynhds->hds_len; ++i) {
  113. entry_free(dynhds->hds[i]);
  114. dynhds->hds[i] = NULL;
  115. }
  116. }
  117. dynhds->hds_len = dynhds->strs_len = 0;
  118. }
  119. size_t Curl_dynhds_count(struct dynhds *dynhds)
  120. {
  121. return dynhds->hds_len;
  122. }
  123. void Curl_dynhds_set_opts(struct dynhds *dynhds, int opts)
  124. {
  125. dynhds->opts = opts;
  126. }
  127. struct dynhds_entry *Curl_dynhds_getn(struct dynhds *dynhds, size_t n)
  128. {
  129. DEBUGASSERT(dynhds);
  130. return (n < dynhds->hds_len)? dynhds->hds[n] : NULL;
  131. }
  132. struct dynhds_entry *Curl_dynhds_get(struct dynhds *dynhds, const char *name,
  133. size_t namelen)
  134. {
  135. size_t i;
  136. for(i = 0; i < dynhds->hds_len; ++i) {
  137. if(dynhds->hds[i]->namelen == namelen &&
  138. strncasecompare(dynhds->hds[i]->name, name, namelen)) {
  139. return dynhds->hds[i];
  140. }
  141. }
  142. return NULL;
  143. }
  144. struct dynhds_entry *Curl_dynhds_cget(struct dynhds *dynhds, const char *name)
  145. {
  146. return Curl_dynhds_get(dynhds, name, strlen(name));
  147. }
  148. CURLcode Curl_dynhds_add(struct dynhds *dynhds,
  149. const char *name, size_t namelen,
  150. const char *value, size_t valuelen)
  151. {
  152. struct dynhds_entry *entry = NULL;
  153. CURLcode result = CURLE_OUT_OF_MEMORY;
  154. DEBUGASSERT(dynhds);
  155. if(dynhds->max_entries && dynhds->hds_len >= dynhds->max_entries)
  156. return CURLE_OUT_OF_MEMORY;
  157. if(dynhds->strs_len + namelen + valuelen > dynhds->max_strs_size)
  158. return CURLE_OUT_OF_MEMORY;
  159. entry = entry_new(name, namelen, value, valuelen, dynhds->opts);
  160. if(!entry)
  161. goto out;
  162. if(dynhds->hds_len + 1 >= dynhds->hds_allc) {
  163. size_t nallc = dynhds->hds_len + 16;
  164. struct dynhds_entry **nhds;
  165. if(dynhds->max_entries && nallc > dynhds->max_entries)
  166. nallc = dynhds->max_entries;
  167. nhds = calloc(nallc, sizeof(struct dynhds_entry *));
  168. if(!nhds)
  169. goto out;
  170. if(dynhds->hds) {
  171. memcpy(nhds, dynhds->hds,
  172. dynhds->hds_len * sizeof(struct dynhds_entry *));
  173. Curl_safefree(dynhds->hds);
  174. }
  175. dynhds->hds = nhds;
  176. dynhds->hds_allc = nallc;
  177. }
  178. dynhds->hds[dynhds->hds_len++] = entry;
  179. entry = NULL;
  180. dynhds->strs_len += namelen + valuelen;
  181. result = CURLE_OK;
  182. out:
  183. if(entry)
  184. entry_free(entry);
  185. return result;
  186. }
  187. CURLcode Curl_dynhds_cadd(struct dynhds *dynhds,
  188. const char *name, const char *value)
  189. {
  190. return Curl_dynhds_add(dynhds, name, strlen(name), value, strlen(value));
  191. }
  192. CURLcode Curl_dynhds_h1_add_line(struct dynhds *dynhds,
  193. const char *line, size_t line_len)
  194. {
  195. const char *p;
  196. const char *name;
  197. size_t namelen;
  198. const char *value;
  199. size_t valuelen, i;
  200. if(!line || !line_len)
  201. return CURLE_OK;
  202. if((line[0] == ' ') || (line[0] == '\t')) {
  203. struct dynhds_entry *e, *e2;
  204. /* header continuation, yikes! */
  205. if(!dynhds->hds_len)
  206. return CURLE_BAD_FUNCTION_ARGUMENT;
  207. while(line_len && ISBLANK(line[0])) {
  208. ++line;
  209. --line_len;
  210. }
  211. if(!line_len)
  212. return CURLE_BAD_FUNCTION_ARGUMENT;
  213. e = dynhds->hds[dynhds->hds_len-1];
  214. e2 = entry_append(e, line, line_len);
  215. if(!e2)
  216. return CURLE_OUT_OF_MEMORY;
  217. dynhds->hds[dynhds->hds_len-1] = e2;
  218. entry_free(e);
  219. return CURLE_OK;
  220. }
  221. else {
  222. p = memchr(line, ':', line_len);
  223. if(!p)
  224. return CURLE_BAD_FUNCTION_ARGUMENT;
  225. name = line;
  226. namelen = p - line;
  227. p++; /* move past the colon */
  228. for(i = namelen + 1; i < line_len; ++i, ++p) {
  229. if(!ISBLANK(*p))
  230. break;
  231. }
  232. value = p;
  233. valuelen = line_len - i;
  234. p = memchr(value, '\r', valuelen);
  235. if(!p)
  236. p = memchr(value, '\n', valuelen);
  237. if(p)
  238. valuelen = (size_t)(p - value);
  239. return Curl_dynhds_add(dynhds, name, namelen, value, valuelen);
  240. }
  241. }
  242. CURLcode Curl_dynhds_h1_cadd_line(struct dynhds *dynhds, const char *line)
  243. {
  244. return Curl_dynhds_h1_add_line(dynhds, line, line? strlen(line) : 0);
  245. }
  246. #ifdef UNITTESTS
  247. /* used by unit2602.c */
  248. bool Curl_dynhds_contains(struct dynhds *dynhds,
  249. const char *name, size_t namelen)
  250. {
  251. return !!Curl_dynhds_get(dynhds, name, namelen);
  252. }
  253. bool Curl_dynhds_ccontains(struct dynhds *dynhds, const char *name)
  254. {
  255. return Curl_dynhds_contains(dynhds, name, strlen(name));
  256. }
  257. size_t Curl_dynhds_count_name(struct dynhds *dynhds,
  258. const char *name, size_t namelen)
  259. {
  260. size_t n = 0;
  261. if(dynhds->hds_len) {
  262. size_t i;
  263. for(i = 0; i < dynhds->hds_len; ++i) {
  264. if((namelen == dynhds->hds[i]->namelen) &&
  265. strncasecompare(name, dynhds->hds[i]->name, namelen))
  266. ++n;
  267. }
  268. }
  269. return n;
  270. }
  271. size_t Curl_dynhds_ccount_name(struct dynhds *dynhds, const char *name)
  272. {
  273. return Curl_dynhds_count_name(dynhds, name, strlen(name));
  274. }
  275. CURLcode Curl_dynhds_set(struct dynhds *dynhds,
  276. const char *name, size_t namelen,
  277. const char *value, size_t valuelen)
  278. {
  279. Curl_dynhds_remove(dynhds, name, namelen);
  280. return Curl_dynhds_add(dynhds, name, namelen, value, valuelen);
  281. }
  282. size_t Curl_dynhds_remove(struct dynhds *dynhds,
  283. const char *name, size_t namelen)
  284. {
  285. size_t n = 0;
  286. if(dynhds->hds_len) {
  287. size_t i, len;
  288. for(i = 0; i < dynhds->hds_len; ++i) {
  289. if((namelen == dynhds->hds[i]->namelen) &&
  290. strncasecompare(name, dynhds->hds[i]->name, namelen)) {
  291. ++n;
  292. --dynhds->hds_len;
  293. dynhds->strs_len -= (dynhds->hds[i]->namelen +
  294. dynhds->hds[i]->valuelen);
  295. entry_free(dynhds->hds[i]);
  296. len = dynhds->hds_len - i; /* remaining entries */
  297. if(len) {
  298. memmove(&dynhds->hds[i], &dynhds->hds[i + 1],
  299. len * sizeof(dynhds->hds[i]));
  300. }
  301. --i; /* do this index again */
  302. }
  303. }
  304. }
  305. return n;
  306. }
  307. size_t Curl_dynhds_cremove(struct dynhds *dynhds, const char *name)
  308. {
  309. return Curl_dynhds_remove(dynhds, name, strlen(name));
  310. }
  311. #endif
  312. CURLcode Curl_dynhds_h1_dprint(struct dynhds *dynhds, struct dynbuf *dbuf)
  313. {
  314. CURLcode result = CURLE_OK;
  315. size_t i;
  316. if(!dynhds->hds_len)
  317. return result;
  318. for(i = 0; i < dynhds->hds_len; ++i) {
  319. result = Curl_dyn_addf(dbuf, "%.*s: %.*s\r\n",
  320. (int)dynhds->hds[i]->namelen, dynhds->hds[i]->name,
  321. (int)dynhds->hds[i]->valuelen, dynhds->hds[i]->value);
  322. if(result)
  323. break;
  324. }
  325. return result;
  326. }
  327. #ifdef USE_NGHTTP2
  328. nghttp2_nv *Curl_dynhds_to_nva(struct dynhds *dynhds, size_t *pcount)
  329. {
  330. nghttp2_nv *nva = calloc(1, sizeof(nghttp2_nv) * dynhds->hds_len);
  331. size_t i;
  332. *pcount = 0;
  333. if(!nva)
  334. return NULL;
  335. for(i = 0; i < dynhds->hds_len; ++i) {
  336. struct dynhds_entry *e = dynhds->hds[i];
  337. DEBUGASSERT(e);
  338. nva[i].name = (unsigned char *)e->name;
  339. nva[i].namelen = e->namelen;
  340. nva[i].value = (unsigned char *)e->value;
  341. nva[i].valuelen = e->valuelen;
  342. nva[i].flags = NGHTTP2_NV_FLAG_NONE;
  343. }
  344. *pcount = dynhds->hds_len;
  345. return nva;
  346. }
  347. #endif /* USE_NGHTTP2 */