sendf.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2008, 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 http://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. * $Id$
  22. ***************************************************************************/
  23. #include "setup.h"
  24. #include <stdio.h>
  25. #include <stdarg.h>
  26. #include <stdlib.h>
  27. #include <errno.h>
  28. #ifdef HAVE_SYS_SOCKET_H
  29. #include <sys/socket.h> /* required for send() & recv() prototypes */
  30. #endif
  31. #ifdef HAVE_UNISTD_H
  32. #include <unistd.h>
  33. #endif
  34. #include <curl/curl.h>
  35. #include "urldata.h"
  36. #include "sendf.h"
  37. #include "connect.h"
  38. #include "sslgen.h"
  39. #include "ssh.h"
  40. #include "multiif.h"
  41. #define _MPRINTF_REPLACE /* use the internal *printf() functions */
  42. #include <curl/mprintf.h>
  43. #if defined(HAVE_KRB4) || defined(HAVE_GSSAPI)
  44. #include "krb4.h"
  45. #else
  46. #define Curl_sec_send(a,b,c,d) -1
  47. #define Curl_sec_read(a,b,c,d) -1
  48. #endif
  49. #include <string.h>
  50. #include "memory.h"
  51. #include "strerror.h"
  52. #include "easyif.h" /* for the Curl_convert_from_network prototype */
  53. /* The last #include file should be: */
  54. #include "memdebug.h"
  55. /* returns last node in linked list */
  56. static struct curl_slist *slist_get_last(struct curl_slist *list)
  57. {
  58. struct curl_slist *item;
  59. /* if caller passed us a NULL, return now */
  60. if(!list)
  61. return NULL;
  62. /* loop through to find the last item */
  63. item = list;
  64. while(item->next) {
  65. item = item->next;
  66. }
  67. return item;
  68. }
  69. /*
  70. * curl_slist_append() appends a string to the linked list. It always returns
  71. * the address of the first record, so that you can use this function as an
  72. * initialization function as well as an append function. If you find this
  73. * bothersome, then simply create a separate _init function and call it
  74. * appropriately from within the program.
  75. */
  76. struct curl_slist *curl_slist_append(struct curl_slist *list,
  77. const char *data)
  78. {
  79. struct curl_slist *last;
  80. struct curl_slist *new_item;
  81. new_item = (struct curl_slist *) malloc(sizeof(struct curl_slist));
  82. if(new_item) {
  83. char *dupdata = strdup(data);
  84. if(dupdata) {
  85. new_item->next = NULL;
  86. new_item->data = dupdata;
  87. }
  88. else {
  89. free(new_item);
  90. return NULL;
  91. }
  92. }
  93. else
  94. return NULL;
  95. if(list) {
  96. last = slist_get_last(list);
  97. last->next = new_item;
  98. return list;
  99. }
  100. /* if this is the first item, then new_item *is* the list */
  101. return new_item;
  102. }
  103. /* be nice and clean up resources */
  104. void curl_slist_free_all(struct curl_slist *list)
  105. {
  106. struct curl_slist *next;
  107. struct curl_slist *item;
  108. if(!list)
  109. return;
  110. item = list;
  111. do {
  112. next = item->next;
  113. if(item->data) {
  114. free(item->data);
  115. }
  116. free(item);
  117. item = next;
  118. } while(next);
  119. }
  120. #ifdef CURL_DO_LINEEND_CONV
  121. /*
  122. * convert_lineends() changes CRLF (\r\n) end-of-line markers to a single LF
  123. * (\n), with special processing for CRLF sequences that are split between two
  124. * blocks of data. Remaining, bare CRs are changed to LFs. The possibly new
  125. * size of the data is returned.
  126. */
  127. static size_t convert_lineends(struct SessionHandle *data,
  128. char *startPtr, size_t size)
  129. {
  130. char *inPtr, *outPtr;
  131. /* sanity check */
  132. if((startPtr == NULL) || (size < 1)) {
  133. return(size);
  134. }
  135. if(data->state.prev_block_had_trailing_cr == TRUE) {
  136. /* The previous block of incoming data
  137. had a trailing CR, which was turned into a LF. */
  138. if(*startPtr == '\n') {
  139. /* This block of incoming data starts with the
  140. previous block's LF so get rid of it */
  141. memmove(startPtr, startPtr+1, size-1);
  142. size--;
  143. /* and it wasn't a bare CR but a CRLF conversion instead */
  144. data->state.crlf_conversions++;
  145. }
  146. data->state.prev_block_had_trailing_cr = FALSE; /* reset the flag */
  147. }
  148. /* find 1st CR, if any */
  149. inPtr = outPtr = memchr(startPtr, '\r', size);
  150. if(inPtr) {
  151. /* at least one CR, now look for CRLF */
  152. while(inPtr < (startPtr+size-1)) {
  153. /* note that it's size-1, so we'll never look past the last byte */
  154. if(memcmp(inPtr, "\r\n", 2) == 0) {
  155. /* CRLF found, bump past the CR and copy the NL */
  156. inPtr++;
  157. *outPtr = *inPtr;
  158. /* keep track of how many CRLFs we converted */
  159. data->state.crlf_conversions++;
  160. }
  161. else {
  162. if(*inPtr == '\r') {
  163. /* lone CR, move LF instead */
  164. *outPtr = '\n';
  165. }
  166. else {
  167. /* not a CRLF nor a CR, just copy whatever it is */
  168. *outPtr = *inPtr;
  169. }
  170. }
  171. outPtr++;
  172. inPtr++;
  173. } /* end of while loop */
  174. if(inPtr < startPtr+size) {
  175. /* handle last byte */
  176. if(*inPtr == '\r') {
  177. /* deal with a CR at the end of the buffer */
  178. *outPtr = '\n'; /* copy a NL instead */
  179. /* note that a CRLF might be split across two blocks */
  180. data->state.prev_block_had_trailing_cr = TRUE;
  181. }
  182. else {
  183. /* copy last byte */
  184. *outPtr = *inPtr;
  185. }
  186. outPtr++;
  187. inPtr++;
  188. }
  189. if(outPtr < startPtr+size) {
  190. /* tidy up by null terminating the now shorter data */
  191. *outPtr = '\0';
  192. }
  193. return(outPtr - startPtr);
  194. }
  195. return(size);
  196. }
  197. #endif /* CURL_DO_LINEEND_CONV */
  198. /* Curl_infof() is for info message along the way */
  199. void Curl_infof(struct SessionHandle *data, const char *fmt, ...)
  200. {
  201. if(data && data->set.verbose) {
  202. va_list ap;
  203. size_t len;
  204. char print_buffer[1024 + 1];
  205. va_start(ap, fmt);
  206. vsnprintf(print_buffer, 1024, fmt, ap);
  207. va_end(ap);
  208. len = strlen(print_buffer);
  209. Curl_debug(data, CURLINFO_TEXT, print_buffer, len, NULL);
  210. }
  211. }
  212. /* Curl_failf() is for messages stating why we failed.
  213. * The message SHALL NOT include any LF or CR.
  214. */
  215. void Curl_failf(struct SessionHandle *data, const char *fmt, ...)
  216. {
  217. va_list ap;
  218. size_t len;
  219. va_start(ap, fmt);
  220. vsnprintf(data->state.buffer, BUFSIZE, fmt, ap);
  221. if(data->set.errorbuffer && !data->state.errorbuf) {
  222. snprintf(data->set.errorbuffer, CURL_ERROR_SIZE, "%s", data->state.buffer);
  223. data->state.errorbuf = TRUE; /* wrote error string */
  224. }
  225. if(data->set.verbose) {
  226. len = strlen(data->state.buffer);
  227. if(len < BUFSIZE - 1) {
  228. data->state.buffer[len] = '\n';
  229. data->state.buffer[++len] = '\0';
  230. }
  231. Curl_debug(data, CURLINFO_TEXT, data->state.buffer, len, NULL);
  232. }
  233. va_end(ap);
  234. }
  235. /* Curl_sendf() sends formated data to the server */
  236. CURLcode Curl_sendf(curl_socket_t sockfd, struct connectdata *conn,
  237. const char *fmt, ...)
  238. {
  239. struct SessionHandle *data = conn->data;
  240. ssize_t bytes_written;
  241. size_t write_len;
  242. CURLcode res = CURLE_OK;
  243. char *s;
  244. char *sptr;
  245. va_list ap;
  246. va_start(ap, fmt);
  247. s = vaprintf(fmt, ap); /* returns an allocated string */
  248. va_end(ap);
  249. if(!s)
  250. return CURLE_OUT_OF_MEMORY; /* failure */
  251. bytes_written=0;
  252. write_len = strlen(s);
  253. sptr = s;
  254. while(1) {
  255. /* Write the buffer to the socket */
  256. res = Curl_write(conn, sockfd, sptr, write_len, &bytes_written);
  257. if(CURLE_OK != res)
  258. break;
  259. if(data->set.verbose)
  260. Curl_debug(data, CURLINFO_DATA_OUT, sptr, (size_t)bytes_written, conn);
  261. if((size_t)bytes_written != write_len) {
  262. /* if not all was written at once, we must advance the pointer, decrease
  263. the size left and try again! */
  264. write_len -= bytes_written;
  265. sptr += bytes_written;
  266. }
  267. else
  268. break;
  269. }
  270. free(s); /* free the output string */
  271. return res;
  272. }
  273. static ssize_t send_plain(struct connectdata *conn,
  274. int num,
  275. const void *mem,
  276. size_t len)
  277. {
  278. curl_socket_t sockfd = conn->sock[num];
  279. ssize_t bytes_written = swrite(sockfd, mem, len);
  280. if(-1 == bytes_written) {
  281. int err = SOCKERRNO;
  282. if(
  283. #ifdef WSAEWOULDBLOCK
  284. /* This is how Windows does it */
  285. (WSAEWOULDBLOCK == err)
  286. #else
  287. /* errno may be EWOULDBLOCK or on some systems EAGAIN when it returned
  288. due to its inability to send off data without blocking. We therefor
  289. treat both error codes the same here */
  290. (EWOULDBLOCK == err) || (EAGAIN == err) || (EINTR == err)
  291. #endif
  292. )
  293. /* this is just a case of EWOULDBLOCK */
  294. bytes_written=0;
  295. else
  296. failf(conn->data, "Send failure: %s",
  297. Curl_strerror(conn, err));
  298. }
  299. return bytes_written;
  300. }
  301. /*
  302. * Curl_write() is an internal write function that sends data to the
  303. * server. Works with plain sockets, SCP, SSL or kerberos.
  304. */
  305. CURLcode Curl_write(struct connectdata *conn,
  306. curl_socket_t sockfd,
  307. const void *mem,
  308. size_t len,
  309. ssize_t *written)
  310. {
  311. ssize_t bytes_written;
  312. CURLcode retcode;
  313. int num = (sockfd == conn->sock[SECONDARYSOCKET]);
  314. if(conn->ssl[num].state == ssl_connection_complete)
  315. bytes_written = Curl_ssl_send(conn, num, mem, len);
  316. else if(Curl_ssh_enabled(conn, PROT_SCP))
  317. bytes_written = Curl_scp_send(conn, num, mem, len);
  318. else if(Curl_ssh_enabled(conn, PROT_SFTP))
  319. bytes_written = Curl_sftp_send(conn, num, mem, len);
  320. else if(conn->sec_complete)
  321. bytes_written = Curl_sec_send(conn, num, mem, len);
  322. else
  323. bytes_written = send_plain(conn, num, mem, len);
  324. *written = bytes_written;
  325. retcode = (-1 != bytes_written)?CURLE_OK:CURLE_SEND_ERROR;
  326. return retcode;
  327. }
  328. /*
  329. * Curl_write_plain() is an internal write function that sends data to the
  330. * server using plain sockets only. Otherwise meant to have the exact same
  331. * proto as Curl_write()
  332. */
  333. CURLcode Curl_write_plain(struct connectdata *conn,
  334. curl_socket_t sockfd,
  335. const void *mem,
  336. size_t len,
  337. ssize_t *written)
  338. {
  339. ssize_t bytes_written;
  340. CURLcode retcode;
  341. int num = (sockfd == conn->sock[SECONDARYSOCKET]);
  342. bytes_written = send_plain(conn, num, mem, len);
  343. *written = bytes_written;
  344. retcode = (-1 != bytes_written)?CURLE_OK:CURLE_SEND_ERROR;
  345. return retcode;
  346. }
  347. static CURLcode pausewrite(struct SessionHandle *data,
  348. int type, /* what type of data */
  349. char *ptr,
  350. size_t len)
  351. {
  352. /* signalled to pause sending on this connection, but since we have data
  353. we want to send we need to dup it to save a copy for when the sending
  354. is again enabled */
  355. struct SingleRequest *k = &data->req;
  356. char *dupl = malloc(len);
  357. if(!dupl)
  358. return CURLE_OUT_OF_MEMORY;
  359. memcpy(dupl, ptr, len);
  360. /* store this information in the state struct for later use */
  361. data->state.tempwrite = dupl;
  362. data->state.tempwritesize = len;
  363. data->state.tempwritetype = type;
  364. /* mark the connection as RECV paused */
  365. k->keepon |= KEEP_READ_PAUSE;
  366. DEBUGF(infof(data, "Pausing with %d bytes in buffer for type %02x\n",
  367. (int)len, type));
  368. return CURLE_OK;
  369. }
  370. /* client_write() sends data to the write callback(s)
  371. The bit pattern defines to what "streams" to write to. Body and/or header.
  372. The defines are in sendf.h of course.
  373. */
  374. CURLcode Curl_client_write(struct connectdata *conn,
  375. int type,
  376. char *ptr,
  377. size_t len)
  378. {
  379. struct SessionHandle *data = conn->data;
  380. size_t wrote;
  381. /* If reading is actually paused, we're forced to append this chunk of data
  382. to the already held data, but only if it is the same type as otherwise it
  383. can't work and it'll return error instead. */
  384. if(data->req.keepon & KEEP_READ_PAUSE) {
  385. size_t newlen;
  386. char *newptr;
  387. if(type != data->state.tempwritetype)
  388. /* major internal confusion */
  389. return CURLE_RECV_ERROR;
  390. /* figure out the new size of the data to save */
  391. newlen = len + data->state.tempwritesize;
  392. /* allocate the new memory area */
  393. newptr = malloc(newlen);
  394. if(!newptr)
  395. return CURLE_OUT_OF_MEMORY;
  396. /* copy the previously held data to the new area */
  397. memcpy(newptr, data->state.tempwrite, data->state.tempwritesize);
  398. /* copy the new data to the end of the new area */
  399. memcpy(newptr + data->state.tempwritesize, ptr, len);
  400. /* free the old data */
  401. free(data->state.tempwrite);
  402. /* update the pointer and the size */
  403. data->state.tempwrite = newptr;
  404. data->state.tempwritesize = newlen;
  405. return CURLE_OK;
  406. }
  407. if(0 == len)
  408. len = strlen(ptr);
  409. if(type & CLIENTWRITE_BODY) {
  410. if((conn->protocol&PROT_FTP) && conn->proto.ftpc.transfertype == 'A') {
  411. #ifdef CURL_DOES_CONVERSIONS
  412. /* convert from the network encoding */
  413. size_t rc;
  414. rc = Curl_convert_from_network(data, ptr, len);
  415. /* Curl_convert_from_network calls failf if unsuccessful */
  416. if(rc != CURLE_OK)
  417. return rc;
  418. #endif /* CURL_DOES_CONVERSIONS */
  419. #ifdef CURL_DO_LINEEND_CONV
  420. /* convert end-of-line markers */
  421. len = convert_lineends(data, ptr, len);
  422. #endif /* CURL_DO_LINEEND_CONV */
  423. }
  424. /* If the previous block of data ended with CR and this block of data is
  425. just a NL, then the length might be zero */
  426. if(len) {
  427. wrote = data->set.fwrite_func(ptr, 1, len, data->set.out);
  428. }
  429. else {
  430. wrote = len;
  431. }
  432. if(CURL_WRITEFUNC_PAUSE == wrote)
  433. return pausewrite(data, type, ptr, len);
  434. if(wrote != len) {
  435. failf(data, "Failed writing body (%d != %d)", (int)wrote, (int)len);
  436. return CURLE_WRITE_ERROR;
  437. }
  438. }
  439. if((type & CLIENTWRITE_HEADER) &&
  440. (data->set.fwrite_header || data->set.writeheader) ) {
  441. /*
  442. * Write headers to the same callback or to the especially setup
  443. * header callback function (added after version 7.7.1).
  444. */
  445. curl_write_callback writeit=
  446. data->set.fwrite_header?data->set.fwrite_header:data->set.fwrite_func;
  447. /* Note: The header is in the host encoding
  448. regardless of the ftp transfer mode (ASCII/Image) */
  449. wrote = writeit(ptr, 1, len, data->set.writeheader);
  450. if(CURL_WRITEFUNC_PAUSE == wrote)
  451. /* here we pass in the HEADER bit only since if this was body as well
  452. then it was passed already and clearly that didn't trigger the pause,
  453. so this is saved for later with the HEADER bit only */
  454. return pausewrite(data, CLIENTWRITE_HEADER, ptr, len);
  455. if(wrote != len) {
  456. failf (data, "Failed writing header");
  457. return CURLE_WRITE_ERROR;
  458. }
  459. }
  460. return CURLE_OK;
  461. }
  462. #ifndef MIN
  463. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  464. #endif
  465. /*
  466. * Internal read-from-socket function. This is meant to deal with plain
  467. * sockets, SSL sockets and kerberos sockets.
  468. *
  469. * If the read would block (EWOULDBLOCK) we return -1. Otherwise we return
  470. * a regular CURLcode value.
  471. */
  472. int Curl_read(struct connectdata *conn, /* connection data */
  473. curl_socket_t sockfd, /* read from this socket */
  474. char *buf, /* store read data here */
  475. size_t sizerequested, /* max amount to read */
  476. ssize_t *n) /* amount bytes read */
  477. {
  478. ssize_t nread = 0;
  479. size_t bytesfromsocket = 0;
  480. char *buffertofill = NULL;
  481. bool pipelining = (bool)(conn->data->multi &&
  482. Curl_multi_canPipeline(conn->data->multi));
  483. /* Set 'num' to 0 or 1, depending on which socket that has been sent here.
  484. If it is the second socket, we set num to 1. Otherwise to 0. This lets
  485. us use the correct ssl handle. */
  486. int num = (sockfd == conn->sock[SECONDARYSOCKET]);
  487. *n=0; /* reset amount to zero */
  488. /* If session can pipeline, check connection buffer */
  489. if(pipelining) {
  490. size_t bytestocopy = MIN(conn->buf_len - conn->read_pos, sizerequested);
  491. /* Copy from our master buffer first if we have some unread data there*/
  492. if(bytestocopy > 0) {
  493. memcpy(buf, conn->master_buffer + conn->read_pos, bytestocopy);
  494. conn->read_pos += bytestocopy;
  495. conn->bits.stream_was_rewound = FALSE;
  496. *n = (ssize_t)bytestocopy;
  497. return CURLE_OK;
  498. }
  499. /* If we come here, it means that there is no data to read from the buffer,
  500. * so we read from the socket */
  501. bytesfromsocket = MIN(sizerequested, BUFSIZE * sizeof (char));
  502. buffertofill = conn->master_buffer;
  503. }
  504. else {
  505. bytesfromsocket = MIN((long)sizerequested, conn->data->set.buffer_size ?
  506. conn->data->set.buffer_size : BUFSIZE);
  507. buffertofill = buf;
  508. }
  509. if(conn->ssl[num].state == ssl_connection_complete) {
  510. nread = Curl_ssl_recv(conn, num, buffertofill, bytesfromsocket);
  511. if(nread == -1) {
  512. return -1; /* -1 from Curl_ssl_recv() means EWOULDBLOCK */
  513. }
  514. }
  515. else if(Curl_ssh_enabled(conn, (PROT_SCP|PROT_SFTP))) {
  516. if(conn->protocol & PROT_SCP)
  517. nread = Curl_scp_recv(conn, num, buffertofill, bytesfromsocket);
  518. else if(conn->protocol & PROT_SFTP)
  519. nread = Curl_sftp_recv(conn, num, buffertofill, bytesfromsocket);
  520. #ifdef LIBSSH2CHANNEL_EAGAIN
  521. if((nread == LIBSSH2CHANNEL_EAGAIN) || (nread == 0))
  522. /* EWOULDBLOCK */
  523. return -1;
  524. #endif
  525. if(nread < 0)
  526. /* since it is negative and not EGAIN, it was a protocol-layer error */
  527. return CURLE_RECV_ERROR;
  528. }
  529. else {
  530. if(conn->sec_complete)
  531. nread = Curl_sec_read(conn, sockfd, buffertofill,
  532. bytesfromsocket);
  533. else
  534. nread = sread(sockfd, buffertofill, bytesfromsocket);
  535. if(-1 == nread) {
  536. int err = SOCKERRNO;
  537. #ifdef USE_WINSOCK
  538. if(WSAEWOULDBLOCK == err)
  539. #else
  540. if((EWOULDBLOCK == err) || (EAGAIN == err) || (EINTR == err))
  541. #endif
  542. return -1;
  543. }
  544. }
  545. if(nread >= 0) {
  546. if(pipelining) {
  547. memcpy(buf, conn->master_buffer, nread);
  548. conn->buf_len = nread;
  549. conn->read_pos = nread;
  550. }
  551. *n += nread;
  552. }
  553. return CURLE_OK;
  554. }
  555. /* return 0 on success */
  556. static int showit(struct SessionHandle *data, curl_infotype type,
  557. char *ptr, size_t size)
  558. {
  559. static const char * const s_infotype[CURLINFO_END] = {
  560. "* ", "< ", "> ", "{ ", "} ", "{ ", "} " };
  561. #ifdef CURL_DOES_CONVERSIONS
  562. char buf[BUFSIZE+1];
  563. size_t conv_size = 0;
  564. switch(type) {
  565. case CURLINFO_HEADER_OUT:
  566. /* assume output headers are ASCII */
  567. /* copy the data into my buffer so the original is unchanged */
  568. if(size > BUFSIZE) {
  569. size = BUFSIZE; /* truncate if necessary */
  570. buf[BUFSIZE] = '\0';
  571. }
  572. conv_size = size;
  573. memcpy(buf, ptr, size);
  574. /* Special processing is needed for this block if it
  575. * contains both headers and data (separated by CRLFCRLF).
  576. * We want to convert just the headers, leaving the data as-is.
  577. */
  578. if(size > 4) {
  579. size_t i;
  580. for(i = 0; i < size-4; i++) {
  581. if(memcmp(&buf[i], "\x0d\x0a\x0d\x0a", 4) == 0) {
  582. /* convert everthing through this CRLFCRLF but no further */
  583. conv_size = i + 4;
  584. break;
  585. }
  586. }
  587. }
  588. Curl_convert_from_network(data, buf, conv_size);
  589. /* Curl_convert_from_network calls failf if unsuccessful */
  590. /* we might as well continue even if it fails... */
  591. ptr = buf; /* switch pointer to use my buffer instead */
  592. break;
  593. default:
  594. /* leave everything else as-is */
  595. break;
  596. }
  597. #endif /* CURL_DOES_CONVERSIONS */
  598. if(data->set.fdebug)
  599. return (*data->set.fdebug)(data, type, ptr, size,
  600. data->set.debugdata);
  601. switch(type) {
  602. case CURLINFO_TEXT:
  603. case CURLINFO_HEADER_OUT:
  604. case CURLINFO_HEADER_IN:
  605. fwrite(s_infotype[type], 2, 1, data->set.err);
  606. fwrite(ptr, size, 1, data->set.err);
  607. #ifdef CURL_DOES_CONVERSIONS
  608. if(size != conv_size) {
  609. /* we had untranslated data so we need an explicit newline */
  610. fwrite("\n", 1, 1, data->set.err);
  611. }
  612. #endif
  613. break;
  614. default: /* nada */
  615. break;
  616. }
  617. return 0;
  618. }
  619. int Curl_debug(struct SessionHandle *data, curl_infotype type,
  620. char *ptr, size_t size,
  621. struct connectdata *conn)
  622. {
  623. int rc;
  624. if(data->set.printhost && conn && conn->host.dispname) {
  625. char buffer[160];
  626. const char *t=NULL;
  627. const char *w="Data";
  628. switch (type) {
  629. case CURLINFO_HEADER_IN:
  630. w = "Header";
  631. case CURLINFO_DATA_IN:
  632. t = "from";
  633. break;
  634. case CURLINFO_HEADER_OUT:
  635. w = "Header";
  636. case CURLINFO_DATA_OUT:
  637. t = "to";
  638. break;
  639. default:
  640. break;
  641. }
  642. if(t) {
  643. snprintf(buffer, sizeof(buffer), "[%s %s %s]", w, t,
  644. conn->host.dispname);
  645. rc = showit(data, CURLINFO_TEXT, buffer, strlen(buffer));
  646. if(rc)
  647. return rc;
  648. }
  649. }
  650. rc = showit(data, type, ptr, size);
  651. return rc;
  652. }