sendf.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, 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. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #ifdef HAVE_NETINET_IN_H
  24. #include <netinet/in.h>
  25. #endif
  26. #ifdef HAVE_LINUX_TCP_H
  27. #include <linux/tcp.h>
  28. #elif defined(HAVE_NETINET_TCP_H)
  29. #include <netinet/tcp.h>
  30. #endif
  31. #include <curl/curl.h>
  32. #include "urldata.h"
  33. #include "sendf.h"
  34. #include "connect.h"
  35. #include "vtls/vtls.h"
  36. #include "vssh/ssh.h"
  37. #include "easyif.h"
  38. #include "multiif.h"
  39. #include "strerror.h"
  40. #include "select.h"
  41. #include "strdup.h"
  42. #include "http2.h"
  43. #include "headers.h"
  44. /* The last 3 #include files should be in this order */
  45. #include "curl_printf.h"
  46. #include "curl_memory.h"
  47. #include "memdebug.h"
  48. #ifdef CURL_DO_LINEEND_CONV
  49. /*
  50. * convert_lineends() changes CRLF (\r\n) end-of-line markers to a single LF
  51. * (\n), with special processing for CRLF sequences that are split between two
  52. * blocks of data. Remaining, bare CRs are changed to LFs. The possibly new
  53. * size of the data is returned.
  54. */
  55. static size_t convert_lineends(struct Curl_easy *data,
  56. char *startPtr, size_t size)
  57. {
  58. char *inPtr, *outPtr;
  59. /* sanity check */
  60. if(!startPtr || (size < 1)) {
  61. return size;
  62. }
  63. if(data->state.prev_block_had_trailing_cr) {
  64. /* The previous block of incoming data
  65. had a trailing CR, which was turned into a LF. */
  66. if(*startPtr == '\n') {
  67. /* This block of incoming data starts with the
  68. previous block's LF so get rid of it */
  69. memmove(startPtr, startPtr + 1, size-1);
  70. size--;
  71. /* and it wasn't a bare CR but a CRLF conversion instead */
  72. data->state.crlf_conversions++;
  73. }
  74. data->state.prev_block_had_trailing_cr = FALSE; /* reset the flag */
  75. }
  76. /* find 1st CR, if any */
  77. inPtr = outPtr = memchr(startPtr, '\r', size);
  78. if(inPtr) {
  79. /* at least one CR, now look for CRLF */
  80. while(inPtr < (startPtr + size-1)) {
  81. /* note that it's size-1, so we'll never look past the last byte */
  82. if(memcmp(inPtr, "\r\n", 2) == 0) {
  83. /* CRLF found, bump past the CR and copy the NL */
  84. inPtr++;
  85. *outPtr = *inPtr;
  86. /* keep track of how many CRLFs we converted */
  87. data->state.crlf_conversions++;
  88. }
  89. else {
  90. if(*inPtr == '\r') {
  91. /* lone CR, move LF instead */
  92. *outPtr = '\n';
  93. }
  94. else {
  95. /* not a CRLF nor a CR, just copy whatever it is */
  96. *outPtr = *inPtr;
  97. }
  98. }
  99. outPtr++;
  100. inPtr++;
  101. } /* end of while loop */
  102. if(inPtr < startPtr + size) {
  103. /* handle last byte */
  104. if(*inPtr == '\r') {
  105. /* deal with a CR at the end of the buffer */
  106. *outPtr = '\n'; /* copy a NL instead */
  107. /* note that a CRLF might be split across two blocks */
  108. data->state.prev_block_had_trailing_cr = TRUE;
  109. }
  110. else {
  111. /* copy last byte */
  112. *outPtr = *inPtr;
  113. }
  114. outPtr++;
  115. }
  116. if(outPtr < startPtr + size)
  117. /* tidy up by null terminating the now shorter data */
  118. *outPtr = '\0';
  119. return (outPtr - startPtr);
  120. }
  121. return size;
  122. }
  123. #endif /* CURL_DO_LINEEND_CONV */
  124. #ifdef USE_RECV_BEFORE_SEND_WORKAROUND
  125. bool Curl_recv_has_postponed_data(struct connectdata *conn, int sockindex)
  126. {
  127. struct postponed_data * const psnd = &(conn->postponed[sockindex]);
  128. return psnd->buffer && psnd->allocated_size &&
  129. psnd->recv_size > psnd->recv_processed;
  130. }
  131. static CURLcode pre_receive_plain(struct Curl_easy *data,
  132. struct connectdata *conn, int num)
  133. {
  134. const curl_socket_t sockfd = conn->sock[num];
  135. struct postponed_data * const psnd = &(conn->postponed[num]);
  136. size_t bytestorecv = psnd->allocated_size - psnd->recv_size;
  137. /* WinSock will destroy unread received data if send() is
  138. failed.
  139. To avoid lossage of received data, recv() must be
  140. performed before every send() if any incoming data is
  141. available. However, skip this, if buffer is already full. */
  142. if((conn->handler->protocol&PROTO_FAMILY_HTTP) != 0 &&
  143. conn->recv[num] == Curl_recv_plain &&
  144. (!psnd->buffer || bytestorecv)) {
  145. const int readymask = Curl_socket_check(sockfd, CURL_SOCKET_BAD,
  146. CURL_SOCKET_BAD, 0);
  147. if(readymask != -1 && (readymask & CURL_CSELECT_IN) != 0) {
  148. /* Have some incoming data */
  149. if(!psnd->buffer) {
  150. /* Use buffer double default size for intermediate buffer */
  151. psnd->allocated_size = 2 * data->set.buffer_size;
  152. psnd->buffer = malloc(psnd->allocated_size);
  153. if(!psnd->buffer)
  154. return CURLE_OUT_OF_MEMORY;
  155. psnd->recv_size = 0;
  156. psnd->recv_processed = 0;
  157. #ifdef DEBUGBUILD
  158. psnd->bindsock = sockfd; /* Used only for DEBUGASSERT */
  159. #endif /* DEBUGBUILD */
  160. bytestorecv = psnd->allocated_size;
  161. }
  162. if(psnd->buffer) {
  163. ssize_t recvedbytes;
  164. DEBUGASSERT(psnd->bindsock == sockfd);
  165. recvedbytes = sread(sockfd, psnd->buffer + psnd->recv_size,
  166. bytestorecv);
  167. if(recvedbytes > 0)
  168. psnd->recv_size += recvedbytes;
  169. }
  170. else
  171. psnd->allocated_size = 0;
  172. }
  173. }
  174. return CURLE_OK;
  175. }
  176. static ssize_t get_pre_recved(struct connectdata *conn, int num, char *buf,
  177. size_t len)
  178. {
  179. struct postponed_data * const psnd = &(conn->postponed[num]);
  180. size_t copysize;
  181. if(!psnd->buffer)
  182. return 0;
  183. DEBUGASSERT(psnd->allocated_size > 0);
  184. DEBUGASSERT(psnd->recv_size <= psnd->allocated_size);
  185. DEBUGASSERT(psnd->recv_processed <= psnd->recv_size);
  186. /* Check and process data that already received and storied in internal
  187. intermediate buffer */
  188. if(psnd->recv_size > psnd->recv_processed) {
  189. DEBUGASSERT(psnd->bindsock == conn->sock[num]);
  190. copysize = CURLMIN(len, psnd->recv_size - psnd->recv_processed);
  191. memcpy(buf, psnd->buffer + psnd->recv_processed, copysize);
  192. psnd->recv_processed += copysize;
  193. }
  194. else
  195. copysize = 0; /* buffer was allocated, but nothing was received */
  196. /* Free intermediate buffer if it has no unprocessed data */
  197. if(psnd->recv_processed == psnd->recv_size) {
  198. free(psnd->buffer);
  199. psnd->buffer = NULL;
  200. psnd->allocated_size = 0;
  201. psnd->recv_size = 0;
  202. psnd->recv_processed = 0;
  203. #ifdef DEBUGBUILD
  204. psnd->bindsock = CURL_SOCKET_BAD;
  205. #endif /* DEBUGBUILD */
  206. }
  207. return (ssize_t)copysize;
  208. }
  209. #else /* ! USE_RECV_BEFORE_SEND_WORKAROUND */
  210. /* Use "do-nothing" macros instead of functions when workaround not used */
  211. bool Curl_recv_has_postponed_data(struct connectdata *conn, int sockindex)
  212. {
  213. (void)conn;
  214. (void)sockindex;
  215. return false;
  216. }
  217. #define pre_receive_plain(d,c,n) CURLE_OK
  218. #define get_pre_recved(c,n,b,l) 0
  219. #endif /* ! USE_RECV_BEFORE_SEND_WORKAROUND */
  220. /* Curl_infof() is for info message along the way */
  221. #define MAXINFO 2048
  222. void Curl_infof(struct Curl_easy *data, const char *fmt, ...)
  223. {
  224. DEBUGASSERT(!strchr(fmt, '\n'));
  225. if(data && data->set.verbose) {
  226. va_list ap;
  227. size_t len;
  228. char buffer[MAXINFO + 2];
  229. va_start(ap, fmt);
  230. len = mvsnprintf(buffer, MAXINFO, fmt, ap);
  231. va_end(ap);
  232. buffer[len++] = '\n';
  233. buffer[len] = '\0';
  234. Curl_debug(data, CURLINFO_TEXT, buffer, len);
  235. }
  236. }
  237. /* Curl_failf() is for messages stating why we failed.
  238. * The message SHALL NOT include any LF or CR.
  239. */
  240. void Curl_failf(struct Curl_easy *data, const char *fmt, ...)
  241. {
  242. DEBUGASSERT(!strchr(fmt, '\n'));
  243. if(data->set.verbose || data->set.errorbuffer) {
  244. va_list ap;
  245. size_t len;
  246. char error[CURL_ERROR_SIZE + 2];
  247. va_start(ap, fmt);
  248. len = mvsnprintf(error, CURL_ERROR_SIZE, fmt, ap);
  249. if(data->set.errorbuffer && !data->state.errorbuf) {
  250. strcpy(data->set.errorbuffer, error);
  251. data->state.errorbuf = TRUE; /* wrote error string */
  252. }
  253. error[len++] = '\n';
  254. error[len] = '\0';
  255. Curl_debug(data, CURLINFO_TEXT, error, len);
  256. va_end(ap);
  257. }
  258. }
  259. /*
  260. * Curl_write() is an internal write function that sends data to the
  261. * server. Works with plain sockets, SCP, SSL or kerberos.
  262. *
  263. * If the write would block (CURLE_AGAIN), we return CURLE_OK and
  264. * (*written == 0). Otherwise we return regular CURLcode value.
  265. */
  266. CURLcode Curl_write(struct Curl_easy *data,
  267. curl_socket_t sockfd,
  268. const void *mem,
  269. size_t len,
  270. ssize_t *written)
  271. {
  272. ssize_t bytes_written;
  273. CURLcode result = CURLE_OK;
  274. struct connectdata *conn;
  275. int num;
  276. DEBUGASSERT(data);
  277. DEBUGASSERT(data->conn);
  278. conn = data->conn;
  279. num = (sockfd == conn->sock[SECONDARYSOCKET]);
  280. #ifdef CURLDEBUG
  281. {
  282. /* Allow debug builds to override this logic to force short sends
  283. */
  284. char *p = getenv("CURL_SMALLSENDS");
  285. if(p) {
  286. size_t altsize = (size_t)strtoul(p, NULL, 10);
  287. if(altsize)
  288. len = CURLMIN(len, altsize);
  289. }
  290. }
  291. #endif
  292. bytes_written = conn->send[num](data, num, mem, len, &result);
  293. *written = bytes_written;
  294. if(bytes_written >= 0)
  295. /* we completely ignore the curlcode value when subzero is not returned */
  296. return CURLE_OK;
  297. /* handle CURLE_AGAIN or a send failure */
  298. switch(result) {
  299. case CURLE_AGAIN:
  300. *written = 0;
  301. return CURLE_OK;
  302. case CURLE_OK:
  303. /* general send failure */
  304. return CURLE_SEND_ERROR;
  305. default:
  306. /* we got a specific curlcode, forward it */
  307. return result;
  308. }
  309. }
  310. ssize_t Curl_send_plain(struct Curl_easy *data, int num,
  311. const void *mem, size_t len, CURLcode *code)
  312. {
  313. struct connectdata *conn;
  314. curl_socket_t sockfd;
  315. ssize_t bytes_written;
  316. DEBUGASSERT(data);
  317. DEBUGASSERT(data->conn);
  318. conn = data->conn;
  319. sockfd = conn->sock[num];
  320. /* WinSock will destroy unread received data if send() is
  321. failed.
  322. To avoid lossage of received data, recv() must be
  323. performed before every send() if any incoming data is
  324. available. */
  325. if(pre_receive_plain(data, conn, num)) {
  326. *code = CURLE_OUT_OF_MEMORY;
  327. return -1;
  328. }
  329. #if defined(MSG_FASTOPEN) && !defined(TCP_FASTOPEN_CONNECT) /* Linux */
  330. if(conn->bits.tcp_fastopen) {
  331. bytes_written = sendto(sockfd, mem, len, MSG_FASTOPEN,
  332. conn->ip_addr->ai_addr, conn->ip_addr->ai_addrlen);
  333. conn->bits.tcp_fastopen = FALSE;
  334. }
  335. else
  336. #endif
  337. bytes_written = swrite(sockfd, mem, len);
  338. *code = CURLE_OK;
  339. if(-1 == bytes_written) {
  340. int err = SOCKERRNO;
  341. if(
  342. #ifdef WSAEWOULDBLOCK
  343. /* This is how Windows does it */
  344. (WSAEWOULDBLOCK == err)
  345. #else
  346. /* errno may be EWOULDBLOCK or on some systems EAGAIN when it returned
  347. due to its inability to send off data without blocking. We therefore
  348. treat both error codes the same here */
  349. (EWOULDBLOCK == err) || (EAGAIN == err) || (EINTR == err) ||
  350. (EINPROGRESS == err)
  351. #endif
  352. ) {
  353. /* this is just a case of EWOULDBLOCK */
  354. bytes_written = 0;
  355. *code = CURLE_AGAIN;
  356. }
  357. else {
  358. char buffer[STRERROR_LEN];
  359. failf(data, "Send failure: %s",
  360. Curl_strerror(err, buffer, sizeof(buffer)));
  361. data->state.os_errno = err;
  362. *code = CURLE_SEND_ERROR;
  363. }
  364. }
  365. return bytes_written;
  366. }
  367. /*
  368. * Curl_write_plain() is an internal write function that sends data to the
  369. * server using plain sockets only. Otherwise meant to have the exact same
  370. * proto as Curl_write()
  371. */
  372. CURLcode Curl_write_plain(struct Curl_easy *data,
  373. curl_socket_t sockfd,
  374. const void *mem,
  375. size_t len,
  376. ssize_t *written)
  377. {
  378. CURLcode result;
  379. struct connectdata *conn = data->conn;
  380. int num;
  381. DEBUGASSERT(conn);
  382. num = (sockfd == conn->sock[SECONDARYSOCKET]);
  383. *written = Curl_send_plain(data, num, mem, len, &result);
  384. return result;
  385. }
  386. ssize_t Curl_recv_plain(struct Curl_easy *data, int num, char *buf,
  387. size_t len, CURLcode *code)
  388. {
  389. struct connectdata *conn;
  390. curl_socket_t sockfd;
  391. ssize_t nread;
  392. DEBUGASSERT(data);
  393. DEBUGASSERT(data->conn);
  394. conn = data->conn;
  395. sockfd = conn->sock[num];
  396. /* Check and return data that already received and storied in internal
  397. intermediate buffer */
  398. nread = get_pre_recved(conn, num, buf, len);
  399. if(nread > 0) {
  400. *code = CURLE_OK;
  401. return nread;
  402. }
  403. nread = sread(sockfd, buf, len);
  404. *code = CURLE_OK;
  405. if(-1 == nread) {
  406. int err = SOCKERRNO;
  407. if(
  408. #ifdef WSAEWOULDBLOCK
  409. /* This is how Windows does it */
  410. (WSAEWOULDBLOCK == err)
  411. #else
  412. /* errno may be EWOULDBLOCK or on some systems EAGAIN when it returned
  413. due to its inability to send off data without blocking. We therefore
  414. treat both error codes the same here */
  415. (EWOULDBLOCK == err) || (EAGAIN == err) || (EINTR == err)
  416. #endif
  417. ) {
  418. /* this is just a case of EWOULDBLOCK */
  419. *code = CURLE_AGAIN;
  420. }
  421. else {
  422. char buffer[STRERROR_LEN];
  423. failf(data, "Recv failure: %s",
  424. Curl_strerror(err, buffer, sizeof(buffer)));
  425. data->state.os_errno = err;
  426. *code = CURLE_RECV_ERROR;
  427. }
  428. }
  429. return nread;
  430. }
  431. static CURLcode pausewrite(struct Curl_easy *data,
  432. int type, /* what type of data */
  433. const char *ptr,
  434. size_t len)
  435. {
  436. /* signalled to pause sending on this connection, but since we have data
  437. we want to send we need to dup it to save a copy for when the sending
  438. is again enabled */
  439. struct SingleRequest *k = &data->req;
  440. struct UrlState *s = &data->state;
  441. unsigned int i;
  442. bool newtype = TRUE;
  443. /* If this transfers over HTTP/2, pause the stream! */
  444. Curl_http2_stream_pause(data, TRUE);
  445. if(s->tempcount) {
  446. for(i = 0; i< s->tempcount; i++) {
  447. if(s->tempwrite[i].type == type) {
  448. /* data for this type exists */
  449. newtype = FALSE;
  450. break;
  451. }
  452. }
  453. DEBUGASSERT(i < 3);
  454. }
  455. else
  456. i = 0;
  457. if(newtype) {
  458. /* store this information in the state struct for later use */
  459. Curl_dyn_init(&s->tempwrite[i].b, DYN_PAUSE_BUFFER);
  460. s->tempwrite[i].type = type;
  461. s->tempcount++;
  462. }
  463. if(Curl_dyn_addn(&s->tempwrite[i].b, (unsigned char *)ptr, len))
  464. return CURLE_OUT_OF_MEMORY;
  465. /* mark the connection as RECV paused */
  466. k->keepon |= KEEP_RECV_PAUSE;
  467. return CURLE_OK;
  468. }
  469. /* chop_write() writes chunks of data not larger than CURL_MAX_WRITE_SIZE via
  470. * client write callback(s) and takes care of pause requests from the
  471. * callbacks.
  472. */
  473. static CURLcode chop_write(struct Curl_easy *data,
  474. int type,
  475. char *optr,
  476. size_t olen)
  477. {
  478. struct connectdata *conn = data->conn;
  479. curl_write_callback writeheader = NULL;
  480. curl_write_callback writebody = NULL;
  481. char *ptr = optr;
  482. size_t len = olen;
  483. if(!len)
  484. return CURLE_OK;
  485. /* If reading is paused, append this data to the already held data for this
  486. type. */
  487. if(data->req.keepon & KEEP_RECV_PAUSE)
  488. return pausewrite(data, type, ptr, len);
  489. /* Determine the callback(s) to use. */
  490. if(type & CLIENTWRITE_BODY)
  491. writebody = data->set.fwrite_func;
  492. if((type & CLIENTWRITE_HEADER) &&
  493. (data->set.fwrite_header || data->set.writeheader)) {
  494. /*
  495. * Write headers to the same callback or to the especially setup
  496. * header callback function (added after version 7.7.1).
  497. */
  498. writeheader =
  499. data->set.fwrite_header? data->set.fwrite_header: data->set.fwrite_func;
  500. }
  501. /* Chop data, write chunks. */
  502. while(len) {
  503. size_t chunklen = len <= CURL_MAX_WRITE_SIZE? len: CURL_MAX_WRITE_SIZE;
  504. if(writebody) {
  505. size_t wrote;
  506. Curl_set_in_callback(data, true);
  507. wrote = writebody(ptr, 1, chunklen, data->set.out);
  508. Curl_set_in_callback(data, false);
  509. if(CURL_WRITEFUNC_PAUSE == wrote) {
  510. if(conn->handler->flags & PROTOPT_NONETWORK) {
  511. /* Protocols that work without network cannot be paused. This is
  512. actually only FILE:// just now, and it can't pause since the
  513. transfer isn't done using the "normal" procedure. */
  514. failf(data, "Write callback asked for PAUSE when not supported");
  515. return CURLE_WRITE_ERROR;
  516. }
  517. return pausewrite(data, type, ptr, len);
  518. }
  519. if(wrote != chunklen) {
  520. failf(data, "Failure writing output to destination");
  521. return CURLE_WRITE_ERROR;
  522. }
  523. }
  524. ptr += chunklen;
  525. len -= chunklen;
  526. }
  527. /* HTTP header, but not status-line */
  528. if((conn->handler->protocol & PROTO_FAMILY_HTTP) &&
  529. (type & CLIENTWRITE_HEADER) && !(type & CLIENTWRITE_STATUS) ) {
  530. CURLcode result =
  531. Curl_headers_push(data, optr,
  532. type & CLIENTWRITE_CONNECT ? CURLH_CONNECT :
  533. (type & CLIENTWRITE_1XX ? CURLH_1XX :
  534. (type & CLIENTWRITE_TRAILER ? CURLH_TRAILER :
  535. CURLH_HEADER)));
  536. if(result)
  537. return result;
  538. }
  539. if(writeheader) {
  540. size_t wrote;
  541. Curl_set_in_callback(data, true);
  542. wrote = writeheader(optr, 1, olen, data->set.writeheader);
  543. Curl_set_in_callback(data, false);
  544. if(CURL_WRITEFUNC_PAUSE == wrote)
  545. /* here we pass in the HEADER bit only since if this was body as well
  546. then it was passed already and clearly that didn't trigger the
  547. pause, so this is saved for later with the HEADER bit only */
  548. return pausewrite(data, CLIENTWRITE_HEADER, optr, olen);
  549. if(wrote != olen) {
  550. failf(data, "Failed writing header");
  551. return CURLE_WRITE_ERROR;
  552. }
  553. }
  554. return CURLE_OK;
  555. }
  556. /* Curl_client_write() sends data to the write callback(s)
  557. The bit pattern defines to what "streams" to write to. Body and/or header.
  558. The defines are in sendf.h of course.
  559. If CURL_DO_LINEEND_CONV is enabled, data is converted IN PLACE to the
  560. local character encoding. This is a problem and should be changed in
  561. the future to leave the original data alone.
  562. */
  563. CURLcode Curl_client_write(struct Curl_easy *data,
  564. int type,
  565. char *ptr,
  566. size_t len)
  567. {
  568. struct connectdata *conn = data->conn;
  569. if(!len)
  570. return CURLE_OK;
  571. /* FTP data may need conversion. */
  572. if((type & CLIENTWRITE_BODY) &&
  573. (conn->handler->protocol & PROTO_FAMILY_FTP) &&
  574. conn->proto.ftpc.transfertype == 'A') {
  575. #ifdef CURL_DO_LINEEND_CONV
  576. /* convert end-of-line markers */
  577. len = convert_lineends(data, ptr, len);
  578. #endif /* CURL_DO_LINEEND_CONV */
  579. }
  580. return chop_write(data, type, ptr, len);
  581. }
  582. CURLcode Curl_read_plain(curl_socket_t sockfd,
  583. char *buf,
  584. size_t bytesfromsocket,
  585. ssize_t *n)
  586. {
  587. ssize_t nread = sread(sockfd, buf, bytesfromsocket);
  588. if(-1 == nread) {
  589. const int err = SOCKERRNO;
  590. const bool return_error =
  591. #ifdef USE_WINSOCK
  592. WSAEWOULDBLOCK == err
  593. #else
  594. EWOULDBLOCK == err || EAGAIN == err || EINTR == err
  595. #endif
  596. ;
  597. *n = 0; /* no data returned */
  598. if(return_error)
  599. return CURLE_AGAIN;
  600. return CURLE_RECV_ERROR;
  601. }
  602. *n = nread;
  603. return CURLE_OK;
  604. }
  605. /*
  606. * Internal read-from-socket function. This is meant to deal with plain
  607. * sockets, SSL sockets and kerberos sockets.
  608. *
  609. * Returns a regular CURLcode value.
  610. */
  611. CURLcode Curl_read(struct Curl_easy *data, /* transfer */
  612. curl_socket_t sockfd, /* read from this socket */
  613. char *buf, /* store read data here */
  614. size_t sizerequested, /* max amount to read */
  615. ssize_t *n) /* amount bytes read */
  616. {
  617. CURLcode result = CURLE_RECV_ERROR;
  618. ssize_t nread = 0;
  619. size_t bytesfromsocket = 0;
  620. char *buffertofill = NULL;
  621. struct connectdata *conn = data->conn;
  622. /* Set 'num' to 0 or 1, depending on which socket that has been sent here.
  623. If it is the second socket, we set num to 1. Otherwise to 0. This lets
  624. us use the correct ssl handle. */
  625. int num = (sockfd == conn->sock[SECONDARYSOCKET]);
  626. *n = 0; /* reset amount to zero */
  627. bytesfromsocket = CURLMIN(sizerequested, (size_t)data->set.buffer_size);
  628. buffertofill = buf;
  629. nread = conn->recv[num](data, num, buffertofill, bytesfromsocket, &result);
  630. if(nread < 0)
  631. return result;
  632. *n += nread;
  633. return CURLE_OK;
  634. }
  635. /* return 0 on success */
  636. int Curl_debug(struct Curl_easy *data, curl_infotype type,
  637. char *ptr, size_t size)
  638. {
  639. int rc = 0;
  640. if(data->set.verbose) {
  641. static const char s_infotype[CURLINFO_END][3] = {
  642. "* ", "< ", "> ", "{ ", "} ", "{ ", "} " };
  643. if(data->set.fdebug) {
  644. Curl_set_in_callback(data, true);
  645. rc = (*data->set.fdebug)(data, type, ptr, size, data->set.debugdata);
  646. Curl_set_in_callback(data, false);
  647. }
  648. else {
  649. switch(type) {
  650. case CURLINFO_TEXT:
  651. case CURLINFO_HEADER_OUT:
  652. case CURLINFO_HEADER_IN:
  653. fwrite(s_infotype[type], 2, 1, data->set.err);
  654. fwrite(ptr, size, 1, data->set.err);
  655. break;
  656. default: /* nada */
  657. break;
  658. }
  659. }
  660. }
  661. return rc;
  662. }