Line | Count | Source (jump to first uncovered line) |
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 <curl/curl.h> |
28 | | |
29 | | #include "urldata.h" |
30 | | #include "cfilters.h" |
31 | | #include "headers.h" |
32 | | #include "multiif.h" |
33 | | #include "sendf.h" |
34 | | #include "transfer.h" |
35 | | #include "cw-out.h" |
36 | | #include "cw-pause.h" |
37 | | |
38 | | /* The last 3 #include files should be in this order */ |
39 | | #include "curl_printf.h" |
40 | | #include "curl_memory.h" |
41 | | #include "memdebug.h" |
42 | | |
43 | | |
44 | | /** |
45 | | * OVERALL DESIGN of this client writer |
46 | | * |
47 | | * The 'cw-out' writer is supposed to be the last writer in a transfer's |
48 | | * stack. It is always added when that stack is initialized. Its purpose |
49 | | * is to pass BODY and HEADER bytes to the client-installed callback |
50 | | * functions. |
51 | | * |
52 | | * These callback may return `CURL_WRITEFUNC_PAUSE` to indicate that the |
53 | | * data had not been written and the whole transfer should stop receiving |
54 | | * new data. Or at least, stop calling the functions. When the transfer |
55 | | * is "unpaused" by the client, the previous data shall be passed as |
56 | | * if nothing happened. |
57 | | * |
58 | | * The `cw-out` writer therefore manages buffers for bytes that could |
59 | | * not be written. Data that was already in flight from the server also |
60 | | * needs buffering on paused transfer when it arrives. |
61 | | * |
62 | | * In addition, the writer allows buffering of "small" body writes, |
63 | | * so client functions are called less often. That is only enabled on a |
64 | | * number of conditions. |
65 | | * |
66 | | * HEADER and BODY data may arrive in any order. For paused transfers, |
67 | | * a list of `struct cw_out_buf` is kept for `cw_out_type` types. The |
68 | | * list may be: [BODY]->[HEADER]->[BODY]->[HEADER].... |
69 | | * When unpausing, this list is "played back" to the client callbacks. |
70 | | * |
71 | | * The amount of bytes being buffered is limited by `DYN_PAUSE_BUFFER` |
72 | | * and when that is exceeded `CURLE_TOO_LARGE` is returned as error. |
73 | | */ |
74 | | typedef enum { |
75 | | CW_OUT_NONE, |
76 | | CW_OUT_BODY, |
77 | | CW_OUT_BODY_0LEN, |
78 | | CW_OUT_HDS |
79 | | } cw_out_type; |
80 | | |
81 | | struct cw_out_buf { |
82 | | struct cw_out_buf *next; |
83 | | struct dynbuf b; |
84 | | cw_out_type type; |
85 | | }; |
86 | | |
87 | | static struct cw_out_buf *cw_out_buf_create(cw_out_type otype) |
88 | 0 | { |
89 | 0 | struct cw_out_buf *cwbuf = calloc(1, sizeof(*cwbuf)); |
90 | 0 | if(cwbuf) { |
91 | 0 | cwbuf->type = otype; |
92 | 0 | curlx_dyn_init(&cwbuf->b, DYN_PAUSE_BUFFER); |
93 | 0 | } |
94 | 0 | return cwbuf; |
95 | 0 | } |
96 | | |
97 | | static void cw_out_buf_free(struct cw_out_buf *cwbuf) |
98 | 0 | { |
99 | 0 | if(cwbuf) { |
100 | 0 | curlx_dyn_free(&cwbuf->b); |
101 | 0 | free(cwbuf); |
102 | 0 | } |
103 | 0 | } |
104 | | |
105 | | struct cw_out_ctx { |
106 | | struct Curl_cwriter super; |
107 | | struct cw_out_buf *buf; |
108 | | BIT(paused); |
109 | | BIT(errored); |
110 | | }; |
111 | | |
112 | | static CURLcode cw_out_write(struct Curl_easy *data, |
113 | | struct Curl_cwriter *writer, int type, |
114 | | const char *buf, size_t nbytes); |
115 | | static void cw_out_close(struct Curl_easy *data, struct Curl_cwriter *writer); |
116 | | static CURLcode cw_out_init(struct Curl_easy *data, |
117 | | struct Curl_cwriter *writer); |
118 | | |
119 | | const struct Curl_cwtype Curl_cwt_out = { |
120 | | "cw-out", |
121 | | NULL, |
122 | | cw_out_init, |
123 | | cw_out_write, |
124 | | cw_out_close, |
125 | | sizeof(struct cw_out_ctx) |
126 | | }; |
127 | | |
128 | | static CURLcode cw_out_init(struct Curl_easy *data, |
129 | | struct Curl_cwriter *writer) |
130 | 0 | { |
131 | 0 | struct cw_out_ctx *ctx = writer->ctx; |
132 | 0 | (void)data; |
133 | 0 | ctx->buf = NULL; |
134 | 0 | return CURLE_OK; |
135 | 0 | } |
136 | | |
137 | | static void cw_out_bufs_free(struct cw_out_ctx *ctx) |
138 | 0 | { |
139 | 0 | while(ctx->buf) { |
140 | 0 | struct cw_out_buf *next = ctx->buf->next; |
141 | 0 | cw_out_buf_free(ctx->buf); |
142 | 0 | ctx->buf = next; |
143 | 0 | } |
144 | 0 | } |
145 | | |
146 | | static size_t cw_out_bufs_len(struct cw_out_ctx *ctx) |
147 | 0 | { |
148 | 0 | struct cw_out_buf *cwbuf = ctx->buf; |
149 | 0 | size_t len = 0; |
150 | 0 | while(cwbuf) { |
151 | 0 | len += curlx_dyn_len(&cwbuf->b); |
152 | 0 | cwbuf = cwbuf->next; |
153 | 0 | } |
154 | 0 | return len; |
155 | 0 | } |
156 | | |
157 | | static void cw_out_close(struct Curl_easy *data, struct Curl_cwriter *writer) |
158 | 0 | { |
159 | 0 | struct cw_out_ctx *ctx = writer->ctx; |
160 | |
|
161 | 0 | (void)data; |
162 | 0 | cw_out_bufs_free(ctx); |
163 | 0 | } |
164 | | |
165 | | /** |
166 | | * Return the current curl_write_callback and user_data for the buf type |
167 | | */ |
168 | | static void cw_get_writefunc(struct Curl_easy *data, cw_out_type otype, |
169 | | curl_write_callback *pwcb, void **pwcb_data, |
170 | | size_t *pmax_write, size_t *pmin_write) |
171 | 0 | { |
172 | 0 | switch(otype) { |
173 | 0 | case CW_OUT_BODY: |
174 | 0 | case CW_OUT_BODY_0LEN: |
175 | 0 | *pwcb = data->set.fwrite_func; |
176 | 0 | *pwcb_data = data->set.out; |
177 | 0 | *pmax_write = CURL_MAX_WRITE_SIZE; |
178 | | /* if we ever want buffering of BODY output, we can set `min_write` |
179 | | * the preferred size. The default should always be to pass data |
180 | | * to the client as it comes without delay */ |
181 | 0 | *pmin_write = 0; |
182 | 0 | break; |
183 | 0 | case CW_OUT_HDS: |
184 | 0 | *pwcb = data->set.fwrite_header ? data->set.fwrite_header : |
185 | 0 | (data->set.writeheader ? data->set.fwrite_func : NULL); |
186 | 0 | *pwcb_data = data->set.writeheader; |
187 | 0 | *pmax_write = 0; /* do not chunk-write headers, write them as they are */ |
188 | 0 | *pmin_write = 0; |
189 | 0 | break; |
190 | 0 | default: |
191 | 0 | *pwcb = NULL; |
192 | 0 | *pwcb_data = NULL; |
193 | 0 | *pmax_write = CURL_MAX_WRITE_SIZE; |
194 | 0 | *pmin_write = 0; |
195 | 0 | } |
196 | 0 | } |
197 | | |
198 | | static CURLcode cw_out_ptr_flush(struct cw_out_ctx *ctx, |
199 | | struct Curl_easy *data, |
200 | | cw_out_type otype, |
201 | | bool flush_all, |
202 | | const char *buf, size_t blen, |
203 | | size_t *pconsumed) |
204 | 0 | { |
205 | 0 | curl_write_callback wcb = NULL; |
206 | 0 | void *wcb_data; |
207 | 0 | size_t max_write, min_write; |
208 | 0 | size_t wlen, nwritten; |
209 | | |
210 | | /* If we errored once, we do not invoke the client callback again */ |
211 | 0 | if(ctx->errored) |
212 | 0 | return CURLE_WRITE_ERROR; |
213 | | |
214 | | /* write callbacks may get NULLed by the client between calls. */ |
215 | 0 | cw_get_writefunc(data, otype, &wcb, &wcb_data, &max_write, &min_write); |
216 | 0 | if(!wcb) { |
217 | 0 | *pconsumed = blen; |
218 | 0 | return CURLE_OK; |
219 | 0 | } |
220 | | |
221 | 0 | *pconsumed = 0; |
222 | 0 | if(otype == CW_OUT_BODY_0LEN) { |
223 | 0 | DEBUGASSERT(!blen); |
224 | 0 | Curl_set_in_callback(data, TRUE); |
225 | 0 | nwritten = wcb((char *)CURL_UNCONST(buf), 1, blen, wcb_data); |
226 | 0 | Curl_set_in_callback(data, FALSE); |
227 | 0 | CURL_TRC_WRITE(data, "[OUT] wrote %zu BODY bytes -> %zu", |
228 | 0 | blen, nwritten); |
229 | 0 | } |
230 | 0 | else { |
231 | 0 | while(blen && !ctx->paused) { |
232 | 0 | if(!flush_all && blen < min_write) |
233 | 0 | break; |
234 | 0 | wlen = max_write ? CURLMIN(blen, max_write) : blen; |
235 | 0 | Curl_set_in_callback(data, TRUE); |
236 | 0 | nwritten = wcb((char *)CURL_UNCONST(buf), 1, wlen, wcb_data); |
237 | 0 | Curl_set_in_callback(data, FALSE); |
238 | 0 | CURL_TRC_WRITE(data, "[OUT] wrote %zu %s bytes -> %zu", |
239 | 0 | wlen, (otype == CW_OUT_BODY) ? "body" : "header", |
240 | 0 | nwritten); |
241 | 0 | if(CURL_WRITEFUNC_PAUSE == nwritten) { |
242 | 0 | if(data->conn && data->conn->handler->flags & PROTOPT_NONETWORK) { |
243 | | /* Protocols that work without network cannot be paused. This is |
244 | | actually only FILE:// just now, and it cannot pause since the |
245 | | transfer is not done using the "normal" procedure. */ |
246 | 0 | failf(data, "Write callback asked for PAUSE when not supported"); |
247 | 0 | return CURLE_WRITE_ERROR; |
248 | 0 | } |
249 | 0 | ctx->paused = TRUE; |
250 | 0 | CURL_TRC_WRITE(data, "[OUT] PAUSE requested by client"); |
251 | 0 | return Curl_xfer_pause_recv(data, TRUE); |
252 | 0 | } |
253 | 0 | else if(CURL_WRITEFUNC_ERROR == nwritten) { |
254 | 0 | failf(data, "client returned ERROR on write of %zu bytes", wlen); |
255 | 0 | return CURLE_WRITE_ERROR; |
256 | 0 | } |
257 | 0 | else if(nwritten != wlen) { |
258 | 0 | failf(data, "Failure writing output to destination, " |
259 | 0 | "passed %zu returned %zd", wlen, nwritten); |
260 | 0 | return CURLE_WRITE_ERROR; |
261 | 0 | } |
262 | 0 | *pconsumed += nwritten; |
263 | 0 | blen -= nwritten; |
264 | 0 | buf += nwritten; |
265 | 0 | } |
266 | 0 | } |
267 | 0 | return CURLE_OK; |
268 | 0 | } |
269 | | |
270 | | static CURLcode cw_out_buf_flush(struct cw_out_ctx *ctx, |
271 | | struct Curl_easy *data, |
272 | | struct cw_out_buf *cwbuf, |
273 | | bool flush_all) |
274 | 0 | { |
275 | 0 | CURLcode result = CURLE_OK; |
276 | |
|
277 | 0 | if(curlx_dyn_len(&cwbuf->b)) { |
278 | 0 | size_t consumed; |
279 | |
|
280 | 0 | result = cw_out_ptr_flush(ctx, data, cwbuf->type, flush_all, |
281 | 0 | curlx_dyn_ptr(&cwbuf->b), |
282 | 0 | curlx_dyn_len(&cwbuf->b), |
283 | 0 | &consumed); |
284 | 0 | if(result) |
285 | 0 | return result; |
286 | | |
287 | 0 | if(consumed) { |
288 | 0 | if(consumed == curlx_dyn_len(&cwbuf->b)) { |
289 | 0 | curlx_dyn_free(&cwbuf->b); |
290 | 0 | } |
291 | 0 | else { |
292 | 0 | DEBUGASSERT(consumed < curlx_dyn_len(&cwbuf->b)); |
293 | 0 | result = curlx_dyn_tail(&cwbuf->b, |
294 | 0 | curlx_dyn_len(&cwbuf->b) - consumed); |
295 | 0 | if(result) |
296 | 0 | return result; |
297 | 0 | } |
298 | 0 | } |
299 | 0 | } |
300 | 0 | return result; |
301 | 0 | } |
302 | | |
303 | | static CURLcode cw_out_flush_chain(struct cw_out_ctx *ctx, |
304 | | struct Curl_easy *data, |
305 | | struct cw_out_buf **pcwbuf, |
306 | | bool flush_all) |
307 | 0 | { |
308 | 0 | struct cw_out_buf *cwbuf = *pcwbuf; |
309 | 0 | CURLcode result; |
310 | |
|
311 | 0 | if(!cwbuf) |
312 | 0 | return CURLE_OK; |
313 | 0 | if(ctx->paused) |
314 | 0 | return CURLE_OK; |
315 | | |
316 | | /* write the end of the chain until it blocks or gets empty */ |
317 | 0 | while(cwbuf->next) { |
318 | 0 | struct cw_out_buf **plast = &cwbuf->next; |
319 | 0 | while((*plast)->next) |
320 | 0 | plast = &(*plast)->next; |
321 | 0 | result = cw_out_flush_chain(ctx, data, plast, flush_all); |
322 | 0 | if(result) |
323 | 0 | return result; |
324 | 0 | if(*plast) { |
325 | | /* could not write last, paused again? */ |
326 | 0 | DEBUGASSERT(ctx->paused); |
327 | 0 | return CURLE_OK; |
328 | 0 | } |
329 | 0 | } |
330 | | |
331 | 0 | result = cw_out_buf_flush(ctx, data, cwbuf, flush_all); |
332 | 0 | if(result) |
333 | 0 | return result; |
334 | 0 | if(!curlx_dyn_len(&cwbuf->b)) { |
335 | 0 | cw_out_buf_free(cwbuf); |
336 | 0 | *pcwbuf = NULL; |
337 | 0 | } |
338 | 0 | return CURLE_OK; |
339 | 0 | } |
340 | | |
341 | | static CURLcode cw_out_append(struct cw_out_ctx *ctx, |
342 | | struct Curl_easy *data, |
343 | | cw_out_type otype, |
344 | | const char *buf, size_t blen) |
345 | 0 | { |
346 | 0 | CURL_TRC_WRITE(data, "[OUT] paused, buffering %zu more bytes (%zu/%d)", |
347 | 0 | blen, cw_out_bufs_len(ctx), DYN_PAUSE_BUFFER); |
348 | 0 | if(cw_out_bufs_len(ctx) + blen > DYN_PAUSE_BUFFER) { |
349 | 0 | failf(data, "pause buffer not large enough -> CURLE_TOO_LARGE"); |
350 | 0 | return CURLE_TOO_LARGE; |
351 | 0 | } |
352 | | |
353 | | /* if we do not have a buffer, or it is of another type, make a new one. |
354 | | * And for CW_OUT_HDS always make a new one, so we "replay" headers |
355 | | * exactly as they came in */ |
356 | 0 | if(!ctx->buf || (ctx->buf->type != otype) || (otype == CW_OUT_HDS)) { |
357 | 0 | struct cw_out_buf *cwbuf = cw_out_buf_create(otype); |
358 | 0 | if(!cwbuf) |
359 | 0 | return CURLE_OUT_OF_MEMORY; |
360 | 0 | cwbuf->next = ctx->buf; |
361 | 0 | ctx->buf = cwbuf; |
362 | 0 | } |
363 | 0 | DEBUGASSERT(ctx->buf && (ctx->buf->type == otype)); |
364 | 0 | return curlx_dyn_addn(&ctx->buf->b, buf, blen); |
365 | 0 | } |
366 | | |
367 | | static CURLcode cw_out_do_write(struct cw_out_ctx *ctx, |
368 | | struct Curl_easy *data, |
369 | | cw_out_type otype, |
370 | | bool flush_all, |
371 | | const char *buf, size_t blen) |
372 | 0 | { |
373 | 0 | CURLcode result = CURLE_OK; |
374 | | |
375 | | /* if we have buffered data and it is a different type than what |
376 | | * we are writing now, try to flush all */ |
377 | 0 | if(ctx->buf && ctx->buf->type != otype) { |
378 | 0 | result = cw_out_flush_chain(ctx, data, &ctx->buf, TRUE); |
379 | 0 | if(result) |
380 | 0 | goto out; |
381 | 0 | } |
382 | | |
383 | 0 | if(ctx->buf) { |
384 | | /* still have buffered data, append and flush */ |
385 | 0 | result = cw_out_append(ctx, data, otype, buf, blen); |
386 | 0 | if(result) |
387 | 0 | return result; |
388 | 0 | result = cw_out_flush_chain(ctx, data, &ctx->buf, flush_all); |
389 | 0 | if(result) |
390 | 0 | goto out; |
391 | 0 | } |
392 | 0 | else { |
393 | | /* nothing buffered, try direct write */ |
394 | 0 | size_t consumed; |
395 | 0 | result = cw_out_ptr_flush(ctx, data, otype, flush_all, |
396 | 0 | buf, blen, &consumed); |
397 | 0 | if(result) |
398 | 0 | return result; |
399 | 0 | if(consumed < blen) { |
400 | | /* did not write all, append the rest */ |
401 | 0 | result = cw_out_append(ctx, data, otype, |
402 | 0 | buf + consumed, blen - consumed); |
403 | 0 | if(result) |
404 | 0 | goto out; |
405 | 0 | } |
406 | 0 | } |
407 | | |
408 | 0 | out: |
409 | 0 | if(result) { |
410 | | /* We do not want to invoked client callbacks a second time after |
411 | | * encountering an error. See issue #13337 */ |
412 | 0 | ctx->errored = TRUE; |
413 | 0 | cw_out_bufs_free(ctx); |
414 | 0 | } |
415 | 0 | return result; |
416 | 0 | } |
417 | | |
418 | | static CURLcode cw_out_write(struct Curl_easy *data, |
419 | | struct Curl_cwriter *writer, int type, |
420 | | const char *buf, size_t blen) |
421 | 0 | { |
422 | 0 | struct cw_out_ctx *ctx = writer->ctx; |
423 | 0 | CURLcode result; |
424 | 0 | bool flush_all = !!(type & CLIENTWRITE_EOS); |
425 | |
|
426 | 0 | if((type & CLIENTWRITE_BODY) || |
427 | 0 | ((type & CLIENTWRITE_HEADER) && data->set.include_header)) { |
428 | 0 | cw_out_type otype = (!blen && (type & CLIENTWRITE_0LEN)) ? |
429 | 0 | CW_OUT_BODY_0LEN : CW_OUT_BODY; |
430 | 0 | result = cw_out_do_write(ctx, data, otype, flush_all, buf, blen); |
431 | 0 | if(result) |
432 | 0 | return result; |
433 | 0 | } |
434 | | |
435 | 0 | if(type & (CLIENTWRITE_HEADER|CLIENTWRITE_INFO)) { |
436 | 0 | result = cw_out_do_write(ctx, data, CW_OUT_HDS, flush_all, buf, blen); |
437 | 0 | if(result) |
438 | 0 | return result; |
439 | 0 | } |
440 | | |
441 | 0 | return CURLE_OK; |
442 | 0 | } |
443 | | |
444 | | bool Curl_cw_out_is_paused(struct Curl_easy *data) |
445 | 0 | { |
446 | 0 | struct Curl_cwriter *cw_out; |
447 | 0 | struct cw_out_ctx *ctx; |
448 | |
|
449 | 0 | cw_out = Curl_cwriter_get_by_type(data, &Curl_cwt_out); |
450 | 0 | if(!cw_out) |
451 | 0 | return FALSE; |
452 | | |
453 | 0 | ctx = (struct cw_out_ctx *)cw_out; |
454 | 0 | return ctx->paused; |
455 | 0 | } |
456 | | |
457 | | static CURLcode cw_out_flush(struct Curl_easy *data, |
458 | | struct Curl_cwriter *cw_out, |
459 | | bool flush_all) |
460 | 0 | { |
461 | 0 | struct cw_out_ctx *ctx = (struct cw_out_ctx *)cw_out; |
462 | 0 | CURLcode result = CURLE_OK; |
463 | |
|
464 | 0 | if(ctx->errored) |
465 | 0 | return CURLE_WRITE_ERROR; |
466 | 0 | if(ctx->paused) |
467 | 0 | return CURLE_OK; /* not doing it */ |
468 | | |
469 | 0 | result = cw_out_flush_chain(ctx, data, &ctx->buf, flush_all); |
470 | 0 | if(result) { |
471 | 0 | ctx->errored = TRUE; |
472 | 0 | cw_out_bufs_free(ctx); |
473 | 0 | return result; |
474 | 0 | } |
475 | 0 | return result; |
476 | 0 | } |
477 | | |
478 | | CURLcode Curl_cw_out_unpause(struct Curl_easy *data) |
479 | 0 | { |
480 | 0 | struct Curl_cwriter *cw_out; |
481 | 0 | CURLcode result = CURLE_OK; |
482 | |
|
483 | 0 | cw_out = Curl_cwriter_get_by_type(data, &Curl_cwt_out); |
484 | 0 | if(cw_out) { |
485 | 0 | struct cw_out_ctx *ctx = (struct cw_out_ctx *)cw_out; |
486 | 0 | CURL_TRC_WRITE(data, "[OUT] unpause"); |
487 | 0 | ctx->paused = FALSE; |
488 | 0 | result = Curl_cw_pause_flush(data); |
489 | 0 | if(!result) |
490 | 0 | result = cw_out_flush(data, cw_out, FALSE); |
491 | 0 | } |
492 | 0 | return result; |
493 | 0 | } |
494 | | |
495 | | CURLcode Curl_cw_out_done(struct Curl_easy *data) |
496 | 0 | { |
497 | 0 | struct Curl_cwriter *cw_out; |
498 | 0 | CURLcode result = CURLE_OK; |
499 | |
|
500 | 0 | cw_out = Curl_cwriter_get_by_type(data, &Curl_cwt_out); |
501 | 0 | if(cw_out) { |
502 | 0 | CURL_TRC_WRITE(data, "[OUT] done"); |
503 | 0 | result = Curl_cw_pause_flush(data); |
504 | 0 | if(!result) |
505 | 0 | result = cw_out_flush(data, cw_out, TRUE); |
506 | 0 | } |
507 | 0 | return result; |
508 | 0 | } |