sendf.c 35 KB

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