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. */
  153. CURLcode Curl_cwriter_write(struct Curl_easy *data,
  154. struct Curl_cwriter *writer, int type,
  155. const char *buf, size_t nbytes)
  156. {
  157. if(!writer)
  158. return CURLE_WRITE_ERROR;
  159. return writer->cwt->do_write(data, writer, type, buf, nbytes);
  160. }
  161. CURLcode Curl_cwriter_def_init(struct Curl_easy *data,
  162. struct Curl_cwriter *writer)
  163. {
  164. (void)data;
  165. (void)writer;
  166. return CURLE_OK;
  167. }
  168. CURLcode Curl_cwriter_def_write(struct Curl_easy *data,
  169. struct Curl_cwriter *writer, int type,
  170. const char *buf, size_t nbytes)
  171. {
  172. return Curl_cwriter_write(data, writer->next, type, buf, nbytes);
  173. }
  174. void Curl_cwriter_def_close(struct Curl_easy *data,
  175. struct Curl_cwriter *writer)
  176. {
  177. (void) data;
  178. (void) writer;
  179. }
  180. static size_t get_max_body_write_len(struct Curl_easy *data, curl_off_t limit)
  181. {
  182. if(limit != -1) {
  183. /* How much more are we allowed to write? */
  184. curl_off_t remain_diff;
  185. remain_diff = limit - data->req.bytecount;
  186. if(remain_diff < 0) {
  187. /* already written too much! */
  188. return 0;
  189. }
  190. #if SIZEOF_CURL_OFF_T > SIZEOF_SIZE_T
  191. else if(remain_diff > SSIZE_T_MAX) {
  192. return SIZE_T_MAX;
  193. }
  194. #endif
  195. else {
  196. return (size_t)remain_diff;
  197. }
  198. }
  199. return SIZE_T_MAX;
  200. }
  201. struct cw_download_ctx {
  202. struct Curl_cwriter super;
  203. BIT(started_response);
  204. };
  205. /* Download client writer in phase CURL_CW_PROTOCOL that
  206. * sees the "real" download body data. */
  207. static CURLcode cw_download_write(struct Curl_easy *data,
  208. struct Curl_cwriter *writer, int type,
  209. const char *buf, size_t nbytes)
  210. {
  211. struct cw_download_ctx *ctx = writer->ctx;
  212. CURLcode result;
  213. size_t nwrite, excess_len = 0;
  214. bool is_connect = !!(type & CLIENTWRITE_CONNECT);
  215. if(!is_connect && !ctx->started_response) {
  216. Curl_pgrsTime(data, TIMER_STARTTRANSFER);
  217. ctx->started_response = TRUE;
  218. }
  219. if(!(type & CLIENTWRITE_BODY)) {
  220. if(is_connect && data->set.suppress_connect_headers)
  221. return CURLE_OK;
  222. result = Curl_cwriter_write(data, writer->next, type, buf, nbytes);
  223. CURL_TRC_WRITE(data, "download_write header(type=%x, blen=%zu) -> %d",
  224. type, nbytes, result);
  225. return result;
  226. }
  227. /* Here, we deal with REAL BODY bytes. All filtering and transfer
  228. * encodings have been applied and only the true content, e.g. BODY,
  229. * bytes are passed here.
  230. * This allows us to check sizes, update stats, etc. independent
  231. * from the protocol in play. */
  232. if(data->req.no_body && nbytes > 0) {
  233. /* BODY arrives although we want none, bail out */
  234. streamclose(data->conn, "ignoring body");
  235. CURL_TRC_WRITE(data, "download_write body(type=%x, blen=%zu), "
  236. "did not want a BODY", type, nbytes);
  237. data->req.download_done = TRUE;
  238. if(data->info.header_size)
  239. /* if headers have been received, this is fine */
  240. return CURLE_OK;
  241. return CURLE_WEIRD_SERVER_REPLY;
  242. }
  243. /* Determine if we see any bytes in excess to what is allowed.
  244. * We write the allowed bytes and handle excess further below.
  245. * This gives deterministic BODY writes on varying buffer receive
  246. * lengths. */
  247. nwrite = nbytes;
  248. if(-1 != data->req.maxdownload) {
  249. size_t wmax = get_max_body_write_len(data, data->req.maxdownload);
  250. if(nwrite > wmax) {
  251. excess_len = nbytes - wmax;
  252. nwrite = wmax;
  253. }
  254. if(nwrite == wmax) {
  255. data->req.download_done = TRUE;
  256. }
  257. }
  258. /* Error on too large filesize is handled below, after writing
  259. * the permitted bytes */
  260. if(data->set.max_filesize) {
  261. size_t wmax = get_max_body_write_len(data, data->set.max_filesize);
  262. if(nwrite > wmax) {
  263. nwrite = wmax;
  264. }
  265. }
  266. if(!data->req.ignorebody && (nwrite || (type & CLIENTWRITE_EOS))) {
  267. result = Curl_cwriter_write(data, writer->next, type, buf, nwrite);
  268. CURL_TRC_WRITE(data, "download_write body(type=%x, blen=%zu) -> %d",
  269. type, nbytes, result);
  270. if(result)
  271. return result;
  272. }
  273. /* Update stats, write and report progress */
  274. data->req.bytecount += nwrite;
  275. ++data->req.bodywrites;
  276. result = Curl_pgrsSetDownloadCounter(data, data->req.bytecount);
  277. if(result)
  278. return result;
  279. if(excess_len) {
  280. if(!data->req.ignorebody) {
  281. infof(data,
  282. "Excess found writing body:"
  283. " excess = %zu"
  284. ", size = %" CURL_FORMAT_CURL_OFF_T
  285. ", maxdownload = %" CURL_FORMAT_CURL_OFF_T
  286. ", bytecount = %" CURL_FORMAT_CURL_OFF_T,
  287. excess_len, data->req.size, data->req.maxdownload,
  288. data->req.bytecount);
  289. connclose(data->conn, "excess found in a read");
  290. }
  291. }
  292. else if(nwrite < nbytes) {
  293. failf(data, "Exceeded the maximum allowed file size "
  294. "(%" CURL_FORMAT_CURL_OFF_T ") with %"
  295. CURL_FORMAT_CURL_OFF_T " bytes",
  296. data->set.max_filesize, data->req.bytecount);
  297. return CURLE_FILESIZE_EXCEEDED;
  298. }
  299. return CURLE_OK;
  300. }
  301. static const struct Curl_cwtype cw_download = {
  302. "protocol",
  303. NULL,
  304. Curl_cwriter_def_init,
  305. cw_download_write,
  306. Curl_cwriter_def_close,
  307. sizeof(struct cw_download_ctx)
  308. };
  309. /* RAW client writer in phase CURL_CW_RAW that
  310. * enabled tracing of raw data. */
  311. static CURLcode cw_raw_write(struct Curl_easy *data,
  312. struct Curl_cwriter *writer, int type,
  313. const char *buf, size_t nbytes)
  314. {
  315. if(type & CLIENTWRITE_BODY && data->set.verbose && !data->req.ignorebody) {
  316. Curl_debug(data, CURLINFO_DATA_IN, (char *)buf, nbytes);
  317. }
  318. return Curl_cwriter_write(data, writer->next, type, buf, nbytes);
  319. }
  320. static const struct Curl_cwtype cw_raw = {
  321. "raw",
  322. NULL,
  323. Curl_cwriter_def_init,
  324. cw_raw_write,
  325. Curl_cwriter_def_close,
  326. sizeof(struct Curl_cwriter)
  327. };
  328. /* Create an unencoding writer stage using the given handler. */
  329. CURLcode Curl_cwriter_create(struct Curl_cwriter **pwriter,
  330. struct Curl_easy *data,
  331. const struct Curl_cwtype *cwt,
  332. Curl_cwriter_phase phase)
  333. {
  334. struct Curl_cwriter *writer = NULL;
  335. CURLcode result = CURLE_OUT_OF_MEMORY;
  336. void *p;
  337. DEBUGASSERT(cwt->cwriter_size >= sizeof(struct Curl_cwriter));
  338. p = calloc(1, cwt->cwriter_size);
  339. if(!p)
  340. goto out;
  341. writer = (struct Curl_cwriter *)p;
  342. writer->cwt = cwt;
  343. writer->ctx = p;
  344. writer->phase = phase;
  345. result = cwt->do_init(data, writer);
  346. out:
  347. *pwriter = result? NULL : writer;
  348. if(result)
  349. free(writer);
  350. return result;
  351. }
  352. void Curl_cwriter_free(struct Curl_easy *data,
  353. struct Curl_cwriter *writer)
  354. {
  355. if(writer) {
  356. writer->cwt->do_close(data, writer);
  357. free(writer);
  358. }
  359. }
  360. size_t Curl_cwriter_count(struct Curl_easy *data, Curl_cwriter_phase phase)
  361. {
  362. struct Curl_cwriter *w;
  363. size_t n = 0;
  364. for(w = data->req.writer_stack; w; w = w->next) {
  365. if(w->phase == phase)
  366. ++n;
  367. }
  368. return n;
  369. }
  370. static CURLcode do_init_writer_stack(struct Curl_easy *data)
  371. {
  372. struct Curl_cwriter *writer;
  373. CURLcode result;
  374. DEBUGASSERT(!data->req.writer_stack);
  375. result = Curl_cwriter_create(&data->req.writer_stack,
  376. data, &Curl_cwt_out, CURL_CW_CLIENT);
  377. if(result)
  378. return result;
  379. result = Curl_cwriter_create(&writer, data, &cw_download, CURL_CW_PROTOCOL);
  380. if(result)
  381. return result;
  382. result = Curl_cwriter_add(data, writer);
  383. if(result) {
  384. Curl_cwriter_free(data, writer);
  385. }
  386. result = Curl_cwriter_create(&writer, data, &cw_raw, CURL_CW_RAW);
  387. if(result)
  388. return result;
  389. result = Curl_cwriter_add(data, writer);
  390. if(result) {
  391. Curl_cwriter_free(data, writer);
  392. }
  393. return result;
  394. }
  395. CURLcode Curl_cwriter_add(struct Curl_easy *data,
  396. struct Curl_cwriter *writer)
  397. {
  398. CURLcode result;
  399. struct Curl_cwriter **anchor = &data->req.writer_stack;
  400. if(!*anchor) {
  401. result = do_init_writer_stack(data);
  402. if(result)
  403. return result;
  404. }
  405. /* Insert the writer as first in its phase.
  406. * Skip existing writers of lower phases. */
  407. while(*anchor && (*anchor)->phase < writer->phase)
  408. anchor = &((*anchor)->next);
  409. writer->next = *anchor;
  410. *anchor = writer;
  411. return CURLE_OK;
  412. }
  413. struct Curl_cwriter *Curl_cwriter_get_by_name(struct Curl_easy *data,
  414. const char *name)
  415. {
  416. struct Curl_cwriter *writer;
  417. for(writer = data->req.writer_stack; writer; writer = writer->next) {
  418. if(!strcmp(name, writer->cwt->name))
  419. return writer;
  420. }
  421. return NULL;
  422. }
  423. struct Curl_cwriter *Curl_cwriter_get_by_type(struct Curl_easy *data,
  424. const struct Curl_cwtype *cwt)
  425. {
  426. struct Curl_cwriter *writer;
  427. for(writer = data->req.writer_stack; writer; writer = writer->next) {
  428. if(writer->cwt == cwt)
  429. return writer;
  430. }
  431. return NULL;
  432. }
  433. void Curl_cwriter_remove_by_name(struct Curl_easy *data,
  434. const char *name)
  435. {
  436. struct Curl_cwriter **anchor = &data->req.writer_stack;
  437. while(*anchor) {
  438. if(!strcmp(name, (*anchor)->cwt->name)) {
  439. struct Curl_cwriter *w = (*anchor);
  440. *anchor = w->next;
  441. Curl_cwriter_free(data, w);
  442. continue;
  443. }
  444. anchor = &((*anchor)->next);
  445. }
  446. }
  447. bool Curl_cwriter_is_paused(struct Curl_easy *data)
  448. {
  449. return Curl_cw_out_is_paused(data);
  450. }
  451. CURLcode Curl_cwriter_unpause(struct Curl_easy *data)
  452. {
  453. return Curl_cw_out_unpause(data);
  454. }
  455. CURLcode Curl_creader_read(struct Curl_easy *data,
  456. struct Curl_creader *reader,
  457. char *buf, size_t blen, size_t *nread, bool *eos)
  458. {
  459. *nread = 0;
  460. *eos = FALSE;
  461. if(!reader)
  462. return CURLE_READ_ERROR;
  463. return reader->crt->do_read(data, reader, buf, blen, nread, eos);
  464. }
  465. CURLcode Curl_creader_def_init(struct Curl_easy *data,
  466. struct Curl_creader *reader)
  467. {
  468. (void)data;
  469. (void)reader;
  470. return CURLE_OK;
  471. }
  472. void Curl_creader_def_close(struct Curl_easy *data,
  473. struct Curl_creader *reader)
  474. {
  475. (void)data;
  476. (void)reader;
  477. }
  478. CURLcode Curl_creader_def_read(struct Curl_easy *data,
  479. struct Curl_creader *reader,
  480. char *buf, size_t blen,
  481. size_t *nread, bool *eos)
  482. {
  483. if(reader->next)
  484. return reader->next->crt->do_read(data, reader->next, buf, blen,
  485. nread, eos);
  486. else {
  487. *nread = 0;
  488. *eos = FALSE;
  489. return CURLE_READ_ERROR;
  490. }
  491. }
  492. bool Curl_creader_def_needs_rewind(struct Curl_easy *data,
  493. struct Curl_creader *reader)
  494. {
  495. (void)data;
  496. (void)reader;
  497. return FALSE;
  498. }
  499. curl_off_t Curl_creader_def_total_length(struct Curl_easy *data,
  500. struct Curl_creader *reader)
  501. {
  502. return reader->next?
  503. reader->next->crt->total_length(data, reader->next) : -1;
  504. }
  505. CURLcode Curl_creader_def_resume_from(struct Curl_easy *data,
  506. struct Curl_creader *reader,
  507. curl_off_t offset)
  508. {
  509. (void)data;
  510. (void)reader;
  511. (void)offset;
  512. return CURLE_READ_ERROR;
  513. }
  514. CURLcode Curl_creader_def_rewind(struct Curl_easy *data,
  515. struct Curl_creader *reader)
  516. {
  517. (void)data;
  518. (void)reader;
  519. return CURLE_OK;
  520. }
  521. CURLcode Curl_creader_def_unpause(struct Curl_easy *data,
  522. struct Curl_creader *reader)
  523. {
  524. (void)data;
  525. (void)reader;
  526. return CURLE_OK;
  527. }
  528. void Curl_creader_def_done(struct Curl_easy *data,
  529. struct Curl_creader *reader, int premature)
  530. {
  531. (void)data;
  532. (void)reader;
  533. (void)premature;
  534. }
  535. struct cr_in_ctx {
  536. struct Curl_creader super;
  537. curl_read_callback read_cb;
  538. void *cb_user_data;
  539. curl_off_t total_len;
  540. curl_off_t read_len;
  541. CURLcode error_result;
  542. BIT(seen_eos);
  543. BIT(errored);
  544. BIT(has_used_cb);
  545. };
  546. static CURLcode cr_in_init(struct Curl_easy *data, struct Curl_creader *reader)
  547. {
  548. struct cr_in_ctx *ctx = reader->ctx;
  549. (void)data;
  550. ctx->read_cb = data->state.fread_func;
  551. ctx->cb_user_data = data->state.in;
  552. ctx->total_len = -1;
  553. ctx->read_len = 0;
  554. return CURLE_OK;
  555. }
  556. /* Real client reader to installed client callbacks. */
  557. static CURLcode cr_in_read(struct Curl_easy *data,
  558. struct Curl_creader *reader,
  559. char *buf, size_t blen,
  560. size_t *pnread, bool *peos)
  561. {
  562. struct cr_in_ctx *ctx = reader->ctx;
  563. size_t nread;
  564. /* Once we have errored, we will return the same error forever */
  565. if(ctx->errored) {
  566. *pnread = 0;
  567. *peos = FALSE;
  568. return ctx->error_result;
  569. }
  570. if(ctx->seen_eos) {
  571. *pnread = 0;
  572. *peos = TRUE;
  573. return CURLE_OK;
  574. }
  575. /* respect length limitations */
  576. if(ctx->total_len >= 0) {
  577. curl_off_t remain = ctx->total_len - ctx->read_len;
  578. if(remain <= 0)
  579. blen = 0;
  580. else if(remain < (curl_off_t)blen)
  581. blen = (size_t)remain;
  582. }
  583. nread = 0;
  584. if(ctx->read_cb && blen) {
  585. Curl_set_in_callback(data, true);
  586. nread = ctx->read_cb(buf, 1, blen, ctx->cb_user_data);
  587. Curl_set_in_callback(data, false);
  588. ctx->has_used_cb = TRUE;
  589. }
  590. switch(nread) {
  591. case 0:
  592. if((ctx->total_len >= 0) && (ctx->read_len < ctx->total_len)) {
  593. failf(data, "client read function EOF fail, only "
  594. "only %"CURL_FORMAT_CURL_OFF_T"/%"CURL_FORMAT_CURL_OFF_T
  595. " of needed bytes read", ctx->read_len, ctx->total_len);
  596. return CURLE_READ_ERROR;
  597. }
  598. *pnread = 0;
  599. *peos = TRUE;
  600. ctx->seen_eos = TRUE;
  601. break;
  602. case CURL_READFUNC_ABORT:
  603. failf(data, "operation aborted by callback");
  604. *pnread = 0;
  605. *peos = FALSE;
  606. ctx->errored = TRUE;
  607. ctx->error_result = CURLE_ABORTED_BY_CALLBACK;
  608. return CURLE_ABORTED_BY_CALLBACK;
  609. case CURL_READFUNC_PAUSE:
  610. if(data->conn->handler->flags & PROTOPT_NONETWORK) {
  611. /* protocols that work without network cannot be paused. This is
  612. actually only FILE:// just now, and it can't pause since the transfer
  613. isn't done using the "normal" procedure. */
  614. failf(data, "Read callback asked for PAUSE when not supported");
  615. return CURLE_READ_ERROR;
  616. }
  617. /* CURL_READFUNC_PAUSE pauses read callbacks that feed socket writes */
  618. data->req.keepon |= KEEP_SEND_PAUSE; /* mark socket send as paused */
  619. *pnread = 0;
  620. *peos = FALSE;
  621. break; /* nothing was read */
  622. default:
  623. if(nread > blen) {
  624. /* the read function returned a too large value */
  625. failf(data, "read function returned funny value");
  626. *pnread = 0;
  627. *peos = FALSE;
  628. ctx->errored = TRUE;
  629. ctx->error_result = CURLE_READ_ERROR;
  630. return CURLE_READ_ERROR;
  631. }
  632. ctx->read_len += nread;
  633. if(ctx->total_len >= 0)
  634. ctx->seen_eos = (ctx->read_len >= ctx->total_len);
  635. *pnread = nread;
  636. *peos = ctx->seen_eos;
  637. break;
  638. }
  639. CURL_TRC_READ(data, "cr_in_read(len=%zu, total=%"CURL_FORMAT_CURL_OFF_T
  640. ", read=%"CURL_FORMAT_CURL_OFF_T") -> %d, nread=%zu, eos=%d",
  641. blen, ctx->total_len, ctx->read_len, CURLE_OK,
  642. *pnread, *peos);
  643. return CURLE_OK;
  644. }
  645. static bool cr_in_needs_rewind(struct Curl_easy *data,
  646. struct Curl_creader *reader)
  647. {
  648. struct cr_in_ctx *ctx = reader->ctx;
  649. (void)data;
  650. return ctx->has_used_cb;
  651. }
  652. static curl_off_t cr_in_total_length(struct Curl_easy *data,
  653. struct Curl_creader *reader)
  654. {
  655. struct cr_in_ctx *ctx = reader->ctx;
  656. (void)data;
  657. return ctx->total_len;
  658. }
  659. static CURLcode cr_in_resume_from(struct Curl_easy *data,
  660. struct Curl_creader *reader,
  661. curl_off_t offset)
  662. {
  663. struct cr_in_ctx *ctx = reader->ctx;
  664. int seekerr = CURL_SEEKFUNC_CANTSEEK;
  665. DEBUGASSERT(data->conn);
  666. /* already started reading? */
  667. if(ctx->read_len)
  668. return CURLE_READ_ERROR;
  669. if(data->set.seek_func) {
  670. Curl_set_in_callback(data, true);
  671. seekerr = data->set.seek_func(data->set.seek_client, offset, SEEK_SET);
  672. Curl_set_in_callback(data, false);
  673. }
  674. if(seekerr != CURL_SEEKFUNC_OK) {
  675. curl_off_t passed = 0;
  676. if(seekerr != CURL_SEEKFUNC_CANTSEEK) {
  677. failf(data, "Could not seek stream");
  678. return CURLE_READ_ERROR;
  679. }
  680. /* when seekerr == CURL_SEEKFUNC_CANTSEEK (can't seek to offset) */
  681. do {
  682. char scratch[4*1024];
  683. size_t readthisamountnow =
  684. (offset - passed > (curl_off_t)sizeof(scratch)) ?
  685. sizeof(scratch) :
  686. curlx_sotouz(offset - passed);
  687. size_t actuallyread;
  688. Curl_set_in_callback(data, true);
  689. actuallyread = ctx->read_cb(scratch, 1, readthisamountnow,
  690. ctx->cb_user_data);
  691. Curl_set_in_callback(data, false);
  692. passed += actuallyread;
  693. if((actuallyread == 0) || (actuallyread > readthisamountnow)) {
  694. /* this checks for greater-than only to make sure that the
  695. CURL_READFUNC_ABORT return code still aborts */
  696. failf(data, "Could only read %" CURL_FORMAT_CURL_OFF_T
  697. " bytes from the input", passed);
  698. return CURLE_READ_ERROR;
  699. }
  700. } while(passed < offset);
  701. }
  702. /* now, decrease the size of the read */
  703. if(ctx->total_len > 0) {
  704. ctx->total_len -= offset;
  705. if(ctx->total_len <= 0) {
  706. failf(data, "File already completely uploaded");
  707. return CURLE_PARTIAL_FILE;
  708. }
  709. }
  710. /* we've passed, proceed as normal */
  711. return CURLE_OK;
  712. }
  713. static CURLcode cr_in_rewind(struct Curl_easy *data,
  714. struct Curl_creader *reader)
  715. {
  716. struct cr_in_ctx *ctx = reader->ctx;
  717. /* If we never invoked the callback, there is noting to rewind */
  718. if(!ctx->has_used_cb)
  719. return CURLE_OK;
  720. if(data->set.seek_func) {
  721. int err;
  722. Curl_set_in_callback(data, true);
  723. err = (data->set.seek_func)(data->set.seek_client, 0, SEEK_SET);
  724. Curl_set_in_callback(data, false);
  725. CURL_TRC_READ(data, "cr_in, rewind via set.seek_func -> %d", err);
  726. if(err) {
  727. failf(data, "seek callback returned error %d", (int)err);
  728. return CURLE_SEND_FAIL_REWIND;
  729. }
  730. }
  731. else if(data->set.ioctl_func) {
  732. curlioerr err;
  733. Curl_set_in_callback(data, true);
  734. err = (data->set.ioctl_func)(data, CURLIOCMD_RESTARTREAD,
  735. data->set.ioctl_client);
  736. Curl_set_in_callback(data, false);
  737. CURL_TRC_READ(data, "cr_in, rewind via set.ioctl_func -> %d", (int)err);
  738. if(err) {
  739. failf(data, "ioctl callback returned error %d", (int)err);
  740. return CURLE_SEND_FAIL_REWIND;
  741. }
  742. }
  743. else {
  744. /* If no CURLOPT_READFUNCTION is used, we know that we operate on a
  745. given FILE * stream and we can actually attempt to rewind that
  746. ourselves with fseek() */
  747. if(data->state.fread_func == (curl_read_callback)fread) {
  748. int err = fseek(data->state.in, 0, SEEK_SET);
  749. CURL_TRC_READ(data, "cr_in, rewind via fseek -> %d(%d)",
  750. (int)err, (int)errno);
  751. if(-1 != err)
  752. /* successful rewind */
  753. return CURLE_OK;
  754. }
  755. /* no callback set or failure above, makes us fail at once */
  756. failf(data, "necessary data rewind wasn't possible");
  757. return CURLE_SEND_FAIL_REWIND;
  758. }
  759. return CURLE_OK;
  760. }
  761. static const struct Curl_crtype cr_in = {
  762. "cr-in",
  763. cr_in_init,
  764. cr_in_read,
  765. Curl_creader_def_close,
  766. cr_in_needs_rewind,
  767. cr_in_total_length,
  768. cr_in_resume_from,
  769. cr_in_rewind,
  770. Curl_creader_def_unpause,
  771. Curl_creader_def_done,
  772. sizeof(struct cr_in_ctx)
  773. };
  774. CURLcode Curl_creader_create(struct Curl_creader **preader,
  775. struct Curl_easy *data,
  776. const struct Curl_crtype *crt,
  777. Curl_creader_phase phase)
  778. {
  779. struct Curl_creader *reader = NULL;
  780. CURLcode result = CURLE_OUT_OF_MEMORY;
  781. void *p;
  782. DEBUGASSERT(crt->creader_size >= sizeof(struct Curl_creader));
  783. p = calloc(1, crt->creader_size);
  784. if(!p)
  785. goto out;
  786. reader = (struct Curl_creader *)p;
  787. reader->crt = crt;
  788. reader->ctx = p;
  789. reader->phase = phase;
  790. result = crt->do_init(data, reader);
  791. out:
  792. *preader = result? NULL : reader;
  793. if(result)
  794. free(reader);
  795. return result;
  796. }
  797. void Curl_creader_free(struct Curl_easy *data, struct Curl_creader *reader)
  798. {
  799. if(reader) {
  800. reader->crt->do_close(data, reader);
  801. free(reader);
  802. }
  803. }
  804. struct cr_lc_ctx {
  805. struct Curl_creader super;
  806. struct bufq buf;
  807. BIT(read_eos); /* we read an EOS from the next reader */
  808. BIT(eos); /* we have returned an EOS */
  809. };
  810. static CURLcode cr_lc_init(struct Curl_easy *data, struct Curl_creader *reader)
  811. {
  812. struct cr_lc_ctx *ctx = reader->ctx;
  813. (void)data;
  814. Curl_bufq_init2(&ctx->buf, (16 * 1024), 1, BUFQ_OPT_SOFT_LIMIT);
  815. return CURLE_OK;
  816. }
  817. static void cr_lc_close(struct Curl_easy *data, struct Curl_creader *reader)
  818. {
  819. struct cr_lc_ctx *ctx = reader->ctx;
  820. (void)data;
  821. Curl_bufq_free(&ctx->buf);
  822. }
  823. /* client reader doing line end conversions. */
  824. static CURLcode cr_lc_read(struct Curl_easy *data,
  825. struct Curl_creader *reader,
  826. char *buf, size_t blen,
  827. size_t *pnread, bool *peos)
  828. {
  829. struct cr_lc_ctx *ctx = reader->ctx;
  830. CURLcode result;
  831. size_t nread, i, start, n;
  832. bool eos;
  833. if(ctx->eos) {
  834. *pnread = 0;
  835. *peos = TRUE;
  836. return CURLE_OK;
  837. }
  838. if(Curl_bufq_is_empty(&ctx->buf)) {
  839. if(ctx->read_eos) {
  840. ctx->eos = TRUE;
  841. *pnread = 0;
  842. *peos = TRUE;
  843. return CURLE_OK;
  844. }
  845. /* Still getting data form the next reader, ctx->buf is empty */
  846. result = Curl_creader_read(data, reader->next, buf, blen, &nread, &eos);
  847. if(result)
  848. return result;
  849. ctx->read_eos = eos;
  850. if(!nread || !memchr(buf, '\n', nread)) {
  851. /* nothing to convert, return this right away */
  852. if(ctx->read_eos)
  853. ctx->eos = TRUE;
  854. *pnread = nread;
  855. *peos = ctx->eos;
  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. }