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