sendf.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2017, 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.haxx.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. #include <curl/curl.h>
  24. #include "urldata.h"
  25. #include "sendf.h"
  26. #include "connect.h"
  27. #include "vtls/vtls.h"
  28. #include "ssh.h"
  29. #include "multiif.h"
  30. #include "non-ascii.h"
  31. #include "strerror.h"
  32. #include "select.h"
  33. #include "strdup.h"
  34. /* The last 3 #include files should be in this order */
  35. #include "curl_printf.h"
  36. #include "curl_memory.h"
  37. #include "memdebug.h"
  38. #ifdef CURL_DO_LINEEND_CONV
  39. /*
  40. * convert_lineends() changes CRLF (\r\n) end-of-line markers to a single LF
  41. * (\n), with special processing for CRLF sequences that are split between two
  42. * blocks of data. Remaining, bare CRs are changed to LFs. The possibly new
  43. * size of the data is returned.
  44. */
  45. static size_t convert_lineends(struct Curl_easy *data,
  46. char *startPtr, size_t size)
  47. {
  48. char *inPtr, *outPtr;
  49. /* sanity check */
  50. if((startPtr == NULL) || (size < 1)) {
  51. return size;
  52. }
  53. if(data->state.prev_block_had_trailing_cr) {
  54. /* The previous block of incoming data
  55. had a trailing CR, which was turned into a LF. */
  56. if(*startPtr == '\n') {
  57. /* This block of incoming data starts with the
  58. previous block's LF so get rid of it */
  59. memmove(startPtr, startPtr+1, size-1);
  60. size--;
  61. /* and it wasn't a bare CR but a CRLF conversion instead */
  62. data->state.crlf_conversions++;
  63. }
  64. data->state.prev_block_had_trailing_cr = FALSE; /* reset the flag */
  65. }
  66. /* find 1st CR, if any */
  67. inPtr = outPtr = memchr(startPtr, '\r', size);
  68. if(inPtr) {
  69. /* at least one CR, now look for CRLF */
  70. while(inPtr < (startPtr+size-1)) {
  71. /* note that it's size-1, so we'll never look past the last byte */
  72. if(memcmp(inPtr, "\r\n", 2) == 0) {
  73. /* CRLF found, bump past the CR and copy the NL */
  74. inPtr++;
  75. *outPtr = *inPtr;
  76. /* keep track of how many CRLFs we converted */
  77. data->state.crlf_conversions++;
  78. }
  79. else {
  80. if(*inPtr == '\r') {
  81. /* lone CR, move LF instead */
  82. *outPtr = '\n';
  83. }
  84. else {
  85. /* not a CRLF nor a CR, just copy whatever it is */
  86. *outPtr = *inPtr;
  87. }
  88. }
  89. outPtr++;
  90. inPtr++;
  91. } /* end of while loop */
  92. if(inPtr < startPtr+size) {
  93. /* handle last byte */
  94. if(*inPtr == '\r') {
  95. /* deal with a CR at the end of the buffer */
  96. *outPtr = '\n'; /* copy a NL instead */
  97. /* note that a CRLF might be split across two blocks */
  98. data->state.prev_block_had_trailing_cr = TRUE;
  99. }
  100. else {
  101. /* copy last byte */
  102. *outPtr = *inPtr;
  103. }
  104. outPtr++;
  105. }
  106. if(outPtr < startPtr+size)
  107. /* tidy up by null terminating the now shorter data */
  108. *outPtr = '\0';
  109. return (outPtr - startPtr);
  110. }
  111. return size;
  112. }
  113. #endif /* CURL_DO_LINEEND_CONV */
  114. #ifdef USE_RECV_BEFORE_SEND_WORKAROUND
  115. bool Curl_recv_has_postponed_data(struct connectdata *conn, int sockindex)
  116. {
  117. struct postponed_data * const psnd = &(conn->postponed[sockindex]);
  118. return psnd->buffer && psnd->allocated_size &&
  119. psnd->recv_size > psnd->recv_processed;
  120. }
  121. static void pre_receive_plain(struct connectdata *conn, int num)
  122. {
  123. const curl_socket_t sockfd = conn->sock[num];
  124. struct postponed_data * const psnd = &(conn->postponed[num]);
  125. size_t bytestorecv = psnd->allocated_size - psnd->recv_size;
  126. /* WinSock will destroy unread received data if send() is
  127. failed.
  128. To avoid lossage of received data, recv() must be
  129. performed before every send() if any incoming data is
  130. available. However, skip this, if buffer is already full. */
  131. if((conn->handler->protocol&PROTO_FAMILY_HTTP) != 0 &&
  132. conn->recv[num] == Curl_recv_plain &&
  133. (!psnd->buffer || bytestorecv)) {
  134. const int readymask = Curl_socket_check(sockfd, CURL_SOCKET_BAD,
  135. CURL_SOCKET_BAD, 0);
  136. if(readymask != -1 && (readymask & CURL_CSELECT_IN) != 0) {
  137. /* Have some incoming data */
  138. if(!psnd->buffer) {
  139. /* Use buffer double default size for intermediate buffer */
  140. psnd->allocated_size = 2 * conn->data->set.buffer_size;
  141. psnd->buffer = malloc(psnd->allocated_size);
  142. psnd->recv_size = 0;
  143. psnd->recv_processed = 0;
  144. #ifdef DEBUGBUILD
  145. psnd->bindsock = sockfd; /* Used only for DEBUGASSERT */
  146. #endif /* DEBUGBUILD */
  147. bytestorecv = psnd->allocated_size;
  148. }
  149. if(psnd->buffer) {
  150. ssize_t recvedbytes;
  151. DEBUGASSERT(psnd->bindsock == sockfd);
  152. recvedbytes = sread(sockfd, psnd->buffer + psnd->recv_size,
  153. bytestorecv);
  154. if(recvedbytes > 0)
  155. psnd->recv_size += recvedbytes;
  156. }
  157. else
  158. psnd->allocated_size = 0;
  159. }
  160. }
  161. }
  162. static ssize_t get_pre_recved(struct connectdata *conn, int num, char *buf,
  163. size_t len)
  164. {
  165. struct postponed_data * const psnd = &(conn->postponed[num]);
  166. size_t copysize;
  167. if(!psnd->buffer)
  168. return 0;
  169. DEBUGASSERT(psnd->allocated_size > 0);
  170. DEBUGASSERT(psnd->recv_size <= psnd->allocated_size);
  171. DEBUGASSERT(psnd->recv_processed <= psnd->recv_size);
  172. /* Check and process data that already received and storied in internal
  173. intermediate buffer */
  174. if(psnd->recv_size > psnd->recv_processed) {
  175. DEBUGASSERT(psnd->bindsock == conn->sock[num]);
  176. copysize = CURLMIN(len, psnd->recv_size - psnd->recv_processed);
  177. memcpy(buf, psnd->buffer + psnd->recv_processed, copysize);
  178. psnd->recv_processed += copysize;
  179. }
  180. else
  181. copysize = 0; /* buffer was allocated, but nothing was received */
  182. /* Free intermediate buffer if it has no unprocessed data */
  183. if(psnd->recv_processed == psnd->recv_size) {
  184. free(psnd->buffer);
  185. psnd->buffer = NULL;
  186. psnd->allocated_size = 0;
  187. psnd->recv_size = 0;
  188. psnd->recv_processed = 0;
  189. #ifdef DEBUGBUILD
  190. psnd->bindsock = CURL_SOCKET_BAD;
  191. #endif /* DEBUGBUILD */
  192. }
  193. return (ssize_t)copysize;
  194. }
  195. #else /* ! USE_RECV_BEFORE_SEND_WORKAROUND */
  196. /* Use "do-nothing" macros instead of functions when workaround not used */
  197. bool Curl_recv_has_postponed_data(struct connectdata *conn, int sockindex)
  198. {
  199. (void)conn;
  200. (void)sockindex;
  201. return false;
  202. }
  203. #define pre_receive_plain(c,n) do {} WHILE_FALSE
  204. #define get_pre_recved(c,n,b,l) 0
  205. #endif /* ! USE_RECV_BEFORE_SEND_WORKAROUND */
  206. /* Curl_infof() is for info message along the way */
  207. void Curl_infof(struct Curl_easy *data, const char *fmt, ...)
  208. {
  209. if(data && data->set.verbose) {
  210. va_list ap;
  211. size_t len;
  212. char print_buffer[2048 + 1];
  213. va_start(ap, fmt);
  214. vsnprintf(print_buffer, sizeof(print_buffer), fmt, ap);
  215. va_end(ap);
  216. len = strlen(print_buffer);
  217. Curl_debug(data, CURLINFO_TEXT, print_buffer, len, NULL);
  218. }
  219. }
  220. /* Curl_failf() is for messages stating why we failed.
  221. * The message SHALL NOT include any LF or CR.
  222. */
  223. void Curl_failf(struct Curl_easy *data, const char *fmt, ...)
  224. {
  225. va_list ap;
  226. size_t len;
  227. char error[CURL_ERROR_SIZE + 2];
  228. va_start(ap, fmt);
  229. vsnprintf(error, CURL_ERROR_SIZE, fmt, ap);
  230. len = strlen(error);
  231. if(data->set.errorbuffer && !data->state.errorbuf) {
  232. strcpy(data->set.errorbuffer, error);
  233. data->state.errorbuf = TRUE; /* wrote error string */
  234. }
  235. if(data->set.verbose) {
  236. error[len] = '\n';
  237. error[++len] = '\0';
  238. Curl_debug(data, CURLINFO_TEXT, error, len, NULL);
  239. }
  240. va_end(ap);
  241. }
  242. /* Curl_sendf() sends formatted data to the server */
  243. CURLcode Curl_sendf(curl_socket_t sockfd, struct connectdata *conn,
  244. const char *fmt, ...)
  245. {
  246. struct Curl_easy *data = conn->data;
  247. ssize_t bytes_written;
  248. size_t write_len;
  249. CURLcode result = CURLE_OK;
  250. char *s;
  251. char *sptr;
  252. va_list ap;
  253. va_start(ap, fmt);
  254. s = vaprintf(fmt, ap); /* returns an allocated string */
  255. va_end(ap);
  256. if(!s)
  257. return CURLE_OUT_OF_MEMORY; /* failure */
  258. bytes_written=0;
  259. write_len = strlen(s);
  260. sptr = s;
  261. for(;;) {
  262. /* Write the buffer to the socket */
  263. result = Curl_write(conn, sockfd, sptr, write_len, &bytes_written);
  264. if(result)
  265. break;
  266. if(data->set.verbose)
  267. Curl_debug(data, CURLINFO_DATA_OUT, sptr, (size_t)bytes_written, conn);
  268. if((size_t)bytes_written != write_len) {
  269. /* if not all was written at once, we must advance the pointer, decrease
  270. the size left and try again! */
  271. write_len -= bytes_written;
  272. sptr += bytes_written;
  273. }
  274. else
  275. break;
  276. }
  277. free(s); /* free the output string */
  278. return result;
  279. }
  280. /*
  281. * Curl_write() is an internal write function that sends data to the
  282. * server. Works with plain sockets, SCP, SSL or kerberos.
  283. *
  284. * If the write would block (CURLE_AGAIN), we return CURLE_OK and
  285. * (*written == 0). Otherwise we return regular CURLcode value.
  286. */
  287. CURLcode Curl_write(struct connectdata *conn,
  288. curl_socket_t sockfd,
  289. const void *mem,
  290. size_t len,
  291. ssize_t *written)
  292. {
  293. ssize_t bytes_written;
  294. CURLcode result = CURLE_OK;
  295. int num = (sockfd == conn->sock[SECONDARYSOCKET]);
  296. bytes_written = conn->send[num](conn, num, mem, len, &result);
  297. *written = bytes_written;
  298. if(bytes_written >= 0)
  299. /* we completely ignore the curlcode value when subzero is not returned */
  300. return CURLE_OK;
  301. /* handle CURLE_AGAIN or a send failure */
  302. switch(result) {
  303. case CURLE_AGAIN:
  304. *written = 0;
  305. return CURLE_OK;
  306. case CURLE_OK:
  307. /* general send failure */
  308. return CURLE_SEND_ERROR;
  309. default:
  310. /* we got a specific curlcode, forward it */
  311. return result;
  312. }
  313. }
  314. ssize_t Curl_send_plain(struct connectdata *conn, int num,
  315. const void *mem, size_t len, CURLcode *code)
  316. {
  317. curl_socket_t sockfd = conn->sock[num];
  318. ssize_t bytes_written;
  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. pre_receive_plain(conn, num);
  325. #ifdef MSG_FASTOPEN /* 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 therefor
  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. failf(conn->data, "Send failure: %s",
  355. Curl_strerror(conn, err));
  356. conn->data->state.os_errno = err;
  357. *code = CURLE_SEND_ERROR;
  358. }
  359. }
  360. return bytes_written;
  361. }
  362. /*
  363. * Curl_write_plain() is an internal write function that sends data to the
  364. * server using plain sockets only. Otherwise meant to have the exact same
  365. * proto as Curl_write()
  366. */
  367. CURLcode Curl_write_plain(struct connectdata *conn,
  368. curl_socket_t sockfd,
  369. const void *mem,
  370. size_t len,
  371. ssize_t *written)
  372. {
  373. ssize_t bytes_written;
  374. CURLcode result;
  375. int num = (sockfd == conn->sock[SECONDARYSOCKET]);
  376. bytes_written = Curl_send_plain(conn, num, mem, len, &result);
  377. *written = bytes_written;
  378. return result;
  379. }
  380. ssize_t Curl_recv_plain(struct connectdata *conn, int num, char *buf,
  381. size_t len, CURLcode *code)
  382. {
  383. curl_socket_t sockfd = conn->sock[num];
  384. ssize_t nread;
  385. /* Check and return data that already received and storied in internal
  386. intermediate buffer */
  387. nread = get_pre_recved(conn, num, buf, len);
  388. if(nread > 0) {
  389. *code = CURLE_OK;
  390. return nread;
  391. }
  392. nread = sread(sockfd, buf, len);
  393. *code = CURLE_OK;
  394. if(-1 == nread) {
  395. int err = SOCKERRNO;
  396. if(
  397. #ifdef WSAEWOULDBLOCK
  398. /* This is how Windows does it */
  399. (WSAEWOULDBLOCK == err)
  400. #else
  401. /* errno may be EWOULDBLOCK or on some systems EAGAIN when it returned
  402. due to its inability to send off data without blocking. We therefor
  403. treat both error codes the same here */
  404. (EWOULDBLOCK == err) || (EAGAIN == err) || (EINTR == err)
  405. #endif
  406. ) {
  407. /* this is just a case of EWOULDBLOCK */
  408. *code = CURLE_AGAIN;
  409. }
  410. else {
  411. failf(conn->data, "Recv failure: %s",
  412. Curl_strerror(conn, err));
  413. conn->data->state.os_errno = err;
  414. *code = CURLE_RECV_ERROR;
  415. }
  416. }
  417. return nread;
  418. }
  419. static CURLcode pausewrite(struct Curl_easy *data,
  420. int type, /* what type of data */
  421. const char *ptr,
  422. size_t len)
  423. {
  424. /* signalled to pause sending on this connection, but since we have data
  425. we want to send we need to dup it to save a copy for when the sending
  426. is again enabled */
  427. struct SingleRequest *k = &data->req;
  428. struct UrlState *s = &data->state;
  429. char *dupl;
  430. unsigned int i;
  431. bool newtype = TRUE;
  432. if(s->tempcount) {
  433. for(i=0; i< s->tempcount; i++) {
  434. if(s->tempwrite[i].type == type) {
  435. /* data for this type exists */
  436. newtype = FALSE;
  437. break;
  438. }
  439. }
  440. DEBUGASSERT(i < 3);
  441. }
  442. else
  443. i = 0;
  444. if(!newtype) {
  445. /* append new data to old data */
  446. /* figure out the new size of the data to save */
  447. size_t newlen = len + s->tempwrite[i].len;
  448. /* allocate the new memory area */
  449. char *newptr = realloc(s->tempwrite[i].buf, newlen);
  450. if(!newptr)
  451. return CURLE_OUT_OF_MEMORY;
  452. /* copy the new data to the end of the new area */
  453. memcpy(newptr + s->tempwrite[i].len, ptr, len);
  454. /* update the pointer and the size */
  455. s->tempwrite[i].buf = newptr;
  456. s->tempwrite[i].len = newlen;
  457. }
  458. else {
  459. dupl = Curl_memdup(ptr, len);
  460. if(!dupl)
  461. return CURLE_OUT_OF_MEMORY;
  462. /* store this information in the state struct for later use */
  463. s->tempwrite[i].buf = dupl;
  464. s->tempwrite[i].len = len;
  465. s->tempwrite[i].type = type;
  466. if(newtype)
  467. s->tempcount++;
  468. }
  469. /* mark the connection as RECV paused */
  470. k->keepon |= KEEP_RECV_PAUSE;
  471. DEBUGF(infof(data, "Paused %zu bytes in buffer for type %02x\n",
  472. len, type));
  473. return CURLE_OK;
  474. }
  475. /* Curl_client_chop_write() writes chunks of data not larger than
  476. * CURL_MAX_WRITE_SIZE via client write callback(s) and
  477. * takes care of pause requests from the callbacks.
  478. */
  479. CURLcode Curl_client_chop_write(struct connectdata *conn,
  480. int type,
  481. char *ptr,
  482. size_t len)
  483. {
  484. struct Curl_easy *data = conn->data;
  485. curl_write_callback writeheader = NULL;
  486. curl_write_callback writebody = NULL;
  487. if(!len)
  488. return CURLE_OK;
  489. /* If reading is paused, append this data to the already held data for this
  490. type. */
  491. if(data->req.keepon & KEEP_RECV_PAUSE)
  492. return pausewrite(data, type, ptr, len);
  493. /* Determine the callback(s) to use. */
  494. if(type & CLIENTWRITE_BODY)
  495. writebody = data->set.fwrite_func;
  496. if((type & CLIENTWRITE_HEADER) &&
  497. (data->set.fwrite_header || data->set.writeheader)) {
  498. /*
  499. * Write headers to the same callback or to the especially setup
  500. * header callback function (added after version 7.7.1).
  501. */
  502. writeheader =
  503. data->set.fwrite_header? data->set.fwrite_header: data->set.fwrite_func;
  504. }
  505. /* Chop data, write chunks. */
  506. while(len) {
  507. size_t chunklen = len <= CURL_MAX_WRITE_SIZE? len: CURL_MAX_WRITE_SIZE;
  508. if(writebody) {
  509. size_t wrote = writebody(ptr, 1, chunklen, data->set.out);
  510. if(CURL_WRITEFUNC_PAUSE == wrote) {
  511. if(conn->handler->flags & PROTOPT_NONETWORK) {
  512. /* Protocols that work without network cannot be paused. This is
  513. actually only FILE:// just now, and it can't pause since the
  514. transfer isn't done using the "normal" procedure. */
  515. failf(data, "Write callback asked for PAUSE when not supported!");
  516. return CURLE_WRITE_ERROR;
  517. }
  518. return pausewrite(data, type, ptr, len);
  519. }
  520. if(wrote != chunklen) {
  521. failf(data, "Failed writing body (%zu != %zu)", wrote, chunklen);
  522. return CURLE_WRITE_ERROR;
  523. }
  524. }
  525. if(writeheader) {
  526. size_t wrote = writeheader(ptr, 1, chunklen, data->set.writeheader);
  527. if(CURL_WRITEFUNC_PAUSE == wrote)
  528. /* here we pass in the HEADER bit only since if this was body as well
  529. then it was passed already and clearly that didn't trigger the
  530. pause, so this is saved for later with the HEADER bit only */
  531. return pausewrite(data, CLIENTWRITE_HEADER, ptr, len);
  532. if(wrote != chunklen) {
  533. failf(data, "Failed writing header");
  534. return CURLE_WRITE_ERROR;
  535. }
  536. }
  537. ptr += chunklen;
  538. len -= chunklen;
  539. }
  540. return CURLE_OK;
  541. }
  542. /* Curl_client_write() sends data to the write callback(s)
  543. The bit pattern defines to what "streams" to write to. Body and/or header.
  544. The defines are in sendf.h of course.
  545. If CURL_DO_LINEEND_CONV is enabled, data is converted IN PLACE to the
  546. local character encoding. This is a problem and should be changed in
  547. the future to leave the original data alone.
  548. */
  549. CURLcode Curl_client_write(struct connectdata *conn,
  550. int type,
  551. char *ptr,
  552. size_t len)
  553. {
  554. struct Curl_easy *data = conn->data;
  555. if(0 == len)
  556. len = strlen(ptr);
  557. DEBUGASSERT(type <= 3);
  558. /* FTP data may need conversion. */
  559. if((type & CLIENTWRITE_BODY) &&
  560. (conn->handler->protocol & PROTO_FAMILY_FTP) &&
  561. conn->proto.ftpc.transfertype == 'A') {
  562. /* convert from the network encoding */
  563. CURLcode result = Curl_convert_from_network(data, ptr, len);
  564. /* Curl_convert_from_network calls failf if unsuccessful */
  565. if(result)
  566. return result;
  567. #ifdef CURL_DO_LINEEND_CONV
  568. /* convert end-of-line markers */
  569. len = convert_lineends(data, ptr, len);
  570. #endif /* CURL_DO_LINEEND_CONV */
  571. }
  572. return Curl_client_chop_write(conn, type, ptr, len);
  573. }
  574. CURLcode Curl_read_plain(curl_socket_t sockfd,
  575. char *buf,
  576. size_t bytesfromsocket,
  577. ssize_t *n)
  578. {
  579. ssize_t nread = sread(sockfd, buf, bytesfromsocket);
  580. if(-1 == nread) {
  581. int err = SOCKERRNO;
  582. int return_error;
  583. #ifdef USE_WINSOCK
  584. return_error = WSAEWOULDBLOCK == err;
  585. #else
  586. return_error = EWOULDBLOCK == err || EAGAIN == err || EINTR == err;
  587. #endif
  588. if(return_error)
  589. return CURLE_AGAIN;
  590. return CURLE_RECV_ERROR;
  591. }
  592. /* we only return number of bytes read when we return OK */
  593. *n = nread;
  594. return CURLE_OK;
  595. }
  596. /*
  597. * Internal read-from-socket function. This is meant to deal with plain
  598. * sockets, SSL sockets and kerberos sockets.
  599. *
  600. * Returns a regular CURLcode value.
  601. */
  602. CURLcode Curl_read(struct connectdata *conn, /* connection data */
  603. curl_socket_t sockfd, /* read from this socket */
  604. char *buf, /* store read data here */
  605. size_t sizerequested, /* max amount to read */
  606. ssize_t *n) /* amount bytes read */
  607. {
  608. CURLcode result = CURLE_RECV_ERROR;
  609. ssize_t nread = 0;
  610. size_t bytesfromsocket = 0;
  611. char *buffertofill = NULL;
  612. struct Curl_easy *data = conn->data;
  613. /* if HTTP/1 pipelining is both wanted and possible */
  614. bool pipelining = Curl_pipeline_wanted(data->multi, CURLPIPE_HTTP1) &&
  615. (conn->bundle->multiuse == BUNDLE_PIPELINING);
  616. /* Set 'num' to 0 or 1, depending on which socket that has been sent here.
  617. If it is the second socket, we set num to 1. Otherwise to 0. This lets
  618. us use the correct ssl handle. */
  619. int num = (sockfd == conn->sock[SECONDARYSOCKET]);
  620. *n=0; /* reset amount to zero */
  621. /* If session can pipeline, check connection buffer */
  622. if(pipelining) {
  623. size_t bytestocopy = CURLMIN(conn->buf_len - conn->read_pos,
  624. sizerequested);
  625. /* Copy from our master buffer first if we have some unread data there*/
  626. if(bytestocopy > 0) {
  627. memcpy(buf, conn->master_buffer + conn->read_pos, bytestocopy);
  628. conn->read_pos += bytestocopy;
  629. conn->bits.stream_was_rewound = FALSE;
  630. *n = (ssize_t)bytestocopy;
  631. return CURLE_OK;
  632. }
  633. /* If we come here, it means that there is no data to read from the buffer,
  634. * so we read from the socket */
  635. bytesfromsocket = CURLMIN(sizerequested, MASTERBUF_SIZE);
  636. buffertofill = conn->master_buffer;
  637. }
  638. else {
  639. bytesfromsocket = CURLMIN(sizerequested, (size_t)data->set.buffer_size);
  640. buffertofill = buf;
  641. }
  642. nread = conn->recv[num](conn, num, buffertofill, bytesfromsocket, &result);
  643. if(nread < 0)
  644. return result;
  645. if(pipelining) {
  646. memcpy(buf, conn->master_buffer, nread);
  647. conn->buf_len = nread;
  648. conn->read_pos = nread;
  649. }
  650. *n += nread;
  651. return CURLE_OK;
  652. }
  653. /* return 0 on success */
  654. static int showit(struct Curl_easy *data, curl_infotype type,
  655. char *ptr, size_t size)
  656. {
  657. static const char s_infotype[CURLINFO_END][3] = {
  658. "* ", "< ", "> ", "{ ", "} ", "{ ", "} " };
  659. int rc = 0;
  660. #ifdef CURL_DOES_CONVERSIONS
  661. char *buf = NULL;
  662. size_t conv_size = 0;
  663. switch(type) {
  664. case CURLINFO_HEADER_OUT:
  665. buf = Curl_memdup(ptr, size);
  666. if(!buf)
  667. return 1;
  668. conv_size = size;
  669. /* Special processing is needed for this block if it
  670. * contains both headers and data (separated by CRLFCRLF).
  671. * We want to convert just the headers, leaving the data as-is.
  672. */
  673. if(size > 4) {
  674. size_t i;
  675. for(i = 0; i < size-4; i++) {
  676. if(memcmp(&buf[i], "\x0d\x0a\x0d\x0a", 4) == 0) {
  677. /* convert everything through this CRLFCRLF but no further */
  678. conv_size = i + 4;
  679. break;
  680. }
  681. }
  682. }
  683. Curl_convert_from_network(data, buf, conv_size);
  684. /* Curl_convert_from_network calls failf if unsuccessful */
  685. /* we might as well continue even if it fails... */
  686. ptr = buf; /* switch pointer to use my buffer instead */
  687. break;
  688. default:
  689. /* leave everything else as-is */
  690. break;
  691. }
  692. #endif /* CURL_DOES_CONVERSIONS */
  693. if(data->set.fdebug)
  694. rc = (*data->set.fdebug)(data, type, ptr, size, data->set.debugdata);
  695. else {
  696. switch(type) {
  697. case CURLINFO_TEXT:
  698. case CURLINFO_HEADER_OUT:
  699. case CURLINFO_HEADER_IN:
  700. fwrite(s_infotype[type], 2, 1, data->set.err);
  701. fwrite(ptr, size, 1, data->set.err);
  702. #ifdef CURL_DOES_CONVERSIONS
  703. if(size != conv_size) {
  704. /* we had untranslated data so we need an explicit newline */
  705. fwrite("\n", 1, 1, data->set.err);
  706. }
  707. #endif
  708. break;
  709. default: /* nada */
  710. break;
  711. }
  712. }
  713. #ifdef CURL_DOES_CONVERSIONS
  714. free(buf);
  715. #endif
  716. return rc;
  717. }
  718. int Curl_debug(struct Curl_easy *data, curl_infotype type,
  719. char *ptr, size_t size,
  720. struct connectdata *conn)
  721. {
  722. int rc;
  723. if(data->set.printhost && conn && conn->host.dispname) {
  724. char buffer[160];
  725. const char *t=NULL;
  726. const char *w="Data";
  727. switch(type) {
  728. case CURLINFO_HEADER_IN:
  729. w = "Header";
  730. /* FALLTHROUGH */
  731. case CURLINFO_DATA_IN:
  732. t = "from";
  733. break;
  734. case CURLINFO_HEADER_OUT:
  735. w = "Header";
  736. /* FALLTHROUGH */
  737. case CURLINFO_DATA_OUT:
  738. t = "to";
  739. break;
  740. default:
  741. break;
  742. }
  743. if(t) {
  744. snprintf(buffer, sizeof(buffer), "[%s %s %s]", w, t,
  745. conn->host.dispname);
  746. rc = showit(data, CURLINFO_TEXT, buffer, strlen(buffer));
  747. if(rc)
  748. return rc;
  749. }
  750. }
  751. rc = showit(data, type, ptr, size);
  752. return rc;
  753. }