sendf.c 18 KB

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