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