sendf.c 38 KB

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