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