sendf.c 22 KB

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