headers.c 11 KB

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