sendf.c 20 KB

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