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