sendf.c 21 KB

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