sendf.c 38 KB

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