sendf.c 39 KB

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