sendf.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  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 "vtls/vtls.h"
  40. #include "vssh/ssh.h"
  41. #include "easyif.h"
  42. #include "multiif.h"
  43. #include "strerror.h"
  44. #include "select.h"
  45. #include "strdup.h"
  46. #include "http2.h"
  47. #include "headers.h"
  48. #include "progress.h"
  49. #include "ws.h"
  50. /* The last 3 #include files should be in this order */
  51. #include "curl_printf.h"
  52. #include "curl_memory.h"
  53. #include "memdebug.h"
  54. static CURLcode do_init_stack(struct Curl_easy *data);
  55. #if defined(CURL_DO_LINEEND_CONV) && !defined(CURL_DISABLE_FTP)
  56. /*
  57. * convert_lineends() changes CRLF (\r\n) end-of-line markers to a single LF
  58. * (\n), with special processing for CRLF sequences that are split between two
  59. * blocks of data. Remaining, bare CRs are changed to LFs. The possibly new
  60. * size of the data is returned.
  61. */
  62. static size_t convert_lineends(struct Curl_easy *data,
  63. char *startPtr, size_t size)
  64. {
  65. char *inPtr, *outPtr;
  66. /* sanity check */
  67. if(!startPtr || (size < 1)) {
  68. return size;
  69. }
  70. if(data->state.prev_block_had_trailing_cr) {
  71. /* The previous block of incoming data
  72. had a trailing CR, which was turned into a LF. */
  73. if(*startPtr == '\n') {
  74. /* This block of incoming data starts with the
  75. previous block's LF so get rid of it */
  76. memmove(startPtr, startPtr + 1, size-1);
  77. size--;
  78. /* and it wasn't a bare CR but a CRLF conversion instead */
  79. data->state.crlf_conversions++;
  80. }
  81. data->state.prev_block_had_trailing_cr = FALSE; /* reset the flag */
  82. }
  83. /* find 1st CR, if any */
  84. inPtr = outPtr = memchr(startPtr, '\r', size);
  85. if(inPtr) {
  86. /* at least one CR, now look for CRLF */
  87. while(inPtr < (startPtr + size-1)) {
  88. /* note that it's size-1, so we'll never look past the last byte */
  89. if(memcmp(inPtr, "\r\n", 2) == 0) {
  90. /* CRLF found, bump past the CR and copy the NL */
  91. inPtr++;
  92. *outPtr = *inPtr;
  93. /* keep track of how many CRLFs we converted */
  94. data->state.crlf_conversions++;
  95. }
  96. else {
  97. if(*inPtr == '\r') {
  98. /* lone CR, move LF instead */
  99. *outPtr = '\n';
  100. }
  101. else {
  102. /* not a CRLF nor a CR, just copy whatever it is */
  103. *outPtr = *inPtr;
  104. }
  105. }
  106. outPtr++;
  107. inPtr++;
  108. } /* end of while loop */
  109. if(inPtr < startPtr + size) {
  110. /* handle last byte */
  111. if(*inPtr == '\r') {
  112. /* deal with a CR at the end of the buffer */
  113. *outPtr = '\n'; /* copy a NL instead */
  114. /* note that a CRLF might be split across two blocks */
  115. data->state.prev_block_had_trailing_cr = TRUE;
  116. }
  117. else {
  118. /* copy last byte */
  119. *outPtr = *inPtr;
  120. }
  121. outPtr++;
  122. }
  123. if(outPtr < startPtr + size)
  124. /* tidy up by null terminating the now shorter data */
  125. *outPtr = '\0';
  126. return (outPtr - startPtr);
  127. }
  128. return size;
  129. }
  130. #endif /* CURL_DO_LINEEND_CONV && !CURL_DISABLE_FTP */
  131. /*
  132. * Curl_nwrite() is an internal write function that sends data to the
  133. * server. Works with a socket index for the connection.
  134. *
  135. * If the write would block (CURLE_AGAIN), it returns CURLE_OK and
  136. * (*nwritten == 0). Otherwise we return regular CURLcode value.
  137. */
  138. CURLcode Curl_nwrite(struct Curl_easy *data,
  139. int sockindex,
  140. const void *buf,
  141. size_t blen,
  142. ssize_t *pnwritten)
  143. {
  144. ssize_t nwritten;
  145. CURLcode result = CURLE_OK;
  146. struct connectdata *conn;
  147. DEBUGASSERT(sockindex >= 0 && sockindex < 2);
  148. DEBUGASSERT(pnwritten);
  149. DEBUGASSERT(data);
  150. DEBUGASSERT(data->conn);
  151. conn = data->conn;
  152. #ifdef CURLDEBUG
  153. {
  154. /* Allow debug builds to override this logic to force short sends
  155. */
  156. char *p = getenv("CURL_SMALLSENDS");
  157. if(p) {
  158. size_t altsize = (size_t)strtoul(p, NULL, 10);
  159. if(altsize)
  160. blen = CURLMIN(blen, altsize);
  161. }
  162. }
  163. #endif
  164. nwritten = conn->send[sockindex](data, sockindex, buf, blen, &result);
  165. if(result == CURLE_AGAIN) {
  166. nwritten = 0;
  167. result = CURLE_OK;
  168. }
  169. else if(result) {
  170. nwritten = -1; /* make sure */
  171. }
  172. else {
  173. DEBUGASSERT(nwritten >= 0);
  174. }
  175. *pnwritten = nwritten;
  176. return result;
  177. }
  178. /*
  179. * Curl_write() is an internal write function that sends data to the
  180. * server. Works with plain sockets, SCP, SSL or kerberos.
  181. *
  182. * If the write would block (CURLE_AGAIN), we return CURLE_OK and
  183. * (*written == 0). Otherwise we return regular CURLcode value.
  184. */
  185. CURLcode Curl_write(struct Curl_easy *data,
  186. curl_socket_t sockfd,
  187. const void *mem,
  188. size_t len,
  189. ssize_t *written)
  190. {
  191. struct connectdata *conn;
  192. int num;
  193. DEBUGASSERT(data);
  194. DEBUGASSERT(data->conn);
  195. conn = data->conn;
  196. num = (sockfd != CURL_SOCKET_BAD && sockfd == conn->sock[SECONDARYSOCKET]);
  197. return Curl_nwrite(data, num, mem, len, written);
  198. }
  199. static CURLcode pausewrite(struct Curl_easy *data,
  200. int type, /* what type of data */
  201. bool paused_body,
  202. const char *ptr,
  203. size_t len)
  204. {
  205. /* signalled to pause sending on this connection, but since we have data
  206. we want to send we need to dup it to save a copy for when the sending
  207. is again enabled */
  208. struct SingleRequest *k = &data->req;
  209. struct UrlState *s = &data->state;
  210. unsigned int i;
  211. bool newtype = TRUE;
  212. Curl_conn_ev_data_pause(data, TRUE);
  213. if(s->tempcount) {
  214. for(i = 0; i< s->tempcount; i++) {
  215. if(s->tempwrite[i].type == type &&
  216. !!s->tempwrite[i].paused_body == !!paused_body) {
  217. /* data for this type exists */
  218. newtype = FALSE;
  219. break;
  220. }
  221. }
  222. DEBUGASSERT(i < 3);
  223. if(i >= 3)
  224. /* There are more types to store than what fits: very bad */
  225. return CURLE_OUT_OF_MEMORY;
  226. }
  227. else
  228. i = 0;
  229. if(newtype) {
  230. /* store this information in the state struct for later use */
  231. Curl_dyn_init(&s->tempwrite[i].b, DYN_PAUSE_BUFFER);
  232. s->tempwrite[i].type = type;
  233. s->tempwrite[i].paused_body = paused_body;
  234. s->tempcount++;
  235. }
  236. if(Curl_dyn_addn(&s->tempwrite[i].b, (unsigned char *)ptr, len))
  237. return CURLE_OUT_OF_MEMORY;
  238. /* mark the connection as RECV paused */
  239. k->keepon |= KEEP_RECV_PAUSE;
  240. return CURLE_OK;
  241. }
  242. /* chop_write() writes chunks of data not larger than CURL_MAX_WRITE_SIZE via
  243. * client write callback(s) and takes care of pause requests from the
  244. * callbacks.
  245. */
  246. static CURLcode chop_write(struct Curl_easy *data,
  247. int type,
  248. bool skip_body_write,
  249. char *optr,
  250. size_t olen)
  251. {
  252. struct connectdata *conn = data->conn;
  253. curl_write_callback writeheader = NULL;
  254. curl_write_callback writebody = NULL;
  255. char *ptr = optr;
  256. size_t len = olen;
  257. void *writebody_ptr = data->set.out;
  258. if(!len)
  259. return CURLE_OK;
  260. /* If reading is paused, append this data to the already held data for this
  261. type. */
  262. if(data->req.keepon & KEEP_RECV_PAUSE)
  263. return pausewrite(data, type, !skip_body_write, ptr, len);
  264. /* Determine the callback(s) to use. */
  265. if(!skip_body_write &&
  266. ((type & CLIENTWRITE_BODY) ||
  267. ((type & CLIENTWRITE_HEADER) && data->set.include_header))) {
  268. #ifdef USE_WEBSOCKETS
  269. if(conn->handler->protocol & (CURLPROTO_WS|CURLPROTO_WSS)) {
  270. writebody = Curl_ws_writecb;
  271. writebody_ptr = data;
  272. }
  273. else
  274. #endif
  275. writebody = data->set.fwrite_func;
  276. }
  277. if((type & (CLIENTWRITE_HEADER|CLIENTWRITE_INFO)) &&
  278. (data->set.fwrite_header || data->set.writeheader)) {
  279. /*
  280. * Write headers to the same callback or to the especially setup
  281. * header callback function (added after version 7.7.1).
  282. */
  283. writeheader =
  284. data->set.fwrite_header? data->set.fwrite_header: data->set.fwrite_func;
  285. }
  286. /* Chop data, write chunks. */
  287. while(len) {
  288. size_t chunklen = len <= CURL_MAX_WRITE_SIZE? len: CURL_MAX_WRITE_SIZE;
  289. if(writebody) {
  290. size_t wrote;
  291. Curl_set_in_callback(data, true);
  292. wrote = writebody(ptr, 1, chunklen, writebody_ptr);
  293. Curl_set_in_callback(data, false);
  294. if(CURL_WRITEFUNC_PAUSE == wrote) {
  295. if(conn->handler->flags & PROTOPT_NONETWORK) {
  296. /* Protocols that work without network cannot be paused. This is
  297. actually only FILE:// just now, and it can't pause since the
  298. transfer isn't done using the "normal" procedure. */
  299. failf(data, "Write callback asked for PAUSE when not supported");
  300. return CURLE_WRITE_ERROR;
  301. }
  302. return pausewrite(data, type, TRUE, ptr, len);
  303. }
  304. if(wrote != chunklen) {
  305. failf(data, "Failure writing output to destination");
  306. return CURLE_WRITE_ERROR;
  307. }
  308. }
  309. ptr += chunklen;
  310. len -= chunklen;
  311. }
  312. #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_HEADERS_API)
  313. /* HTTP header, but not status-line */
  314. if((conn->handler->protocol & PROTO_FAMILY_HTTP) &&
  315. (type & CLIENTWRITE_HEADER) && !(type & CLIENTWRITE_STATUS) ) {
  316. unsigned char htype = (unsigned char)
  317. (type & CLIENTWRITE_CONNECT ? CURLH_CONNECT :
  318. (type & CLIENTWRITE_1XX ? CURLH_1XX :
  319. (type & CLIENTWRITE_TRAILER ? CURLH_TRAILER :
  320. CURLH_HEADER)));
  321. CURLcode result = Curl_headers_push(data, optr, htype);
  322. if(result)
  323. return result;
  324. }
  325. #endif
  326. if(writeheader) {
  327. size_t wrote;
  328. Curl_set_in_callback(data, true);
  329. wrote = writeheader(optr, 1, olen, data->set.writeheader);
  330. Curl_set_in_callback(data, false);
  331. if(CURL_WRITEFUNC_PAUSE == wrote)
  332. return pausewrite(data, type, FALSE, optr, olen);
  333. if(wrote != olen) {
  334. failf(data, "Failed writing header");
  335. return CURLE_WRITE_ERROR;
  336. }
  337. }
  338. return CURLE_OK;
  339. }
  340. /* Curl_client_write() sends data to the write callback(s)
  341. The bit pattern defines to what "streams" to write to. Body and/or header.
  342. The defines are in sendf.h of course.
  343. If CURL_DO_LINEEND_CONV is enabled, data is converted IN PLACE to the
  344. local character encoding. This is a problem and should be changed in
  345. the future to leave the original data alone.
  346. */
  347. CURLcode Curl_client_write(struct Curl_easy *data,
  348. int type, char *buf, size_t blen)
  349. {
  350. CURLcode result;
  351. #if !defined(CURL_DISABLE_FTP) && defined(CURL_DO_LINEEND_CONV)
  352. /* FTP data may need conversion. */
  353. if((type & CLIENTWRITE_BODY) &&
  354. (data->conn->handler->protocol & PROTO_FAMILY_FTP) &&
  355. data->conn->proto.ftpc.transfertype == 'A') {
  356. /* convert end-of-line markers */
  357. blen = convert_lineends(data, buf, blen);
  358. }
  359. #endif
  360. /* it is one of those, at least */
  361. DEBUGASSERT(type & (CLIENTWRITE_BODY|CLIENTWRITE_HEADER|CLIENTWRITE_INFO));
  362. /* BODY is only BODY */
  363. DEBUGASSERT(!(type & CLIENTWRITE_BODY) || (type == CLIENTWRITE_BODY));
  364. /* INFO is only INFO */
  365. DEBUGASSERT(!(type & CLIENTWRITE_INFO) || (type == CLIENTWRITE_INFO));
  366. if(!data->req.writer_stack) {
  367. result = do_init_stack(data);
  368. if(result)
  369. return result;
  370. DEBUGASSERT(data->req.writer_stack);
  371. }
  372. return Curl_cwriter_write(data, data->req.writer_stack, type, buf, blen);
  373. }
  374. CURLcode Curl_client_unpause(struct Curl_easy *data)
  375. {
  376. CURLcode result = CURLE_OK;
  377. if(data->state.tempcount) {
  378. /* there are buffers for sending that can be delivered as the receive
  379. pausing is lifted! */
  380. unsigned int i;
  381. unsigned int count = data->state.tempcount;
  382. struct tempbuf writebuf[3]; /* there can only be three */
  383. /* copy the structs to allow for immediate re-pausing */
  384. for(i = 0; i < data->state.tempcount; i++) {
  385. writebuf[i] = data->state.tempwrite[i];
  386. Curl_dyn_init(&data->state.tempwrite[i].b, DYN_PAUSE_BUFFER);
  387. }
  388. data->state.tempcount = 0;
  389. for(i = 0; i < count; i++) {
  390. /* even if one function returns error, this loops through and frees
  391. all buffers */
  392. if(!result)
  393. result = chop_write(data, writebuf[i].type,
  394. !writebuf[i].paused_body,
  395. Curl_dyn_ptr(&writebuf[i].b),
  396. Curl_dyn_len(&writebuf[i].b));
  397. Curl_dyn_free(&writebuf[i].b);
  398. }
  399. }
  400. return result;
  401. }
  402. void Curl_client_cleanup(struct Curl_easy *data)
  403. {
  404. struct Curl_cwriter *writer = data->req.writer_stack;
  405. size_t i;
  406. while(writer) {
  407. data->req.writer_stack = writer->next;
  408. writer->cwt->do_close(data, writer);
  409. free(writer);
  410. writer = data->req.writer_stack;
  411. }
  412. for(i = 0; i < data->state.tempcount; i++) {
  413. Curl_dyn_free(&data->state.tempwrite[i].b);
  414. }
  415. data->state.tempcount = 0;
  416. data->req.bytecount = 0;
  417. data->req.headerline = 0;
  418. }
  419. /* Write data using an unencoding writer stack. "nbytes" is not
  420. allowed to be 0. */
  421. CURLcode Curl_cwriter_write(struct Curl_easy *data,
  422. struct Curl_cwriter *writer, int type,
  423. const char *buf, size_t nbytes)
  424. {
  425. if(!nbytes)
  426. return CURLE_OK;
  427. if(!writer)
  428. return CURLE_WRITE_ERROR;
  429. return writer->cwt->do_write(data, writer, type, buf, nbytes);
  430. }
  431. CURLcode Curl_cwriter_def_init(struct Curl_easy *data,
  432. struct Curl_cwriter *writer)
  433. {
  434. (void)data;
  435. (void)writer;
  436. return CURLE_OK;
  437. }
  438. CURLcode Curl_cwriter_def_write(struct Curl_easy *data,
  439. struct Curl_cwriter *writer, int type,
  440. const char *buf, size_t nbytes)
  441. {
  442. return Curl_cwriter_write(data, writer->next, type, buf, nbytes);
  443. }
  444. void Curl_cwriter_def_close(struct Curl_easy *data,
  445. struct Curl_cwriter *writer)
  446. {
  447. (void) data;
  448. (void) writer;
  449. }
  450. /* Real client writer to installed callbacks. */
  451. static CURLcode cw_client_write(struct Curl_easy *data,
  452. struct Curl_cwriter *writer, int type,
  453. const char *buf, size_t nbytes)
  454. {
  455. (void)writer;
  456. if(!nbytes)
  457. return CURLE_OK;
  458. return chop_write(data, type, FALSE, (char *)buf, nbytes);
  459. }
  460. static const struct Curl_cwtype cw_client = {
  461. "client",
  462. NULL,
  463. Curl_cwriter_def_init,
  464. cw_client_write,
  465. Curl_cwriter_def_close,
  466. sizeof(struct Curl_cwriter)
  467. };
  468. static size_t get_max_body_write_len(struct Curl_easy *data, curl_off_t limit)
  469. {
  470. if(limit != -1) {
  471. /* How much more are we allowed to write? */
  472. curl_off_t remain_diff;
  473. remain_diff = limit - data->req.bytecount;
  474. if(remain_diff < 0) {
  475. /* already written too much! */
  476. return 0;
  477. }
  478. #if SIZEOF_CURL_OFF_T > SIZEOF_SIZE_T
  479. else if(remain_diff > SSIZE_T_MAX) {
  480. return SIZE_T_MAX;
  481. }
  482. #endif
  483. else {
  484. return (size_t)remain_diff;
  485. }
  486. }
  487. return SIZE_T_MAX;
  488. }
  489. /* Download client writer in phase CURL_CW_PROTOCOL that
  490. * sees the "real" download body data. */
  491. static CURLcode cw_download_write(struct Curl_easy *data,
  492. struct Curl_cwriter *writer, int type,
  493. const char *buf, size_t nbytes)
  494. {
  495. CURLcode result;
  496. size_t nwrite, excess_len = 0;
  497. const char *excess_data = NULL;
  498. if(!(type & CLIENTWRITE_BODY)) {
  499. if((type & CLIENTWRITE_CONNECT) && data->set.suppress_connect_headers)
  500. return CURLE_OK;
  501. return Curl_cwriter_write(data, writer->next, type, buf, nbytes);
  502. }
  503. nwrite = nbytes;
  504. if(-1 != data->req.maxdownload) {
  505. size_t wmax = get_max_body_write_len(data, data->req.maxdownload);
  506. if(nwrite > wmax) {
  507. excess_len = nbytes - wmax;
  508. nwrite = wmax;
  509. excess_data = buf + nwrite;
  510. }
  511. if(nwrite == wmax) {
  512. data->req.download_done = TRUE;
  513. }
  514. }
  515. if(data->set.max_filesize) {
  516. size_t wmax = get_max_body_write_len(data, data->set.max_filesize);
  517. if(nwrite > wmax) {
  518. nwrite = wmax;
  519. }
  520. }
  521. data->req.bytecount += nwrite;
  522. ++data->req.bodywrites;
  523. if(!data->req.ignorebody && nwrite) {
  524. result = Curl_cwriter_write(data, writer->next, type, buf, nwrite);
  525. if(result)
  526. return result;
  527. }
  528. result = Curl_pgrsSetDownloadCounter(data, data->req.bytecount);
  529. if(result)
  530. return result;
  531. if(excess_len) {
  532. if(data->conn->handler->readwrite) {
  533. /* RTSP hack moved from transfer loop to here */
  534. bool readmore = FALSE; /* indicates data is incomplete, need more */
  535. size_t consumed = 0;
  536. result = data->conn->handler->readwrite(data, data->conn,
  537. excess_data, excess_len,
  538. &consumed, &readmore);
  539. if(result)
  540. return result;
  541. DEBUGASSERT(consumed <= excess_len);
  542. excess_len -= consumed;
  543. if(readmore) {
  544. data->req.download_done = FALSE;
  545. data->req.keepon |= KEEP_RECV; /* we're not done reading */
  546. }
  547. }
  548. if(excess_len && !data->req.ignorebody) {
  549. infof(data,
  550. "Excess found writing body:"
  551. " excess = %zu"
  552. ", size = %" CURL_FORMAT_CURL_OFF_T
  553. ", maxdownload = %" CURL_FORMAT_CURL_OFF_T
  554. ", bytecount = %" CURL_FORMAT_CURL_OFF_T,
  555. excess_len, data->req.size, data->req.maxdownload,
  556. data->req.bytecount);
  557. connclose(data->conn, "excess found in a read");
  558. }
  559. }
  560. else if(nwrite < nbytes) {
  561. failf(data, "Exceeded the maximum allowed file size "
  562. "(%" CURL_FORMAT_CURL_OFF_T ") with %"
  563. CURL_FORMAT_CURL_OFF_T " bytes",
  564. data->set.max_filesize, data->req.bytecount);
  565. return CURLE_FILESIZE_EXCEEDED;
  566. }
  567. return CURLE_OK;
  568. }
  569. static const struct Curl_cwtype cw_download = {
  570. "download",
  571. NULL,
  572. Curl_cwriter_def_init,
  573. cw_download_write,
  574. Curl_cwriter_def_close,
  575. sizeof(struct Curl_cwriter)
  576. };
  577. /* RAW client writer in phase CURL_CW_RAW that
  578. * enabled tracing of raw data. */
  579. static CURLcode cw_raw_write(struct Curl_easy *data,
  580. struct Curl_cwriter *writer, int type,
  581. const char *buf, size_t nbytes)
  582. {
  583. if(type & CLIENTWRITE_BODY && data->set.verbose && !data->req.ignorebody) {
  584. Curl_debug(data, CURLINFO_DATA_IN, (char *)buf, nbytes);
  585. }
  586. return Curl_cwriter_write(data, writer->next, type, buf, nbytes);
  587. }
  588. static const struct Curl_cwtype cw_raw = {
  589. "raw",
  590. NULL,
  591. Curl_cwriter_def_init,
  592. cw_raw_write,
  593. Curl_cwriter_def_close,
  594. sizeof(struct Curl_cwriter)
  595. };
  596. /* Create an unencoding writer stage using the given handler. */
  597. CURLcode Curl_cwriter_create(struct Curl_cwriter **pwriter,
  598. struct Curl_easy *data,
  599. const struct Curl_cwtype *cwt,
  600. Curl_cwriter_phase phase)
  601. {
  602. struct Curl_cwriter *writer;
  603. CURLcode result = CURLE_OUT_OF_MEMORY;
  604. DEBUGASSERT(cwt->cwriter_size >= sizeof(struct Curl_cwriter));
  605. writer = (struct Curl_cwriter *) calloc(1, cwt->cwriter_size);
  606. if(!writer)
  607. goto out;
  608. writer->cwt = cwt;
  609. writer->phase = phase;
  610. result = cwt->do_init(data, writer);
  611. out:
  612. *pwriter = result? NULL : writer;
  613. if(result)
  614. free(writer);
  615. return result;
  616. }
  617. void Curl_cwriter_free(struct Curl_easy *data,
  618. struct Curl_cwriter *writer)
  619. {
  620. if(writer) {
  621. writer->cwt->do_close(data, writer);
  622. free(writer);
  623. }
  624. }
  625. size_t Curl_cwriter_count(struct Curl_easy *data, Curl_cwriter_phase phase)
  626. {
  627. struct Curl_cwriter *w;
  628. size_t n = 0;
  629. for(w = data->req.writer_stack; w; w = w->next) {
  630. if(w->phase == phase)
  631. ++n;
  632. }
  633. return n;
  634. }
  635. static CURLcode do_init_stack(struct Curl_easy *data)
  636. {
  637. struct Curl_cwriter *writer;
  638. CURLcode result;
  639. DEBUGASSERT(!data->req.writer_stack);
  640. result = Curl_cwriter_create(&data->req.writer_stack,
  641. data, &cw_client, CURL_CW_CLIENT);
  642. if(result)
  643. return result;
  644. result = Curl_cwriter_create(&writer, data, &cw_download, CURL_CW_PROTOCOL);
  645. if(result)
  646. return result;
  647. result = Curl_cwriter_add(data, writer);
  648. if(result) {
  649. Curl_cwriter_free(data, writer);
  650. }
  651. result = Curl_cwriter_create(&writer, data, &cw_raw, CURL_CW_RAW);
  652. if(result)
  653. return result;
  654. result = Curl_cwriter_add(data, writer);
  655. if(result) {
  656. Curl_cwriter_free(data, writer);
  657. }
  658. return result;
  659. }
  660. CURLcode Curl_cwriter_add(struct Curl_easy *data,
  661. struct Curl_cwriter *writer)
  662. {
  663. CURLcode result;
  664. struct Curl_cwriter **anchor = &data->req.writer_stack;
  665. if(!*anchor) {
  666. result = do_init_stack(data);
  667. if(result)
  668. return result;
  669. }
  670. /* Insert the writer as first in its phase.
  671. * Skip existing writers of lower phases. */
  672. while(*anchor && (*anchor)->phase < writer->phase)
  673. anchor = &((*anchor)->next);
  674. writer->next = *anchor;
  675. *anchor = writer;
  676. return CURLE_OK;
  677. }
  678. /*
  679. * Internal read-from-socket function. This is meant to deal with plain
  680. * sockets, SSL sockets and kerberos sockets.
  681. *
  682. * Returns a regular CURLcode value.
  683. */
  684. CURLcode Curl_read(struct Curl_easy *data, /* transfer */
  685. curl_socket_t sockfd, /* read from this socket */
  686. char *buf, /* store read data here */
  687. size_t sizerequested, /* max amount to read */
  688. ssize_t *n) /* amount bytes read */
  689. {
  690. CURLcode result = CURLE_RECV_ERROR;
  691. ssize_t nread = 0;
  692. size_t bytesfromsocket = 0;
  693. char *buffertofill = NULL;
  694. struct connectdata *conn = data->conn;
  695. /* Set 'num' to 0 or 1, depending on which socket that has been sent here.
  696. If it is the second socket, we set num to 1. Otherwise to 0. This lets
  697. us use the correct ssl handle. */
  698. int num = (sockfd == conn->sock[SECONDARYSOCKET]);
  699. *n = 0; /* reset amount to zero */
  700. bytesfromsocket = CURLMIN(sizerequested, (size_t)data->set.buffer_size);
  701. buffertofill = buf;
  702. nread = conn->recv[num](data, num, buffertofill, bytesfromsocket, &result);
  703. if(nread < 0)
  704. goto out;
  705. *n += nread;
  706. result = CURLE_OK;
  707. out:
  708. return result;
  709. }