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