sendf.c 37 KB

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