sendf.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2016, 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. /* The last 3 #include files should be in this order */
  34. #include "curl_printf.h"
  35. #include "curl_memory.h"
  36. #include "memdebug.h"
  37. #ifdef CURL_DO_LINEEND_CONV
  38. /*
  39. * convert_lineends() changes CRLF (\r\n) end-of-line markers to a single LF
  40. * (\n), with special processing for CRLF sequences that are split between two
  41. * blocks of data. Remaining, bare CRs are changed to LFs. The possibly new
  42. * size of the data is returned.
  43. */
  44. static size_t convert_lineends(struct Curl_easy *data,
  45. char *startPtr, size_t size)
  46. {
  47. char *inPtr, *outPtr;
  48. /* sanity check */
  49. if((startPtr == NULL) || (size < 1)) {
  50. return size;
  51. }
  52. if(data->state.prev_block_had_trailing_cr) {
  53. /* The previous block of incoming data
  54. had a trailing CR, which was turned into a LF. */
  55. if(*startPtr == '\n') {
  56. /* This block of incoming data starts with the
  57. previous block's LF so get rid of it */
  58. memmove(startPtr, startPtr+1, size-1);
  59. size--;
  60. /* and it wasn't a bare CR but a CRLF conversion instead */
  61. data->state.crlf_conversions++;
  62. }
  63. data->state.prev_block_had_trailing_cr = FALSE; /* reset the flag */
  64. }
  65. /* find 1st CR, if any */
  66. inPtr = outPtr = memchr(startPtr, '\r', size);
  67. if(inPtr) {
  68. /* at least one CR, now look for CRLF */
  69. while(inPtr < (startPtr+size-1)) {
  70. /* note that it's size-1, so we'll never look past the last byte */
  71. if(memcmp(inPtr, "\r\n", 2) == 0) {
  72. /* CRLF found, bump past the CR and copy the NL */
  73. inPtr++;
  74. *outPtr = *inPtr;
  75. /* keep track of how many CRLFs we converted */
  76. data->state.crlf_conversions++;
  77. }
  78. else {
  79. if(*inPtr == '\r') {
  80. /* lone CR, move LF instead */
  81. *outPtr = '\n';
  82. }
  83. else {
  84. /* not a CRLF nor a CR, just copy whatever it is */
  85. *outPtr = *inPtr;
  86. }
  87. }
  88. outPtr++;
  89. inPtr++;
  90. } /* end of while loop */
  91. if(inPtr < startPtr+size) {
  92. /* handle last byte */
  93. if(*inPtr == '\r') {
  94. /* deal with a CR at the end of the buffer */
  95. *outPtr = '\n'; /* copy a NL instead */
  96. /* note that a CRLF might be split across two blocks */
  97. data->state.prev_block_had_trailing_cr = TRUE;
  98. }
  99. else {
  100. /* copy last byte */
  101. *outPtr = *inPtr;
  102. }
  103. outPtr++;
  104. }
  105. if(outPtr < startPtr+size)
  106. /* tidy up by null terminating the now shorter data */
  107. *outPtr = '\0';
  108. return (outPtr - startPtr);
  109. }
  110. return size;
  111. }
  112. #endif /* CURL_DO_LINEEND_CONV */
  113. #ifdef USE_RECV_BEFORE_SEND_WORKAROUND
  114. bool Curl_recv_has_postponed_data(struct connectdata *conn, int sockindex)
  115. {
  116. struct postponed_data * const psnd = &(conn->postponed[sockindex]);
  117. return psnd->buffer && psnd->allocated_size &&
  118. psnd->recv_size > psnd->recv_processed;
  119. }
  120. static void pre_receive_plain(struct connectdata *conn, int num)
  121. {
  122. const curl_socket_t sockfd = conn->sock[num];
  123. struct postponed_data * const psnd = &(conn->postponed[num]);
  124. size_t bytestorecv = psnd->allocated_size - psnd->recv_size;
  125. /* WinSock will destroy unread received data if send() is
  126. failed.
  127. To avoid lossage of received data, recv() must be
  128. performed before every send() if any incoming data is
  129. available. However, skip this, if buffer is already full. */
  130. if((conn->handler->protocol&PROTO_FAMILY_HTTP) != 0 &&
  131. conn->recv[num] == Curl_recv_plain &&
  132. (!psnd->buffer || bytestorecv)) {
  133. const int readymask = Curl_socket_check(sockfd, CURL_SOCKET_BAD,
  134. CURL_SOCKET_BAD, 0);
  135. if(readymask != -1 && (readymask & CURL_CSELECT_IN) != 0) {
  136. /* Have some incoming data */
  137. if(!psnd->buffer) {
  138. /* Use buffer double default size for intermediate buffer */
  139. psnd->allocated_size = 2 * BUFSIZE;
  140. psnd->buffer = malloc(psnd->allocated_size);
  141. psnd->recv_size = 0;
  142. psnd->recv_processed = 0;
  143. #ifdef DEBUGBUILD
  144. psnd->bindsock = sockfd; /* Used only for DEBUGASSERT */
  145. #endif /* DEBUGBUILD */
  146. bytestorecv = psnd->allocated_size;
  147. }
  148. if(psnd->buffer) {
  149. ssize_t recvedbytes;
  150. DEBUGASSERT(psnd->bindsock == sockfd);
  151. recvedbytes = sread(sockfd, psnd->buffer + psnd->recv_size,
  152. bytestorecv);
  153. if(recvedbytes > 0)
  154. psnd->recv_size += recvedbytes;
  155. }
  156. else
  157. psnd->allocated_size = 0;
  158. }
  159. }
  160. }
  161. static ssize_t get_pre_recved(struct connectdata *conn, int num, char *buf,
  162. size_t len)
  163. {
  164. struct postponed_data * const psnd = &(conn->postponed[num]);
  165. size_t copysize;
  166. if(!psnd->buffer)
  167. return 0;
  168. DEBUGASSERT(psnd->allocated_size > 0);
  169. DEBUGASSERT(psnd->recv_size <= psnd->allocated_size);
  170. DEBUGASSERT(psnd->recv_processed <= psnd->recv_size);
  171. /* Check and process data that already received and storied in internal
  172. intermediate buffer */
  173. if(psnd->recv_size > psnd->recv_processed) {
  174. DEBUGASSERT(psnd->bindsock == conn->sock[num]);
  175. copysize = CURLMIN(len, psnd->recv_size - psnd->recv_processed);
  176. memcpy(buf, psnd->buffer + psnd->recv_processed, copysize);
  177. psnd->recv_processed += copysize;
  178. }
  179. else
  180. copysize = 0; /* buffer was allocated, but nothing was received */
  181. /* Free intermediate buffer if it has no unprocessed data */
  182. if(psnd->recv_processed == psnd->recv_size) {
  183. free(psnd->buffer);
  184. psnd->buffer = NULL;
  185. psnd->allocated_size = 0;
  186. psnd->recv_size = 0;
  187. psnd->recv_processed = 0;
  188. #ifdef DEBUGBUILD
  189. psnd->bindsock = CURL_SOCKET_BAD;
  190. #endif /* DEBUGBUILD */
  191. }
  192. return (ssize_t)copysize;
  193. }
  194. #else /* ! USE_RECV_BEFORE_SEND_WORKAROUND */
  195. /* Use "do-nothing" macros instead of functions when workaround not used */
  196. bool Curl_recv_has_postponed_data(struct connectdata *conn, int sockindex)
  197. {
  198. (void)conn;
  199. (void)sockindex;
  200. return false;
  201. }
  202. #define pre_receive_plain(c,n) do {} WHILE_FALSE
  203. #define get_pre_recved(c,n,b,l) 0
  204. #endif /* ! USE_RECV_BEFORE_SEND_WORKAROUND */
  205. /* Curl_infof() is for info message along the way */
  206. void Curl_infof(struct Curl_easy *data, const char *fmt, ...)
  207. {
  208. if(data && data->set.verbose) {
  209. va_list ap;
  210. size_t len;
  211. char print_buffer[2048 + 1];
  212. va_start(ap, fmt);
  213. vsnprintf(print_buffer, sizeof(print_buffer), fmt, ap);
  214. va_end(ap);
  215. len = strlen(print_buffer);
  216. Curl_debug(data, CURLINFO_TEXT, print_buffer, len, NULL);
  217. }
  218. }
  219. /* Curl_failf() is for messages stating why we failed.
  220. * The message SHALL NOT include any LF or CR.
  221. */
  222. void Curl_failf(struct Curl_easy *data, const char *fmt, ...)
  223. {
  224. va_list ap;
  225. size_t len;
  226. va_start(ap, fmt);
  227. vsnprintf(data->state.buffer, BUFSIZE, fmt, ap);
  228. if(data->set.errorbuffer && !data->state.errorbuf) {
  229. snprintf(data->set.errorbuffer, CURL_ERROR_SIZE, "%s", data->state.buffer);
  230. data->state.errorbuf = TRUE; /* wrote error string */
  231. }
  232. if(data->set.verbose) {
  233. len = strlen(data->state.buffer);
  234. if(len < BUFSIZE - 1) {
  235. data->state.buffer[len] = '\n';
  236. data->state.buffer[++len] = '\0';
  237. }
  238. Curl_debug(data, CURLINFO_TEXT, data->state.buffer, len, NULL);
  239. }
  240. va_end(ap);
  241. }
  242. /* Curl_sendf() sends formated 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. char *dupl = malloc(len);
  429. if(!dupl)
  430. return CURLE_OUT_OF_MEMORY;
  431. memcpy(dupl, ptr, len);
  432. /* store this information in the state struct for later use */
  433. data->state.tempwrite = dupl;
  434. data->state.tempwritesize = len;
  435. data->state.tempwritetype = type;
  436. /* mark the connection as RECV paused */
  437. k->keepon |= KEEP_RECV_PAUSE;
  438. DEBUGF(infof(data, "Pausing with %zu bytes in buffer for type %02x\n",
  439. len, type));
  440. return CURLE_OK;
  441. }
  442. /* Curl_client_chop_write() writes chunks of data not larger than
  443. * CURL_MAX_WRITE_SIZE via client write callback(s) and
  444. * takes care of pause requests from the callbacks.
  445. */
  446. CURLcode Curl_client_chop_write(struct connectdata *conn,
  447. int type,
  448. char *ptr,
  449. size_t len)
  450. {
  451. struct Curl_easy *data = conn->data;
  452. curl_write_callback writeheader = NULL;
  453. curl_write_callback writebody = NULL;
  454. if(!len)
  455. return CURLE_OK;
  456. /* If reading is actually paused, we're forced to append this chunk of data
  457. to the already held data, but only if it is the same type as otherwise it
  458. can't work and it'll return error instead. */
  459. if(data->req.keepon & KEEP_RECV_PAUSE) {
  460. size_t newlen;
  461. char *newptr;
  462. if(type != data->state.tempwritetype)
  463. /* major internal confusion */
  464. return CURLE_RECV_ERROR;
  465. DEBUGASSERT(data->state.tempwrite);
  466. /* figure out the new size of the data to save */
  467. newlen = len + data->state.tempwritesize;
  468. /* allocate the new memory area */
  469. newptr = realloc(data->state.tempwrite, newlen);
  470. if(!newptr)
  471. return CURLE_OUT_OF_MEMORY;
  472. /* copy the new data to the end of the new area */
  473. memcpy(newptr + data->state.tempwritesize, ptr, len);
  474. /* update the pointer and the size */
  475. data->state.tempwrite = newptr;
  476. data->state.tempwritesize = newlen;
  477. return CURLE_OK;
  478. }
  479. /* Determine the callback(s) to use. */
  480. if(type & CLIENTWRITE_BODY)
  481. writebody = data->set.fwrite_func;
  482. if((type & CLIENTWRITE_HEADER) &&
  483. (data->set.fwrite_header || data->set.writeheader)) {
  484. /*
  485. * Write headers to the same callback or to the especially setup
  486. * header callback function (added after version 7.7.1).
  487. */
  488. writeheader =
  489. data->set.fwrite_header? data->set.fwrite_header: data->set.fwrite_func;
  490. }
  491. /* Chop data, write chunks. */
  492. while(len) {
  493. size_t chunklen = len <= CURL_MAX_WRITE_SIZE? len: CURL_MAX_WRITE_SIZE;
  494. if(writebody) {
  495. size_t wrote = writebody(ptr, 1, chunklen, data->set.out);
  496. if(CURL_WRITEFUNC_PAUSE == wrote) {
  497. if(conn->handler->flags & PROTOPT_NONETWORK) {
  498. /* Protocols that work without network cannot be paused. This is
  499. actually only FILE:// just now, and it can't pause since the
  500. transfer isn't done using the "normal" procedure. */
  501. failf(data, "Write callback asked for PAUSE when not supported!");
  502. return CURLE_WRITE_ERROR;
  503. }
  504. else
  505. return pausewrite(data, type, ptr, len);
  506. }
  507. else if(wrote != chunklen) {
  508. failf(data, "Failed writing body (%zu != %zu)", wrote, chunklen);
  509. return CURLE_WRITE_ERROR;
  510. }
  511. }
  512. if(writeheader) {
  513. size_t wrote = writeheader(ptr, 1, chunklen, data->set.writeheader);
  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 != chunklen) {
  520. failf(data, "Failed writing header");
  521. return CURLE_WRITE_ERROR;
  522. }
  523. }
  524. ptr += chunklen;
  525. len -= chunklen;
  526. }
  527. return CURLE_OK;
  528. }
  529. /* Curl_client_write() sends data to the write callback(s)
  530. The bit pattern defines to what "streams" to write to. Body and/or header.
  531. The defines are in sendf.h of course.
  532. If CURL_DO_LINEEND_CONV is enabled, data is converted IN PLACE to the
  533. local character encoding. This is a problem and should be changed in
  534. the future to leave the original data alone.
  535. */
  536. CURLcode Curl_client_write(struct connectdata *conn,
  537. int type,
  538. char *ptr,
  539. size_t len)
  540. {
  541. struct Curl_easy *data = conn->data;
  542. if(0 == len)
  543. len = strlen(ptr);
  544. /* FTP data may need conversion. */
  545. if((type & CLIENTWRITE_BODY) &&
  546. (conn->handler->protocol & PROTO_FAMILY_FTP) &&
  547. conn->proto.ftpc.transfertype == 'A') {
  548. /* convert from the network encoding */
  549. CURLcode result = Curl_convert_from_network(data, ptr, len);
  550. /* Curl_convert_from_network calls failf if unsuccessful */
  551. if(result)
  552. return result;
  553. #ifdef CURL_DO_LINEEND_CONV
  554. /* convert end-of-line markers */
  555. len = convert_lineends(data, ptr, len);
  556. #endif /* CURL_DO_LINEEND_CONV */
  557. }
  558. return Curl_client_chop_write(conn, type, ptr, len);
  559. }
  560. CURLcode Curl_read_plain(curl_socket_t sockfd,
  561. char *buf,
  562. size_t bytesfromsocket,
  563. ssize_t *n)
  564. {
  565. ssize_t nread = sread(sockfd, buf, bytesfromsocket);
  566. if(-1 == nread) {
  567. int err = SOCKERRNO;
  568. int return_error;
  569. #ifdef USE_WINSOCK
  570. return_error = WSAEWOULDBLOCK == err;
  571. #else
  572. return_error = EWOULDBLOCK == err || EAGAIN == err || EINTR == err;
  573. #endif
  574. if(return_error)
  575. return CURLE_AGAIN;
  576. else
  577. return CURLE_RECV_ERROR;
  578. }
  579. /* we only return number of bytes read when we return OK */
  580. *n = nread;
  581. return CURLE_OK;
  582. }
  583. /*
  584. * Internal read-from-socket function. This is meant to deal with plain
  585. * sockets, SSL sockets and kerberos sockets.
  586. *
  587. * Returns a regular CURLcode value.
  588. */
  589. CURLcode Curl_read(struct connectdata *conn, /* connection data */
  590. curl_socket_t sockfd, /* read from this socket */
  591. char *buf, /* store read data here */
  592. size_t sizerequested, /* max amount to read */
  593. ssize_t *n) /* amount bytes read */
  594. {
  595. CURLcode result = CURLE_RECV_ERROR;
  596. ssize_t nread = 0;
  597. size_t bytesfromsocket = 0;
  598. char *buffertofill = NULL;
  599. /* if HTTP/1 pipelining is both wanted and possible */
  600. bool pipelining = Curl_pipeline_wanted(conn->data->multi, CURLPIPE_HTTP1) &&
  601. (conn->bundle->multiuse == BUNDLE_PIPELINING);
  602. /* Set 'num' to 0 or 1, depending on which socket that has been sent here.
  603. If it is the second socket, we set num to 1. Otherwise to 0. This lets
  604. us use the correct ssl handle. */
  605. int num = (sockfd == conn->sock[SECONDARYSOCKET]);
  606. *n=0; /* reset amount to zero */
  607. /* If session can pipeline, check connection buffer */
  608. if(pipelining) {
  609. size_t bytestocopy = CURLMIN(conn->buf_len - conn->read_pos,
  610. sizerequested);
  611. /* Copy from our master buffer first if we have some unread data there*/
  612. if(bytestocopy > 0) {
  613. memcpy(buf, conn->master_buffer + conn->read_pos, bytestocopy);
  614. conn->read_pos += bytestocopy;
  615. conn->bits.stream_was_rewound = FALSE;
  616. *n = (ssize_t)bytestocopy;
  617. return CURLE_OK;
  618. }
  619. /* If we come here, it means that there is no data to read from the buffer,
  620. * so we read from the socket */
  621. bytesfromsocket = CURLMIN(sizerequested, BUFSIZE * sizeof(char));
  622. buffertofill = conn->master_buffer;
  623. }
  624. else {
  625. bytesfromsocket = CURLMIN((long)sizerequested,
  626. conn->data->set.buffer_size ?
  627. conn->data->set.buffer_size : BUFSIZE);
  628. buffertofill = buf;
  629. }
  630. nread = conn->recv[num](conn, num, buffertofill, bytesfromsocket, &result);
  631. if(nread < 0)
  632. return result;
  633. if(pipelining) {
  634. memcpy(buf, conn->master_buffer, nread);
  635. conn->buf_len = nread;
  636. conn->read_pos = nread;
  637. }
  638. *n += nread;
  639. return CURLE_OK;
  640. }
  641. /* return 0 on success */
  642. static int showit(struct Curl_easy *data, curl_infotype type,
  643. char *ptr, size_t size)
  644. {
  645. static const char s_infotype[CURLINFO_END][3] = {
  646. "* ", "< ", "> ", "{ ", "} ", "{ ", "} " };
  647. #ifdef CURL_DOES_CONVERSIONS
  648. char buf[BUFSIZE+1];
  649. size_t conv_size = 0;
  650. switch(type) {
  651. case CURLINFO_HEADER_OUT:
  652. /* assume output headers are ASCII */
  653. /* copy the data into my buffer so the original is unchanged */
  654. if(size > BUFSIZE) {
  655. size = BUFSIZE; /* truncate if necessary */
  656. buf[BUFSIZE] = '\0';
  657. }
  658. conv_size = size;
  659. memcpy(buf, ptr, size);
  660. /* Special processing is needed for this block if it
  661. * contains both headers and data (separated by CRLFCRLF).
  662. * We want to convert just the headers, leaving the data as-is.
  663. */
  664. if(size > 4) {
  665. size_t i;
  666. for(i = 0; i < size-4; i++) {
  667. if(memcmp(&buf[i], "\x0d\x0a\x0d\x0a", 4) == 0) {
  668. /* convert everything through this CRLFCRLF but no further */
  669. conv_size = i + 4;
  670. break;
  671. }
  672. }
  673. }
  674. Curl_convert_from_network(data, buf, conv_size);
  675. /* Curl_convert_from_network calls failf if unsuccessful */
  676. /* we might as well continue even if it fails... */
  677. ptr = buf; /* switch pointer to use my buffer instead */
  678. break;
  679. default:
  680. /* leave everything else as-is */
  681. break;
  682. }
  683. #endif /* CURL_DOES_CONVERSIONS */
  684. if(data->set.fdebug)
  685. return (*data->set.fdebug)(data, type, ptr, size,
  686. data->set.debugdata);
  687. switch(type) {
  688. case CURLINFO_TEXT:
  689. case CURLINFO_HEADER_OUT:
  690. case CURLINFO_HEADER_IN:
  691. fwrite(s_infotype[type], 2, 1, data->set.err);
  692. fwrite(ptr, size, 1, data->set.err);
  693. #ifdef CURL_DOES_CONVERSIONS
  694. if(size != conv_size) {
  695. /* we had untranslated data so we need an explicit newline */
  696. fwrite("\n", 1, 1, data->set.err);
  697. }
  698. #endif
  699. break;
  700. default: /* nada */
  701. break;
  702. }
  703. return 0;
  704. }
  705. int Curl_debug(struct Curl_easy *data, curl_infotype type,
  706. char *ptr, size_t size,
  707. struct connectdata *conn)
  708. {
  709. int rc;
  710. if(data->set.printhost && conn && conn->host.dispname) {
  711. char buffer[160];
  712. const char *t=NULL;
  713. const char *w="Data";
  714. switch(type) {
  715. case CURLINFO_HEADER_IN:
  716. w = "Header";
  717. /* FALLTHROUGH */
  718. case CURLINFO_DATA_IN:
  719. t = "from";
  720. break;
  721. case CURLINFO_HEADER_OUT:
  722. w = "Header";
  723. /* FALLTHROUGH */
  724. case CURLINFO_DATA_OUT:
  725. t = "to";
  726. break;
  727. default:
  728. break;
  729. }
  730. if(t) {
  731. snprintf(buffer, sizeof(buffer), "[%s %s %s]", w, t,
  732. conn->host.dispname);
  733. rc = showit(data, CURLINFO_TEXT, buffer, strlen(buffer));
  734. if(rc)
  735. return rc;
  736. }
  737. }
  738. rc = showit(data, type, ptr, size);
  739. return rc;
  740. }