sendf.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #ifdef HAVE_NETINET_IN_H
  26. #include <netinet/in.h>
  27. #endif
  28. #ifdef HAVE_LINUX_TCP_H
  29. #include <linux/tcp.h>
  30. #elif defined(HAVE_NETINET_TCP_H)
  31. #include <netinet/tcp.h>
  32. #endif
  33. #include <curl/curl.h>
  34. #include "urldata.h"
  35. #include "sendf.h"
  36. #include "cfilters.h"
  37. #include "connect.h"
  38. #include "content_encoding.h"
  39. #include "cw-out.h"
  40. #include "vtls/vtls.h"
  41. #include "vssh/ssh.h"
  42. #include "easyif.h"
  43. #include "multiif.h"
  44. #include "strerror.h"
  45. #include "select.h"
  46. #include "strdup.h"
  47. #include "http2.h"
  48. #include "progress.h"
  49. #include "warnless.h"
  50. #include "ws.h"
  51. /* The last 3 #include files should be in this order */
  52. #include "curl_printf.h"
  53. #include "curl_memory.h"
  54. #include "memdebug.h"
  55. static CURLcode do_init_writer_stack(struct Curl_easy *data);
  56. /* Curl_client_write() sends data to the write callback(s)
  57. The bit pattern defines to what "streams" to write to. Body and/or header.
  58. The defines are in sendf.h of course.
  59. */
  60. CURLcode Curl_client_write(struct Curl_easy *data,
  61. int type, const char *buf, size_t blen)
  62. {
  63. CURLcode result;
  64. /* it is one of those, at least */
  65. DEBUGASSERT(type & (CLIENTWRITE_BODY|CLIENTWRITE_HEADER|CLIENTWRITE_INFO));
  66. /* BODY is only BODY (with optional EOS) */
  67. DEBUGASSERT(!(type & CLIENTWRITE_BODY) ||
  68. ((type & ~(CLIENTWRITE_BODY|CLIENTWRITE_EOS)) == 0));
  69. /* INFO is only INFO (with optional EOS) */
  70. DEBUGASSERT(!(type & CLIENTWRITE_INFO) ||
  71. ((type & ~(CLIENTWRITE_INFO|CLIENTWRITE_EOS)) == 0));
  72. if(!data->req.writer_stack) {
  73. result = do_init_writer_stack(data);
  74. if(result)
  75. return result;
  76. DEBUGASSERT(data->req.writer_stack);
  77. }
  78. result = Curl_cwriter_write(data, data->req.writer_stack, type, buf, blen);
  79. CURL_TRC_WRITE(data, "client_write(type=%x, len=%zu) -> %d",
  80. type, blen, result);
  81. return result;
  82. }
  83. static void cl_reset_writer(struct Curl_easy *data)
  84. {
  85. struct Curl_cwriter *writer = data->req.writer_stack;
  86. while(writer) {
  87. data->req.writer_stack = writer->next;
  88. writer->cwt->do_close(data, writer);
  89. free(writer);
  90. writer = data->req.writer_stack;
  91. }
  92. }
  93. static void cl_reset_reader(struct Curl_easy *data)
  94. {
  95. struct Curl_creader *reader = data->req.reader_stack;
  96. while(reader) {
  97. data->req.reader_stack = reader->next;
  98. reader->crt->do_close(data, reader);
  99. free(reader);
  100. reader = data->req.reader_stack;
  101. }
  102. }
  103. void Curl_client_cleanup(struct Curl_easy *data)
  104. {
  105. cl_reset_reader(data);
  106. cl_reset_writer(data);
  107. data->req.bytecount = 0;
  108. data->req.headerline = 0;
  109. }
  110. void Curl_client_reset(struct Curl_easy *data)
  111. {
  112. if(data->req.rewind_read) {
  113. /* already requested */
  114. CURL_TRC_READ(data, "client_reset, will rewind reader");
  115. }
  116. else {
  117. CURL_TRC_READ(data, "client_reset, clear readers");
  118. cl_reset_reader(data);
  119. }
  120. cl_reset_writer(data);
  121. data->req.bytecount = 0;
  122. data->req.headerline = 0;
  123. }
  124. CURLcode Curl_client_start(struct Curl_easy *data)
  125. {
  126. if(data->req.rewind_read) {
  127. struct Curl_creader *r = data->req.reader_stack;
  128. CURLcode result = CURLE_OK;
  129. CURL_TRC_READ(data, "client start, rewind readers");
  130. while(r) {
  131. result = r->crt->rewind(data, r);
  132. if(result) {
  133. failf(data, "rewind of client reader '%s' failed: %d",
  134. r->crt->name, result);
  135. return result;
  136. }
  137. r = r->next;
  138. }
  139. data->req.rewind_read = FALSE;
  140. cl_reset_reader(data);
  141. }
  142. return CURLE_OK;
  143. }
  144. bool Curl_creader_will_rewind(struct Curl_easy *data)
  145. {
  146. return data->req.rewind_read;
  147. }
  148. void Curl_creader_set_rewind(struct Curl_easy *data, bool enable)
  149. {
  150. data->req.rewind_read = !!enable;
  151. }
  152. /* Write data using an unencoding writer stack. "nbytes" is not
  153. allowed to be 0. */
  154. CURLcode Curl_cwriter_write(struct Curl_easy *data,
  155. struct Curl_cwriter *writer, int type,
  156. const char *buf, size_t nbytes)
  157. {
  158. if(!writer)
  159. return CURLE_WRITE_ERROR;
  160. return writer->cwt->do_write(data, writer, type, buf, nbytes);
  161. }
  162. CURLcode Curl_cwriter_def_init(struct Curl_easy *data,
  163. struct Curl_cwriter *writer)
  164. {
  165. (void)data;
  166. (void)writer;
  167. return CURLE_OK;
  168. }
  169. CURLcode Curl_cwriter_def_write(struct Curl_easy *data,
  170. struct Curl_cwriter *writer, int type,
  171. const char *buf, size_t nbytes)
  172. {
  173. return Curl_cwriter_write(data, writer->next, type, buf, nbytes);
  174. }
  175. void Curl_cwriter_def_close(struct Curl_easy *data,
  176. struct Curl_cwriter *writer)
  177. {
  178. (void) data;
  179. (void) writer;
  180. }
  181. static size_t get_max_body_write_len(struct Curl_easy *data, curl_off_t limit)
  182. {
  183. if(limit != -1) {
  184. /* How much more are we allowed to write? */
  185. curl_off_t remain_diff;
  186. remain_diff = limit - data->req.bytecount;
  187. if(remain_diff < 0) {
  188. /* already written too much! */
  189. return 0;
  190. }
  191. #if SIZEOF_CURL_OFF_T > SIZEOF_SIZE_T
  192. else if(remain_diff > SSIZE_T_MAX) {
  193. return SIZE_T_MAX;
  194. }
  195. #endif
  196. else {
  197. return (size_t)remain_diff;
  198. }
  199. }
  200. return SIZE_T_MAX;
  201. }
  202. struct cw_download_ctx {
  203. struct Curl_cwriter super;
  204. BIT(started_response);
  205. };
  206. /* Download client writer in phase CURL_CW_PROTOCOL that
  207. * sees the "real" download body data. */
  208. static CURLcode cw_download_write(struct Curl_easy *data,
  209. struct Curl_cwriter *writer, int type,
  210. const char *buf, size_t nbytes)
  211. {
  212. struct cw_download_ctx *ctx = writer->ctx;
  213. CURLcode result;
  214. size_t nwrite, excess_len = 0;
  215. bool is_connect = !!(type & CLIENTWRITE_CONNECT);
  216. if(!is_connect && !ctx->started_response) {
  217. Curl_pgrsTime(data, TIMER_STARTTRANSFER);
  218. ctx->started_response = TRUE;
  219. }
  220. if(!(type & CLIENTWRITE_BODY)) {
  221. if(is_connect && data->set.suppress_connect_headers)
  222. return CURLE_OK;
  223. result = Curl_cwriter_write(data, writer->next, type, buf, nbytes);
  224. CURL_TRC_WRITE(data, "download_write header(type=%x, blen=%zu) -> %d",
  225. type, nbytes, result);
  226. return result;
  227. }
  228. /* Here, we deal with REAL BODY bytes. All filtering and transfer
  229. * encodings have been applied and only the true content, e.g. BODY,
  230. * bytes are passed here.
  231. * This allows us to check sizes, update stats, etc. independent
  232. * from the protocol in play. */
  233. if(data->req.no_body && nbytes > 0) {
  234. /* BODY arrives although we want none, bail out */
  235. streamclose(data->conn, "ignoring body");
  236. CURL_TRC_WRITE(data, "download_write body(type=%x, blen=%zu), "
  237. "did not want a BODY", type, nbytes);
  238. data->req.download_done = TRUE;
  239. if(data->info.header_size)
  240. /* if headers have been received, this is fine */
  241. return CURLE_OK;
  242. return CURLE_WEIRD_SERVER_REPLY;
  243. }
  244. /* Determine if we see any bytes in excess to what is allowed.
  245. * We write the allowed bytes and handle excess further below.
  246. * This gives deterministic BODY writes on varying buffer receive
  247. * lengths. */
  248. nwrite = nbytes;
  249. if(-1 != data->req.maxdownload) {
  250. size_t wmax = get_max_body_write_len(data, data->req.maxdownload);
  251. if(nwrite > wmax) {
  252. excess_len = nbytes - wmax;
  253. nwrite = wmax;
  254. }
  255. if(nwrite == wmax) {
  256. data->req.download_done = TRUE;
  257. }
  258. }
  259. /* Error on too large filesize is handled below, after writing
  260. * the permitted bytes */
  261. if(data->set.max_filesize) {
  262. size_t wmax = get_max_body_write_len(data, data->set.max_filesize);
  263. if(nwrite > wmax) {
  264. nwrite = wmax;
  265. }
  266. }
  267. if(!data->req.ignorebody && (nwrite || (type & CLIENTWRITE_EOS))) {
  268. result = Curl_cwriter_write(data, writer->next, type, buf, nwrite);
  269. CURL_TRC_WRITE(data, "download_write body(type=%x, blen=%zu) -> %d",
  270. type, nbytes, result);
  271. if(result)
  272. return result;
  273. }
  274. /* Update stats, write and report progress */
  275. data->req.bytecount += nwrite;
  276. ++data->req.bodywrites;
  277. result = Curl_pgrsSetDownloadCounter(data, data->req.bytecount);
  278. if(result)
  279. return result;
  280. if(excess_len) {
  281. if(!data->req.ignorebody) {
  282. infof(data,
  283. "Excess found writing body:"
  284. " excess = %zu"
  285. ", size = %" CURL_FORMAT_CURL_OFF_T
  286. ", maxdownload = %" CURL_FORMAT_CURL_OFF_T
  287. ", bytecount = %" CURL_FORMAT_CURL_OFF_T,
  288. excess_len, data->req.size, data->req.maxdownload,
  289. data->req.bytecount);
  290. connclose(data->conn, "excess found in a read");
  291. }
  292. }
  293. else if(nwrite < nbytes) {
  294. failf(data, "Exceeded the maximum allowed file size "
  295. "(%" CURL_FORMAT_CURL_OFF_T ") with %"
  296. CURL_FORMAT_CURL_OFF_T " bytes",
  297. data->set.max_filesize, data->req.bytecount);
  298. return CURLE_FILESIZE_EXCEEDED;
  299. }
  300. return CURLE_OK;
  301. }
  302. static const struct Curl_cwtype cw_download = {
  303. "protocol",
  304. NULL,
  305. Curl_cwriter_def_init,
  306. cw_download_write,
  307. Curl_cwriter_def_close,
  308. sizeof(struct cw_download_ctx)
  309. };
  310. /* RAW client writer in phase CURL_CW_RAW that
  311. * enabled tracing of raw data. */
  312. static CURLcode cw_raw_write(struct Curl_easy *data,
  313. struct Curl_cwriter *writer, int type,
  314. const char *buf, size_t nbytes)
  315. {
  316. if(type & CLIENTWRITE_BODY && data->set.verbose && !data->req.ignorebody) {
  317. Curl_debug(data, CURLINFO_DATA_IN, (char *)buf, nbytes);
  318. }
  319. return Curl_cwriter_write(data, writer->next, type, buf, nbytes);
  320. }
  321. static const struct Curl_cwtype cw_raw = {
  322. "raw",
  323. NULL,
  324. Curl_cwriter_def_init,
  325. cw_raw_write,
  326. Curl_cwriter_def_close,
  327. sizeof(struct Curl_cwriter)
  328. };
  329. /* Create an unencoding writer stage using the given handler. */
  330. CURLcode Curl_cwriter_create(struct Curl_cwriter **pwriter,
  331. struct Curl_easy *data,
  332. const struct Curl_cwtype *cwt,
  333. Curl_cwriter_phase phase)
  334. {
  335. struct Curl_cwriter *writer = NULL;
  336. CURLcode result = CURLE_OUT_OF_MEMORY;
  337. void *p;
  338. DEBUGASSERT(cwt->cwriter_size >= sizeof(struct Curl_cwriter));
  339. p = calloc(1, cwt->cwriter_size);
  340. if(!p)
  341. goto out;
  342. writer = (struct Curl_cwriter *)p;
  343. writer->cwt = cwt;
  344. writer->ctx = p;
  345. writer->phase = phase;
  346. result = cwt->do_init(data, writer);
  347. out:
  348. *pwriter = result? NULL : writer;
  349. if(result)
  350. free(writer);
  351. return result;
  352. }
  353. void Curl_cwriter_free(struct Curl_easy *data,
  354. struct Curl_cwriter *writer)
  355. {
  356. if(writer) {
  357. writer->cwt->do_close(data, writer);
  358. free(writer);
  359. }
  360. }
  361. size_t Curl_cwriter_count(struct Curl_easy *data, Curl_cwriter_phase phase)
  362. {
  363. struct Curl_cwriter *w;
  364. size_t n = 0;
  365. for(w = data->req.writer_stack; w; w = w->next) {
  366. if(w->phase == phase)
  367. ++n;
  368. }
  369. return n;
  370. }
  371. static CURLcode do_init_writer_stack(struct Curl_easy *data)
  372. {
  373. struct Curl_cwriter *writer;
  374. CURLcode result;
  375. DEBUGASSERT(!data->req.writer_stack);
  376. result = Curl_cwriter_create(&data->req.writer_stack,
  377. data, &Curl_cwt_out, CURL_CW_CLIENT);
  378. if(result)
  379. return result;
  380. result = Curl_cwriter_create(&writer, data, &cw_download, CURL_CW_PROTOCOL);
  381. if(result)
  382. return result;
  383. result = Curl_cwriter_add(data, writer);
  384. if(result) {
  385. Curl_cwriter_free(data, writer);
  386. }
  387. result = Curl_cwriter_create(&writer, data, &cw_raw, CURL_CW_RAW);
  388. if(result)
  389. return result;
  390. result = Curl_cwriter_add(data, writer);
  391. if(result) {
  392. Curl_cwriter_free(data, writer);
  393. }
  394. return result;
  395. }
  396. CURLcode Curl_cwriter_add(struct Curl_easy *data,
  397. struct Curl_cwriter *writer)
  398. {
  399. CURLcode result;
  400. struct Curl_cwriter **anchor = &data->req.writer_stack;
  401. if(!*anchor) {
  402. result = do_init_writer_stack(data);
  403. if(result)
  404. return result;
  405. }
  406. /* Insert the writer as first in its phase.
  407. * Skip existing writers of lower phases. */
  408. while(*anchor && (*anchor)->phase < writer->phase)
  409. anchor = &((*anchor)->next);
  410. writer->next = *anchor;
  411. *anchor = writer;
  412. return CURLE_OK;
  413. }
  414. struct Curl_cwriter *Curl_cwriter_get_by_name(struct Curl_easy *data,
  415. const char *name)
  416. {
  417. struct Curl_cwriter *writer;
  418. for(writer = data->req.writer_stack; writer; writer = writer->next) {
  419. if(!strcmp(name, writer->cwt->name))
  420. return writer;
  421. }
  422. return NULL;
  423. }
  424. struct Curl_cwriter *Curl_cwriter_get_by_type(struct Curl_easy *data,
  425. const struct Curl_cwtype *cwt)
  426. {
  427. struct Curl_cwriter *writer;
  428. for(writer = data->req.writer_stack; writer; writer = writer->next) {
  429. if(writer->cwt == cwt)
  430. return writer;
  431. }
  432. return NULL;
  433. }
  434. void Curl_cwriter_remove_by_name(struct Curl_easy *data,
  435. const char *name)
  436. {
  437. struct Curl_cwriter **anchor = &data->req.writer_stack;
  438. while(*anchor) {
  439. if(!strcmp(name, (*anchor)->cwt->name)) {
  440. struct Curl_cwriter *w = (*anchor);
  441. *anchor = w->next;
  442. Curl_cwriter_free(data, w);
  443. continue;
  444. }
  445. anchor = &((*anchor)->next);
  446. }
  447. }
  448. bool Curl_cwriter_is_paused(struct Curl_easy *data)
  449. {
  450. return Curl_cw_out_is_paused(data);
  451. }
  452. CURLcode Curl_cwriter_unpause(struct Curl_easy *data)
  453. {
  454. return Curl_cw_out_unpause(data);
  455. }
  456. CURLcode Curl_creader_read(struct Curl_easy *data,
  457. struct Curl_creader *reader,
  458. char *buf, size_t blen, size_t *nread, bool *eos)
  459. {
  460. if(!reader)
  461. return CURLE_READ_ERROR;
  462. return reader->crt->do_read(data, reader, buf, blen, nread, eos);
  463. }
  464. CURLcode Curl_creader_def_init(struct Curl_easy *data,
  465. struct Curl_creader *reader)
  466. {
  467. (void)data;
  468. (void)reader;
  469. return CURLE_OK;
  470. }
  471. void Curl_creader_def_close(struct Curl_easy *data,
  472. struct Curl_creader *reader)
  473. {
  474. (void)data;
  475. (void)reader;
  476. }
  477. CURLcode Curl_creader_def_read(struct Curl_easy *data,
  478. struct Curl_creader *reader,
  479. char *buf, size_t blen,
  480. size_t *nread, bool *eos)
  481. {
  482. if(reader->next)
  483. return reader->next->crt->do_read(data, reader->next, buf, blen,
  484. nread, eos);
  485. else {
  486. *nread = 0;
  487. *eos = FALSE;
  488. return CURLE_READ_ERROR;
  489. }
  490. }
  491. bool Curl_creader_def_needs_rewind(struct Curl_easy *data,
  492. struct Curl_creader *reader)
  493. {
  494. (void)data;
  495. (void)reader;
  496. return FALSE;
  497. }
  498. curl_off_t Curl_creader_def_total_length(struct Curl_easy *data,
  499. struct Curl_creader *reader)
  500. {
  501. return reader->next?
  502. reader->next->crt->total_length(data, reader->next) : -1;
  503. }
  504. CURLcode Curl_creader_def_resume_from(struct Curl_easy *data,
  505. struct Curl_creader *reader,
  506. curl_off_t offset)
  507. {
  508. (void)data;
  509. (void)reader;
  510. (void)offset;
  511. return CURLE_READ_ERROR;
  512. }
  513. CURLcode Curl_creader_def_rewind(struct Curl_easy *data,
  514. struct Curl_creader *reader)
  515. {
  516. (void)data;
  517. (void)reader;
  518. return CURLE_OK;
  519. }
  520. CURLcode Curl_creader_def_unpause(struct Curl_easy *data,
  521. struct Curl_creader *reader)
  522. {
  523. (void)data;
  524. (void)reader;
  525. return CURLE_OK;
  526. }
  527. void Curl_creader_def_done(struct Curl_easy *data,
  528. struct Curl_creader *reader, int premature)
  529. {
  530. (void)data;
  531. (void)reader;
  532. (void)premature;
  533. }
  534. struct cr_in_ctx {
  535. struct Curl_creader super;
  536. curl_read_callback read_cb;
  537. void *cb_user_data;
  538. curl_off_t total_len;
  539. curl_off_t read_len;
  540. CURLcode error_result;
  541. BIT(seen_eos);
  542. BIT(errored);
  543. BIT(has_used_cb);
  544. };
  545. static CURLcode cr_in_init(struct Curl_easy *data, struct Curl_creader *reader)
  546. {
  547. struct cr_in_ctx *ctx = reader->ctx;
  548. (void)data;
  549. ctx->read_cb = data->state.fread_func;
  550. ctx->cb_user_data = data->state.in;
  551. ctx->total_len = -1;
  552. ctx->read_len = 0;
  553. return CURLE_OK;
  554. }
  555. /* Real client reader to installed client callbacks. */
  556. static CURLcode cr_in_read(struct Curl_easy *data,
  557. struct Curl_creader *reader,
  558. char *buf, size_t blen,
  559. size_t *pnread, bool *peos)
  560. {
  561. struct cr_in_ctx *ctx = reader->ctx;
  562. size_t nread;
  563. /* Once we have errored, we will return the same error forever */
  564. if(ctx->errored) {
  565. *pnread = 0;
  566. *peos = FALSE;
  567. return ctx->error_result;
  568. }
  569. if(ctx->seen_eos) {
  570. *pnread = 0;
  571. *peos = TRUE;
  572. return CURLE_OK;
  573. }
  574. /* respect length limitations */
  575. if(ctx->total_len >= 0) {
  576. curl_off_t remain = ctx->total_len - ctx->read_len;
  577. if(remain <= 0)
  578. blen = 0;
  579. else if(remain < (curl_off_t)blen)
  580. blen = (size_t)remain;
  581. }
  582. nread = 0;
  583. if(ctx->read_cb && blen) {
  584. Curl_set_in_callback(data, true);
  585. nread = ctx->read_cb(buf, 1, blen, ctx->cb_user_data);
  586. Curl_set_in_callback(data, false);
  587. ctx->has_used_cb = TRUE;
  588. }
  589. switch(nread) {
  590. case 0:
  591. if((ctx->total_len >= 0) && (ctx->read_len < ctx->total_len)) {
  592. failf(data, "client read function EOF fail, only "
  593. "only %"CURL_FORMAT_CURL_OFF_T"/%"CURL_FORMAT_CURL_OFF_T
  594. " of needed bytes read", ctx->read_len, ctx->total_len);
  595. return CURLE_READ_ERROR;
  596. }
  597. *pnread = 0;
  598. *peos = TRUE;
  599. ctx->seen_eos = TRUE;
  600. break;
  601. case CURL_READFUNC_ABORT:
  602. failf(data, "operation aborted by callback");
  603. *pnread = 0;
  604. *peos = FALSE;
  605. ctx->errored = TRUE;
  606. ctx->error_result = CURLE_ABORTED_BY_CALLBACK;
  607. return CURLE_ABORTED_BY_CALLBACK;
  608. case CURL_READFUNC_PAUSE:
  609. if(data->conn->handler->flags & PROTOPT_NONETWORK) {
  610. /* protocols that work without network cannot be paused. This is
  611. actually only FILE:// just now, and it can't pause since the transfer
  612. isn't done using the "normal" procedure. */
  613. failf(data, "Read callback asked for PAUSE when not supported");
  614. return CURLE_READ_ERROR;
  615. }
  616. /* CURL_READFUNC_PAUSE pauses read callbacks that feed socket writes */
  617. data->req.keepon |= KEEP_SEND_PAUSE; /* mark socket send as paused */
  618. *pnread = 0;
  619. *peos = FALSE;
  620. break; /* nothing was read */
  621. default:
  622. if(nread > blen) {
  623. /* the read function returned a too large value */
  624. failf(data, "read function returned funny value");
  625. *pnread = 0;
  626. *peos = FALSE;
  627. ctx->errored = TRUE;
  628. ctx->error_result = CURLE_READ_ERROR;
  629. return CURLE_READ_ERROR;
  630. }
  631. ctx->read_len += nread;
  632. if(ctx->total_len >= 0)
  633. ctx->seen_eos = (ctx->read_len >= ctx->total_len);
  634. *pnread = nread;
  635. *peos = ctx->seen_eos;
  636. break;
  637. }
  638. CURL_TRC_READ(data, "cr_in_read(len=%zu, total=%"CURL_FORMAT_CURL_OFF_T
  639. ", read=%"CURL_FORMAT_CURL_OFF_T") -> %d, nread=%zu, eos=%d",
  640. blen, ctx->total_len, ctx->read_len, CURLE_OK,
  641. *pnread, *peos);
  642. return CURLE_OK;
  643. }
  644. static bool cr_in_needs_rewind(struct Curl_easy *data,
  645. struct Curl_creader *reader)
  646. {
  647. struct cr_in_ctx *ctx = reader->ctx;
  648. (void)data;
  649. return ctx->has_used_cb;
  650. }
  651. static curl_off_t cr_in_total_length(struct Curl_easy *data,
  652. struct Curl_creader *reader)
  653. {
  654. struct cr_in_ctx *ctx = reader->ctx;
  655. (void)data;
  656. return ctx->total_len;
  657. }
  658. static CURLcode cr_in_resume_from(struct Curl_easy *data,
  659. struct Curl_creader *reader,
  660. curl_off_t offset)
  661. {
  662. struct cr_in_ctx *ctx = reader->ctx;
  663. int seekerr = CURL_SEEKFUNC_CANTSEEK;
  664. DEBUGASSERT(data->conn);
  665. /* already started reading? */
  666. if(ctx->read_len)
  667. return CURLE_READ_ERROR;
  668. if(data->set.seek_func) {
  669. Curl_set_in_callback(data, true);
  670. seekerr = data->set.seek_func(data->set.seek_client, offset, SEEK_SET);
  671. Curl_set_in_callback(data, false);
  672. }
  673. if(seekerr != CURL_SEEKFUNC_OK) {
  674. curl_off_t passed = 0;
  675. if(seekerr != CURL_SEEKFUNC_CANTSEEK) {
  676. failf(data, "Could not seek stream");
  677. return CURLE_READ_ERROR;
  678. }
  679. /* when seekerr == CURL_SEEKFUNC_CANTSEEK (can't seek to offset) */
  680. do {
  681. char scratch[4*1024];
  682. size_t readthisamountnow =
  683. (offset - passed > (curl_off_t)sizeof(scratch)) ?
  684. sizeof(scratch) :
  685. curlx_sotouz(offset - passed);
  686. size_t actuallyread;
  687. Curl_set_in_callback(data, true);
  688. actuallyread = ctx->read_cb(scratch, 1, readthisamountnow,
  689. ctx->cb_user_data);
  690. Curl_set_in_callback(data, false);
  691. passed += actuallyread;
  692. if((actuallyread == 0) || (actuallyread > readthisamountnow)) {
  693. /* this checks for greater-than only to make sure that the
  694. CURL_READFUNC_ABORT return code still aborts */
  695. failf(data, "Could only read %" CURL_FORMAT_CURL_OFF_T
  696. " bytes from the input", passed);
  697. return CURLE_READ_ERROR;
  698. }
  699. } while(passed < offset);
  700. }
  701. /* now, decrease the size of the read */
  702. if(ctx->total_len > 0) {
  703. ctx->total_len -= offset;
  704. if(ctx->total_len <= 0) {
  705. failf(data, "File already completely uploaded");
  706. return CURLE_PARTIAL_FILE;
  707. }
  708. }
  709. /* we've passed, proceed as normal */
  710. return CURLE_OK;
  711. }
  712. static CURLcode cr_in_rewind(struct Curl_easy *data,
  713. struct Curl_creader *reader)
  714. {
  715. struct cr_in_ctx *ctx = reader->ctx;
  716. /* If we never invoked the callback, there is noting to rewind */
  717. if(!ctx->has_used_cb)
  718. return CURLE_OK;
  719. if(data->set.seek_func) {
  720. int err;
  721. Curl_set_in_callback(data, true);
  722. err = (data->set.seek_func)(data->set.seek_client, 0, SEEK_SET);
  723. Curl_set_in_callback(data, false);
  724. CURL_TRC_READ(data, "cr_in, rewind via set.seek_func -> %d", err);
  725. if(err) {
  726. failf(data, "seek callback returned error %d", (int)err);
  727. return CURLE_SEND_FAIL_REWIND;
  728. }
  729. }
  730. else if(data->set.ioctl_func) {
  731. curlioerr err;
  732. Curl_set_in_callback(data, true);
  733. err = (data->set.ioctl_func)(data, CURLIOCMD_RESTARTREAD,
  734. data->set.ioctl_client);
  735. Curl_set_in_callback(data, false);
  736. CURL_TRC_READ(data, "cr_in, rewind via set.ioctl_func -> %d", (int)err);
  737. if(err) {
  738. failf(data, "ioctl callback returned error %d", (int)err);
  739. return CURLE_SEND_FAIL_REWIND;
  740. }
  741. }
  742. else {
  743. /* If no CURLOPT_READFUNCTION is used, we know that we operate on a
  744. given FILE * stream and we can actually attempt to rewind that
  745. ourselves with fseek() */
  746. if(data->state.fread_func == (curl_read_callback)fread) {
  747. int err = fseek(data->state.in, 0, SEEK_SET);
  748. CURL_TRC_READ(data, "cr_in, rewind via fseek -> %d(%d)",
  749. (int)err, (int)errno);
  750. if(-1 != err)
  751. /* successful rewind */
  752. return CURLE_OK;
  753. }
  754. /* no callback set or failure above, makes us fail at once */
  755. failf(data, "necessary data rewind wasn't possible");
  756. return CURLE_SEND_FAIL_REWIND;
  757. }
  758. return CURLE_OK;
  759. }
  760. static const struct Curl_crtype cr_in = {
  761. "cr-in",
  762. cr_in_init,
  763. cr_in_read,
  764. Curl_creader_def_close,
  765. cr_in_needs_rewind,
  766. cr_in_total_length,
  767. cr_in_resume_from,
  768. cr_in_rewind,
  769. Curl_creader_def_unpause,
  770. Curl_creader_def_done,
  771. sizeof(struct cr_in_ctx)
  772. };
  773. CURLcode Curl_creader_create(struct Curl_creader **preader,
  774. struct Curl_easy *data,
  775. const struct Curl_crtype *crt,
  776. Curl_creader_phase phase)
  777. {
  778. struct Curl_creader *reader = NULL;
  779. CURLcode result = CURLE_OUT_OF_MEMORY;
  780. void *p;
  781. DEBUGASSERT(crt->creader_size >= sizeof(struct Curl_creader));
  782. p = calloc(1, crt->creader_size);
  783. if(!p)
  784. goto out;
  785. reader = (struct Curl_creader *)p;
  786. reader->crt = crt;
  787. reader->ctx = p;
  788. reader->phase = phase;
  789. result = crt->do_init(data, reader);
  790. out:
  791. *preader = result? NULL : reader;
  792. if(result)
  793. free(reader);
  794. return result;
  795. }
  796. void Curl_creader_free(struct Curl_easy *data, struct Curl_creader *reader)
  797. {
  798. if(reader) {
  799. reader->crt->do_close(data, reader);
  800. free(reader);
  801. }
  802. }
  803. struct cr_lc_ctx {
  804. struct Curl_creader super;
  805. struct bufq buf;
  806. BIT(read_eos); /* we read an EOS from the next reader */
  807. BIT(eos); /* we have returned an EOS */
  808. };
  809. static CURLcode cr_lc_init(struct Curl_easy *data, struct Curl_creader *reader)
  810. {
  811. struct cr_lc_ctx *ctx = reader->ctx;
  812. (void)data;
  813. Curl_bufq_init2(&ctx->buf, (16 * 1024), 1, BUFQ_OPT_SOFT_LIMIT);
  814. return CURLE_OK;
  815. }
  816. static void cr_lc_close(struct Curl_easy *data, struct Curl_creader *reader)
  817. {
  818. struct cr_lc_ctx *ctx = reader->ctx;
  819. (void)data;
  820. Curl_bufq_free(&ctx->buf);
  821. }
  822. /* client reader doing line end conversions. */
  823. static CURLcode cr_lc_read(struct Curl_easy *data,
  824. struct Curl_creader *reader,
  825. char *buf, size_t blen,
  826. size_t *pnread, bool *peos)
  827. {
  828. struct cr_lc_ctx *ctx = reader->ctx;
  829. CURLcode result;
  830. size_t nread, i, start, n;
  831. bool eos;
  832. if(ctx->eos) {
  833. *pnread = 0;
  834. *peos = TRUE;
  835. return CURLE_OK;
  836. }
  837. if(Curl_bufq_is_empty(&ctx->buf)) {
  838. if(ctx->read_eos) {
  839. ctx->eos = TRUE;
  840. *pnread = 0;
  841. *peos = TRUE;
  842. return CURLE_OK;
  843. }
  844. /* Still getting data form the next reader, ctx->buf is empty */
  845. result = Curl_creader_read(data, reader->next, buf, blen, &nread, &eos);
  846. if(result)
  847. return result;
  848. ctx->read_eos = eos;
  849. if(!nread || !memchr(buf, '\n', nread)) {
  850. /* nothing to convert, return this right away */
  851. if(ctx->read_eos)
  852. ctx->eos = TRUE;
  853. *pnread = nread;
  854. *peos = ctx->eos;
  855. result = CURLE_OK;
  856. goto out;
  857. }
  858. /* at least one \n needs conversion to '\r\n', place into ctx->buf */
  859. for(i = start = 0; i < nread; ++i) {
  860. if(buf[i] != '\n')
  861. continue;
  862. /* on a soft limit bufq, we do not need to check length */
  863. result = Curl_bufq_cwrite(&ctx->buf, buf + start, i - start, &n);
  864. if(!result)
  865. result = Curl_bufq_cwrite(&ctx->buf, STRCONST("\r\n"), &n);
  866. if(result)
  867. return result;
  868. start = i + 1;
  869. if(!data->set.crlf && (data->state.infilesize != -1)) {
  870. /* we're here only because FTP is in ASCII mode...
  871. bump infilesize for the LF we just added */
  872. data->state.infilesize++;
  873. /* comment: this might work for FTP, but in HTTP we could not change
  874. * the content length after having started the request... */
  875. }
  876. }
  877. }
  878. DEBUGASSERT(!Curl_bufq_is_empty(&ctx->buf));
  879. *peos = FALSE;
  880. result = Curl_bufq_cread(&ctx->buf, buf, blen, pnread);
  881. if(!result && ctx->read_eos && Curl_bufq_is_empty(&ctx->buf)) {
  882. /* no more data, read all, done. */
  883. ctx->eos = TRUE;
  884. *peos = TRUE;
  885. }
  886. out:
  887. CURL_TRC_READ(data, "cr_lc_read(len=%zu) -> %d, nread=%zu, eos=%d",
  888. blen, result, *pnread, *peos);
  889. return result;
  890. }
  891. static curl_off_t cr_lc_total_length(struct Curl_easy *data,
  892. struct Curl_creader *reader)
  893. {
  894. /* this reader changes length depending on input */
  895. (void)data;
  896. (void)reader;
  897. return -1;
  898. }
  899. static const struct Curl_crtype cr_lc = {
  900. "cr-lineconv",
  901. cr_lc_init,
  902. cr_lc_read,
  903. cr_lc_close,
  904. Curl_creader_def_needs_rewind,
  905. cr_lc_total_length,
  906. Curl_creader_def_resume_from,
  907. Curl_creader_def_rewind,
  908. Curl_creader_def_unpause,
  909. Curl_creader_def_done,
  910. sizeof(struct cr_lc_ctx)
  911. };
  912. static CURLcode cr_lc_add(struct Curl_easy *data)
  913. {
  914. struct Curl_creader *reader = NULL;
  915. CURLcode result;
  916. result = Curl_creader_create(&reader, data, &cr_lc,
  917. CURL_CR_CONTENT_ENCODE);
  918. if(!result)
  919. result = Curl_creader_add(data, reader);
  920. if(result && reader)
  921. Curl_creader_free(data, reader);
  922. return result;
  923. }
  924. static CURLcode do_init_reader_stack(struct Curl_easy *data,
  925. struct Curl_creader *r)
  926. {
  927. CURLcode result = CURLE_OK;
  928. curl_off_t clen;
  929. DEBUGASSERT(r);
  930. DEBUGASSERT(r->crt);
  931. DEBUGASSERT(r->phase == CURL_CR_CLIENT);
  932. DEBUGASSERT(!data->req.reader_stack);
  933. data->req.reader_stack = r;
  934. clen = r->crt->total_length(data, r);
  935. /* if we do not have 0 length init, and crlf conversion is wanted,
  936. * add the reader for it */
  937. if(clen && (data->set.crlf
  938. #ifdef CURL_DO_LINEEND_CONV
  939. || data->state.prefer_ascii
  940. #endif
  941. )) {
  942. result = cr_lc_add(data);
  943. if(result)
  944. return result;
  945. }
  946. return result;
  947. }
  948. CURLcode Curl_creader_set_fread(struct Curl_easy *data, curl_off_t len)
  949. {
  950. CURLcode result;
  951. struct Curl_creader *r;
  952. struct cr_in_ctx *ctx;
  953. result = Curl_creader_create(&r, data, &cr_in, CURL_CR_CLIENT);
  954. if(result)
  955. goto out;
  956. ctx = r->ctx;
  957. ctx->total_len = len;
  958. cl_reset_reader(data);
  959. result = do_init_reader_stack(data, r);
  960. out:
  961. CURL_TRC_READ(data, "add fread reader, len=%"CURL_FORMAT_CURL_OFF_T
  962. " -> %d", len, result);
  963. return result;
  964. }
  965. CURLcode Curl_creader_add(struct Curl_easy *data,
  966. struct Curl_creader *reader)
  967. {
  968. CURLcode result;
  969. struct Curl_creader **anchor = &data->req.reader_stack;
  970. if(!*anchor) {
  971. result = Curl_creader_set_fread(data, data->state.infilesize);
  972. if(result)
  973. return result;
  974. }
  975. /* Insert the writer as first in its phase.
  976. * Skip existing readers of lower phases. */
  977. while(*anchor && (*anchor)->phase < reader->phase)
  978. anchor = &((*anchor)->next);
  979. reader->next = *anchor;
  980. *anchor = reader;
  981. return CURLE_OK;
  982. }
  983. CURLcode Curl_creader_set(struct Curl_easy *data, struct Curl_creader *r)
  984. {
  985. CURLcode result;
  986. DEBUGASSERT(r);
  987. DEBUGASSERT(r->crt);
  988. DEBUGASSERT(r->phase == CURL_CR_CLIENT);
  989. cl_reset_reader(data);
  990. result = do_init_reader_stack(data, r);
  991. if(result)
  992. Curl_creader_free(data, r);
  993. return result;
  994. }
  995. CURLcode Curl_client_read(struct Curl_easy *data, char *buf, size_t blen,
  996. size_t *nread, bool *eos)
  997. {
  998. CURLcode result;
  999. DEBUGASSERT(buf);
  1000. DEBUGASSERT(blen);
  1001. DEBUGASSERT(nread);
  1002. DEBUGASSERT(eos);
  1003. if(!data->req.reader_stack) {
  1004. result = Curl_creader_set_fread(data, data->state.infilesize);
  1005. if(result)
  1006. return result;
  1007. DEBUGASSERT(data->req.reader_stack);
  1008. }
  1009. result = Curl_creader_read(data, data->req.reader_stack, buf, blen,
  1010. nread, eos);
  1011. CURL_TRC_READ(data, "client_read(len=%zu) -> %d, nread=%zu, eos=%d",
  1012. blen, result, *nread, *eos);
  1013. return result;
  1014. }
  1015. bool Curl_creader_needs_rewind(struct Curl_easy *data)
  1016. {
  1017. struct Curl_creader *reader = data->req.reader_stack;
  1018. while(reader) {
  1019. if(reader->crt->needs_rewind(data, reader)) {
  1020. CURL_TRC_READ(data, "client reader needs rewind before next request");
  1021. return TRUE;
  1022. }
  1023. reader = reader->next;
  1024. }
  1025. return FALSE;
  1026. }
  1027. static CURLcode cr_null_read(struct Curl_easy *data,
  1028. struct Curl_creader *reader,
  1029. char *buf, size_t blen,
  1030. size_t *pnread, bool *peos)
  1031. {
  1032. (void)data;
  1033. (void)reader;
  1034. (void)buf;
  1035. (void)blen;
  1036. *pnread = 0;
  1037. *peos = TRUE;
  1038. return CURLE_OK;
  1039. }
  1040. static curl_off_t cr_null_total_length(struct Curl_easy *data,
  1041. struct Curl_creader *reader)
  1042. {
  1043. /* this reader changes length depending on input */
  1044. (void)data;
  1045. (void)reader;
  1046. return 0;
  1047. }
  1048. static const struct Curl_crtype cr_null = {
  1049. "cr-null",
  1050. Curl_creader_def_init,
  1051. cr_null_read,
  1052. Curl_creader_def_close,
  1053. Curl_creader_def_needs_rewind,
  1054. cr_null_total_length,
  1055. Curl_creader_def_resume_from,
  1056. Curl_creader_def_rewind,
  1057. Curl_creader_def_unpause,
  1058. Curl_creader_def_done,
  1059. sizeof(struct Curl_creader)
  1060. };
  1061. CURLcode Curl_creader_set_null(struct Curl_easy *data)
  1062. {
  1063. struct Curl_creader *r;
  1064. CURLcode result;
  1065. result = Curl_creader_create(&r, data, &cr_null, CURL_CR_CLIENT);
  1066. if(result)
  1067. return result;
  1068. cl_reset_reader(data);
  1069. return do_init_reader_stack(data, r);
  1070. }
  1071. struct cr_buf_ctx {
  1072. struct Curl_creader super;
  1073. const char *buf;
  1074. size_t blen;
  1075. size_t index;
  1076. };
  1077. static CURLcode cr_buf_read(struct Curl_easy *data,
  1078. struct Curl_creader *reader,
  1079. char *buf, size_t blen,
  1080. size_t *pnread, bool *peos)
  1081. {
  1082. struct cr_buf_ctx *ctx = reader->ctx;
  1083. size_t nread = ctx->blen - ctx->index;
  1084. (void)data;
  1085. if(!nread || !ctx->buf) {
  1086. *pnread = 0;
  1087. *peos = TRUE;
  1088. }
  1089. else {
  1090. if(nread > blen)
  1091. nread = blen;
  1092. memcpy(buf, ctx->buf + ctx->index, nread);
  1093. *pnread = nread;
  1094. ctx->index += nread;
  1095. *peos = (ctx->index == ctx->blen);
  1096. }
  1097. CURL_TRC_READ(data, "cr_buf_read(len=%zu) -> 0, nread=%zu, eos=%d",
  1098. blen, *pnread, *peos);
  1099. return CURLE_OK;
  1100. }
  1101. static bool cr_buf_needs_rewind(struct Curl_easy *data,
  1102. struct Curl_creader *reader)
  1103. {
  1104. struct cr_buf_ctx *ctx = reader->ctx;
  1105. (void)data;
  1106. return ctx->index > 0;
  1107. }
  1108. static curl_off_t cr_buf_total_length(struct Curl_easy *data,
  1109. struct Curl_creader *reader)
  1110. {
  1111. struct cr_buf_ctx *ctx = reader->ctx;
  1112. (void)data;
  1113. return (curl_off_t)ctx->blen;
  1114. }
  1115. static CURLcode cr_buf_resume_from(struct Curl_easy *data,
  1116. struct Curl_creader *reader,
  1117. curl_off_t offset)
  1118. {
  1119. struct cr_buf_ctx *ctx = reader->ctx;
  1120. size_t boffset;
  1121. (void)data;
  1122. DEBUGASSERT(data->conn);
  1123. /* already started reading? */
  1124. if(ctx->index)
  1125. return CURLE_READ_ERROR;
  1126. if(offset <= 0)
  1127. return CURLE_OK;
  1128. boffset = (size_t)offset;
  1129. if(boffset > ctx->blen)
  1130. return CURLE_READ_ERROR;
  1131. ctx->buf += boffset;
  1132. ctx->blen -= boffset;
  1133. return CURLE_OK;
  1134. }
  1135. static const struct Curl_crtype cr_buf = {
  1136. "cr-buf",
  1137. Curl_creader_def_init,
  1138. cr_buf_read,
  1139. Curl_creader_def_close,
  1140. cr_buf_needs_rewind,
  1141. cr_buf_total_length,
  1142. cr_buf_resume_from,
  1143. Curl_creader_def_rewind,
  1144. Curl_creader_def_unpause,
  1145. Curl_creader_def_done,
  1146. sizeof(struct cr_buf_ctx)
  1147. };
  1148. CURLcode Curl_creader_set_buf(struct Curl_easy *data,
  1149. const char *buf, size_t blen)
  1150. {
  1151. CURLcode result;
  1152. struct Curl_creader *r;
  1153. struct cr_buf_ctx *ctx;
  1154. result = Curl_creader_create(&r, data, &cr_buf, CURL_CR_CLIENT);
  1155. if(result)
  1156. goto out;
  1157. ctx = r->ctx;
  1158. ctx->buf = buf;
  1159. ctx->blen = blen;
  1160. ctx->index = 0;
  1161. cl_reset_reader(data);
  1162. result = do_init_reader_stack(data, r);
  1163. out:
  1164. CURL_TRC_READ(data, "add buf reader, len=%zu -> %d", blen, result);
  1165. return result;
  1166. }
  1167. curl_off_t Curl_creader_total_length(struct Curl_easy *data)
  1168. {
  1169. struct Curl_creader *r = data->req.reader_stack;
  1170. return r? r->crt->total_length(data, r) : -1;
  1171. }
  1172. curl_off_t Curl_creader_client_length(struct Curl_easy *data)
  1173. {
  1174. struct Curl_creader *r = data->req.reader_stack;
  1175. while(r && r->phase != CURL_CR_CLIENT)
  1176. r = r->next;
  1177. return r? r->crt->total_length(data, r) : -1;
  1178. }
  1179. CURLcode Curl_creader_resume_from(struct Curl_easy *data, curl_off_t offset)
  1180. {
  1181. struct Curl_creader *r = data->req.reader_stack;
  1182. while(r && r->phase != CURL_CR_CLIENT)
  1183. r = r->next;
  1184. return r? r->crt->resume_from(data, r, offset) : CURLE_READ_ERROR;
  1185. }
  1186. CURLcode Curl_creader_unpause(struct Curl_easy *data)
  1187. {
  1188. struct Curl_creader *reader = data->req.reader_stack;
  1189. CURLcode result = CURLE_OK;
  1190. while(reader) {
  1191. result = reader->crt->unpause(data, reader);
  1192. if(result)
  1193. break;
  1194. reader = reader->next;
  1195. }
  1196. return result;
  1197. }
  1198. void Curl_creader_done(struct Curl_easy *data, int premature)
  1199. {
  1200. struct Curl_creader *reader = data->req.reader_stack;
  1201. while(reader) {
  1202. reader->crt->done(data, reader, premature);
  1203. reader = reader->next;
  1204. }
  1205. }
  1206. struct Curl_creader *Curl_creader_get_by_type(struct Curl_easy *data,
  1207. const struct Curl_crtype *crt)
  1208. {
  1209. struct Curl_creader *r;
  1210. for(r = data->req.reader_stack; r; r = r->next) {
  1211. if(r->crt == crt)
  1212. return r;
  1213. }
  1214. return NULL;
  1215. }