/src/curl/lib/http_chunks.c
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 | | #ifndef CURL_DISABLE_HTTP |
28 | | |
29 | | #include "urldata.h" /* it includes http_chunks.h */ |
30 | | #include "curl_trc.h" |
31 | | #include "sendf.h" /* for the client write stuff */ |
32 | | #include "curlx/dynbuf.h" |
33 | | #include "content_encoding.h" |
34 | | #include "http.h" |
35 | | #include "multiif.h" |
36 | | #include "curlx/strparse.h" |
37 | | #include "curlx/warnless.h" |
38 | | |
39 | | /* The last #include files should be: */ |
40 | | #include "curl_memory.h" |
41 | | #include "memdebug.h" |
42 | | |
43 | | /* |
44 | | * Chunk format (simplified): |
45 | | * |
46 | | * <HEX SIZE>[ chunk extension ] CRLF |
47 | | * <DATA> CRLF |
48 | | * |
49 | | * Highlights from RFC2616 section 3.6 say: |
50 | | |
51 | | The chunked encoding modifies the body of a message in order to |
52 | | transfer it as a series of chunks, each with its own size indicator, |
53 | | followed by an OPTIONAL trailer containing entity-header fields. This |
54 | | allows dynamically produced content to be transferred along with the |
55 | | information necessary for the recipient to verify that it has |
56 | | received the full message. |
57 | | |
58 | | Chunked-Body = *chunk |
59 | | last-chunk |
60 | | trailer |
61 | | CRLF |
62 | | |
63 | | chunk = chunk-size [ chunk-extension ] CRLF |
64 | | chunk-data CRLF |
65 | | chunk-size = 1*HEX |
66 | | last-chunk = 1*("0") [ chunk-extension ] CRLF |
67 | | |
68 | | chunk-extension= *( ";" chunk-ext-name [ "=" chunk-ext-val ] ) |
69 | | chunk-ext-name = token |
70 | | chunk-ext-val = token | quoted-string |
71 | | chunk-data = chunk-size(OCTET) |
72 | | trailer = *(entity-header CRLF) |
73 | | |
74 | | The chunk-size field is a string of hex digits indicating the size of |
75 | | the chunk. The chunked encoding is ended by any chunk whose size is |
76 | | zero, followed by the trailer, which is terminated by an empty line. |
77 | | |
78 | | */ |
79 | | |
80 | | void Curl_httpchunk_init(struct Curl_easy *data, struct Curl_chunker *ch, |
81 | | bool ignore_body) |
82 | 19.7k | { |
83 | 19.7k | (void)data; |
84 | 19.7k | ch->hexindex = 0; /* start at 0 */ |
85 | 19.7k | ch->state = CHUNK_HEX; /* we get hex first! */ |
86 | 19.7k | ch->last_code = CHUNKE_OK; |
87 | 19.7k | curlx_dyn_init(&ch->trailer, DYN_H1_TRAILER); |
88 | 19.7k | ch->ignore_body = ignore_body; |
89 | 19.7k | } |
90 | | |
91 | | void Curl_httpchunk_reset(struct Curl_easy *data, struct Curl_chunker *ch, |
92 | | bool ignore_body) |
93 | 2.34k | { |
94 | 2.34k | (void)data; |
95 | 2.34k | ch->hexindex = 0; /* start at 0 */ |
96 | 2.34k | ch->state = CHUNK_HEX; /* we get hex first! */ |
97 | 2.34k | ch->last_code = CHUNKE_OK; |
98 | 2.34k | curlx_dyn_reset(&ch->trailer); |
99 | 2.34k | ch->ignore_body = ignore_body; |
100 | 2.34k | } |
101 | | |
102 | | void Curl_httpchunk_free(struct Curl_easy *data, struct Curl_chunker *ch) |
103 | 19.7k | { |
104 | 19.7k | (void)data; |
105 | 19.7k | curlx_dyn_free(&ch->trailer); |
106 | 19.7k | } |
107 | | |
108 | | bool Curl_httpchunk_is_done(struct Curl_easy *data, struct Curl_chunker *ch) |
109 | 641k | { |
110 | 641k | (void)data; |
111 | 641k | return ch->state == CHUNK_DONE; |
112 | 641k | } |
113 | | |
114 | | static CURLcode httpchunk_readwrite(struct Curl_easy *data, |
115 | | struct Curl_chunker *ch, |
116 | | struct Curl_cwriter *cw_next, |
117 | | const char *buf, size_t blen, |
118 | | size_t *pconsumed) |
119 | 643k | { |
120 | 643k | CURLcode result = CURLE_OK; |
121 | 643k | size_t piece; |
122 | | |
123 | 643k | *pconsumed = 0; /* nothing's written yet */ |
124 | | /* first check terminal states that will not progress anywhere */ |
125 | 643k | if(ch->state == CHUNK_DONE) |
126 | 43 | return CURLE_OK; |
127 | 643k | if(ch->state == CHUNK_FAILED) |
128 | 0 | return CURLE_RECV_ERROR; |
129 | | |
130 | | /* the original data is written to the client, but we go on with the |
131 | | chunk read process, to properly calculate the content length */ |
132 | 643k | if(data->set.http_te_skip && !ch->ignore_body) { |
133 | 60 | if(cw_next) |
134 | 60 | result = Curl_cwriter_write(data, cw_next, CLIENTWRITE_BODY, buf, blen); |
135 | 0 | else |
136 | 0 | result = Curl_client_write(data, CLIENTWRITE_BODY, buf, blen); |
137 | 60 | if(result) { |
138 | 2 | ch->state = CHUNK_FAILED; |
139 | 2 | ch->last_code = CHUNKE_PASSTHRU_ERROR; |
140 | 2 | return result; |
141 | 2 | } |
142 | 60 | } |
143 | | |
144 | 1.38M | while(blen) { |
145 | 739k | switch(ch->state) { |
146 | 6.85k | case CHUNK_HEX: |
147 | 6.85k | if(ISXDIGIT(*buf)) { |
148 | 5.66k | if(ch->hexindex >= CHUNK_MAXNUM_LEN) { |
149 | 22 | failf(data, "chunk hex-length longer than %d", CHUNK_MAXNUM_LEN); |
150 | 22 | ch->state = CHUNK_FAILED; |
151 | 22 | ch->last_code = CHUNKE_TOO_LONG_HEX; /* longer than we support */ |
152 | 22 | return CURLE_RECV_ERROR; |
153 | 22 | } |
154 | 5.64k | ch->hexbuffer[ch->hexindex++] = *buf; |
155 | 5.64k | buf++; |
156 | 5.64k | blen--; |
157 | 5.64k | (*pconsumed)++; |
158 | 5.64k | } |
159 | 1.19k | else { |
160 | 1.19k | const char *p; |
161 | 1.19k | if(ch->hexindex == 0) { |
162 | | /* This is illegal data, we received junk where we expected |
163 | | a hexadecimal digit. */ |
164 | 47 | failf(data, "chunk hex-length char not a hex digit: 0x%x", *buf); |
165 | 47 | ch->state = CHUNK_FAILED; |
166 | 47 | ch->last_code = CHUNKE_ILLEGAL_HEX; |
167 | 47 | return CURLE_RECV_ERROR; |
168 | 47 | } |
169 | | /* blen and buf are unmodified */ |
170 | 1.14k | ch->hexbuffer[ch->hexindex] = 0; |
171 | 1.14k | p = &ch->hexbuffer[0]; |
172 | 1.14k | if(curlx_str_hex(&p, &ch->datasize, CURL_OFF_T_MAX)) { |
173 | 7 | failf(data, "invalid chunk size: '%s'", ch->hexbuffer); |
174 | 7 | ch->state = CHUNK_FAILED; |
175 | 7 | ch->last_code = CHUNKE_ILLEGAL_HEX; |
176 | 7 | return CURLE_RECV_ERROR; |
177 | 7 | } |
178 | 1.13k | ch->state = CHUNK_LF; /* now wait for the CRLF */ |
179 | 1.13k | } |
180 | 6.78k | break; |
181 | | |
182 | 43.4k | case CHUNK_LF: |
183 | | /* waiting for the LF after a chunk size */ |
184 | 43.4k | if(*buf == 0x0a) { |
185 | | /* we are now expecting data to come, unless size was zero! */ |
186 | 1.06k | if(ch->datasize == 0) { |
187 | 281 | ch->state = CHUNK_TRAILER; /* now check for trailers */ |
188 | 281 | } |
189 | 785 | else { |
190 | 785 | ch->state = CHUNK_DATA; |
191 | 785 | CURL_TRC_WRITE(data, "http_chunked, chunk start of %" |
192 | 785 | FMT_OFF_T " bytes", ch->datasize); |
193 | 785 | } |
194 | 1.06k | } |
195 | | |
196 | 43.4k | buf++; |
197 | 43.4k | blen--; |
198 | 43.4k | (*pconsumed)++; |
199 | 43.4k | break; |
200 | | |
201 | 331k | case CHUNK_DATA: |
202 | | /* We expect 'datasize' of data. We have 'blen' right now, it can be |
203 | | more or less than 'datasize'. Get the smallest piece. |
204 | | */ |
205 | 331k | piece = blen; |
206 | 331k | if(ch->datasize < (curl_off_t)blen) |
207 | 157 | piece = curlx_sotouz(ch->datasize); |
208 | | |
209 | | /* Write the data portion available */ |
210 | 331k | if(!data->set.http_te_skip && !ch->ignore_body) { |
211 | 330 | if(cw_next) |
212 | 330 | result = Curl_cwriter_write(data, cw_next, CLIENTWRITE_BODY, |
213 | 330 | buf, piece); |
214 | 0 | else |
215 | 0 | result = Curl_client_write(data, CLIENTWRITE_BODY, buf, piece); |
216 | 330 | if(result) { |
217 | 7 | ch->state = CHUNK_FAILED; |
218 | 7 | ch->last_code = CHUNKE_PASSTHRU_ERROR; |
219 | 7 | return result; |
220 | 7 | } |
221 | 330 | } |
222 | | |
223 | 331k | *pconsumed += piece; |
224 | 331k | ch->datasize -= piece; /* decrease amount left to expect */ |
225 | 331k | buf += piece; /* move read pointer forward */ |
226 | 331k | blen -= piece; /* decrease space left in this round */ |
227 | 331k | CURL_TRC_WRITE(data, "http_chunked, write %zu body bytes, %" |
228 | 331k | FMT_OFF_T " bytes in chunk remain", |
229 | 331k | piece, ch->datasize); |
230 | | |
231 | 331k | if(ch->datasize == 0) |
232 | | /* end of data this round, we now expect a trailing CRLF */ |
233 | 302 | ch->state = CHUNK_POSTLF; |
234 | 331k | break; |
235 | | |
236 | 947 | case CHUNK_POSTLF: |
237 | 947 | if(*buf == 0x0a) { |
238 | | /* The last one before we go back to hex state and start all over. */ |
239 | 199 | Curl_httpchunk_reset(data, ch, ch->ignore_body); |
240 | 199 | } |
241 | 748 | else if(*buf != 0x0d) { |
242 | 101 | ch->state = CHUNK_FAILED; |
243 | 101 | ch->last_code = CHUNKE_BAD_CHUNK; |
244 | 101 | return CURLE_RECV_ERROR; |
245 | 101 | } |
246 | 846 | buf++; |
247 | 846 | blen--; |
248 | 846 | (*pconsumed)++; |
249 | 846 | break; |
250 | | |
251 | 344k | case CHUNK_TRAILER: |
252 | 344k | if((*buf == 0x0d) || (*buf == 0x0a)) { |
253 | 6.28k | char *tr = curlx_dyn_ptr(&ch->trailer); |
254 | | /* this is the end of a trailer, but if the trailer was zero bytes |
255 | | there was no trailer and we move on */ |
256 | | |
257 | 6.28k | if(tr) { |
258 | 6.26k | result = curlx_dyn_addn(&ch->trailer, STRCONST("\x0d\x0a")); |
259 | 6.26k | if(result) { |
260 | 4 | ch->state = CHUNK_FAILED; |
261 | 4 | ch->last_code = CHUNKE_OUT_OF_MEMORY; |
262 | 4 | return result; |
263 | 4 | } |
264 | 6.26k | tr = curlx_dyn_ptr(&ch->trailer); |
265 | 6.26k | if(!data->set.http_te_skip) { |
266 | 5.00k | size_t trlen = curlx_dyn_len(&ch->trailer); |
267 | 5.00k | if(cw_next) |
268 | 2.04k | result = Curl_cwriter_write(data, cw_next, |
269 | 2.04k | CLIENTWRITE_HEADER| |
270 | 2.04k | CLIENTWRITE_TRAILER, |
271 | 2.04k | tr, trlen); |
272 | 2.95k | else |
273 | 2.95k | result = Curl_client_write(data, |
274 | 2.95k | CLIENTWRITE_HEADER| |
275 | 2.95k | CLIENTWRITE_TRAILER, |
276 | 2.95k | tr, trlen); |
277 | 5.00k | if(result) { |
278 | 51 | ch->state = CHUNK_FAILED; |
279 | 51 | ch->last_code = CHUNKE_PASSTHRU_ERROR; |
280 | 51 | return result; |
281 | 51 | } |
282 | 5.00k | } |
283 | 6.21k | curlx_dyn_reset(&ch->trailer); |
284 | 6.21k | ch->state = CHUNK_TRAILER_CR; |
285 | 6.21k | if(*buf == 0x0a) |
286 | | /* already on the LF */ |
287 | 5.73k | break; |
288 | 6.21k | } |
289 | 21 | else { |
290 | | /* no trailer, we are on the final CRLF pair */ |
291 | 21 | ch->state = CHUNK_TRAILER_POSTCR; |
292 | 21 | break; /* do not advance the pointer */ |
293 | 21 | } |
294 | 6.28k | } |
295 | 338k | else { |
296 | 338k | result = curlx_dyn_addn(&ch->trailer, buf, 1); |
297 | 338k | if(result) { |
298 | 5 | ch->state = CHUNK_FAILED; |
299 | 5 | ch->last_code = CHUNKE_OUT_OF_MEMORY; |
300 | 5 | return result; |
301 | 5 | } |
302 | 338k | } |
303 | 338k | buf++; |
304 | 338k | blen--; |
305 | 338k | (*pconsumed)++; |
306 | 338k | break; |
307 | | |
308 | 6.21k | case CHUNK_TRAILER_CR: |
309 | 6.21k | if(*buf == 0x0a) { |
310 | 6.15k | ch->state = CHUNK_TRAILER_POSTCR; |
311 | 6.15k | buf++; |
312 | 6.15k | blen--; |
313 | 6.15k | (*pconsumed)++; |
314 | 6.15k | } |
315 | 53 | else { |
316 | 53 | ch->state = CHUNK_FAILED; |
317 | 53 | ch->last_code = CHUNKE_BAD_CHUNK; |
318 | 53 | return CURLE_RECV_ERROR; |
319 | 53 | } |
320 | 6.15k | break; |
321 | | |
322 | 6.17k | case CHUNK_TRAILER_POSTCR: |
323 | | /* We enter this state when a CR should arrive so we expect to |
324 | | have to first pass a CR before we wait for LF */ |
325 | 6.17k | if((*buf != 0x0d) && (*buf != 0x0a)) { |
326 | | /* not a CR then it must be another header in the trailer */ |
327 | 6.08k | ch->state = CHUNK_TRAILER; |
328 | 6.08k | break; |
329 | 6.08k | } |
330 | 92 | if(*buf == 0x0d) { |
331 | | /* skip if CR */ |
332 | 10 | buf++; |
333 | 10 | blen--; |
334 | 10 | (*pconsumed)++; |
335 | 10 | } |
336 | | /* now wait for the final LF */ |
337 | 92 | ch->state = CHUNK_STOP; |
338 | 92 | break; |
339 | | |
340 | 92 | case CHUNK_STOP: |
341 | 92 | if(*buf == 0x0a) { |
342 | 82 | blen--; |
343 | 82 | (*pconsumed)++; |
344 | | /* Record the length of any data left in the end of the buffer |
345 | | even if there is no more chunks to read */ |
346 | 82 | ch->datasize = blen; |
347 | 82 | ch->state = CHUNK_DONE; |
348 | 82 | CURL_TRC_WRITE(data, "http_chunk, response complete"); |
349 | 82 | return CURLE_OK; |
350 | 82 | } |
351 | 10 | else { |
352 | 10 | ch->state = CHUNK_FAILED; |
353 | 10 | ch->last_code = CHUNKE_BAD_CHUNK; |
354 | 10 | CURL_TRC_WRITE(data, "http_chunk error, expected 0x0a, seeing 0x%ux", |
355 | 10 | (unsigned int)*buf); |
356 | 10 | return CURLE_RECV_ERROR; |
357 | 10 | } |
358 | 0 | case CHUNK_DONE: |
359 | 0 | return CURLE_OK; |
360 | | |
361 | 0 | case CHUNK_FAILED: |
362 | 0 | return CURLE_RECV_ERROR; |
363 | 739k | } |
364 | | |
365 | 739k | } |
366 | 643k | return CURLE_OK; |
367 | 643k | } |
368 | | |
369 | | static const char *Curl_chunked_strerror(CHUNKcode code) |
370 | 96 | { |
371 | 96 | switch(code) { |
372 | 0 | default: |
373 | 0 | return "OK"; |
374 | 12 | case CHUNKE_TOO_LONG_HEX: |
375 | 12 | return "Too long hexadecimal number"; |
376 | 21 | case CHUNKE_ILLEGAL_HEX: |
377 | 21 | return "Illegal or missing hexadecimal sequence"; |
378 | 60 | case CHUNKE_BAD_CHUNK: |
379 | 60 | return "Malformed encoding found"; |
380 | 0 | case CHUNKE_PASSTHRU_ERROR: |
381 | 0 | return "Error writing data to client"; |
382 | 0 | case CHUNKE_BAD_ENCODING: |
383 | 0 | return "Bad content-encoding found"; |
384 | 3 | case CHUNKE_OUT_OF_MEMORY: |
385 | 3 | return "Out of memory"; |
386 | 96 | } |
387 | 96 | } |
388 | | |
389 | | CURLcode Curl_httpchunk_read(struct Curl_easy *data, |
390 | | struct Curl_chunker *ch, |
391 | | char *buf, size_t blen, |
392 | | size_t *pconsumed) |
393 | 641k | { |
394 | 641k | return httpchunk_readwrite(data, ch, NULL, buf, blen, pconsumed); |
395 | 641k | } |
396 | | |
397 | | struct chunked_writer { |
398 | | struct Curl_cwriter super; |
399 | | struct Curl_chunker ch; |
400 | | }; |
401 | | |
402 | | static CURLcode cw_chunked_init(struct Curl_easy *data, |
403 | | struct Curl_cwriter *writer) |
404 | 582 | { |
405 | 582 | struct chunked_writer *ctx = writer->ctx; |
406 | | |
407 | 582 | data->req.chunk = TRUE; /* chunks coming our way. */ |
408 | 582 | Curl_httpchunk_init(data, &ctx->ch, FALSE); |
409 | 582 | return CURLE_OK; |
410 | 582 | } |
411 | | |
412 | | static void cw_chunked_close(struct Curl_easy *data, |
413 | | struct Curl_cwriter *writer) |
414 | 582 | { |
415 | 582 | struct chunked_writer *ctx = writer->ctx; |
416 | 582 | Curl_httpchunk_free(data, &ctx->ch); |
417 | 582 | } |
418 | | |
419 | | static CURLcode cw_chunked_write(struct Curl_easy *data, |
420 | | struct Curl_cwriter *writer, int type, |
421 | | const char *buf, size_t blen) |
422 | 9.39k | { |
423 | 9.39k | struct chunked_writer *ctx = writer->ctx; |
424 | 9.39k | CURLcode result; |
425 | 9.39k | size_t consumed; |
426 | | |
427 | 9.39k | if(!(type & CLIENTWRITE_BODY)) |
428 | 7.46k | return Curl_cwriter_write(data, writer->next, type, buf, blen); |
429 | | |
430 | 1.93k | consumed = 0; |
431 | 1.93k | result = httpchunk_readwrite(data, &ctx->ch, writer->next, buf, blen, |
432 | 1.93k | &consumed); |
433 | | |
434 | 1.93k | if(result) { |
435 | 130 | if(CHUNKE_PASSTHRU_ERROR == ctx->ch.last_code) { |
436 | 34 | failf(data, "Failed reading the chunked-encoded stream"); |
437 | 34 | } |
438 | 96 | else { |
439 | 96 | failf(data, "%s in chunked-encoding", |
440 | 96 | Curl_chunked_strerror(ctx->ch.last_code)); |
441 | 96 | } |
442 | 130 | return result; |
443 | 130 | } |
444 | | |
445 | 1.80k | blen -= consumed; |
446 | 1.80k | if(CHUNK_DONE == ctx->ch.state) { |
447 | | /* chunks read successfully, download is complete */ |
448 | 42 | data->req.download_done = TRUE; |
449 | 42 | if(blen) { |
450 | 38 | infof(data, "Leftovers after chunking: %zu bytes", blen); |
451 | 38 | } |
452 | 42 | } |
453 | 1.75k | else if((type & CLIENTWRITE_EOS) && !data->req.no_body) { |
454 | 238 | failf(data, "transfer closed with outstanding read data remaining"); |
455 | 238 | return CURLE_PARTIAL_FILE; |
456 | 238 | } |
457 | | |
458 | 1.56k | return CURLE_OK; |
459 | 1.80k | } |
460 | | |
461 | | /* HTTP chunked Transfer-Encoding decoder */ |
462 | | const struct Curl_cwtype Curl_httpchunk_unencoder = { |
463 | | "chunked", |
464 | | NULL, |
465 | | cw_chunked_init, |
466 | | cw_chunked_write, |
467 | | cw_chunked_close, |
468 | | sizeof(struct chunked_writer) |
469 | | }; |
470 | | |
471 | | /* max length of an HTTP chunk that we want to generate */ |
472 | | #define CURL_CHUNKED_MINLEN (1024) |
473 | 711 | #define CURL_CHUNKED_MAXLEN (64 * 1024) |
474 | | |
475 | | struct chunked_reader { |
476 | | struct Curl_creader super; |
477 | | struct bufq chunkbuf; |
478 | | BIT(read_eos); /* we read an EOS from the next reader */ |
479 | | BIT(eos); /* we have returned an EOS */ |
480 | | }; |
481 | | |
482 | | static CURLcode cr_chunked_init(struct Curl_easy *data, |
483 | | struct Curl_creader *reader) |
484 | 711 | { |
485 | 711 | struct chunked_reader *ctx = reader->ctx; |
486 | 711 | (void)data; |
487 | 711 | Curl_bufq_init2(&ctx->chunkbuf, CURL_CHUNKED_MAXLEN, 2, BUFQ_OPT_SOFT_LIMIT); |
488 | 711 | return CURLE_OK; |
489 | 711 | } |
490 | | |
491 | | static void cr_chunked_close(struct Curl_easy *data, |
492 | | struct Curl_creader *reader) |
493 | 711 | { |
494 | 711 | struct chunked_reader *ctx = reader->ctx; |
495 | 711 | (void)data; |
496 | 711 | Curl_bufq_free(&ctx->chunkbuf); |
497 | 711 | } |
498 | | |
499 | | static CURLcode add_last_chunk(struct Curl_easy *data, |
500 | | struct Curl_creader *reader) |
501 | 453 | { |
502 | 453 | struct chunked_reader *ctx = reader->ctx; |
503 | 453 | struct curl_slist *trailers = NULL, *tr; |
504 | 453 | CURLcode result; |
505 | 453 | size_t n; |
506 | 453 | int rc; |
507 | | |
508 | 453 | if(!data->set.trailer_callback) { |
509 | 453 | CURL_TRC_READ(data, "http_chunk, added last, empty chunk"); |
510 | 453 | return Curl_bufq_cwrite(&ctx->chunkbuf, STRCONST("0\r\n\r\n"), &n); |
511 | 453 | } |
512 | | |
513 | 0 | result = Curl_bufq_cwrite(&ctx->chunkbuf, STRCONST("0\r\n"), &n); |
514 | 0 | if(result) |
515 | 0 | goto out; |
516 | | |
517 | 0 | Curl_set_in_callback(data, TRUE); |
518 | 0 | rc = data->set.trailer_callback(&trailers, data->set.trailer_data); |
519 | 0 | Curl_set_in_callback(data, FALSE); |
520 | |
|
521 | 0 | if(rc != CURL_TRAILERFUNC_OK) { |
522 | 0 | failf(data, "operation aborted by trailing headers callback"); |
523 | 0 | result = CURLE_ABORTED_BY_CALLBACK; |
524 | 0 | goto out; |
525 | 0 | } |
526 | | |
527 | 0 | for(tr = trailers; tr; tr = tr->next) { |
528 | | /* only add correctly formatted trailers */ |
529 | 0 | char *ptr = strchr(tr->data, ':'); |
530 | 0 | if(!ptr || *(ptr + 1) != ' ') { |
531 | 0 | infof(data, "Malformatted trailing header, skipping trailer"); |
532 | 0 | continue; |
533 | 0 | } |
534 | | |
535 | 0 | result = Curl_bufq_cwrite(&ctx->chunkbuf, tr->data, |
536 | 0 | strlen(tr->data), &n); |
537 | 0 | if(!result) |
538 | 0 | result = Curl_bufq_cwrite(&ctx->chunkbuf, STRCONST("\r\n"), &n); |
539 | 0 | if(result) |
540 | 0 | goto out; |
541 | 0 | } |
542 | | |
543 | 0 | result = Curl_bufq_cwrite(&ctx->chunkbuf, STRCONST("\r\n"), &n); |
544 | |
|
545 | 0 | out: |
546 | 0 | curl_slist_free_all(trailers); |
547 | 0 | CURL_TRC_READ(data, "http_chunk, added last chunk with trailers " |
548 | 0 | "from client -> %d", result); |
549 | 0 | return result; |
550 | 0 | } |
551 | | |
552 | | static CURLcode add_chunk(struct Curl_easy *data, |
553 | | struct Curl_creader *reader, |
554 | | char *buf, size_t blen) |
555 | 5.18k | { |
556 | 5.18k | struct chunked_reader *ctx = reader->ctx; |
557 | 5.18k | CURLcode result; |
558 | 5.18k | char tmp[CURL_CHUNKED_MINLEN]; |
559 | 5.18k | size_t nread; |
560 | 5.18k | bool eos; |
561 | | |
562 | 5.18k | DEBUGASSERT(!ctx->read_eos); |
563 | 5.18k | blen = CURLMIN(blen, CURL_CHUNKED_MAXLEN); /* respect our buffer pref */ |
564 | 5.18k | if(blen < sizeof(tmp)) { |
565 | | /* small read, make a chunk of decent size */ |
566 | 25 | buf = tmp; |
567 | 25 | blen = sizeof(tmp); |
568 | 25 | } |
569 | 5.15k | else { |
570 | | /* larger read, make a chunk that will fit when read back */ |
571 | 5.15k | blen -= (8 + 2 + 2); /* deduct max overhead, 8 hex + 2*crlf */ |
572 | 5.15k | } |
573 | | |
574 | 5.18k | result = Curl_creader_read(data, reader->next, buf, blen, &nread, &eos); |
575 | 5.18k | if(result) |
576 | 10 | return result; |
577 | 5.17k | if(eos) |
578 | 453 | ctx->read_eos = TRUE; |
579 | | |
580 | 5.17k | if(nread) { |
581 | | /* actually got bytes, wrap them into the chunkbuf */ |
582 | 1.23k | char hd[11] = ""; |
583 | 1.23k | int hdlen; |
584 | 1.23k | size_t n; |
585 | | |
586 | 1.23k | hdlen = curl_msnprintf(hd, sizeof(hd), "%zx\r\n", nread); |
587 | 1.23k | if(hdlen <= 0) |
588 | 0 | return CURLE_READ_ERROR; |
589 | | /* On a soft-limited bufq, we do not need to check that all was written */ |
590 | 1.23k | result = Curl_bufq_cwrite(&ctx->chunkbuf, hd, hdlen, &n); |
591 | 1.23k | if(!result) |
592 | 1.23k | result = Curl_bufq_cwrite(&ctx->chunkbuf, buf, nread, &n); |
593 | 1.23k | if(!result) |
594 | 1.23k | result = Curl_bufq_cwrite(&ctx->chunkbuf, "\r\n", 2, &n); |
595 | 1.23k | CURL_TRC_READ(data, "http_chunk, made chunk of %zu bytes -> %d", |
596 | 1.23k | nread, result); |
597 | 1.23k | if(result) |
598 | 0 | return result; |
599 | 1.23k | } |
600 | | |
601 | 5.17k | if(ctx->read_eos) |
602 | 453 | return add_last_chunk(data, reader); |
603 | 4.71k | return CURLE_OK; |
604 | 5.17k | } |
605 | | |
606 | | static CURLcode cr_chunked_read(struct Curl_easy *data, |
607 | | struct Curl_creader *reader, |
608 | | char *buf, size_t blen, |
609 | | size_t *pnread, bool *peos) |
610 | 5.18k | { |
611 | 5.18k | struct chunked_reader *ctx = reader->ctx; |
612 | 5.18k | CURLcode result = CURLE_READ_ERROR; |
613 | | |
614 | 5.18k | *pnread = 0; |
615 | 5.18k | *peos = ctx->eos; |
616 | | |
617 | 5.18k | if(!ctx->eos) { |
618 | 5.18k | if(!ctx->read_eos && Curl_bufq_is_empty(&ctx->chunkbuf)) { |
619 | | /* Still getting data form the next reader, buffer is empty */ |
620 | 5.18k | result = add_chunk(data, reader, buf, blen); |
621 | 5.18k | if(result) |
622 | 10 | return result; |
623 | 5.18k | } |
624 | | |
625 | 5.17k | if(!Curl_bufq_is_empty(&ctx->chunkbuf)) { |
626 | 1.25k | result = Curl_bufq_cread(&ctx->chunkbuf, buf, blen, pnread); |
627 | 1.25k | if(!result && ctx->read_eos && Curl_bufq_is_empty(&ctx->chunkbuf)) { |
628 | | /* no more data, read all, done. */ |
629 | 445 | ctx->eos = TRUE; |
630 | 445 | *peos = TRUE; |
631 | 445 | } |
632 | 1.25k | return result; |
633 | 1.25k | } |
634 | 5.17k | } |
635 | | /* We may get here, because we are done or because callbacks paused */ |
636 | 3.92k | DEBUGASSERT(ctx->eos || !ctx->read_eos); |
637 | 3.92k | return CURLE_OK; |
638 | 3.92k | } |
639 | | |
640 | | static curl_off_t cr_chunked_total_length(struct Curl_easy *data, |
641 | | struct Curl_creader *reader) |
642 | 1.59k | { |
643 | | /* this reader changes length depending on input */ |
644 | 1.59k | (void)data; |
645 | 1.59k | (void)reader; |
646 | 1.59k | return -1; |
647 | 1.59k | } |
648 | | |
649 | | /* HTTP chunked Transfer-Encoding encoder */ |
650 | | const struct Curl_crtype Curl_httpchunk_encoder = { |
651 | | "chunked", |
652 | | cr_chunked_init, |
653 | | cr_chunked_read, |
654 | | cr_chunked_close, |
655 | | Curl_creader_def_needs_rewind, |
656 | | cr_chunked_total_length, |
657 | | Curl_creader_def_resume_from, |
658 | | Curl_creader_def_cntrl, |
659 | | Curl_creader_def_is_paused, |
660 | | Curl_creader_def_done, |
661 | | sizeof(struct chunked_reader) |
662 | | }; |
663 | | |
664 | | CURLcode Curl_httpchunk_add_reader(struct Curl_easy *data) |
665 | 711 | { |
666 | 711 | struct Curl_creader *reader = NULL; |
667 | 711 | CURLcode result; |
668 | | |
669 | 711 | result = Curl_creader_create(&reader, data, &Curl_httpchunk_encoder, |
670 | 711 | CURL_CR_TRANSFER_ENCODE); |
671 | 711 | if(!result) |
672 | 711 | result = Curl_creader_add(data, reader); |
673 | | |
674 | 711 | if(result && reader) |
675 | 0 | Curl_creader_free(data, reader); |
676 | 711 | return result; |
677 | 711 | } |
678 | | |
679 | | #endif /* CURL_DISABLE_HTTP */ |