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 | 19.3k | { |
108 | 19.3k | struct cw_out_ctx *ctx = writer->ctx; |
109 | 19.3k | (void)data; |
110 | 19.3k | ctx->buf = NULL; |
111 | 19.3k | ctx->tail = NULL; |
112 | 19.3k | ctx->buffered_len = 0; |
113 | 19.3k | return CURLE_OK; |
114 | 19.3k | } |
115 | | |
116 | | static void cw_out_bufs_free(struct cw_out_ctx *ctx) |
117 | 19.3k | { |
118 | 19.3k | 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 | 19.3k | ctx->tail = NULL; |
124 | 19.3k | ctx->buffered_len = 0; |
125 | 19.3k | } |
126 | | |
127 | | static void cw_out_close(struct Curl_easy *data, struct Curl_cwriter *writer) |
128 | 19.3k | { |
129 | 19.3k | struct cw_out_ctx *ctx = writer->ctx; |
130 | | |
131 | 19.3k | (void)data; |
132 | 19.3k | cw_out_bufs_free(ctx); |
133 | 19.3k | } |
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 | 304k | { |
142 | 304k | switch(otype) { |
143 | 171k | case CW_OUT_BODY: |
144 | 171k | case CW_OUT_BODY_0LEN: |
145 | 171k | *pwcb = data->set.fwrite_func; |
146 | 171k | *pwcb_data = data->set.out; |
147 | 171k | *pmax_write = CURL_MAX_WRITE_SIZE; |
148 | | /* if we ever want buffering of BODY output, we can set `min_write` |
149 | | * the preferred size. The default should always be to pass data |
150 | | * to the client as it comes without delay */ |
151 | 171k | *pmin_write = 0; |
152 | 171k | break; |
153 | 133k | case CW_OUT_HDS: |
154 | 133k | *pwcb = data->set.fwrite_header ? data->set.fwrite_header : |
155 | 133k | (data->set.writeheader ? data->set.fwrite_func : NULL); |
156 | 133k | *pwcb_data = data->set.writeheader; |
157 | 133k | *pmax_write = 0; /* do not chunk-write headers, write them as they are */ |
158 | 133k | *pmin_write = 0; |
159 | 133k | break; |
160 | 0 | default: |
161 | 0 | *pwcb = NULL; |
162 | 0 | *pwcb_data = NULL; |
163 | 0 | *pmax_write = CURL_MAX_WRITE_SIZE; |
164 | 0 | *pmin_write = 0; |
165 | 304k | } |
166 | 304k | } |
167 | | |
168 | | static CURLcode cw_out_cb_write(struct Curl_easy *data, |
169 | | curl_write_callback wcb, |
170 | | void *wcb_data, |
171 | | cw_out_type otype, |
172 | | const char *buf, size_t blen, |
173 | | size_t *pnwritten) |
174 | 292k | { |
175 | 292k | size_t nwritten; |
176 | 292k | CURLcode result; |
177 | | |
178 | 292k | NOVERBOSE((void)otype); |
179 | | |
180 | 292k | DEBUGASSERT(data->conn); |
181 | 292k | *pnwritten = 0; |
182 | 292k | { |
183 | 292k | struct Curl_mapi_guard guard; |
184 | 292k | CURL_CBAPI_START(&guard, data, easy_cw_out_cb); |
185 | 292k | nwritten = wcb((char *)CURL_UNCONST(buf), 1, blen, wcb_data); |
186 | 292k | CURL_CBAPI_END(&guard); |
187 | 292k | } |
188 | 292k | CURL_TRC_WRITE(data, "[OUT] wrote %zu %s bytes, type=%x -> %zu", |
189 | 292k | blen, (otype == CW_OUT_HDS) ? "header" : "body", |
190 | 292k | (unsigned int)otype, nwritten); |
191 | 292k | if(nwritten == CURL_WRITEFUNC_PAUSE) { |
192 | 0 | if(data->conn->scheme->flags & PROTOPT_NONETWORK) { |
193 | | /* Protocols that work without network cannot be paused. This is |
194 | | actually only file:// now, and it cannot pause since the transfer is |
195 | | not done using the "normal" procedure. */ |
196 | 0 | failf(data, "Write callback asked for PAUSE when not supported"); |
197 | 0 | return CURLE_WRITE_ERROR; |
198 | 0 | } |
199 | 0 | data->req.writer.paused = TRUE; |
200 | 0 | CURL_TRC_WRITE(data, "[OUT] PAUSE requested by client"); |
201 | 0 | result = Curl_xfer_pause_recv(data, TRUE); |
202 | 0 | return result ? result : CURLE_AGAIN; |
203 | 0 | } |
204 | 292k | else if(nwritten == CURL_WRITEFUNC_ERROR) { |
205 | 0 | failf(data, "client returned ERROR on write of %zu bytes", blen); |
206 | 0 | return CURLE_WRITE_ERROR; |
207 | 0 | } |
208 | 292k | else if(nwritten != blen) { |
209 | 0 | failf(data, "Failure writing output to destination, " |
210 | 0 | "passed %zu returned %zu", blen, nwritten); |
211 | 0 | return CURLE_WRITE_ERROR; |
212 | 0 | } |
213 | 292k | *pnwritten = nwritten; |
214 | 292k | return CURLE_OK; |
215 | 292k | } |
216 | | |
217 | | static CURLcode cw_out_ptr_flush(struct cw_out_ctx *ctx, |
218 | | struct Curl_easy *data, |
219 | | cw_out_type otype, |
220 | | bool flush_all, |
221 | | const char *buf, size_t blen, |
222 | | size_t *pconsumed) |
223 | 304k | { |
224 | 304k | curl_write_callback wcb = NULL; |
225 | 304k | void *wcb_data; |
226 | 304k | size_t max_write, min_write; |
227 | 304k | size_t wlen, nwritten = 0; |
228 | 304k | CURLcode result = CURLE_OK; |
229 | | |
230 | | /* If we errored once, we do not invoke the client callback again */ |
231 | 304k | if(ctx->errored) |
232 | 0 | return CURLE_WRITE_ERROR; |
233 | | |
234 | | /* write callbacks may get NULLed by the client between calls. */ |
235 | 304k | cw_get_writefunc(data, otype, &wcb, &wcb_data, &max_write, &min_write); |
236 | 304k | if(!wcb) { |
237 | 0 | *pconsumed = blen; |
238 | 0 | return CURLE_OK; |
239 | 0 | } |
240 | | |
241 | 304k | *pconsumed = 0; |
242 | 304k | if(otype == CW_OUT_BODY_0LEN) { |
243 | 0 | DEBUGASSERT(!blen); |
244 | 0 | return cw_out_cb_write(data, wcb, wcb_data, otype, |
245 | 0 | buf, blen, &nwritten); |
246 | 0 | } |
247 | 304k | else { |
248 | 597k | while(blen && !data->req.writer.paused) { |
249 | 292k | if(!flush_all && blen < min_write) |
250 | 0 | break; |
251 | 292k | wlen = max_write ? CURLMIN(blen, max_write) : blen; |
252 | 292k | if(otype == CW_OUT_BODY) |
253 | 158k | result = Curl_pgrs_deliver_check(data, wlen); |
254 | 292k | if(!result) |
255 | 292k | result = cw_out_cb_write(data, wcb, wcb_data, otype, |
256 | 292k | buf, wlen, &nwritten); |
257 | 292k | if(result) |
258 | 30 | return result; |
259 | 292k | if(otype == CW_OUT_BODY) |
260 | 158k | Curl_pgrs_deliver_inc(data, nwritten); |
261 | 292k | *pconsumed += nwritten; |
262 | 292k | blen -= nwritten; |
263 | 292k | buf += nwritten; |
264 | 292k | } |
265 | 304k | } |
266 | 304k | return CURLE_OK; |
267 | 304k | } |
268 | | |
269 | | static CURLcode cw_out_buf_flush(struct cw_out_ctx *ctx, |
270 | | struct Curl_easy *data, |
271 | | struct cw_out_buf *cwbuf, |
272 | | bool flush_all) |
273 | 0 | { |
274 | 0 | CURLcode result = CURLE_OK; |
275 | |
|
276 | 0 | if(curlx_dyn_len(&cwbuf->b) || (cwbuf->type == CW_OUT_BODY_0LEN)) { |
277 | 0 | size_t consumed; |
278 | |
|
279 | 0 | result = cw_out_ptr_flush(ctx, data, cwbuf->type, flush_all, |
280 | 0 | curlx_dyn_ptr(&cwbuf->b), |
281 | 0 | curlx_dyn_len(&cwbuf->b), |
282 | 0 | &consumed); |
283 | 0 | if(result && (result != CURLE_AGAIN)) |
284 | 0 | return result; |
285 | 0 | result = CURLE_OK; |
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 | | bool flush_all) |
306 | 0 | { |
307 | 0 | CURLcode result; |
308 | | |
309 | | /* write the chain oldest first until it blocks or gets empty */ |
310 | 0 | while(ctx->buf) { |
311 | 0 | struct cw_out_buf *cwbuf = ctx->buf; |
312 | 0 | size_t blen_before; |
313 | |
|
314 | 0 | if(data->req.writer.paused) |
315 | 0 | return CURLE_OK; |
316 | | |
317 | 0 | blen_before = curlx_dyn_len(&cwbuf->b); |
318 | 0 | result = cw_out_buf_flush(ctx, data, cwbuf, flush_all); |
319 | 0 | if(result) |
320 | 0 | return result; |
321 | 0 | ctx->buffered_len -= blen_before - curlx_dyn_len(&cwbuf->b); |
322 | |
|
323 | 0 | if(curlx_dyn_len(&cwbuf->b)) { |
324 | | /* could not write it all, paused again? */ |
325 | 0 | DEBUGASSERT(data->req.writer.paused); |
326 | 0 | return CURLE_OK; |
327 | 0 | } |
328 | | |
329 | 0 | ctx->buf = cwbuf->next; |
330 | 0 | if(!ctx->buf) |
331 | 0 | ctx->tail = NULL; |
332 | 0 | cw_out_buf_free(cwbuf); |
333 | 0 | } |
334 | 0 | return CURLE_OK; |
335 | 0 | } |
336 | | |
337 | | static CURLcode cw_out_append(struct cw_out_ctx *ctx, |
338 | | struct Curl_easy *data, |
339 | | cw_out_type otype, |
340 | | const char *buf, size_t blen) |
341 | 0 | { |
342 | 0 | CURLcode result; |
343 | |
|
344 | 0 | CURL_TRC_WRITE(data, "[OUT] paused, buffering %zu more bytes (%zu/%d)", |
345 | 0 | blen, ctx->buffered_len, DYN_PAUSE_BUFFER); |
346 | 0 | if(ctx->buffered_len + blen > DYN_PAUSE_BUFFER) { |
347 | 0 | failf(data, "pause buffer not large enough -> CURLE_TOO_LARGE"); |
348 | 0 | return CURLE_TOO_LARGE; |
349 | 0 | } |
350 | | |
351 | | /* if we do not have a buffer, or it is of another type, make a new one |
352 | | * and append it at the tail. For CW_OUT_HDS always make a new one, so |
353 | | * we "replay" headers exactly as they came in */ |
354 | 0 | if(!ctx->tail || (ctx->tail->type != otype) || (otype == CW_OUT_HDS)) { |
355 | 0 | struct cw_out_buf *cwbuf = cw_out_buf_create(otype); |
356 | 0 | if(!cwbuf) |
357 | 0 | return CURLE_OUT_OF_MEMORY; |
358 | 0 | if(ctx->tail) |
359 | 0 | ctx->tail->next = cwbuf; |
360 | 0 | else |
361 | 0 | ctx->buf = cwbuf; |
362 | 0 | ctx->tail = cwbuf; |
363 | 0 | } |
364 | 0 | DEBUGASSERT(ctx->tail && (ctx->tail->type == otype)); |
365 | 0 | result = curlx_dyn_addn(&ctx->tail->b, buf, blen); |
366 | 0 | if(!result) |
367 | 0 | ctx->buffered_len += blen; |
368 | 0 | return result; |
369 | 0 | } |
370 | | |
371 | | static CURLcode cw_out_do_write(struct cw_out_ctx *ctx, |
372 | | struct Curl_easy *data, |
373 | | cw_out_type otype, |
374 | | bool flush_all, |
375 | | const char *buf, size_t blen) |
376 | 304k | { |
377 | 304k | CURLcode result = CURLE_OK; |
378 | | |
379 | | /* if we have buffered data and the last chunk buffered is of a |
380 | | * different type than what we are writing now, try to flush all */ |
381 | 304k | if(ctx->tail && ctx->tail->type != otype) { |
382 | 0 | result = cw_out_flush_chain(ctx, data, TRUE); |
383 | 0 | if(result) |
384 | 0 | goto out; |
385 | 0 | } |
386 | | |
387 | 304k | if(ctx->buf) { |
388 | | /* still have buffered data, append and flush */ |
389 | 0 | result = cw_out_append(ctx, data, otype, buf, blen); |
390 | 0 | if(result) |
391 | 0 | goto out; |
392 | 0 | result = cw_out_flush_chain(ctx, data, flush_all); |
393 | 0 | if(result) |
394 | 0 | goto out; |
395 | 0 | } |
396 | 304k | else { |
397 | | /* nothing buffered, try direct write */ |
398 | 304k | size_t consumed; |
399 | 304k | result = cw_out_ptr_flush(ctx, data, otype, flush_all, |
400 | 304k | buf, blen, &consumed); |
401 | 304k | if(result && (result != CURLE_AGAIN)) |
402 | 30 | return result; |
403 | 304k | result = CURLE_OK; |
404 | 304k | if(consumed < blen) { |
405 | | /* did not write all, append the rest */ |
406 | 0 | result = cw_out_append(ctx, data, otype, |
407 | 0 | buf + consumed, blen - consumed); |
408 | 0 | if(result) |
409 | 0 | goto out; |
410 | 0 | } |
411 | 304k | } |
412 | | |
413 | 304k | out: |
414 | 304k | if(result) { |
415 | | /* We do not want to invoke client callbacks a second time after |
416 | | * encountering an error. See issue #13337 */ |
417 | 0 | ctx->errored = TRUE; |
418 | 0 | cw_out_bufs_free(ctx); |
419 | 0 | } |
420 | 304k | return result; |
421 | 304k | } |
422 | | |
423 | | static CURLcode cw_out_write(struct Curl_easy *data, |
424 | | struct Curl_cwriter *writer, int type, |
425 | | const char *buf, size_t blen) |
426 | 304k | { |
427 | 304k | struct cw_out_ctx *ctx = writer->ctx; |
428 | 304k | CURLcode result; |
429 | 304k | bool flush_all = !!(type & CLIENTWRITE_EOS); |
430 | | |
431 | 304k | if((type & CLIENTWRITE_BODY) || |
432 | 171k | ((type & CLIENTWRITE_HEADER) && data->set.include_header)) { |
433 | 171k | cw_out_type otype = (!blen && (type & CLIENTWRITE_0LEN)) ? |
434 | 171k | CW_OUT_BODY_0LEN : CW_OUT_BODY; |
435 | 171k | result = cw_out_do_write(ctx, data, otype, flush_all, buf, blen); |
436 | 171k | if(result) |
437 | 30 | return result; |
438 | 171k | } |
439 | | |
440 | 304k | if(type & (CLIENTWRITE_HEADER | CLIENTWRITE_INFO)) { |
441 | 133k | result = cw_out_do_write(ctx, data, CW_OUT_HDS, flush_all, buf, blen); |
442 | 133k | if(result) |
443 | 0 | return result; |
444 | 133k | } |
445 | | |
446 | 304k | return CURLE_OK; |
447 | 304k | } |
448 | | |
449 | | static CURLcode cw_out_do_flush(struct Curl_easy *data, |
450 | | struct Curl_cwriter *cw_out, |
451 | | bool flush_all) |
452 | 38.6k | { |
453 | 38.6k | struct cw_out_ctx *ctx = (struct cw_out_ctx *)cw_out; |
454 | | |
455 | 38.6k | if(ctx->errored) |
456 | 0 | return CURLE_WRITE_ERROR; |
457 | | |
458 | 38.6k | if(!data->req.writer.paused && ctx->buf) { |
459 | 0 | CURLcode result; |
460 | |
|
461 | 0 | CURL_TRC_WRITE(data, "[OUT] flush"); |
462 | 0 | result = cw_out_flush_chain(ctx, data, flush_all); |
463 | 0 | if(result) { |
464 | 0 | ctx->errored = TRUE; |
465 | 0 | cw_out_bufs_free(ctx); |
466 | 0 | return result; |
467 | 0 | } |
468 | 0 | } |
469 | 38.6k | return CURLE_OK; |
470 | 38.6k | } |
471 | | |
472 | | static CURLcode cw_out_flush(struct Curl_easy *data, |
473 | | struct Curl_cwriter *writer) |
474 | 19.3k | { |
475 | 19.3k | return cw_out_do_flush(data, writer, FALSE); |
476 | 19.3k | } |
477 | | |
478 | | const struct Curl_cwtype Curl_cwt_out = { |
479 | | "cw-out", |
480 | | NULL, |
481 | | 0, |
482 | | cw_out_init, |
483 | | cw_out_write, |
484 | | cw_out_flush, |
485 | | cw_out_close, |
486 | | sizeof(struct cw_out_ctx) |
487 | | }; |
488 | | |
489 | | CURLcode Curl_cw_out_done(struct Curl_easy *data) |
490 | 19.3k | { |
491 | 19.3k | struct Curl_cwriter *cw_out; |
492 | 19.3k | CURLcode result = CURLE_OK; |
493 | | |
494 | 19.3k | cw_out = Curl_cwriter_get_by_type(data, &Curl_cwt_out); |
495 | 19.3k | if(cw_out) { |
496 | 19.3k | CURL_TRC_WRITE(data, "[OUT] done"); |
497 | 19.3k | result = Curl_client_flush(data); |
498 | 19.3k | if(!result) |
499 | 19.3k | result = cw_out_do_flush(data, cw_out, TRUE); |
500 | 19.3k | } |
501 | 19.3k | return result; |
502 | 19.3k | } |