Line | Count | Source |
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 | | |
26 | | #include "urldata.h" |
27 | | #include "sendf.h" |
28 | | #include "curl_trc.h" |
29 | | #include "headers.h" |
30 | | |
31 | | #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_HEADERS_API) |
32 | | |
33 | | /* Generate the curl_header struct for the user. This function MUST assign all |
34 | | struct fields in the output struct. */ |
35 | | static void copy_header_external(struct Curl_header_store *hs, |
36 | | size_t index, |
37 | | size_t amount, |
38 | | struct Curl_llist_node *e, |
39 | | struct curl_header *hout) |
40 | 0 | { |
41 | 0 | struct curl_header *h = hout; |
42 | 0 | h->name = hs->name; |
43 | 0 | h->value = hs->value; |
44 | 0 | h->amount = amount; |
45 | 0 | h->index = index; |
46 | | /* this will randomly OR a reserved bit for the sole purpose of making it |
47 | | impossible for applications to do == comparisons, as that would otherwise |
48 | | be tempting and then lead to the reserved bits not being reserved |
49 | | anymore. */ |
50 | 0 | h->origin = (unsigned int)(hs->type | (1 << 27)); |
51 | 0 | h->anchor = e; |
52 | 0 | } |
53 | | |
54 | | /* public API */ |
55 | | CURLHcode curl_easy_header(CURL *curl, |
56 | | const char *name, |
57 | | size_t nameindex, |
58 | | unsigned int origin, |
59 | | int request, |
60 | | struct curl_header **hout) |
61 | 0 | { |
62 | 0 | struct Curl_eapi_guard guard; |
63 | 0 | CURLHcode hresult = CURLHE_OK; |
64 | 0 | CURLcode result; |
65 | |
|
66 | 0 | if(CURL_EAPI_ENTER(&guard, curl, easy_header, &result)) { |
67 | 0 | struct Curl_easy *data = curl; |
68 | 0 | struct Curl_llist_node *e; |
69 | 0 | struct Curl_llist_node *e_pick = NULL; |
70 | 0 | size_t match = 0; |
71 | 0 | size_t amount = 0; |
72 | 0 | struct Curl_header_store *hs = NULL; |
73 | 0 | struct Curl_header_store *pick = NULL; |
74 | 0 | if(!name || !hout || !data || |
75 | 0 | (origin > (CURLH_HEADER | CURLH_TRAILER | CURLH_CONNECT | CURLH_1XX | |
76 | 0 | CURLH_PSEUDO)) || !origin || (request < -1)) { |
77 | 0 | hresult = CURLHE_BAD_ARGUMENT; |
78 | 0 | goto out; |
79 | 0 | } |
80 | 0 | if(!Curl_llist_count(&data->state.httphdrs)) { |
81 | 0 | hresult = CURLHE_NOHEADERS; /* no headers available */ |
82 | 0 | goto out; |
83 | 0 | } |
84 | 0 | if(request > data->state.requests) { |
85 | 0 | hresult = CURLHE_NOREQUEST; |
86 | 0 | goto out; |
87 | 0 | } |
88 | 0 | if(request == -1) |
89 | 0 | request = data->state.requests; |
90 | | |
91 | | /* we need a first round to count amount of this header */ |
92 | 0 | for(e = Curl_llist_head(&data->state.httphdrs); e; e = Curl_node_next(e)) { |
93 | 0 | hs = Curl_node_elem(e); |
94 | 0 | if(curl_strequal(hs->name, name) && |
95 | 0 | (hs->type & origin) && |
96 | 0 | (hs->request == request)) { |
97 | 0 | amount++; |
98 | 0 | pick = hs; |
99 | 0 | e_pick = e; |
100 | 0 | } |
101 | 0 | } |
102 | 0 | if(!amount) |
103 | 0 | hresult = CURLHE_MISSING; |
104 | 0 | else if(nameindex >= amount) |
105 | 0 | hresult = CURLHE_BADINDEX; |
106 | 0 | if(hresult) |
107 | 0 | goto out; |
108 | | |
109 | 0 | if(nameindex == amount - 1) |
110 | | /* if the last or only occurrence is what's asked for, then we know it */ |
111 | 0 | hs = pick; |
112 | 0 | else { |
113 | 0 | for(e = Curl_llist_head(&data->state.httphdrs); e; |
114 | 0 | e = Curl_node_next(e)) { |
115 | 0 | hs = Curl_node_elem(e); |
116 | 0 | if(curl_strequal(hs->name, name) && |
117 | 0 | (hs->type & origin) && |
118 | 0 | (hs->request == request) && |
119 | 0 | (match++ == nameindex)) { |
120 | 0 | e_pick = e; |
121 | 0 | break; |
122 | 0 | } |
123 | 0 | } |
124 | 0 | if(!e) { /* this should not happen */ |
125 | 0 | hresult = CURLHE_MISSING; |
126 | 0 | goto out; |
127 | 0 | } |
128 | 0 | } |
129 | | /* this is the name we want */ |
130 | 0 | copy_header_external(hs, nameindex, amount, e_pick, |
131 | 0 | &data->state.headerout[0]); |
132 | 0 | *hout = &data->state.headerout[0]; |
133 | 0 | hresult = CURLHE_OK; |
134 | 0 | } |
135 | 0 | out: |
136 | 0 | CURL_EAPI_LEAVE(&guard); |
137 | 0 | if(result) |
138 | 0 | hresult = Curl_eapi_hcode(result); |
139 | 0 | return hresult; |
140 | 0 | } |
141 | | |
142 | | /* public API */ |
143 | | struct curl_header *curl_easy_nextheader(CURL *curl, |
144 | | unsigned int origin, |
145 | | int request, |
146 | | struct curl_header *prev) |
147 | 0 | { |
148 | 0 | struct Curl_eapi_guard guard; |
149 | 0 | struct curl_header *hd = NULL; |
150 | 0 | CURLcode result; |
151 | |
|
152 | 0 | if(CURL_EAPI_ENTER(&guard, curl, easy_nextheader, &result)) { |
153 | 0 | struct Curl_easy *data = curl; |
154 | 0 | struct Curl_llist_node *pick; |
155 | 0 | struct Curl_llist_node *e; |
156 | 0 | struct Curl_header_store *hs; |
157 | 0 | size_t amount = 0; |
158 | 0 | size_t index = 0; |
159 | |
|
160 | 0 | if(request > data->state.requests) |
161 | 0 | goto out; |
162 | 0 | if(request == -1) |
163 | 0 | request = data->state.requests; |
164 | |
|
165 | 0 | if(prev) { |
166 | 0 | pick = prev->anchor; |
167 | 0 | if(!pick) |
168 | | /* something is wrong */ |
169 | 0 | goto out; |
170 | 0 | pick = Curl_node_next(pick); |
171 | 0 | } |
172 | 0 | else |
173 | 0 | pick = Curl_llist_head(&data->state.httphdrs); |
174 | | |
175 | 0 | if(pick) { |
176 | | /* make sure it is the next header of the desired type */ |
177 | 0 | do { |
178 | 0 | hs = Curl_node_elem(pick); |
179 | 0 | if((hs->type & origin) && (hs->request == request)) |
180 | 0 | break; |
181 | 0 | pick = Curl_node_next(pick); |
182 | 0 | } while(pick); |
183 | 0 | } |
184 | |
|
185 | 0 | if(!pick) |
186 | | /* no more headers available */ |
187 | 0 | goto out; |
188 | | |
189 | 0 | hs = Curl_node_elem(pick); |
190 | | |
191 | | /* count number of occurrences of this name within the mask and figure out |
192 | | the index for the currently selected entry */ |
193 | 0 | for(e = Curl_llist_head(&data->state.httphdrs); e; e = Curl_node_next(e)) { |
194 | 0 | struct Curl_header_store *check = Curl_node_elem(e); |
195 | 0 | if(curl_strequal(hs->name, check->name) && |
196 | 0 | (check->request == request) && |
197 | 0 | (check->type & origin)) |
198 | 0 | amount++; |
199 | 0 | if(e == pick) |
200 | 0 | index = amount - 1; |
201 | 0 | } |
202 | |
|
203 | 0 | copy_header_external(hs, index, amount, pick, |
204 | 0 | &data->state.headerout[1]); |
205 | 0 | hd = &data->state.headerout[1]; |
206 | 0 | } |
207 | 0 | out: |
208 | 0 | CURL_EAPI_LEAVE(&guard); |
209 | 0 | return hd; |
210 | 0 | } |
211 | | |
212 | | static CURLcode namevalue(char *header, size_t hlen, unsigned int type, |
213 | | char **name, char **value) |
214 | 233k | { |
215 | 233k | char *end = header + hlen - 1; /* point to the last byte */ |
216 | 233k | DEBUGASSERT(hlen); |
217 | 233k | *name = header; |
218 | | |
219 | 233k | if(type == CURLH_PSEUDO) { |
220 | 4.83k | if(*header != ':') |
221 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
222 | 4.83k | header++; |
223 | 4.83k | } |
224 | | |
225 | | /* Find the end of the header name */ |
226 | 2.88M | while(*header && (*header != ':')) |
227 | 2.65M | ++header; |
228 | | |
229 | 233k | if(*header) |
230 | | /* Skip over colon, null it */ |
231 | 233k | *header++ = 0; |
232 | 361 | else |
233 | 361 | return CURLE_BAD_FUNCTION_ARGUMENT; |
234 | | |
235 | | /* skip all leading blank letters */ |
236 | 423k | while(ISBLANK(*header)) |
237 | 190k | header++; |
238 | | |
239 | 233k | *value = header; |
240 | | |
241 | | /* skip all trailing space letters */ |
242 | 240k | while((end > header) && ISBLANK(*end)) |
243 | 7.53k | *end-- = 0; /* null-terminate */ |
244 | 233k | return CURLE_OK; |
245 | 233k | } |
246 | | |
247 | | /* |
248 | | * Curl_headers_push() gets passed a full HTTP header to store. It gets called |
249 | | * immediately before the header callback. The header is CRLF, CR or LF |
250 | | * terminated. |
251 | | */ |
252 | | CURLcode Curl_headers_push(struct Curl_easy *data, const char *header, |
253 | | size_t hlen, /* length of header */ |
254 | | unsigned char type) |
255 | 250k | { |
256 | 250k | char *value = NULL; |
257 | 250k | char *name = NULL; |
258 | 250k | struct Curl_header_store *hs; |
259 | 250k | CURLcode result = CURLE_OUT_OF_MEMORY; |
260 | 250k | const size_t ilen = hlen; |
261 | | |
262 | 250k | if((header[0] == '\r') || (header[0] == '\n')) |
263 | | /* ignore the body separator */ |
264 | 12.8k | return CURLE_OK; |
265 | | |
266 | | /* trim off newline characters */ |
267 | 237k | if(hlen && (header[hlen - 1] == '\n')) |
268 | 228k | hlen--; |
269 | 237k | if(hlen && (header[hlen - 1] == '\r')) |
270 | 183k | hlen--; |
271 | 237k | if(hlen == ilen) |
272 | | /* neither CR nor LF as terminator is not a valid header */ |
273 | 3.84k | return CURLE_WEIRD_SERVER_REPLY; |
274 | | |
275 | 233k | if(ISBLANK(header[0])) { |
276 | | /* pass leading blanks */ |
277 | 2.91k | while(hlen && ISBLANK(*header)) { |
278 | 2.41k | header++; |
279 | 2.41k | hlen--; |
280 | 2.41k | } |
281 | 501 | if(!hlen) |
282 | 18 | return CURLE_WEIRD_SERVER_REPLY; |
283 | 501 | } |
284 | 233k | if(Curl_llist_count(&data->state.httphdrs) >= MAX_HTTP_RESP_HEADER_COUNT) { |
285 | 1 | failf(data, "Too many response headers, %d is max", |
286 | 1 | MAX_HTTP_RESP_HEADER_COUNT); |
287 | 1 | return CURLE_TOO_LARGE; |
288 | 1 | } |
289 | | |
290 | 233k | hs = curlx_calloc(1, sizeof(*hs) + hlen); |
291 | 233k | if(!hs) |
292 | 0 | return CURLE_OUT_OF_MEMORY; |
293 | 233k | memcpy(hs->buffer, header, hlen); |
294 | 233k | hs->buffer[hlen] = 0; /* null-terminate */ |
295 | | |
296 | 233k | result = namevalue(hs->buffer, hlen, type, &name, &value); |
297 | 233k | if(!result) { |
298 | 233k | hs->name = name; |
299 | 233k | hs->value = value; |
300 | 233k | hs->type = type; |
301 | 233k | hs->request = data->state.requests; |
302 | | |
303 | | /* insert this node into the list of headers */ |
304 | 233k | Curl_llist_append(&data->state.httphdrs, hs, &hs->node); |
305 | 233k | } |
306 | 361 | else { |
307 | 361 | failf(data, "Invalid response header"); |
308 | 361 | curlx_free(hs); |
309 | 361 | } |
310 | 233k | return result; |
311 | 233k | } |
312 | | |
313 | | /* |
314 | | * Curl_headers_reset(). Reset the headers subsystem. |
315 | | */ |
316 | | static void headers_reset(struct Curl_easy *data) |
317 | 598k | { |
318 | 598k | Curl_llist_init(&data->state.httphdrs, NULL); |
319 | 598k | } |
320 | | |
321 | | struct hds_cw_collect_ctx { |
322 | | struct Curl_cwriter super; |
323 | | }; |
324 | | |
325 | | static CURLcode hds_cw_collect_write(struct Curl_easy *data, |
326 | | struct Curl_cwriter *writer, int type, |
327 | | const char *buf, size_t blen) |
328 | 277k | { |
329 | 277k | if((type & CLIENTWRITE_HEADER) && !(type & CLIENTWRITE_STATUS)) { |
330 | 245k | unsigned char htype = (unsigned char) |
331 | 245k | (type & CLIENTWRITE_CONNECT ? CURLH_CONNECT : |
332 | 245k | (type & CLIENTWRITE_1XX ? CURLH_1XX : |
333 | 234k | (type & CLIENTWRITE_TRAILER ? CURLH_TRAILER : |
334 | 224k | CURLH_HEADER))); |
335 | 245k | CURLcode result = Curl_headers_push(data, buf, blen, htype); |
336 | 245k | CURL_TRC_WRITE(data, "header_collect pushed(type=%x, len=%zu) -> %d", |
337 | 245k | htype, blen, (int)result); |
338 | 245k | if(result) |
339 | 4.22k | return result; |
340 | 245k | } |
341 | 273k | return Curl_cwriter_write(data, writer->next, type, buf, blen); |
342 | 277k | } |
343 | | |
344 | | static const struct Curl_cwtype hds_cw_collect = { |
345 | | "hds-collect", |
346 | | NULL, |
347 | | 0, |
348 | | Curl_cwriter_def_init, |
349 | | hds_cw_collect_write, |
350 | | Curl_cwriter_def_flush, |
351 | | Curl_cwriter_def_close, |
352 | | sizeof(struct hds_cw_collect_ctx) |
353 | | }; |
354 | | |
355 | | CURLcode Curl_headers_init(struct Curl_easy *data) |
356 | 179k | { |
357 | 179k | struct Curl_cwriter *writer; |
358 | 179k | CURLcode result; |
359 | | |
360 | 179k | if(data->conn && (data->conn->scheme->protocol & PROTO_FAMILY_HTTP)) { |
361 | | /* avoid installing it twice */ |
362 | 108k | if(Curl_cwriter_get_by_name(data, hds_cw_collect.name)) |
363 | 41.8k | return CURLE_OK; |
364 | | |
365 | 67.0k | result = Curl_cwriter_create(&writer, data, &hds_cw_collect, |
366 | 67.0k | CURL_CW_PROTOCOL); |
367 | 67.0k | if(result) |
368 | 0 | return result; |
369 | | |
370 | 67.0k | result = Curl_cwriter_add(data, writer); |
371 | 67.0k | if(result) { |
372 | 0 | Curl_cwriter_free(data, writer); |
373 | 0 | return result; |
374 | 0 | } |
375 | 67.0k | } |
376 | 137k | return CURLE_OK; |
377 | 179k | } |
378 | | |
379 | | /* |
380 | | * Curl_headers_cleanup(). Free all stored headers and associated memory. |
381 | | */ |
382 | | CURLcode Curl_headers_cleanup(struct Curl_easy *data) |
383 | 598k | { |
384 | 598k | struct Curl_llist_node *e; |
385 | 598k | struct Curl_llist_node *n; |
386 | | |
387 | 831k | for(e = Curl_llist_head(&data->state.httphdrs); e; e = n) { |
388 | 233k | struct Curl_header_store *hs = Curl_node_elem(e); |
389 | 233k | n = Curl_node_next(e); |
390 | 233k | curlx_free(hs); |
391 | 233k | } |
392 | 598k | headers_reset(data); |
393 | 598k | return CURLE_OK; |
394 | 598k | } |
395 | | |
396 | | #else /* HTTP-disabled builds below */ |
397 | | |
398 | | CURLHcode curl_easy_header(CURL *easy, |
399 | | const char *name, |
400 | | size_t index, |
401 | | unsigned int origin, |
402 | | int request, |
403 | | struct curl_header **hout) |
404 | | { |
405 | | (void)easy; |
406 | | (void)name; |
407 | | (void)index; |
408 | | (void)origin; |
409 | | (void)request; |
410 | | (void)hout; |
411 | | return CURLHE_NOT_BUILT_IN; |
412 | | } |
413 | | |
414 | | struct curl_header *curl_easy_nextheader(CURL *easy, |
415 | | unsigned int type, |
416 | | int request, |
417 | | struct curl_header *prev) |
418 | | { |
419 | | (void)easy; |
420 | | (void)type; |
421 | | (void)request; |
422 | | (void)prev; |
423 | | return NULL; |
424 | | } |
425 | | #endif |