sendf.c 22 KB

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