sendf.c 20 KB

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