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 | | #ifdef HAVE_NETINET_IN_H |
27 | | #include <netinet/in.h> |
28 | | #endif |
29 | | |
30 | | #ifdef HAVE_LINUX_TCP_H |
31 | | #include <linux/tcp.h> |
32 | | #elif defined(HAVE_NETINET_TCP_H) |
33 | | #include <netinet/tcp.h> |
34 | | #endif |
35 | | |
36 | | #include "urldata.h" |
37 | | #include "sendf.h" |
38 | | #include "curl_trc.h" |
39 | | #include "transfer.h" |
40 | | #include "cfilters.h" |
41 | | #include "connect.h" |
42 | | #include "cw-out.h" |
43 | | #include "cw-pause.h" |
44 | | #include "multiif.h" |
45 | | #include "progress.h" |
46 | | |
47 | | static void cl_reset_writer(struct Curl_easy *data) |
48 | 0 | { |
49 | 0 | struct Curl_cwriter *writer = data->req.writer.stack; |
50 | 0 | while(writer) { |
51 | 0 | data->req.writer.stack = writer->next; |
52 | 0 | writer->cwt->do_close(data, writer); |
53 | 0 | curlx_free(writer); |
54 | 0 | writer = data->req.writer.stack; |
55 | 0 | } |
56 | 0 | data->req.writer.paused = FALSE; |
57 | 0 | } |
58 | | |
59 | | static void cl_reset_reader(struct Curl_easy *data) |
60 | 0 | { |
61 | 0 | struct Curl_creader *reader = data->req.reader.stack; |
62 | 0 | data->req.reader_started = FALSE; |
63 | 0 | while(reader) { |
64 | 0 | data->req.reader.stack = reader->next; |
65 | 0 | reader->crt->do_close(data, reader); |
66 | 0 | curlx_free(reader); |
67 | 0 | reader = data->req.reader.stack; |
68 | 0 | } |
69 | 0 | } |
70 | | |
71 | | void Curl_client_cleanup(struct Curl_easy *data) |
72 | 0 | { |
73 | 0 | cl_reset_reader(data); |
74 | 0 | cl_reset_writer(data); |
75 | |
|
76 | 0 | data->req.bytecount = 0; |
77 | 0 | data->req.headerline = 0; |
78 | 0 | } |
79 | | |
80 | | void Curl_client_reset(struct Curl_easy *data) |
81 | 0 | { |
82 | 0 | if(data->req.rewind_read) { |
83 | | /* already requested */ |
84 | 0 | CURL_TRC_READ(data, "client_reset, will rewind reader"); |
85 | 0 | } |
86 | 0 | else { |
87 | 0 | CURL_TRC_READ(data, "client_reset, clear readers"); |
88 | 0 | cl_reset_reader(data); |
89 | 0 | } |
90 | 0 | cl_reset_writer(data); |
91 | |
|
92 | 0 | data->req.bytecount = 0; |
93 | 0 | data->req.headerline = 0; |
94 | 0 | } |
95 | | |
96 | | CURLcode Curl_client_start(struct Curl_easy *data) |
97 | 0 | { |
98 | 0 | if(data->req.rewind_read) { |
99 | 0 | struct Curl_creader *r = data->req.reader.stack; |
100 | 0 | CURLcode result = CURLE_OK; |
101 | |
|
102 | 0 | CURL_TRC_READ(data, "client start, rewind readers"); |
103 | 0 | while(r) { |
104 | 0 | result = r->crt->cntrl(data, r, CURL_CRCNTRL_REWIND); |
105 | 0 | if(result) { |
106 | 0 | failf(data, "rewind of client reader '%s' failed: %d", |
107 | 0 | r->crt->name, (int)result); |
108 | 0 | return result; |
109 | 0 | } |
110 | 0 | r = r->next; |
111 | 0 | } |
112 | 0 | data->req.rewind_read = FALSE; |
113 | 0 | cl_reset_reader(data); |
114 | 0 | } |
115 | 0 | return CURLE_OK; |
116 | 0 | } |
117 | | |
118 | | bool Curl_creader_will_rewind(struct Curl_easy *data) |
119 | 0 | { |
120 | 0 | return (bool)data->req.rewind_read; |
121 | 0 | } |
122 | | |
123 | | void Curl_creader_set_rewind(struct Curl_easy *data, bool enable) |
124 | 0 | { |
125 | 0 | data->req.rewind_read = !!enable; |
126 | 0 | } |
127 | | |
128 | | CURLcode Curl_cwriter_def_init(struct Curl_easy *data, |
129 | | struct Curl_cwriter *writer) |
130 | 0 | { |
131 | 0 | (void)data; |
132 | 0 | (void)writer; |
133 | 0 | return CURLE_OK; |
134 | 0 | } |
135 | | |
136 | | CURLcode Curl_cwriter_def_write(struct Curl_easy *data, |
137 | | struct Curl_cwriter *writer, int type, |
138 | | const char *buf, size_t nbytes) |
139 | 0 | { |
140 | 0 | return Curl_cwriter_write(data, writer->next, type, buf, nbytes); |
141 | 0 | } |
142 | | |
143 | | CURLcode Curl_cwriter_def_flush(struct Curl_easy *data, |
144 | | struct Curl_cwriter *writer) |
145 | 0 | { |
146 | 0 | return Curl_cwriter_flush(data, writer->next); |
147 | 0 | } |
148 | | |
149 | | void Curl_cwriter_def_close(struct Curl_easy *data, |
150 | | struct Curl_cwriter *writer) |
151 | 0 | { |
152 | 0 | (void)data; |
153 | 0 | (void)writer; |
154 | 0 | } |
155 | | |
156 | | static size_t get_max_body_write_len(struct Curl_easy *data, curl_off_t limit) |
157 | 0 | { |
158 | 0 | if(limit != -1) { |
159 | | /* How much more are we allowed to write? */ |
160 | 0 | return curlx_sotouz_range(limit - data->req.bytecount, 0, SIZE_MAX); |
161 | 0 | } |
162 | 0 | return SIZE_MAX; |
163 | 0 | } |
164 | | |
165 | | struct cw_download_ctx { |
166 | | struct Curl_cwriter super; |
167 | | BIT(started_response); |
168 | | BIT(started_body); |
169 | | }; |
170 | | |
171 | | /* Download client writer in phase CURL_CW_PROTOCOL that |
172 | | * sees the "real" download body data. */ |
173 | | static CURLcode cw_download_write(struct Curl_easy *data, |
174 | | struct Curl_cwriter *writer, int type, |
175 | | const char *buf, size_t nbytes) |
176 | 0 | { |
177 | 0 | struct cw_download_ctx *ctx = writer->ctx; |
178 | 0 | CURLcode result; |
179 | 0 | size_t nwrite, excess_len = 0; |
180 | 0 | bool is_connect = !!(type & CLIENTWRITE_CONNECT); |
181 | |
|
182 | 0 | if(!ctx->started_response && |
183 | 0 | !(type & CLIENTWRITE_CONNECT) && |
184 | 0 | (!(type & CLIENTWRITE_INFO) || data->req.upload_done)) { |
185 | 0 | Curl_pgrsTime(data, TIMER_STARTTRANSFER); |
186 | 0 | ctx->started_response = TRUE; |
187 | 0 | } |
188 | |
|
189 | 0 | if(!(type & CLIENTWRITE_BODY)) { |
190 | 0 | if(is_connect && data->set.suppress_connect_headers) |
191 | 0 | return CURLE_OK; |
192 | 0 | #ifdef DEBUGBUILD |
193 | 0 | if(is_connect && getenv("CURL_DBG_SUPPRESS_CONNECT_HDS")) |
194 | 0 | return CURLE_OK; |
195 | 0 | #endif |
196 | 0 | result = Curl_cwriter_write(data, writer->next, type, buf, nbytes); |
197 | 0 | CURL_TRC_WRITE(data, "download_write header(type=%x, blen=%zu) -> %d", |
198 | 0 | (unsigned int)type, nbytes, (int)result); |
199 | 0 | return result; |
200 | 0 | } |
201 | | |
202 | 0 | if(!ctx->started_body && |
203 | 0 | !(type & (CLIENTWRITE_INFO | CLIENTWRITE_CONNECT))) { |
204 | 0 | Curl_rlimit_start(&data->progress.dl.rlimit, Curl_pgrs_now(data), |
205 | 0 | data->req.size); |
206 | 0 | ctx->started_body = TRUE; |
207 | 0 | } |
208 | | |
209 | | /* Here, we deal with REAL BODY bytes. All filtering and transfer |
210 | | * encodings have been applied and only the true content, e.g. BODY, |
211 | | * bytes are passed here. |
212 | | * This allows us to check sizes, update stats, etc. independent |
213 | | * from the protocol in play. */ |
214 | |
|
215 | 0 | if(data->req.no_body && nbytes > 0) { |
216 | | /* BODY arrives although we want none, bail out */ |
217 | 0 | streamclose(data->conn); |
218 | 0 | CURL_TRC_WRITE(data, "download_write body(type=%x, blen=%zu), " |
219 | 0 | "did not want a BODY", (unsigned int)type, nbytes); |
220 | 0 | data->req.download_done = TRUE; |
221 | 0 | if(data->info.header_size) |
222 | | /* if headers have been received, this is fine */ |
223 | 0 | return CURLE_OK; |
224 | 0 | return CURLE_WEIRD_SERVER_REPLY; |
225 | 0 | } |
226 | | |
227 | | /* Determine if we see any bytes in excess to what is allowed. |
228 | | * We write the allowed bytes and handle excess further below. |
229 | | * This gives deterministic BODY writes on varying buffer receive |
230 | | * lengths. */ |
231 | 0 | nwrite = nbytes; |
232 | 0 | if(data->req.maxdownload != -1) { |
233 | 0 | size_t wmax = get_max_body_write_len(data, data->req.maxdownload); |
234 | 0 | if(nwrite > wmax) { |
235 | 0 | excess_len = nbytes - wmax; |
236 | 0 | nwrite = wmax; |
237 | 0 | } |
238 | |
|
239 | 0 | if(nwrite == wmax) { |
240 | 0 | data->req.download_done = TRUE; |
241 | 0 | } |
242 | |
|
243 | 0 | if((type & CLIENTWRITE_EOS) && !data->req.no_body && |
244 | 0 | (data->req.size > data->req.bytecount)) { |
245 | 0 | failf(data, "end of response with %" FMT_OFF_T " bytes missing", |
246 | 0 | data->req.size - data->req.bytecount); |
247 | 0 | return CURLE_PARTIAL_FILE; |
248 | 0 | } |
249 | 0 | } |
250 | | |
251 | | /* Error on too large filesize is handled below, after writing |
252 | | * the permitted bytes */ |
253 | 0 | if(data->set.max_filesize && !data->req.ignorebody) { |
254 | 0 | size_t wmax = get_max_body_write_len(data, data->set.max_filesize); |
255 | 0 | if(nwrite > wmax) { |
256 | 0 | nwrite = wmax; |
257 | 0 | } |
258 | 0 | } |
259 | |
|
260 | 0 | if(!data->req.ignorebody && (nwrite || (type & CLIENTWRITE_EOS))) { |
261 | 0 | result = Curl_cwriter_write(data, writer->next, type, buf, nwrite); |
262 | 0 | CURL_TRC_WRITE(data, "download_write body(type=%x, blen=%zu) -> %d", |
263 | 0 | (unsigned int)type, nbytes, (int)result); |
264 | 0 | if(result) |
265 | 0 | return result; |
266 | 0 | } |
267 | | |
268 | | /* Update stats, write and report progress */ |
269 | 0 | if(nwrite) { |
270 | 0 | data->req.bytecount += nwrite; |
271 | 0 | Curl_pgrs_download_inc(data, nwrite); |
272 | 0 | } |
273 | |
|
274 | 0 | if(excess_len) { |
275 | 0 | if(!data->req.ignorebody) { |
276 | 0 | infof(data, |
277 | 0 | "Excess found writing body:" |
278 | 0 | " excess = %zu" |
279 | 0 | ", size = %" FMT_OFF_T |
280 | 0 | ", maxdownload = %" FMT_OFF_T |
281 | 0 | ", bytecount = %" FMT_OFF_T, |
282 | 0 | excess_len, data->req.size, data->req.maxdownload, |
283 | 0 | data->req.bytecount); |
284 | 0 | connclose(data->conn); |
285 | 0 | } |
286 | 0 | } |
287 | 0 | else if((nwrite < nbytes) && !data->req.ignorebody) { |
288 | 0 | failf(data, "Exceeded the maximum allowed file size " |
289 | 0 | "(%" FMT_OFF_T ") with %" FMT_OFF_T " bytes", |
290 | 0 | data->set.max_filesize, data->req.bytecount); |
291 | 0 | return CURLE_FILESIZE_EXCEEDED; |
292 | 0 | } |
293 | | |
294 | 0 | return CURLE_OK; |
295 | 0 | } |
296 | | |
297 | | static const struct Curl_cwtype cw_download = { |
298 | | "protocol", |
299 | | NULL, |
300 | | 0, |
301 | | Curl_cwriter_def_init, |
302 | | cw_download_write, |
303 | | Curl_cwriter_def_flush, |
304 | | Curl_cwriter_def_close, |
305 | | sizeof(struct cw_download_ctx) |
306 | | }; |
307 | | |
308 | | /* RAW client writer in phase CURL_CW_RAW that |
309 | | * enabled tracing of raw data. */ |
310 | | static CURLcode cw_raw_write(struct Curl_easy *data, |
311 | | struct Curl_cwriter *writer, int type, |
312 | | const char *buf, size_t nbytes) |
313 | 0 | { |
314 | 0 | if(type & CLIENTWRITE_BODY && data->set.verbose && !data->req.ignorebody) { |
315 | 0 | Curl_debug(data, CURLINFO_DATA_IN, buf, nbytes); |
316 | 0 | } |
317 | 0 | return Curl_cwriter_write(data, writer->next, type, buf, nbytes); |
318 | 0 | } |
319 | | |
320 | | static const struct Curl_cwtype cw_raw = { |
321 | | "raw", |
322 | | NULL, |
323 | | 0, |
324 | | Curl_cwriter_def_init, |
325 | | cw_raw_write, |
326 | | Curl_cwriter_def_flush, |
327 | | Curl_cwriter_def_close, |
328 | | sizeof(struct Curl_cwriter) |
329 | | }; |
330 | | |
331 | | static void cwriter_add(struct Curl_easy *data, |
332 | | struct Curl_cwriter *writer) |
333 | 0 | { |
334 | 0 | struct Curl_cwriter **anchor = &data->req.writer.stack; |
335 | | |
336 | | /* Insert the writer as first in its phase. |
337 | | * Skip existing writers of lower phases. */ |
338 | 0 | while(*anchor && (*anchor)->phase < writer->phase) |
339 | 0 | anchor = &(*anchor)->next; |
340 | 0 | writer->next = *anchor; |
341 | 0 | *anchor = writer; |
342 | 0 | } |
343 | | |
344 | | static CURLcode do_init_writer_stack(struct Curl_easy *data) |
345 | 0 | { |
346 | 0 | struct Curl_cwriter *writer; |
347 | 0 | CURLcode result; |
348 | |
|
349 | 0 | DEBUGASSERT(!data->req.writer.stack); |
350 | 0 | result = Curl_cwriter_create(&data->req.writer.stack, |
351 | 0 | data, &Curl_cwt_out, CURL_CW_CLIENT); |
352 | 0 | if(result) |
353 | 0 | return result; |
354 | | |
355 | 0 | result = Curl_cwriter_create(&writer, data, &cw_download, CURL_CW_PROTOCOL); |
356 | 0 | if(result) |
357 | 0 | return result; |
358 | 0 | cwriter_add(data, writer); |
359 | |
|
360 | 0 | result = Curl_cwriter_create(&writer, data, &cw_raw, CURL_CW_RAW); |
361 | 0 | if(result) |
362 | 0 | return result; |
363 | 0 | cwriter_add(data, writer); |
364 | |
|
365 | 0 | return result; |
366 | 0 | } |
367 | | |
368 | | /* Curl_client_write() sends data to the write callback(s) |
369 | | |
370 | | The bit pattern defines to what "streams" to write to. Body and/or header. |
371 | | The defines are in sendf.h of course. |
372 | | */ |
373 | | CURLcode Curl_client_write(struct Curl_easy *data, int type, const char *buf, |
374 | | size_t len) |
375 | 0 | { |
376 | 0 | CURLcode result; |
377 | | |
378 | | /* it is one of those, at least */ |
379 | 0 | DEBUGASSERT(type & |
380 | 0 | (CLIENTWRITE_BODY | CLIENTWRITE_HEADER | CLIENTWRITE_INFO)); |
381 | | /* BODY is only BODY (with optional EOS) */ |
382 | 0 | DEBUGASSERT(!(type & CLIENTWRITE_BODY) || |
383 | 0 | ((type & ~(CLIENTWRITE_BODY | CLIENTWRITE_EOS)) == 0)); |
384 | | /* INFO is only INFO (with optional EOS) */ |
385 | 0 | DEBUGASSERT(!(type & CLIENTWRITE_INFO) || |
386 | 0 | ((type & ~(CLIENTWRITE_INFO | CLIENTWRITE_EOS)) == 0)); |
387 | |
|
388 | 0 | if(!data->req.writer.stack) { |
389 | 0 | result = do_init_writer_stack(data); |
390 | 0 | if(result) |
391 | 0 | return result; |
392 | 0 | DEBUGASSERT(data->req.writer.stack); |
393 | 0 | } |
394 | | |
395 | 0 | result = Curl_cwriter_write(data, data->req.writer.stack, type, buf, len); |
396 | 0 | CURL_TRC_WRITE(data, "client_write(type=%x, len=%zu) -> %d", |
397 | 0 | (unsigned int)type, len, (int)result); |
398 | 0 | return result; |
399 | 0 | } |
400 | | |
401 | | CURLcode Curl_client_flush(struct Curl_easy *data) |
402 | 0 | { |
403 | 0 | CURLcode result; |
404 | |
|
405 | 0 | if(!data->req.writer.stack) { |
406 | 0 | result = do_init_writer_stack(data); |
407 | 0 | if(result) |
408 | 0 | return result; |
409 | 0 | DEBUGASSERT(data->req.writer.stack); |
410 | 0 | } |
411 | | |
412 | 0 | result = Curl_cwriter_flush(data, data->req.writer.stack); |
413 | 0 | CURL_TRC_WRITE(data, "client_flush() -> %d", (int)result); |
414 | 0 | return result; |
415 | 0 | } |
416 | | |
417 | | /* Create an unencoding writer stage using the given handler. */ |
418 | | CURLcode Curl_cwriter_create(struct Curl_cwriter **pwriter, |
419 | | struct Curl_easy *data, |
420 | | const struct Curl_cwtype *cwt, |
421 | | Curl_cwriter_phase phase) |
422 | 0 | { |
423 | 0 | struct Curl_cwriter *writer = NULL; |
424 | 0 | CURLcode result = CURLE_OUT_OF_MEMORY; |
425 | 0 | void *p; |
426 | |
|
427 | 0 | DEBUGASSERT(cwt->cwriter_size >= sizeof(struct Curl_cwriter)); |
428 | 0 | p = curlx_calloc(1, cwt->cwriter_size); |
429 | 0 | if(!p) |
430 | 0 | goto out; |
431 | | |
432 | 0 | writer = (struct Curl_cwriter *)p; |
433 | 0 | writer->cwt = cwt; |
434 | 0 | writer->ctx = p; |
435 | 0 | writer->phase = phase; |
436 | 0 | result = cwt->do_init(data, writer); |
437 | |
|
438 | 0 | out: |
439 | 0 | *pwriter = result ? NULL : writer; |
440 | 0 | if(result) |
441 | 0 | curlx_free(writer); |
442 | 0 | return result; |
443 | 0 | } |
444 | | |
445 | | void Curl_cwriter_free(struct Curl_easy *data, |
446 | | struct Curl_cwriter *writer) |
447 | 0 | { |
448 | 0 | if(writer) { |
449 | 0 | writer->cwt->do_close(data, writer); |
450 | 0 | curlx_free(writer); |
451 | 0 | } |
452 | 0 | } |
453 | | |
454 | | size_t Curl_cwriter_count(struct Curl_easy *data, Curl_cwriter_phase phase) |
455 | 0 | { |
456 | 0 | struct Curl_cwriter *w; |
457 | 0 | size_t n = 0; |
458 | |
|
459 | 0 | for(w = data->req.writer.stack; w; w = w->next) { |
460 | 0 | if(w->phase == phase) |
461 | 0 | ++n; |
462 | 0 | } |
463 | 0 | return n; |
464 | 0 | } |
465 | | |
466 | | static CURLcode cwriter_ensure_pause_writer(struct Curl_easy *data) |
467 | 0 | { |
468 | 0 | struct Curl_cwriter *writer = |
469 | 0 | Curl_cwriter_get_by_type(data, &Curl_cwt_pause); |
470 | 0 | CURLcode result = CURLE_OK; |
471 | |
|
472 | 0 | if(!writer) { |
473 | 0 | result = Curl_cwriter_create(&writer, data, &Curl_cwt_pause, |
474 | 0 | CURL_CW_BEFORE_DECODE); |
475 | 0 | if(!result) |
476 | 0 | cwriter_add(data, writer); |
477 | 0 | } |
478 | 0 | return result; |
479 | 0 | } |
480 | | |
481 | | CURLcode Curl_cwriter_add(struct Curl_easy *data, |
482 | | struct Curl_cwriter *writer) |
483 | 0 | { |
484 | 0 | CURLcode result; |
485 | 0 | struct Curl_cwriter **anchor = &data->req.writer.stack; |
486 | |
|
487 | 0 | if(!*anchor) { |
488 | 0 | result = do_init_writer_stack(data); |
489 | 0 | if(result) |
490 | 0 | return result; |
491 | 0 | } |
492 | | |
493 | 0 | if(writer->cwt->flags & CURL_CW_FLAG_BLOWUP) { |
494 | | /* On adding a writer that may blow up write sizes, e.g. zip bombs, |
495 | | * add the pause writer. Do this first as any failure will make the |
496 | | * caller destroy the writer again. */ |
497 | 0 | result = cwriter_ensure_pause_writer(data); |
498 | 0 | if(result) |
499 | 0 | return result; |
500 | 0 | } |
501 | | |
502 | 0 | cwriter_add(data, writer); |
503 | 0 | return CURLE_OK; |
504 | 0 | } |
505 | | |
506 | | struct Curl_cwriter *Curl_cwriter_get_by_name(struct Curl_easy *data, |
507 | | const char *name) |
508 | 0 | { |
509 | 0 | struct Curl_cwriter *writer; |
510 | 0 | for(writer = data->req.writer.stack; writer; writer = writer->next) { |
511 | 0 | if(!strcmp(name, writer->cwt->name)) |
512 | 0 | return writer; |
513 | 0 | } |
514 | 0 | return NULL; |
515 | 0 | } |
516 | | |
517 | | struct Curl_cwriter *Curl_cwriter_get_by_type(struct Curl_easy *data, |
518 | | const struct Curl_cwtype *cwt) |
519 | 0 | { |
520 | 0 | struct Curl_cwriter *writer; |
521 | 0 | for(writer = data->req.writer.stack; writer; writer = writer->next) { |
522 | 0 | if(writer->cwt == cwt) |
523 | 0 | return writer; |
524 | 0 | } |
525 | 0 | return NULL; |
526 | 0 | } |
527 | | |
528 | | CURLcode Curl_cwriter_unpause(struct Curl_easy *data) |
529 | 0 | { |
530 | 0 | data->req.writer.paused = FALSE; |
531 | 0 | return Curl_cwriter_flush(data, data->req.writer.stack); |
532 | 0 | } |
533 | | |
534 | | CURLcode Curl_creader_read(struct Curl_easy *data, |
535 | | struct Curl_creader *reader, |
536 | | char *buf, size_t blen, size_t *nread, bool *eos) |
537 | 0 | { |
538 | 0 | *nread = 0; |
539 | 0 | *eos = FALSE; |
540 | 0 | if(!reader) |
541 | 0 | return CURLE_READ_ERROR; |
542 | 0 | return reader->crt->do_read(data, reader, buf, blen, nread, eos); |
543 | 0 | } |
544 | | |
545 | | void Curl_creader_clear_eos(struct Curl_easy *data, |
546 | | struct Curl_creader *reader) |
547 | 0 | { |
548 | 0 | while(reader) { |
549 | 0 | (void)reader->crt->cntrl(data, reader, CURL_CRCNTRL_CLEAR_EOS); |
550 | 0 | reader = reader->next; |
551 | 0 | } |
552 | 0 | } |
553 | | |
554 | | CURLcode Curl_creader_def_init(struct Curl_easy *data, |
555 | | struct Curl_creader *reader) |
556 | 0 | { |
557 | 0 | (void)data; |
558 | 0 | (void)reader; |
559 | 0 | return CURLE_OK; |
560 | 0 | } |
561 | | |
562 | | void Curl_creader_def_close(struct Curl_easy *data, |
563 | | struct Curl_creader *reader) |
564 | 0 | { |
565 | 0 | (void)data; |
566 | 0 | (void)reader; |
567 | 0 | } |
568 | | |
569 | | CURLcode Curl_creader_def_read(struct Curl_easy *data, |
570 | | struct Curl_creader *reader, |
571 | | char *buf, size_t blen, |
572 | | size_t *nread, bool *eos) |
573 | 0 | { |
574 | 0 | if(reader->next) |
575 | 0 | return reader->next->crt->do_read(data, reader->next, buf, blen, |
576 | 0 | nread, eos); |
577 | 0 | else { |
578 | 0 | *nread = 0; |
579 | 0 | *eos = FALSE; |
580 | 0 | return CURLE_READ_ERROR; |
581 | 0 | } |
582 | 0 | } |
583 | | |
584 | | bool Curl_creader_def_needs_rewind(struct Curl_easy *data, |
585 | | struct Curl_creader *reader) |
586 | 0 | { |
587 | 0 | (void)data; |
588 | 0 | (void)reader; |
589 | 0 | return FALSE; |
590 | 0 | } |
591 | | |
592 | | curl_off_t Curl_creader_def_total_length(struct Curl_easy *data, |
593 | | struct Curl_creader *reader) |
594 | 0 | { |
595 | 0 | return reader->next ? |
596 | 0 | reader->next->crt->total_length(data, reader->next) : -1; |
597 | 0 | } |
598 | | |
599 | | CURLcode Curl_creader_def_resume_from(struct Curl_easy *data, |
600 | | struct Curl_creader *reader, |
601 | | curl_off_t offset) |
602 | 0 | { |
603 | 0 | (void)data; |
604 | 0 | (void)reader; |
605 | 0 | (void)offset; |
606 | 0 | return CURLE_READ_ERROR; |
607 | 0 | } |
608 | | |
609 | | CURLcode Curl_creader_def_cntrl(struct Curl_easy *data, |
610 | | struct Curl_creader *reader, |
611 | | Curl_creader_cntrl opcode) |
612 | 0 | { |
613 | 0 | (void)data; |
614 | 0 | (void)reader; |
615 | 0 | (void)opcode; |
616 | 0 | return CURLE_OK; |
617 | 0 | } |
618 | | |
619 | | bool Curl_creader_def_is_paused(struct Curl_easy *data, |
620 | | struct Curl_creader *reader) |
621 | 0 | { |
622 | 0 | (void)data; |
623 | 0 | (void)reader; |
624 | 0 | return FALSE; |
625 | 0 | } |
626 | | |
627 | | void Curl_creader_def_done(struct Curl_easy *data, |
628 | | struct Curl_creader *reader, int premature) |
629 | 0 | { |
630 | 0 | (void)data; |
631 | 0 | (void)reader; |
632 | 0 | (void)premature; |
633 | 0 | } |
634 | | |
635 | | struct cr_in_ctx { |
636 | | struct Curl_creader super; |
637 | | curl_read_callback read_cb; |
638 | | void *cb_user_data; |
639 | | curl_off_t total_len; |
640 | | curl_off_t read_len; |
641 | | CURLcode error_result; |
642 | | BIT(seen_eos); |
643 | | BIT(errored); |
644 | | BIT(has_used_cb); |
645 | | BIT(is_paused); |
646 | | }; |
647 | | |
648 | | static CURLcode cr_in_init(struct Curl_easy *data, struct Curl_creader *reader) |
649 | 0 | { |
650 | 0 | struct cr_in_ctx *ctx = reader->ctx; |
651 | 0 | ctx->read_cb = data->state.fread_func; |
652 | 0 | ctx->cb_user_data = data->state.in; |
653 | 0 | ctx->total_len = -1; |
654 | 0 | ctx->read_len = 0; |
655 | 0 | return CURLE_OK; |
656 | 0 | } |
657 | | |
658 | | /* Real client reader to installed client callbacks. */ |
659 | | static CURLcode cr_in_read(struct Curl_easy *data, |
660 | | struct Curl_creader *reader, |
661 | | char *buf, size_t blen, |
662 | | size_t *pnread, bool *peos) |
663 | 0 | { |
664 | 0 | struct cr_in_ctx *ctx = reader->ctx; |
665 | 0 | CURLcode result = CURLE_OK; |
666 | 0 | size_t nread; |
667 | |
|
668 | 0 | ctx->is_paused = FALSE; |
669 | | |
670 | | /* Once we have errored, we will return the same error forever */ |
671 | 0 | if(ctx->errored) { |
672 | 0 | *pnread = 0; |
673 | 0 | *peos = FALSE; |
674 | 0 | return ctx->error_result; |
675 | 0 | } |
676 | 0 | if(ctx->seen_eos) { |
677 | 0 | *pnread = 0; |
678 | 0 | *peos = TRUE; |
679 | 0 | return CURLE_OK; |
680 | 0 | } |
681 | | /* respect length limitations */ |
682 | 0 | if(ctx->total_len >= 0) { |
683 | 0 | blen = curlx_sotouz_range(ctx->total_len - ctx->read_len, 0, blen); |
684 | 0 | } |
685 | 0 | nread = 0; |
686 | 0 | if(ctx->read_cb && blen) { |
687 | 0 | struct Curl_mapi_guard guard; |
688 | 0 | CURL_CBAPI_START(&guard, data, easy_cr_in_read); |
689 | 0 | nread = ctx->read_cb(buf, 1, blen, ctx->cb_user_data); |
690 | 0 | CURL_CBAPI_END(&guard); |
691 | 0 | ctx->has_used_cb = TRUE; |
692 | 0 | } |
693 | |
|
694 | 0 | switch(nread) { |
695 | 0 | case 0: |
696 | 0 | if((ctx->total_len >= 0) && (ctx->read_len < ctx->total_len)) { |
697 | 0 | failf(data, "client read function EOF fail, " |
698 | 0 | "only %" FMT_OFF_T "/%" FMT_OFF_T " of needed bytes read", |
699 | 0 | ctx->read_len, ctx->total_len); |
700 | 0 | result = CURLE_READ_ERROR; |
701 | 0 | break; |
702 | 0 | } |
703 | 0 | *pnread = 0; |
704 | 0 | *peos = TRUE; |
705 | 0 | ctx->seen_eos = TRUE; |
706 | 0 | break; |
707 | | |
708 | 0 | case CURL_READFUNC_ABORT: |
709 | 0 | failf(data, "operation aborted by callback"); |
710 | 0 | *pnread = 0; |
711 | 0 | *peos = FALSE; |
712 | 0 | ctx->errored = TRUE; |
713 | 0 | ctx->error_result = CURLE_ABORTED_BY_CALLBACK; |
714 | 0 | result = CURLE_ABORTED_BY_CALLBACK; |
715 | 0 | break; |
716 | | |
717 | 0 | case CURL_READFUNC_PAUSE: |
718 | 0 | if(data->conn->scheme->flags & PROTOPT_NONETWORK) { |
719 | | /* protocols that work without network cannot be paused. This is |
720 | | actually only file:// now, and it cannot pause since the transfer |
721 | | is not done using the "normal" procedure. */ |
722 | 0 | failf(data, "Read callback asked for PAUSE when not supported"); |
723 | 0 | result = CURLE_READ_ERROR; |
724 | 0 | break; |
725 | 0 | } |
726 | | /* CURL_READFUNC_PAUSE pauses read callbacks that feed socket writes */ |
727 | 0 | CURL_TRC_READ(data, "cr_in_read, callback returned CURL_READFUNC_PAUSE"); |
728 | 0 | ctx->is_paused = TRUE; |
729 | 0 | *pnread = 0; |
730 | 0 | *peos = FALSE; |
731 | 0 | result = Curl_xfer_pause_send(data, TRUE); |
732 | 0 | break; /* nothing was read */ |
733 | | |
734 | 0 | default: |
735 | 0 | if(nread > blen) { |
736 | | /* the read function returned a too large value */ |
737 | 0 | failf(data, "read function returned funny value"); |
738 | 0 | *pnread = 0; |
739 | 0 | *peos = FALSE; |
740 | 0 | ctx->errored = TRUE; |
741 | 0 | ctx->error_result = CURLE_READ_ERROR; |
742 | 0 | result = CURLE_READ_ERROR; |
743 | 0 | break; |
744 | 0 | } |
745 | 0 | ctx->read_len += nread; |
746 | 0 | if(ctx->total_len >= 0) |
747 | 0 | ctx->seen_eos = (ctx->read_len >= ctx->total_len); |
748 | 0 | *pnread = nread; |
749 | 0 | *peos = (bool)ctx->seen_eos; |
750 | 0 | break; |
751 | 0 | } |
752 | 0 | CURL_TRC_READ(data, "cr_in_read(len=%zu, total=%" FMT_OFF_T |
753 | 0 | ", read=%" FMT_OFF_T ") -> %d, nread=%zu, eos=%d", |
754 | 0 | blen, ctx->total_len, ctx->read_len, (int)result, |
755 | 0 | *pnread, *peos); |
756 | 0 | return result; |
757 | 0 | } |
758 | | |
759 | | static bool cr_in_needs_rewind(struct Curl_easy *data, |
760 | | struct Curl_creader *reader) |
761 | 0 | { |
762 | 0 | struct cr_in_ctx *ctx = reader->ctx; |
763 | 0 | (void)data; |
764 | 0 | return (bool)ctx->has_used_cb; |
765 | 0 | } |
766 | | |
767 | | static curl_off_t cr_in_total_length(struct Curl_easy *data, |
768 | | struct Curl_creader *reader) |
769 | 0 | { |
770 | 0 | struct cr_in_ctx *ctx = reader->ctx; |
771 | 0 | (void)data; |
772 | 0 | return ctx->total_len; |
773 | 0 | } |
774 | | |
775 | | static CURLcode cr_in_resume_from(struct Curl_easy *data, |
776 | | struct Curl_creader *reader, |
777 | | curl_off_t offset) |
778 | 0 | { |
779 | 0 | struct cr_in_ctx *ctx = reader->ctx; |
780 | 0 | int seekerr = CURL_SEEKFUNC_CANTSEEK; |
781 | |
|
782 | 0 | DEBUGASSERT(data->conn); |
783 | | /* already started reading? */ |
784 | 0 | if(ctx->read_len) |
785 | 0 | return CURLE_READ_ERROR; |
786 | | |
787 | 0 | if(data->set.seek_func) { |
788 | 0 | struct Curl_mapi_guard guard; |
789 | 0 | CURL_CBAPI_START(&guard, data, easy_seek_func); |
790 | 0 | seekerr = data->set.seek_func(data->set.seek_client, offset, SEEK_SET); |
791 | 0 | CURL_CBAPI_END(&guard); |
792 | 0 | } |
793 | |
|
794 | 0 | if(seekerr != CURL_SEEKFUNC_OK) { |
795 | 0 | curl_off_t passed = 0; |
796 | |
|
797 | 0 | if(seekerr != CURL_SEEKFUNC_CANTSEEK) { |
798 | 0 | failf(data, "Could not seek stream"); |
799 | 0 | return CURLE_READ_ERROR; |
800 | 0 | } |
801 | | /* when seekerr == CURL_SEEKFUNC_CANTSEEK (cannot seek to offset) */ |
802 | 0 | do { |
803 | 0 | struct Curl_mapi_guard guard; |
804 | 0 | char scratch[4 * 1024]; |
805 | 0 | size_t readthisamountnow = |
806 | 0 | (offset - passed > (curl_off_t)sizeof(scratch)) ? |
807 | 0 | sizeof(scratch) : |
808 | 0 | curlx_sotouz(offset - passed); |
809 | 0 | size_t actuallyread; |
810 | |
|
811 | 0 | CURL_CBAPI_START(&guard, data, easy_cr_in_resume_from); |
812 | 0 | actuallyread = ctx->read_cb(scratch, 1, readthisamountnow, |
813 | 0 | ctx->cb_user_data); |
814 | 0 | CURL_CBAPI_END(&guard); |
815 | |
|
816 | 0 | passed += actuallyread; |
817 | 0 | if((actuallyread == 0) || (actuallyread > readthisamountnow)) { |
818 | | /* this checks for greater-than only to make sure that the |
819 | | CURL_READFUNC_ABORT return code still aborts */ |
820 | 0 | failf(data, "Could only read %" FMT_OFF_T " bytes from the input", |
821 | 0 | passed); |
822 | 0 | return CURLE_READ_ERROR; |
823 | 0 | } |
824 | 0 | } while(passed < offset); |
825 | 0 | } |
826 | | |
827 | | /* now, decrease the size of the read */ |
828 | 0 | if(ctx->total_len > 0) { |
829 | 0 | ctx->total_len -= offset; |
830 | |
|
831 | 0 | if(ctx->total_len <= 0) { |
832 | 0 | failf(data, "File already completely uploaded"); |
833 | 0 | return CURLE_PARTIAL_FILE; |
834 | 0 | } |
835 | 0 | } |
836 | | /* we have passed, proceed as normal */ |
837 | 0 | return CURLE_OK; |
838 | 0 | } |
839 | | |
840 | | static CURLcode cr_in_rewind(struct Curl_easy *data, |
841 | | struct Curl_creader *reader) |
842 | 0 | { |
843 | 0 | struct cr_in_ctx *ctx = reader->ctx; |
844 | | |
845 | | /* If we never invoked the callback, there is noting to rewind */ |
846 | 0 | if(!ctx->has_used_cb) |
847 | 0 | return CURLE_OK; |
848 | | |
849 | 0 | if(data->set.seek_func) { |
850 | 0 | struct Curl_mapi_guard guard; |
851 | 0 | int err; |
852 | |
|
853 | 0 | CURL_CBAPI_START(&guard, data, easy_seek_func); |
854 | 0 | err = data->set.seek_func(data->set.seek_client, 0, SEEK_SET); |
855 | 0 | CURL_CBAPI_END(&guard); |
856 | 0 | CURL_TRC_READ(data, "cr_in, rewind via set.seek_func -> %d", err); |
857 | 0 | if(err) { |
858 | 0 | failf(data, "seek callback returned error %d", err); |
859 | 0 | return CURLE_SEND_FAIL_REWIND; |
860 | 0 | } |
861 | 0 | } |
862 | 0 | else if(data->set.ioctl_func) { |
863 | 0 | struct Curl_mapi_guard guard; |
864 | 0 | curlioerr err; |
865 | |
|
866 | 0 | CURL_CBAPI_START(&guard, data, easy_ioctl_func); |
867 | 0 | err = data->set.ioctl_func(data, CURLIOCMD_RESTARTREAD, |
868 | 0 | data->set.ioctl_client); |
869 | 0 | CURL_CBAPI_END(&guard); |
870 | 0 | CURL_TRC_READ(data, "cr_in, rewind via set.ioctl_func -> %d", (int)err); |
871 | 0 | if(err) { |
872 | 0 | failf(data, "ioctl callback returned error %d", (int)err); |
873 | 0 | return CURLE_SEND_FAIL_REWIND; |
874 | 0 | } |
875 | 0 | } |
876 | 0 | else { |
877 | | /* If no CURLOPT_READFUNCTION is used, we know that we operate on a |
878 | | given FILE * stream and we can actually attempt to rewind that |
879 | | ourselves with fseek() */ |
880 | 0 | #if defined(__clang__) && __clang_major__ >= 16 |
881 | 0 | #pragma clang diagnostic push |
882 | 0 | #pragma clang diagnostic ignored "-Wcast-function-type-strict" |
883 | 0 | #endif |
884 | 0 | if(data->state.fread_func == (curl_read_callback)fread) { |
885 | 0 | #if defined(__clang__) && __clang_major__ >= 16 |
886 | 0 | #pragma clang diagnostic pop |
887 | 0 | #endif |
888 | 0 | int err = fseek(data->state.in, 0, SEEK_SET); |
889 | 0 | CURL_TRC_READ(data, "cr_in, rewind via fseek -> %d(%d)", |
890 | 0 | (int)err, (int)errno); |
891 | 0 | if(err != -1) |
892 | | /* successful rewind */ |
893 | 0 | return CURLE_OK; |
894 | 0 | } |
895 | | |
896 | | /* no callback set or failure above, makes us fail at once */ |
897 | 0 | failf(data, "necessary data rewind was not possible"); |
898 | 0 | return CURLE_SEND_FAIL_REWIND; |
899 | 0 | } |
900 | 0 | return CURLE_OK; |
901 | 0 | } |
902 | | |
903 | | static CURLcode cr_in_cntrl(struct Curl_easy *data, |
904 | | struct Curl_creader *reader, |
905 | | Curl_creader_cntrl opcode) |
906 | 0 | { |
907 | 0 | struct cr_in_ctx *ctx = reader->ctx; |
908 | |
|
909 | 0 | switch(opcode) { |
910 | 0 | case CURL_CRCNTRL_REWIND: |
911 | 0 | return cr_in_rewind(data, reader); |
912 | 0 | case CURL_CRCNTRL_UNPAUSE: |
913 | 0 | ctx->is_paused = FALSE; |
914 | 0 | break; |
915 | 0 | case CURL_CRCNTRL_CLEAR_EOS: |
916 | 0 | ctx->seen_eos = FALSE; |
917 | 0 | break; |
918 | 0 | default: |
919 | 0 | break; |
920 | 0 | } |
921 | 0 | return CURLE_OK; |
922 | 0 | } |
923 | | |
924 | | static bool cr_in_is_paused(struct Curl_easy *data, |
925 | | struct Curl_creader *reader) |
926 | 0 | { |
927 | 0 | struct cr_in_ctx *ctx = reader->ctx; |
928 | 0 | (void)data; |
929 | 0 | return (bool)ctx->is_paused; |
930 | 0 | } |
931 | | |
932 | | static const struct Curl_crtype cr_in = { |
933 | | "cr-in", |
934 | | cr_in_init, |
935 | | cr_in_read, |
936 | | Curl_creader_def_close, |
937 | | cr_in_needs_rewind, |
938 | | cr_in_total_length, |
939 | | cr_in_resume_from, |
940 | | cr_in_cntrl, |
941 | | cr_in_is_paused, |
942 | | Curl_creader_def_done, |
943 | | sizeof(struct cr_in_ctx) |
944 | | }; |
945 | | |
946 | | CURLcode Curl_creader_create(struct Curl_creader **preader, |
947 | | struct Curl_easy *data, |
948 | | const struct Curl_crtype *crt, |
949 | | Curl_creader_phase phase) |
950 | 0 | { |
951 | 0 | struct Curl_creader *reader = NULL; |
952 | 0 | CURLcode result = CURLE_OUT_OF_MEMORY; |
953 | 0 | void *p; |
954 | |
|
955 | 0 | DEBUGASSERT(crt->creader_size >= sizeof(struct Curl_creader)); |
956 | 0 | p = curlx_calloc(1, crt->creader_size); |
957 | 0 | if(!p) |
958 | 0 | goto out; |
959 | | |
960 | 0 | reader = (struct Curl_creader *)p; |
961 | 0 | reader->crt = crt; |
962 | 0 | reader->ctx = p; |
963 | 0 | reader->phase = phase; |
964 | 0 | result = crt->do_init(data, reader); |
965 | |
|
966 | 0 | out: |
967 | 0 | *preader = result ? NULL : reader; |
968 | 0 | if(result) |
969 | 0 | curlx_free(reader); |
970 | 0 | return result; |
971 | 0 | } |
972 | | |
973 | | void Curl_creader_free(struct Curl_easy *data, struct Curl_creader *reader) |
974 | 0 | { |
975 | 0 | if(reader) { |
976 | 0 | reader->crt->do_close(data, reader); |
977 | 0 | curlx_free(reader); |
978 | 0 | } |
979 | 0 | } |
980 | | |
981 | | struct cr_lc_ctx { |
982 | | struct Curl_creader super; |
983 | | struct bufq buf; |
984 | | BIT(read_eos); /* we read an EOS from the next reader */ |
985 | | BIT(eos); /* we have returned an EOS */ |
986 | | BIT(prev_cr); /* the last byte was a CR */ |
987 | | }; |
988 | | |
989 | | static CURLcode cr_lc_init(struct Curl_easy *data, struct Curl_creader *reader) |
990 | 0 | { |
991 | 0 | struct cr_lc_ctx *ctx = reader->ctx; |
992 | 0 | (void)data; |
993 | 0 | Curl_bufq_init2(&ctx->buf, (16 * 1024), 1, BUFQ_OPT_SOFT_LIMIT); |
994 | 0 | return CURLE_OK; |
995 | 0 | } |
996 | | |
997 | | static void cr_lc_close(struct Curl_easy *data, struct Curl_creader *reader) |
998 | 0 | { |
999 | 0 | struct cr_lc_ctx *ctx = reader->ctx; |
1000 | 0 | (void)data; |
1001 | 0 | Curl_bufq_free(&ctx->buf); |
1002 | 0 | } |
1003 | | |
1004 | | /* client reader doing line end conversions. */ |
1005 | | static CURLcode cr_lc_read(struct Curl_easy *data, |
1006 | | struct Curl_creader *reader, |
1007 | | char *buf, size_t blen, |
1008 | | size_t *pnread, bool *peos) |
1009 | 0 | { |
1010 | 0 | struct cr_lc_ctx *ctx = reader->ctx; |
1011 | 0 | CURLcode result; |
1012 | 0 | size_t nread, i, start, n; |
1013 | 0 | bool eos; |
1014 | |
|
1015 | 0 | if(ctx->eos) { |
1016 | 0 | *pnread = 0; |
1017 | 0 | *peos = TRUE; |
1018 | 0 | return CURLE_OK; |
1019 | 0 | } |
1020 | | |
1021 | 0 | if(Curl_bufq_is_empty(&ctx->buf)) { |
1022 | 0 | if(ctx->read_eos) { |
1023 | 0 | ctx->eos = TRUE; |
1024 | 0 | *pnread = 0; |
1025 | 0 | *peos = TRUE; |
1026 | 0 | return CURLE_OK; |
1027 | 0 | } |
1028 | | /* Still getting data form the next reader, ctx->buf is empty */ |
1029 | 0 | result = Curl_creader_read(data, reader->next, buf, blen, &nread, &eos); |
1030 | 0 | if(result) |
1031 | 0 | return result; |
1032 | 0 | ctx->read_eos = eos; |
1033 | |
|
1034 | 0 | if(!nread || !memchr(buf, '\n', nread)) { |
1035 | | /* nothing to convert, return this right away */ |
1036 | 0 | if(nread) |
1037 | 0 | ctx->prev_cr = (buf[nread - 1] == '\r'); |
1038 | 0 | if(ctx->read_eos) |
1039 | 0 | ctx->eos = TRUE; |
1040 | 0 | *pnread = nread; |
1041 | 0 | *peos = (bool)ctx->eos; |
1042 | 0 | goto out; |
1043 | 0 | } |
1044 | | |
1045 | | /* at least one \n might need conversion to '\r\n', place into ctx->buf */ |
1046 | 0 | for(i = start = 0; i < nread; ++i) { |
1047 | | /* if this byte is not an LF character, or if the preceding character is |
1048 | | a CR (meaning this already is a CRLF pair), go to next */ |
1049 | 0 | if((buf[i] != '\n') || ctx->prev_cr) { |
1050 | 0 | ctx->prev_cr = (buf[i] == '\r'); |
1051 | 0 | continue; |
1052 | 0 | } |
1053 | 0 | ctx->prev_cr = FALSE; |
1054 | | /* on a soft limit bufq, we do not need to check length */ |
1055 | 0 | result = Curl_bufq_cwrite(&ctx->buf, buf + start, i - start, &n); |
1056 | 0 | if(!result) |
1057 | 0 | result = Curl_bufq_cwrite(&ctx->buf, STRCONST("\r\n"), &n); |
1058 | 0 | if(result) |
1059 | 0 | return result; |
1060 | 0 | start = i + 1; |
1061 | 0 | } |
1062 | | |
1063 | 0 | if(start < i) { /* leftover */ |
1064 | 0 | result = Curl_bufq_cwrite(&ctx->buf, buf + start, i - start, &n); |
1065 | 0 | if(result) |
1066 | 0 | return result; |
1067 | 0 | } |
1068 | 0 | } |
1069 | | |
1070 | 0 | DEBUGASSERT(!Curl_bufq_is_empty(&ctx->buf)); |
1071 | 0 | *peos = FALSE; |
1072 | 0 | result = Curl_bufq_cread(&ctx->buf, buf, blen, pnread); |
1073 | 0 | if(!result && ctx->read_eos && Curl_bufq_is_empty(&ctx->buf)) { |
1074 | | /* no more data, read all, done. */ |
1075 | 0 | ctx->eos = TRUE; |
1076 | 0 | *peos = TRUE; |
1077 | 0 | } |
1078 | |
|
1079 | 0 | out: |
1080 | 0 | CURL_TRC_READ(data, "cr_lc_read(len=%zu) -> %d, nread=%zu, eos=%d", |
1081 | 0 | blen, (int)result, *pnread, *peos); |
1082 | 0 | return result; |
1083 | 0 | } |
1084 | | |
1085 | | static curl_off_t cr_lc_total_length(struct Curl_easy *data, |
1086 | | struct Curl_creader *reader) |
1087 | 0 | { |
1088 | | /* this reader changes length depending on input */ |
1089 | 0 | (void)data; |
1090 | 0 | (void)reader; |
1091 | 0 | return -1; |
1092 | 0 | } |
1093 | | |
1094 | | static const struct Curl_crtype cr_lc = { |
1095 | | "cr-lineconv", |
1096 | | cr_lc_init, |
1097 | | cr_lc_read, |
1098 | | cr_lc_close, |
1099 | | Curl_creader_def_needs_rewind, |
1100 | | cr_lc_total_length, |
1101 | | Curl_creader_def_resume_from, |
1102 | | Curl_creader_def_cntrl, |
1103 | | Curl_creader_def_is_paused, |
1104 | | Curl_creader_def_done, |
1105 | | sizeof(struct cr_lc_ctx) |
1106 | | }; |
1107 | | |
1108 | | static CURLcode cr_lc_add(struct Curl_easy *data) |
1109 | 0 | { |
1110 | 0 | struct Curl_creader *reader = NULL; |
1111 | 0 | CURLcode result; |
1112 | |
|
1113 | 0 | result = Curl_creader_create(&reader, data, &cr_lc, CURL_CR_CONTENT_ENCODE); |
1114 | 0 | if(!result) |
1115 | 0 | result = Curl_creader_add(data, reader); |
1116 | |
|
1117 | 0 | if(result && reader) |
1118 | 0 | Curl_creader_free(data, reader); |
1119 | 0 | return result; |
1120 | 0 | } |
1121 | | |
1122 | | static CURLcode do_init_reader_stack(struct Curl_easy *data, |
1123 | | struct Curl_creader *r) |
1124 | 0 | { |
1125 | 0 | CURLcode result = CURLE_OK; |
1126 | 0 | curl_off_t clen; |
1127 | |
|
1128 | 0 | DEBUGASSERT(r); |
1129 | 0 | DEBUGASSERT(r->crt); |
1130 | 0 | DEBUGASSERT(r->phase == CURL_CR_CLIENT); |
1131 | 0 | DEBUGASSERT(!data->req.reader.stack); |
1132 | |
|
1133 | 0 | data->req.reader.stack = r; |
1134 | 0 | clen = r->crt->total_length(data, r); |
1135 | | /* if we do not have 0 length init, and CRLF conversion is wanted, |
1136 | | * add the reader for it */ |
1137 | 0 | if(clen && |
1138 | 0 | #ifdef CURL_PREFER_LF_LINEENDS |
1139 | 0 | (data->set.crlf || data->state.prefer_ascii) |
1140 | | #else |
1141 | | data->set.crlf |
1142 | | #endif |
1143 | 0 | ) { |
1144 | 0 | result = cr_lc_add(data); |
1145 | 0 | if(result) |
1146 | 0 | return result; |
1147 | 0 | } |
1148 | | |
1149 | 0 | return result; |
1150 | 0 | } |
1151 | | |
1152 | | CURLcode Curl_creader_set_fread(struct Curl_easy *data, curl_off_t len) |
1153 | 0 | { |
1154 | 0 | CURLcode result; |
1155 | 0 | struct Curl_creader *r; |
1156 | 0 | struct cr_in_ctx *ctx; |
1157 | |
|
1158 | 0 | result = Curl_creader_create(&r, data, &cr_in, CURL_CR_CLIENT); |
1159 | 0 | if(result || !r) |
1160 | 0 | goto out; |
1161 | 0 | ctx = r->ctx; |
1162 | 0 | ctx->total_len = len; |
1163 | |
|
1164 | 0 | cl_reset_reader(data); |
1165 | 0 | result = do_init_reader_stack(data, r); |
1166 | 0 | out: |
1167 | 0 | CURL_TRC_READ(data, "add fread reader, len=%" FMT_OFF_T " -> %d", |
1168 | 0 | len, (int)result); |
1169 | 0 | return result; |
1170 | 0 | } |
1171 | | |
1172 | | CURLcode Curl_creader_add(struct Curl_easy *data, |
1173 | | struct Curl_creader *reader) |
1174 | 0 | { |
1175 | 0 | CURLcode result; |
1176 | 0 | struct Curl_creader **anchor = &data->req.reader.stack; |
1177 | |
|
1178 | 0 | if(!*anchor) { |
1179 | 0 | result = Curl_creader_set_fread(data, data->state.infilesize); |
1180 | 0 | if(result) |
1181 | 0 | return result; |
1182 | 0 | } |
1183 | | |
1184 | | /* Insert the writer as first in its phase. |
1185 | | * Skip existing readers of lower phases. */ |
1186 | 0 | while(*anchor && (*anchor)->phase < reader->phase) |
1187 | 0 | anchor = &(*anchor)->next; |
1188 | 0 | reader->next = *anchor; |
1189 | 0 | *anchor = reader; |
1190 | 0 | return CURLE_OK; |
1191 | 0 | } |
1192 | | |
1193 | | CURLcode Curl_creader_set(struct Curl_easy *data, struct Curl_creader *r) |
1194 | 0 | { |
1195 | 0 | CURLcode result; |
1196 | |
|
1197 | 0 | DEBUGASSERT(r); |
1198 | 0 | DEBUGASSERT(r->crt); |
1199 | 0 | DEBUGASSERT(r->phase == CURL_CR_CLIENT); |
1200 | |
|
1201 | 0 | cl_reset_reader(data); |
1202 | 0 | result = do_init_reader_stack(data, r); |
1203 | 0 | if(result) |
1204 | 0 | Curl_creader_free(data, r); |
1205 | 0 | return result; |
1206 | 0 | } |
1207 | | |
1208 | | CURLcode Curl_client_read(struct Curl_easy *data, char *buf, size_t blen, |
1209 | | size_t *nread, bool *eos) |
1210 | 0 | { |
1211 | 0 | CURLcode result; |
1212 | |
|
1213 | 0 | DEBUGASSERT(buf); |
1214 | 0 | DEBUGASSERT(blen); |
1215 | 0 | DEBUGASSERT(nread); |
1216 | 0 | DEBUGASSERT(eos); |
1217 | 0 | *nread = 0; |
1218 | |
|
1219 | 0 | if(!data->req.reader.stack) { |
1220 | 0 | result = Curl_creader_set_fread(data, data->state.infilesize); |
1221 | 0 | if(result) |
1222 | 0 | return result; |
1223 | 0 | DEBUGASSERT(data->req.reader.stack); |
1224 | 0 | } |
1225 | 0 | if(!data->req.reader_started) { |
1226 | 0 | Curl_rlimit_start(&data->progress.ul.rlimit, Curl_pgrs_now(data), -1); |
1227 | 0 | data->req.reader_started = TRUE; |
1228 | 0 | } |
1229 | |
|
1230 | 0 | if(Curl_rlimit_active(&data->progress.ul.rlimit)) { |
1231 | 0 | curl_off_t ul_avail = Curl_rlimit_avail(&data->progress.ul.rlimit, NULL); |
1232 | 0 | if(ul_avail <= 0) { |
1233 | 0 | result = CURLE_OK; |
1234 | 0 | *eos = FALSE; |
1235 | 0 | goto out; |
1236 | 0 | } |
1237 | 0 | if(ul_avail < (curl_off_t)blen) |
1238 | 0 | blen = (size_t)ul_avail; |
1239 | 0 | } |
1240 | 0 | result = Curl_creader_read(data, data->req.reader.stack, buf, blen, |
1241 | 0 | nread, eos); |
1242 | |
|
1243 | 0 | out: |
1244 | 0 | CURL_TRC_READ(data, "client_read(len=%zu) -> %d, nread=%zu, eos=%d", |
1245 | 0 | blen, (int)result, *nread, *eos); |
1246 | 0 | return result; |
1247 | 0 | } |
1248 | | |
1249 | | bool Curl_creader_needs_rewind(struct Curl_easy *data) |
1250 | 0 | { |
1251 | 0 | struct Curl_creader *reader = data->req.reader.stack; |
1252 | 0 | while(reader) { |
1253 | 0 | if(reader->crt->needs_rewind(data, reader)) { |
1254 | 0 | CURL_TRC_READ(data, "client reader needs rewind before next request"); |
1255 | 0 | return TRUE; |
1256 | 0 | } |
1257 | 0 | reader = reader->next; |
1258 | 0 | } |
1259 | 0 | return FALSE; |
1260 | 0 | } |
1261 | | |
1262 | | static CURLcode cr_null_read(struct Curl_easy *data, |
1263 | | struct Curl_creader *reader, |
1264 | | char *buf, size_t blen, |
1265 | | size_t *pnread, bool *peos) |
1266 | 0 | { |
1267 | 0 | (void)data; |
1268 | 0 | (void)reader; |
1269 | 0 | (void)buf; |
1270 | 0 | (void)blen; |
1271 | 0 | *pnread = 0; |
1272 | 0 | *peos = TRUE; |
1273 | 0 | return CURLE_OK; |
1274 | 0 | } |
1275 | | |
1276 | | static curl_off_t cr_null_total_length(struct Curl_easy *data, |
1277 | | struct Curl_creader *reader) |
1278 | 0 | { |
1279 | | /* this reader changes length depending on input */ |
1280 | 0 | (void)data; |
1281 | 0 | (void)reader; |
1282 | 0 | return 0; |
1283 | 0 | } |
1284 | | |
1285 | | static const struct Curl_crtype cr_null = { |
1286 | | "cr-null", |
1287 | | Curl_creader_def_init, |
1288 | | cr_null_read, |
1289 | | Curl_creader_def_close, |
1290 | | Curl_creader_def_needs_rewind, |
1291 | | cr_null_total_length, |
1292 | | Curl_creader_def_resume_from, |
1293 | | Curl_creader_def_cntrl, |
1294 | | Curl_creader_def_is_paused, |
1295 | | Curl_creader_def_done, |
1296 | | sizeof(struct Curl_creader) |
1297 | | }; |
1298 | | |
1299 | | CURLcode Curl_creader_set_null(struct Curl_easy *data) |
1300 | 0 | { |
1301 | 0 | struct Curl_creader *r; |
1302 | 0 | CURLcode result; |
1303 | |
|
1304 | 0 | result = Curl_creader_create(&r, data, &cr_null, CURL_CR_CLIENT); |
1305 | 0 | if(result) |
1306 | 0 | return result; |
1307 | | |
1308 | 0 | cl_reset_reader(data); |
1309 | 0 | return do_init_reader_stack(data, r); |
1310 | 0 | } |
1311 | | |
1312 | | struct cr_buf_ctx { |
1313 | | struct Curl_creader super; |
1314 | | const char *buf; |
1315 | | size_t blen; |
1316 | | size_t index; |
1317 | | }; |
1318 | | |
1319 | | static CURLcode cr_buf_read(struct Curl_easy *data, |
1320 | | struct Curl_creader *reader, |
1321 | | char *buf, size_t blen, |
1322 | | size_t *pnread, bool *peos) |
1323 | 0 | { |
1324 | 0 | struct cr_buf_ctx *ctx = reader->ctx; |
1325 | 0 | size_t nread = ctx->blen - ctx->index; |
1326 | |
|
1327 | 0 | if(!nread || !ctx->buf) { |
1328 | 0 | *pnread = 0; |
1329 | 0 | *peos = TRUE; |
1330 | 0 | } |
1331 | 0 | else { |
1332 | 0 | if(nread > blen) |
1333 | 0 | nread = blen; |
1334 | 0 | memcpy(buf, ctx->buf + ctx->index, nread); |
1335 | 0 | *pnread = nread; |
1336 | 0 | ctx->index += nread; |
1337 | 0 | *peos = (ctx->index == ctx->blen); |
1338 | 0 | } |
1339 | 0 | CURL_TRC_READ(data, "cr_buf_read(len=%zu) -> 0, nread=%zu, eos=%d", |
1340 | 0 | blen, *pnread, *peos); |
1341 | 0 | return CURLE_OK; |
1342 | 0 | } |
1343 | | |
1344 | | static bool cr_buf_needs_rewind(struct Curl_easy *data, |
1345 | | struct Curl_creader *reader) |
1346 | 0 | { |
1347 | 0 | struct cr_buf_ctx *ctx = reader->ctx; |
1348 | 0 | (void)data; |
1349 | 0 | return ctx->index > 0; |
1350 | 0 | } |
1351 | | |
1352 | | static CURLcode cr_buf_cntrl(struct Curl_easy *data, |
1353 | | struct Curl_creader *reader, |
1354 | | Curl_creader_cntrl opcode) |
1355 | 0 | { |
1356 | 0 | struct cr_buf_ctx *ctx = reader->ctx; |
1357 | 0 | (void)data; |
1358 | 0 | switch(opcode) { |
1359 | 0 | case CURL_CRCNTRL_REWIND: |
1360 | 0 | ctx->index = 0; |
1361 | 0 | break; |
1362 | 0 | default: |
1363 | 0 | break; |
1364 | 0 | } |
1365 | 0 | return CURLE_OK; |
1366 | 0 | } |
1367 | | |
1368 | | static curl_off_t cr_buf_total_length(struct Curl_easy *data, |
1369 | | struct Curl_creader *reader) |
1370 | 0 | { |
1371 | 0 | struct cr_buf_ctx *ctx = reader->ctx; |
1372 | 0 | (void)data; |
1373 | 0 | return (curl_off_t)ctx->blen; |
1374 | 0 | } |
1375 | | |
1376 | | static CURLcode cr_buf_resume_from(struct Curl_easy *data, |
1377 | | struct Curl_creader *reader, |
1378 | | curl_off_t offset) |
1379 | 0 | { |
1380 | 0 | struct cr_buf_ctx *ctx = reader->ctx; |
1381 | 0 | size_t boffset; |
1382 | |
|
1383 | 0 | (void)data; |
1384 | 0 | DEBUGASSERT(data->conn); |
1385 | | /* already started reading? */ |
1386 | 0 | if(ctx->index) |
1387 | 0 | return CURLE_READ_ERROR; |
1388 | 0 | boffset = curlx_sotouz_range(offset, 0, SIZE_MAX); |
1389 | 0 | if(!boffset) |
1390 | 0 | return CURLE_OK; |
1391 | 0 | if(boffset > ctx->blen) |
1392 | 0 | return CURLE_READ_ERROR; |
1393 | | |
1394 | 0 | ctx->buf += boffset; |
1395 | 0 | ctx->blen -= boffset; |
1396 | 0 | return CURLE_OK; |
1397 | 0 | } |
1398 | | |
1399 | | static const struct Curl_crtype cr_buf = { |
1400 | | "cr-buf", |
1401 | | Curl_creader_def_init, |
1402 | | cr_buf_read, |
1403 | | Curl_creader_def_close, |
1404 | | cr_buf_needs_rewind, |
1405 | | cr_buf_total_length, |
1406 | | cr_buf_resume_from, |
1407 | | cr_buf_cntrl, |
1408 | | Curl_creader_def_is_paused, |
1409 | | Curl_creader_def_done, |
1410 | | sizeof(struct cr_buf_ctx) |
1411 | | }; |
1412 | | |
1413 | | CURLcode Curl_creader_set_buf(struct Curl_easy *data, |
1414 | | const char *buf, size_t blen) |
1415 | 0 | { |
1416 | 0 | CURLcode result; |
1417 | 0 | struct Curl_creader *r; |
1418 | 0 | struct cr_buf_ctx *ctx; |
1419 | |
|
1420 | 0 | result = Curl_creader_create(&r, data, &cr_buf, CURL_CR_CLIENT); |
1421 | 0 | if(result) |
1422 | 0 | goto out; |
1423 | 0 | ctx = r->ctx; |
1424 | 0 | ctx->buf = buf; |
1425 | 0 | ctx->blen = blen; |
1426 | 0 | ctx->index = 0; |
1427 | |
|
1428 | 0 | cl_reset_reader(data); |
1429 | 0 | result = do_init_reader_stack(data, r); |
1430 | 0 | out: |
1431 | 0 | CURL_TRC_READ(data, "add buf reader, len=%zu -> %d", blen, (int)result); |
1432 | 0 | return result; |
1433 | 0 | } |
1434 | | |
1435 | | curl_off_t Curl_creader_total_length(struct Curl_easy *data) |
1436 | 0 | { |
1437 | 0 | struct Curl_creader *r = data->req.reader.stack; |
1438 | 0 | return r ? r->crt->total_length(data, r) : -1; |
1439 | 0 | } |
1440 | | |
1441 | | curl_off_t Curl_creader_client_length(struct Curl_easy *data) |
1442 | 0 | { |
1443 | 0 | struct Curl_creader *r = data->req.reader.stack; |
1444 | 0 | while(r && r->phase != CURL_CR_CLIENT) |
1445 | 0 | r = r->next; |
1446 | 0 | return r ? r->crt->total_length(data, r) : -1; |
1447 | 0 | } |
1448 | | |
1449 | | CURLcode Curl_creader_resume_from(struct Curl_easy *data, curl_off_t offset) |
1450 | 0 | { |
1451 | 0 | struct Curl_creader *r = data->req.reader.stack; |
1452 | 0 | while(r && r->phase != CURL_CR_CLIENT) |
1453 | 0 | r = r->next; |
1454 | 0 | return r ? r->crt->resume_from(data, r, offset) : CURLE_READ_ERROR; |
1455 | 0 | } |
1456 | | |
1457 | | CURLcode Curl_creader_unpause(struct Curl_easy *data) |
1458 | 0 | { |
1459 | 0 | struct Curl_creader *reader = data->req.reader.stack; |
1460 | 0 | CURLcode result = CURLE_OK; |
1461 | |
|
1462 | 0 | while(reader) { |
1463 | 0 | result = reader->crt->cntrl(data, reader, CURL_CRCNTRL_UNPAUSE); |
1464 | 0 | CURL_TRC_READ(data, "unpausing %s -> %d", reader->crt->name, (int)result); |
1465 | 0 | if(result) |
1466 | 0 | break; |
1467 | 0 | reader = reader->next; |
1468 | 0 | } |
1469 | 0 | return result; |
1470 | 0 | } |
1471 | | |
1472 | | bool Curl_creader_is_paused(struct Curl_easy *data) |
1473 | 0 | { |
1474 | 0 | struct Curl_creader *reader = data->req.reader.stack; |
1475 | |
|
1476 | 0 | while(reader) { |
1477 | 0 | if(reader->crt->is_paused(data, reader)) |
1478 | 0 | return TRUE; |
1479 | 0 | reader = reader->next; |
1480 | 0 | } |
1481 | 0 | return FALSE; |
1482 | 0 | } |
1483 | | |
1484 | | void Curl_creader_done(struct Curl_easy *data, int premature) |
1485 | 0 | { |
1486 | 0 | struct Curl_creader *reader = data->req.reader.stack; |
1487 | 0 | while(reader) { |
1488 | 0 | reader->crt->done(data, reader, premature); |
1489 | 0 | reader = reader->next; |
1490 | 0 | } |
1491 | 0 | } |
1492 | | |
1493 | | struct Curl_creader *Curl_creader_get_by_type(struct Curl_easy *data, |
1494 | | const struct Curl_crtype *crt) |
1495 | 0 | { |
1496 | 0 | struct Curl_creader *r; |
1497 | 0 | for(r = data->req.reader.stack; r; r = r->next) { |
1498 | 0 | if(r->crt == crt) |
1499 | 0 | return r; |
1500 | 0 | } |
1501 | 0 | return NULL; |
1502 | 0 | } |