headers.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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 "sendf.h"
  29. #include "headers.h"
  30. /* The last 3 #include files should be in this order */
  31. #include "curl_printf.h"
  32. #include "curl_memory.h"
  33. #include "memdebug.h"
  34. #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_HEADERS_API)
  35. /* Generate the curl_header struct for the user. This function MUST assign all
  36. struct fields in the output struct. */
  37. static void copy_header_external(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;
  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(hs, nameindex, amount, e_pick,
  114. &data->state.headerout[0]);
  115. *hout = &data->state.headerout[0];
  116. return CURLHE_OK;
  117. }
  118. /* public API */
  119. struct curl_header *curl_easy_nextheader(CURL *easy,
  120. unsigned int type,
  121. int request,
  122. struct curl_header *prev)
  123. {
  124. struct Curl_easy *data = easy;
  125. struct Curl_llist_element *pick;
  126. struct Curl_llist_element *e;
  127. struct Curl_header_store *hs;
  128. size_t amount = 0;
  129. size_t index = 0;
  130. if(request > data->state.requests)
  131. return NULL;
  132. if(request == -1)
  133. request = data->state.requests;
  134. if(prev) {
  135. pick = prev->anchor;
  136. if(!pick)
  137. /* something is wrong */
  138. return NULL;
  139. pick = pick->next;
  140. }
  141. else
  142. pick = data->state.httphdrs.head;
  143. if(pick) {
  144. /* make sure it is the next header of the desired type */
  145. do {
  146. hs = pick->ptr;
  147. if((hs->type & type) && (hs->request == request))
  148. break;
  149. pick = pick->next;
  150. } while(pick);
  151. }
  152. if(!pick)
  153. /* no more headers available */
  154. return NULL;
  155. hs = pick->ptr;
  156. /* count number of occurrences of this name within the mask and figure out
  157. the index for the currently selected entry */
  158. for(e = data->state.httphdrs.head; e; e = e->next) {
  159. struct Curl_header_store *check = e->ptr;
  160. if(strcasecompare(hs->name, check->name) &&
  161. (check->request == request) &&
  162. (check->type & type))
  163. amount++;
  164. if(e == pick)
  165. index = amount - 1;
  166. }
  167. copy_header_external(hs, index, amount, pick,
  168. &data->state.headerout[1]);
  169. return &data->state.headerout[1];
  170. }
  171. static CURLcode namevalue(char *header, size_t hlen, unsigned int type,
  172. char **name, char **value)
  173. {
  174. char *end = header + hlen - 1; /* point to the last byte */
  175. DEBUGASSERT(hlen);
  176. *name = header;
  177. if(type == CURLH_PSEUDO) {
  178. if(*header != ':')
  179. return CURLE_BAD_FUNCTION_ARGUMENT;
  180. header++;
  181. }
  182. /* Find the end of the header name */
  183. while(*header && (*header != ':'))
  184. ++header;
  185. if(*header)
  186. /* Skip over colon, null it */
  187. *header++ = 0;
  188. else
  189. return CURLE_BAD_FUNCTION_ARGUMENT;
  190. /* skip all leading space letters */
  191. while(*header && ISBLANK(*header))
  192. header++;
  193. *value = header;
  194. /* skip all trailing space letters */
  195. while((end > header) && ISSPACE(*end))
  196. *end-- = 0; /* nul terminate */
  197. return CURLE_OK;
  198. }
  199. static CURLcode unfold_value(struct Curl_easy *data, const char *value,
  200. size_t vlen) /* length of the incoming header */
  201. {
  202. struct Curl_header_store *hs;
  203. struct Curl_header_store *newhs;
  204. size_t olen; /* length of the old value */
  205. size_t oalloc; /* length of the old name + value + separator */
  206. size_t offset;
  207. DEBUGASSERT(data->state.prevhead);
  208. hs = data->state.prevhead;
  209. olen = strlen(hs->value);
  210. offset = hs->value - hs->buffer;
  211. oalloc = olen + offset + 1;
  212. /* skip all trailing space letters */
  213. while(vlen && ISSPACE(value[vlen - 1]))
  214. vlen--;
  215. /* save only one leading space */
  216. while((vlen > 1) && ISBLANK(value[0]) && ISBLANK(value[1])) {
  217. vlen--;
  218. value++;
  219. }
  220. /* since this header block might move in the realloc below, it needs to
  221. first be unlinked from the list and then re-added again after the
  222. realloc */
  223. Curl_llist_remove(&data->state.httphdrs, &hs->node, NULL);
  224. /* new size = struct + new value length + old name+value length */
  225. newhs = Curl_saferealloc(hs, sizeof(*hs) + vlen + oalloc + 1);
  226. if(!newhs)
  227. return CURLE_OUT_OF_MEMORY;
  228. /* ->name' and ->value point into ->buffer (to keep the header allocation
  229. in a single memory block), which now potentially have moved. Adjust
  230. them. */
  231. newhs->name = newhs->buffer;
  232. newhs->value = &newhs->buffer[offset];
  233. /* put the data at the end of the previous data, not the newline */
  234. memcpy(&newhs->value[olen], value, vlen);
  235. newhs->value[olen + vlen] = 0; /* null-terminate at newline */
  236. /* insert this node into the list of headers */
  237. Curl_llist_append(&data->state.httphdrs, 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. /* neither CR nor LF as terminator is not a valid header */
  262. return CURLE_WEIRD_SERVER_REPLY;
  263. }
  264. hlen = end - header;
  265. if((header[0] == ' ') || (header[0] == '\t')) {
  266. if(data->state.prevhead)
  267. /* line folding, append value to the previous header's value */
  268. return unfold_value(data, header, hlen);
  269. else {
  270. /* Can't unfold without a previous header. Instead of erroring, just
  271. pass the leading blanks. */
  272. while(hlen && ISBLANK(*header)) {
  273. header++;
  274. hlen--;
  275. }
  276. if(!hlen)
  277. return CURLE_WEIRD_SERVER_REPLY;
  278. }
  279. }
  280. hs = calloc(1, sizeof(*hs) + hlen);
  281. if(!hs)
  282. return CURLE_OUT_OF_MEMORY;
  283. memcpy(hs->buffer, header, hlen);
  284. hs->buffer[hlen] = 0; /* nul terminate */
  285. result = namevalue(hs->buffer, hlen, type, &name, &value);
  286. if(!result) {
  287. hs->name = name;
  288. hs->value = value;
  289. hs->type = type;
  290. hs->request = data->state.requests;
  291. /* insert this node into the list of headers */
  292. Curl_llist_append(&data->state.httphdrs, hs, &hs->node);
  293. data->state.prevhead = hs;
  294. }
  295. else
  296. free(hs);
  297. return result;
  298. }
  299. /*
  300. * Curl_headers_reset(). Reset the headers subsystem.
  301. */
  302. static void headers_reset(struct Curl_easy *data)
  303. {
  304. Curl_llist_init(&data->state.httphdrs, NULL);
  305. data->state.prevhead = NULL;
  306. }
  307. struct hds_cw_collect_ctx {
  308. struct Curl_cwriter super;
  309. };
  310. static CURLcode hds_cw_collect_write(struct Curl_easy *data,
  311. struct Curl_cwriter *writer, int type,
  312. const char *buf, size_t blen)
  313. {
  314. if((type & CLIENTWRITE_HEADER) && !(type & CLIENTWRITE_STATUS)) {
  315. unsigned char htype = (unsigned char)
  316. (type & CLIENTWRITE_CONNECT ? CURLH_CONNECT :
  317. (type & CLIENTWRITE_1XX ? CURLH_1XX :
  318. (type & CLIENTWRITE_TRAILER ? CURLH_TRAILER :
  319. CURLH_HEADER)));
  320. CURLcode result = Curl_headers_push(data, buf, htype);
  321. CURL_TRC_WRITE(data, "header_collect pushed(type=%x, len=%zu) -> %d",
  322. htype, blen, result);
  323. if(result)
  324. return result;
  325. }
  326. return Curl_cwriter_write(data, writer->next, type, buf, blen);
  327. }
  328. static const struct Curl_cwtype hds_cw_collect = {
  329. "hds-collect",
  330. NULL,
  331. Curl_cwriter_def_init,
  332. hds_cw_collect_write,
  333. Curl_cwriter_def_close,
  334. sizeof(struct hds_cw_collect_ctx)
  335. };
  336. CURLcode Curl_headers_init(struct Curl_easy *data)
  337. {
  338. struct Curl_cwriter *writer;
  339. CURLcode result;
  340. if(data->conn && (data->conn->handler->protocol & PROTO_FAMILY_HTTP)) {
  341. /* avoid installing it twice */
  342. if(Curl_cwriter_get_by_name(data, hds_cw_collect.name))
  343. return CURLE_OK;
  344. result = Curl_cwriter_create(&writer, data, &hds_cw_collect,
  345. CURL_CW_PROTOCOL);
  346. if(result)
  347. return result;
  348. result = Curl_cwriter_add(data, writer);
  349. if(result) {
  350. Curl_cwriter_free(data, writer);
  351. return result;
  352. }
  353. }
  354. return CURLE_OK;
  355. }
  356. /*
  357. * Curl_headers_cleanup(). Free all stored headers and associated memory.
  358. */
  359. CURLcode Curl_headers_cleanup(struct Curl_easy *data)
  360. {
  361. struct Curl_llist_element *e;
  362. struct Curl_llist_element *n;
  363. for(e = data->state.httphdrs.head; e; e = n) {
  364. struct Curl_header_store *hs = e->ptr;
  365. n = e->next;
  366. free(hs);
  367. }
  368. headers_reset(data);
  369. return CURLE_OK;
  370. }
  371. #else /* HTTP-disabled builds below */
  372. CURLHcode curl_easy_header(CURL *easy,
  373. const char *name,
  374. size_t index,
  375. unsigned int origin,
  376. int request,
  377. struct curl_header **hout)
  378. {
  379. (void)easy;
  380. (void)name;
  381. (void)index;
  382. (void)origin;
  383. (void)request;
  384. (void)hout;
  385. return CURLHE_NOT_BUILT_IN;
  386. }
  387. struct curl_header *curl_easy_nextheader(CURL *easy,
  388. unsigned int type,
  389. int request,
  390. struct curl_header *prev)
  391. {
  392. (void)easy;
  393. (void)type;
  394. (void)request;
  395. (void)prev;
  396. return NULL;
  397. }
  398. #endif