sendf.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2021, 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. ***************************************************************************/
  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 "non-ascii.h"
  40. #include "strerror.h"
  41. #include "select.h"
  42. #include "strdup.h"
  43. #include "http2.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 == NULL) || (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. void Curl_infof(struct Curl_easy *data, const char *fmt, ...)
  222. {
  223. if(data && data->set.verbose) {
  224. va_list ap;
  225. size_t len;
  226. char print_buffer[2048 + 1];
  227. va_start(ap, fmt);
  228. len = mvsnprintf(print_buffer, sizeof(print_buffer), fmt, ap);
  229. /*
  230. * Indicate truncation of the input by replacing the last 3 characters
  231. * with "...", and transfer the newline over in case the format had one.
  232. */
  233. if(len >= sizeof(print_buffer)) {
  234. len = strlen(fmt);
  235. if(fmt[--len] == '\n')
  236. msnprintf(print_buffer + (sizeof(print_buffer) - 5), 5, "...\n");
  237. else
  238. msnprintf(print_buffer + (sizeof(print_buffer) - 4), 4, "...");
  239. }
  240. va_end(ap);
  241. len = strlen(print_buffer);
  242. Curl_debug(data, CURLINFO_TEXT, print_buffer, len);
  243. }
  244. }
  245. /* Curl_failf() is for messages stating why we failed.
  246. * The message SHALL NOT include any LF or CR.
  247. */
  248. void Curl_failf(struct Curl_easy *data, const char *fmt, ...)
  249. {
  250. DEBUGASSERT(!strchr(fmt, '\n'));
  251. if(data->set.verbose || data->set.errorbuffer) {
  252. va_list ap;
  253. size_t len;
  254. char error[CURL_ERROR_SIZE + 2];
  255. va_start(ap, fmt);
  256. (void)mvsnprintf(error, CURL_ERROR_SIZE, fmt, ap);
  257. len = strlen(error);
  258. if(data->set.errorbuffer && !data->state.errorbuf) {
  259. strcpy(data->set.errorbuffer, error);
  260. data->state.errorbuf = TRUE; /* wrote error string */
  261. }
  262. error[len++] = '\n';
  263. Curl_debug(data, CURLINFO_TEXT, error, len);
  264. va_end(ap);
  265. }
  266. }
  267. /*
  268. * Curl_write() is an internal write function that sends data to the
  269. * server. Works with plain sockets, SCP, SSL or kerberos.
  270. *
  271. * If the write would block (CURLE_AGAIN), we return CURLE_OK and
  272. * (*written == 0). Otherwise we return regular CURLcode value.
  273. */
  274. CURLcode Curl_write(struct Curl_easy *data,
  275. curl_socket_t sockfd,
  276. const void *mem,
  277. size_t len,
  278. ssize_t *written)
  279. {
  280. ssize_t bytes_written;
  281. CURLcode result = CURLE_OK;
  282. struct connectdata *conn;
  283. int num;
  284. DEBUGASSERT(data);
  285. DEBUGASSERT(data->conn);
  286. conn = data->conn;
  287. num = (sockfd == conn->sock[SECONDARYSOCKET]);
  288. bytes_written = conn->send[num](data, num, mem, len, &result);
  289. *written = bytes_written;
  290. if(bytes_written >= 0)
  291. /* we completely ignore the curlcode value when subzero is not returned */
  292. return CURLE_OK;
  293. /* handle CURLE_AGAIN or a send failure */
  294. switch(result) {
  295. case CURLE_AGAIN:
  296. *written = 0;
  297. return CURLE_OK;
  298. case CURLE_OK:
  299. /* general send failure */
  300. return CURLE_SEND_ERROR;
  301. default:
  302. /* we got a specific curlcode, forward it */
  303. return result;
  304. }
  305. }
  306. ssize_t Curl_send_plain(struct Curl_easy *data, int num,
  307. const void *mem, size_t len, CURLcode *code)
  308. {
  309. struct connectdata *conn;
  310. curl_socket_t sockfd;
  311. ssize_t bytes_written;
  312. DEBUGASSERT(data);
  313. DEBUGASSERT(data->conn);
  314. conn = data->conn;
  315. sockfd = conn->sock[num];
  316. /* WinSock will destroy unread received data if send() is
  317. failed.
  318. To avoid lossage of received data, recv() must be
  319. performed before every send() if any incoming data is
  320. available. */
  321. if(pre_receive_plain(data, conn, num)) {
  322. *code = CURLE_OUT_OF_MEMORY;
  323. return -1;
  324. }
  325. #if defined(MSG_FASTOPEN) && !defined(TCP_FASTOPEN_CONNECT) /* Linux */
  326. if(conn->bits.tcp_fastopen) {
  327. bytes_written = sendto(sockfd, mem, len, MSG_FASTOPEN,
  328. conn->ip_addr->ai_addr, conn->ip_addr->ai_addrlen);
  329. conn->bits.tcp_fastopen = FALSE;
  330. }
  331. else
  332. #endif
  333. bytes_written = swrite(sockfd, mem, len);
  334. *code = CURLE_OK;
  335. if(-1 == bytes_written) {
  336. int err = SOCKERRNO;
  337. if(
  338. #ifdef WSAEWOULDBLOCK
  339. /* This is how Windows does it */
  340. (WSAEWOULDBLOCK == err)
  341. #else
  342. /* errno may be EWOULDBLOCK or on some systems EAGAIN when it returned
  343. due to its inability to send off data without blocking. We therefore
  344. treat both error codes the same here */
  345. (EWOULDBLOCK == err) || (EAGAIN == err) || (EINTR == err) ||
  346. (EINPROGRESS == err)
  347. #endif
  348. ) {
  349. /* this is just a case of EWOULDBLOCK */
  350. bytes_written = 0;
  351. *code = CURLE_AGAIN;
  352. }
  353. else {
  354. char buffer[STRERROR_LEN];
  355. failf(data, "Send failure: %s",
  356. Curl_strerror(err, buffer, sizeof(buffer)));
  357. data->state.os_errno = err;
  358. *code = CURLE_SEND_ERROR;
  359. }
  360. }
  361. return bytes_written;
  362. }
  363. /*
  364. * Curl_write_plain() is an internal write function that sends data to the
  365. * server using plain sockets only. Otherwise meant to have the exact same
  366. * proto as Curl_write()
  367. */
  368. CURLcode Curl_write_plain(struct Curl_easy *data,
  369. curl_socket_t sockfd,
  370. const void *mem,
  371. size_t len,
  372. ssize_t *written)
  373. {
  374. CURLcode result;
  375. struct connectdata *conn = data->conn;
  376. int num;
  377. DEBUGASSERT(conn);
  378. num = (sockfd == conn->sock[SECONDARYSOCKET]);
  379. *written = Curl_send_plain(data, num, mem, len, &result);
  380. return result;
  381. }
  382. ssize_t Curl_recv_plain(struct Curl_easy *data, int num, char *buf,
  383. size_t len, CURLcode *code)
  384. {
  385. struct connectdata *conn;
  386. curl_socket_t sockfd;
  387. ssize_t nread;
  388. DEBUGASSERT(data);
  389. DEBUGASSERT(data->conn);
  390. conn = data->conn;
  391. sockfd = conn->sock[num];
  392. /* Check and return data that already received and storied in internal
  393. intermediate buffer */
  394. nread = get_pre_recved(conn, num, buf, len);
  395. if(nread > 0) {
  396. *code = CURLE_OK;
  397. return nread;
  398. }
  399. nread = sread(sockfd, buf, len);
  400. *code = CURLE_OK;
  401. if(-1 == nread) {
  402. int err = SOCKERRNO;
  403. if(
  404. #ifdef WSAEWOULDBLOCK
  405. /* This is how Windows does it */
  406. (WSAEWOULDBLOCK == err)
  407. #else
  408. /* errno may be EWOULDBLOCK or on some systems EAGAIN when it returned
  409. due to its inability to send off data without blocking. We therefore
  410. treat both error codes the same here */
  411. (EWOULDBLOCK == err) || (EAGAIN == err) || (EINTR == err)
  412. #endif
  413. ) {
  414. /* this is just a case of EWOULDBLOCK */
  415. *code = CURLE_AGAIN;
  416. }
  417. else {
  418. char buffer[STRERROR_LEN];
  419. failf(data, "Recv failure: %s",
  420. Curl_strerror(err, buffer, sizeof(buffer)));
  421. data->state.os_errno = err;
  422. *code = CURLE_RECV_ERROR;
  423. }
  424. }
  425. return nread;
  426. }
  427. static CURLcode pausewrite(struct Curl_easy *data,
  428. int type, /* what type of data */
  429. const char *ptr,
  430. size_t len)
  431. {
  432. /* signalled to pause sending on this connection, but since we have data
  433. we want to send we need to dup it to save a copy for when the sending
  434. is again enabled */
  435. struct SingleRequest *k = &data->req;
  436. struct UrlState *s = &data->state;
  437. unsigned int i;
  438. bool newtype = TRUE;
  439. /* If this transfers over HTTP/2, pause the stream! */
  440. Curl_http2_stream_pause(data, TRUE);
  441. if(s->tempcount) {
  442. for(i = 0; i< s->tempcount; i++) {
  443. if(s->tempwrite[i].type == type) {
  444. /* data for this type exists */
  445. newtype = FALSE;
  446. break;
  447. }
  448. }
  449. DEBUGASSERT(i < 3);
  450. }
  451. else
  452. i = 0;
  453. if(newtype) {
  454. /* store this information in the state struct for later use */
  455. Curl_dyn_init(&s->tempwrite[i].b, DYN_PAUSE_BUFFER);
  456. s->tempwrite[i].type = type;
  457. if(newtype)
  458. s->tempcount++;
  459. }
  460. if(Curl_dyn_addn(&s->tempwrite[i].b, (unsigned char *)ptr, len))
  461. return CURLE_OUT_OF_MEMORY;
  462. /* mark the connection as RECV paused */
  463. k->keepon |= KEEP_RECV_PAUSE;
  464. return CURLE_OK;
  465. }
  466. /* chop_write() writes chunks of data not larger than CURL_MAX_WRITE_SIZE via
  467. * client write callback(s) and takes care of pause requests from the
  468. * callbacks.
  469. */
  470. static CURLcode chop_write(struct Curl_easy *data,
  471. int type,
  472. char *optr,
  473. size_t olen)
  474. {
  475. struct connectdata *conn = data->conn;
  476. curl_write_callback writeheader = NULL;
  477. curl_write_callback writebody = NULL;
  478. char *ptr = optr;
  479. size_t len = olen;
  480. if(!len)
  481. return CURLE_OK;
  482. /* If reading is paused, append this data to the already held data for this
  483. type. */
  484. if(data->req.keepon & KEEP_RECV_PAUSE)
  485. return pausewrite(data, type, ptr, len);
  486. /* Determine the callback(s) to use. */
  487. if(type & CLIENTWRITE_BODY)
  488. writebody = data->set.fwrite_func;
  489. if((type & CLIENTWRITE_HEADER) &&
  490. (data->set.fwrite_header || data->set.writeheader)) {
  491. /*
  492. * Write headers to the same callback or to the especially setup
  493. * header callback function (added after version 7.7.1).
  494. */
  495. writeheader =
  496. data->set.fwrite_header? data->set.fwrite_header: data->set.fwrite_func;
  497. }
  498. /* Chop data, write chunks. */
  499. while(len) {
  500. size_t chunklen = len <= CURL_MAX_WRITE_SIZE? len: CURL_MAX_WRITE_SIZE;
  501. if(writebody) {
  502. size_t wrote;
  503. Curl_set_in_callback(data, true);
  504. wrote = writebody(ptr, 1, chunklen, data->set.out);
  505. Curl_set_in_callback(data, false);
  506. if(CURL_WRITEFUNC_PAUSE == wrote) {
  507. if(conn->handler->flags & PROTOPT_NONETWORK) {
  508. /* Protocols that work without network cannot be paused. This is
  509. actually only FILE:// just now, and it can't pause since the
  510. transfer isn't done using the "normal" procedure. */
  511. failf(data, "Write callback asked for PAUSE when not supported!");
  512. return CURLE_WRITE_ERROR;
  513. }
  514. return pausewrite(data, type, ptr, len);
  515. }
  516. if(wrote != chunklen) {
  517. failf(data, "Failure writing output to destination");
  518. return CURLE_WRITE_ERROR;
  519. }
  520. }
  521. ptr += chunklen;
  522. len -= chunklen;
  523. }
  524. if(writeheader) {
  525. size_t wrote;
  526. ptr = optr;
  527. len = olen;
  528. Curl_set_in_callback(data, true);
  529. wrote = writeheader(ptr, 1, len, data->set.writeheader);
  530. Curl_set_in_callback(data, false);
  531. if(CURL_WRITEFUNC_PAUSE == wrote)
  532. /* here we pass in the HEADER bit only since if this was body as well
  533. then it was passed already and clearly that didn't trigger the
  534. pause, so this is saved for later with the HEADER bit only */
  535. return pausewrite(data, CLIENTWRITE_HEADER, ptr, len);
  536. if(wrote != len) {
  537. failf(data, "Failed writing header");
  538. return CURLE_WRITE_ERROR;
  539. }
  540. }
  541. return CURLE_OK;
  542. }
  543. /* Curl_client_write() sends data to the write callback(s)
  544. The bit pattern defines to what "streams" to write to. Body and/or header.
  545. The defines are in sendf.h of course.
  546. If CURL_DO_LINEEND_CONV is enabled, data is converted IN PLACE to the
  547. local character encoding. This is a problem and should be changed in
  548. the future to leave the original data alone.
  549. */
  550. CURLcode Curl_client_write(struct Curl_easy *data,
  551. int type,
  552. char *ptr,
  553. size_t len)
  554. {
  555. struct connectdata *conn = data->conn;
  556. if(0 == len)
  557. len = strlen(ptr);
  558. DEBUGASSERT(type <= 3);
  559. /* FTP data may need conversion. */
  560. if((type & CLIENTWRITE_BODY) &&
  561. (conn->handler->protocol & PROTO_FAMILY_FTP) &&
  562. conn->proto.ftpc.transfertype == 'A') {
  563. /* convert from the network encoding */
  564. CURLcode result = Curl_convert_from_network(data, ptr, len);
  565. /* Curl_convert_from_network calls failf if unsuccessful */
  566. if(result)
  567. return result;
  568. #ifdef CURL_DO_LINEEND_CONV
  569. /* convert end-of-line markers */
  570. len = convert_lineends(data, ptr, len);
  571. #endif /* CURL_DO_LINEEND_CONV */
  572. }
  573. return chop_write(data, type, ptr, len);
  574. }
  575. CURLcode Curl_read_plain(curl_socket_t sockfd,
  576. char *buf,
  577. size_t bytesfromsocket,
  578. ssize_t *n)
  579. {
  580. ssize_t nread = sread(sockfd, buf, bytesfromsocket);
  581. if(-1 == nread) {
  582. const int err = SOCKERRNO;
  583. const bool return_error =
  584. #ifdef USE_WINSOCK
  585. WSAEWOULDBLOCK == err
  586. #else
  587. EWOULDBLOCK == err || EAGAIN == err || EINTR == err
  588. #endif
  589. ;
  590. *n = 0; /* no data returned */
  591. if(return_error)
  592. return CURLE_AGAIN;
  593. return CURLE_RECV_ERROR;
  594. }
  595. *n = nread;
  596. return CURLE_OK;
  597. }
  598. /*
  599. * Internal read-from-socket function. This is meant to deal with plain
  600. * sockets, SSL sockets and kerberos sockets.
  601. *
  602. * Returns a regular CURLcode value.
  603. */
  604. CURLcode Curl_read(struct Curl_easy *data, /* transfer */
  605. curl_socket_t sockfd, /* read from this socket */
  606. char *buf, /* store read data here */
  607. size_t sizerequested, /* max amount to read */
  608. ssize_t *n) /* amount bytes read */
  609. {
  610. CURLcode result = CURLE_RECV_ERROR;
  611. ssize_t nread = 0;
  612. size_t bytesfromsocket = 0;
  613. char *buffertofill = NULL;
  614. struct connectdata *conn = data->conn;
  615. /* Set 'num' to 0 or 1, depending on which socket that has been sent here.
  616. If it is the second socket, we set num to 1. Otherwise to 0. This lets
  617. us use the correct ssl handle. */
  618. int num = (sockfd == conn->sock[SECONDARYSOCKET]);
  619. *n = 0; /* reset amount to zero */
  620. bytesfromsocket = CURLMIN(sizerequested, (size_t)data->set.buffer_size);
  621. buffertofill = buf;
  622. nread = conn->recv[num](data, num, buffertofill, bytesfromsocket, &result);
  623. if(nread < 0)
  624. return result;
  625. *n += nread;
  626. return CURLE_OK;
  627. }
  628. /* return 0 on success */
  629. int Curl_debug(struct Curl_easy *data, curl_infotype type,
  630. char *ptr, size_t size)
  631. {
  632. int rc = 0;
  633. if(data->set.verbose) {
  634. static const char s_infotype[CURLINFO_END][3] = {
  635. "* ", "< ", "> ", "{ ", "} ", "{ ", "} " };
  636. #ifdef CURL_DOES_CONVERSIONS
  637. char *buf = NULL;
  638. size_t conv_size = 0;
  639. switch(type) {
  640. case CURLINFO_HEADER_OUT:
  641. buf = Curl_memdup(ptr, size);
  642. if(!buf)
  643. return 1;
  644. conv_size = size;
  645. /* Special processing is needed for this block if it
  646. * contains both headers and data (separated by CRLFCRLF).
  647. * We want to convert just the headers, leaving the data as-is.
  648. */
  649. if(size > 4) {
  650. size_t i;
  651. for(i = 0; i < size-4; i++) {
  652. if(memcmp(&buf[i], "\x0d\x0a\x0d\x0a", 4) == 0) {
  653. /* convert everything through this CRLFCRLF but no further */
  654. conv_size = i + 4;
  655. break;
  656. }
  657. }
  658. }
  659. Curl_convert_from_network(data, buf, conv_size);
  660. /* Curl_convert_from_network calls failf if unsuccessful */
  661. /* we might as well continue even if it fails... */
  662. ptr = buf; /* switch pointer to use my buffer instead */
  663. break;
  664. default:
  665. /* leave everything else as-is */
  666. break;
  667. }
  668. #endif /* CURL_DOES_CONVERSIONS */
  669. if(data->set.fdebug) {
  670. Curl_set_in_callback(data, true);
  671. rc = (*data->set.fdebug)(data, type, ptr, size, data->set.debugdata);
  672. Curl_set_in_callback(data, false);
  673. }
  674. else {
  675. switch(type) {
  676. case CURLINFO_TEXT:
  677. case CURLINFO_HEADER_OUT:
  678. case CURLINFO_HEADER_IN:
  679. fwrite(s_infotype[type], 2, 1, data->set.err);
  680. fwrite(ptr, size, 1, data->set.err);
  681. #ifdef CURL_DOES_CONVERSIONS
  682. if(size != conv_size) {
  683. /* we had untranslated data so we need an explicit newline */
  684. fwrite("\n", 1, 1, data->set.err);
  685. }
  686. #endif
  687. break;
  688. default: /* nada */
  689. break;
  690. }
  691. }
  692. #ifdef CURL_DOES_CONVERSIONS
  693. free(buf);
  694. #endif
  695. }
  696. return rc;
  697. }