headers.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2022, 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 "urldata.h"
  26. #include "strdup.h"
  27. #include "strcase.h"
  28. #include "headers.h"
  29. /* The last 3 #include files should be in this order */
  30. #include "curl_printf.h"
  31. #include "curl_memory.h"
  32. #include "memdebug.h"
  33. #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_HEADERS_API)
  34. /* Generate the curl_header struct for the user. This function MUST assign all
  35. struct fields in the output struct. */
  36. static void copy_header_external(struct Curl_easy *data,
  37. struct Curl_header_store *hs,
  38. size_t index,
  39. size_t amount,
  40. struct Curl_llist_element *e,
  41. struct curl_header **hout)
  42. {
  43. struct curl_header *h = *hout = &data->state.headerout;
  44. h->name = hs->name;
  45. h->value = hs->value;
  46. h->amount = amount;
  47. h->index = index;
  48. /* this will randomly OR a reserved bit for the sole purpose of making it
  49. impossible for applications to do == comparisons, as that would otherwise
  50. be very tempting and then lead to the reserved bits not being reserved
  51. anymore. */
  52. h->origin = hs->type | (1<<27);
  53. h->anchor = e;
  54. }
  55. /* public API */
  56. CURLHcode curl_easy_header(CURL *easy,
  57. const char *name,
  58. size_t nameindex,
  59. unsigned int type,
  60. int request,
  61. struct curl_header **hout)
  62. {
  63. struct Curl_llist_element *e;
  64. struct Curl_llist_element *e_pick = NULL;
  65. struct Curl_easy *data = easy;
  66. size_t match = 0;
  67. size_t amount = 0;
  68. struct Curl_header_store *hs = NULL;
  69. struct Curl_header_store *pick = NULL;
  70. if(!name || !hout || !data ||
  71. (type > (CURLH_HEADER|CURLH_TRAILER|CURLH_CONNECT|CURLH_1XX|
  72. CURLH_PSEUDO)) || !type || (request < -1))
  73. return CURLHE_BAD_ARGUMENT;
  74. if(!Curl_llist_count(&data->state.httphdrs))
  75. return CURLHE_NOHEADERS; /* no headers available */
  76. if(request > data->state.requests)
  77. return CURLHE_NOREQUEST;
  78. if(request == -1)
  79. request = data->state.requests;
  80. /* we need a first round to count amount of this header */
  81. for(e = data->state.httphdrs.head; e; e = e->next) {
  82. hs = e->ptr;
  83. if(strcasecompare(hs->name, name) &&
  84. (hs->type & type) &&
  85. (hs->request == request)) {
  86. amount++;
  87. pick = hs;
  88. e_pick = e;
  89. }
  90. }
  91. if(!amount)
  92. return CURLHE_MISSING;
  93. else if(nameindex >= amount)
  94. return CURLHE_BADINDEX;
  95. if(nameindex == amount - 1)
  96. /* if the last or only occurrence is what's asked for, then we know it */
  97. hs = pick;
  98. else {
  99. for(e = data->state.httphdrs.head; e; e = e->next) {
  100. hs = e->ptr;
  101. if(strcasecompare(hs->name, name) &&
  102. (hs->type & type) &&
  103. (hs->request == request) &&
  104. (match++ == nameindex)) {
  105. e_pick = e;
  106. break;
  107. }
  108. }
  109. if(!e) /* this shouldn't happen */
  110. return CURLHE_MISSING;
  111. }
  112. /* this is the name we want */
  113. copy_header_external(data, hs, nameindex, amount, e_pick, hout);
  114. return CURLHE_OK;
  115. }
  116. /* public API */
  117. struct curl_header *curl_easy_nextheader(CURL *easy,
  118. unsigned int type,
  119. int request,
  120. struct curl_header *prev)
  121. {
  122. struct Curl_easy *data = easy;
  123. struct Curl_llist_element *pick;
  124. struct Curl_llist_element *e;
  125. struct Curl_header_store *hs;
  126. struct curl_header *hout;
  127. size_t amount = 0;
  128. size_t index = 0;
  129. if(request > data->state.requests)
  130. return NULL;
  131. if(request == -1)
  132. request = data->state.requests;
  133. if(prev) {
  134. pick = prev->anchor;
  135. if(!pick)
  136. /* something is wrong */
  137. return NULL;
  138. pick = pick->next;
  139. }
  140. else
  141. pick = data->state.httphdrs.head;
  142. if(pick) {
  143. /* make sure it is the next header of the desired type */
  144. do {
  145. hs = pick->ptr;
  146. if((hs->type & type) && (hs->request == request))
  147. break;
  148. pick = pick->next;
  149. } while(pick);
  150. }
  151. if(!pick)
  152. /* no more headers available */
  153. return NULL;
  154. hs = pick->ptr;
  155. /* count number of occurrences of this name within the mask and figure out
  156. the index for the currently selected entry */
  157. for(e = data->state.httphdrs.head; e; e = e->next) {
  158. struct Curl_header_store *check = e->ptr;
  159. if(strcasecompare(hs->name, check->name) &&
  160. (check->request == request) &&
  161. (check->type & type))
  162. amount++;
  163. if(e == pick)
  164. index = amount - 1;
  165. }
  166. copy_header_external(data, hs, index, amount, pick, &hout);
  167. return hout;
  168. }
  169. static CURLcode namevalue(char *header, size_t hlen, unsigned int type,
  170. char **name, char **value)
  171. {
  172. char *end = header + hlen - 1; /* point to the last byte */
  173. DEBUGASSERT(hlen);
  174. *name = header;
  175. if(type == CURLH_PSEUDO) {
  176. if(*header != ':')
  177. return CURLE_BAD_FUNCTION_ARGUMENT;
  178. header++;
  179. }
  180. /* Find the end of the header name */
  181. while(*header && (*header != ':'))
  182. ++header;
  183. if(*header)
  184. /* Skip over colon, null it */
  185. *header++ = 0;
  186. else
  187. return CURLE_BAD_FUNCTION_ARGUMENT;
  188. /* skip all leading space letters */
  189. while(*header && ISBLANK(*header))
  190. header++;
  191. *value = header;
  192. /* skip all trailing space letters */
  193. while((end > header) && ISSPACE(*end))
  194. *end-- = 0; /* nul terminate */
  195. return CURLE_OK;
  196. }
  197. static CURLcode unfold_value(struct Curl_easy *data, const char *value,
  198. size_t vlen) /* length of the incoming header */
  199. {
  200. struct Curl_header_store *hs;
  201. struct Curl_header_store *newhs;
  202. size_t olen; /* length of the old value */
  203. size_t oalloc; /* length of the old name + value + separator */
  204. size_t offset;
  205. DEBUGASSERT(data->state.prevhead);
  206. hs = data->state.prevhead;
  207. olen = strlen(hs->value);
  208. offset = hs->value - hs->buffer;
  209. oalloc = olen + offset + 1;
  210. /* skip all trailing space letters */
  211. while(vlen && ISSPACE(value[vlen - 1]))
  212. vlen--;
  213. /* save only one leading space */
  214. while((vlen > 1) && ISBLANK(value[0]) && ISBLANK(value[1])) {
  215. vlen--;
  216. value++;
  217. }
  218. /* since this header block might move in the realloc below, it needs to
  219. first be unlinked from the list and then re-added again after the
  220. realloc */
  221. Curl_llist_remove(&data->state.httphdrs, &hs->node, NULL);
  222. /* new size = struct + new value length + old name+value length */
  223. newhs = Curl_saferealloc(hs, sizeof(*hs) + vlen + oalloc + 1);
  224. if(!newhs)
  225. return CURLE_OUT_OF_MEMORY;
  226. /* ->name' and ->value point into ->buffer (to keep the header allocation
  227. in a single memory block), which now potentially have moved. Adjust
  228. them. */
  229. newhs->name = newhs->buffer;
  230. newhs->value = &newhs->buffer[offset];
  231. /* put the data at the end of the previous data, not the newline */
  232. memcpy(&newhs->value[olen], value, vlen);
  233. newhs->value[olen + vlen] = 0; /* null-terminate at newline */
  234. /* insert this node into the list of headers */
  235. Curl_llist_insert_next(&data->state.httphdrs, data->state.httphdrs.tail,
  236. newhs, &newhs->node);
  237. data->state.prevhead = newhs;
  238. return CURLE_OK;
  239. }
  240. /*
  241. * Curl_headers_push() gets passed a full HTTP header to store. It gets called
  242. * immediately before the header callback. The header is CRLF terminated.
  243. */
  244. CURLcode Curl_headers_push(struct Curl_easy *data, const char *header,
  245. unsigned char type)
  246. {
  247. char *value = NULL;
  248. char *name = NULL;
  249. char *end;
  250. size_t hlen; /* length of the incoming header */
  251. struct Curl_header_store *hs;
  252. CURLcode result = CURLE_OUT_OF_MEMORY;
  253. if((header[0] == '\r') || (header[0] == '\n'))
  254. /* ignore the body separator */
  255. return CURLE_OK;
  256. end = strchr(header, '\r');
  257. if(!end) {
  258. end = strchr(header, '\n');
  259. if(!end)
  260. return CURLE_BAD_FUNCTION_ARGUMENT;
  261. }
  262. hlen = end - header + 1;
  263. if((header[0] == ' ') || (header[0] == '\t')) {
  264. if(data->state.prevhead)
  265. /* line folding, append value to the previous header's value */
  266. return unfold_value(data, header, hlen);
  267. else
  268. /* can't unfold without a previous header */
  269. return CURLE_BAD_FUNCTION_ARGUMENT;
  270. }
  271. hs = calloc(1, sizeof(*hs) + hlen);
  272. if(!hs)
  273. return CURLE_OUT_OF_MEMORY;
  274. memcpy(hs->buffer, header, hlen);
  275. hs->buffer[hlen] = 0; /* nul terminate */
  276. result = namevalue(hs->buffer, hlen, type, &name, &value);
  277. if(result)
  278. goto fail;
  279. hs->name = name;
  280. hs->value = value;
  281. hs->type = type;
  282. hs->request = data->state.requests;
  283. /* insert this node into the list of headers */
  284. Curl_llist_insert_next(&data->state.httphdrs, data->state.httphdrs.tail,
  285. hs, &hs->node);
  286. data->state.prevhead = hs;
  287. return CURLE_OK;
  288. fail:
  289. free(hs);
  290. return result;
  291. }
  292. /*
  293. * Curl_headers_init(). Init the headers subsystem.
  294. */
  295. static void headers_init(struct Curl_easy *data)
  296. {
  297. Curl_llist_init(&data->state.httphdrs, NULL);
  298. }
  299. /*
  300. * Curl_headers_cleanup(). Free all stored headers and associated memory.
  301. */
  302. CURLcode Curl_headers_cleanup(struct Curl_easy *data)
  303. {
  304. struct Curl_llist_element *e;
  305. struct Curl_llist_element *n;
  306. for(e = data->state.httphdrs.head; e; e = n) {
  307. struct Curl_header_store *hs = e->ptr;
  308. n = e->next;
  309. free(hs);
  310. }
  311. headers_init(data);
  312. return CURLE_OK;
  313. }
  314. #else /* HTTP-disabled builds below */
  315. CURLHcode curl_easy_header(CURL *easy,
  316. const char *name,
  317. size_t index,
  318. unsigned int origin,
  319. int request,
  320. struct curl_header **hout)
  321. {
  322. (void)easy;
  323. (void)name;
  324. (void)index;
  325. (void)origin;
  326. (void)request;
  327. (void)hout;
  328. return CURLHE_NOT_BUILT_IN;
  329. }
  330. struct curl_header *curl_easy_nextheader(CURL *easy,
  331. unsigned int type,
  332. int request,
  333. struct curl_header *prev)
  334. {
  335. (void)easy;
  336. (void)type;
  337. (void)request;
  338. (void)prev;
  339. return NULL;
  340. }
  341. #endif