sendf.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2004, 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_ourerrno() proto */
  41. #define _MPRINTF_REPLACE /* use the internal *printf() functions */
  42. #include <curl/mprintf.h>
  43. #ifdef HAVE_KRB4
  44. #include "security.h"
  45. #endif
  46. #include <string.h>
  47. #include "curl_memory.h"
  48. /* The last #include file should be: */
  49. #include "memdebug.h"
  50. /* returns last node in linked list */
  51. static struct curl_slist *slist_get_last(struct curl_slist *list)
  52. {
  53. struct curl_slist *item;
  54. /* if caller passed us a NULL, return now */
  55. if (!list)
  56. return NULL;
  57. /* loop through to find the last item */
  58. item = list;
  59. while (item->next) {
  60. item = item->next;
  61. }
  62. return item;
  63. }
  64. /*
  65. * curl_slist_append() appends a string to the linked list. It always retunrs
  66. * the address of the first record, so that you can sure this function as an
  67. * initialization function as well as an append function. If you find this
  68. * bothersome, then simply create a separate _init function and call it
  69. * appropriately from within the proram.
  70. */
  71. struct curl_slist *curl_slist_append(struct curl_slist *list,
  72. const char *data)
  73. {
  74. struct curl_slist *last;
  75. struct curl_slist *new_item;
  76. new_item = (struct curl_slist *) malloc(sizeof(struct curl_slist));
  77. if (new_item) {
  78. char *dup = strdup(data);
  79. if(dup) {
  80. new_item->next = NULL;
  81. new_item->data = dup;
  82. }
  83. else {
  84. free(new_item);
  85. return NULL;
  86. }
  87. }
  88. else
  89. return NULL;
  90. if (list) {
  91. last = slist_get_last(list);
  92. last->next = new_item;
  93. return list;
  94. }
  95. /* if this is the first item, then new_item *is* the list */
  96. return new_item;
  97. }
  98. /* be nice and clean up resources */
  99. void curl_slist_free_all(struct curl_slist *list)
  100. {
  101. struct curl_slist *next;
  102. struct curl_slist *item;
  103. if (!list)
  104. return;
  105. item = list;
  106. do {
  107. next = item->next;
  108. if (item->data) {
  109. free(item->data);
  110. }
  111. free(item);
  112. item = next;
  113. } while (next);
  114. }
  115. /* Curl_infof() is for info message along the way */
  116. void Curl_infof(struct SessionHandle *data, const char *fmt, ...)
  117. {
  118. if(data && data->set.verbose) {
  119. va_list ap;
  120. char print_buffer[1024 + 1];
  121. va_start(ap, fmt);
  122. vsnprintf(print_buffer, 1024, fmt, ap);
  123. va_end(ap);
  124. Curl_debug(data, CURLINFO_TEXT, print_buffer, strlen(print_buffer), NULL);
  125. }
  126. }
  127. /* Curl_failf() is for messages stating why we failed.
  128. * The message SHALL NOT include any LF or CR.
  129. */
  130. void Curl_failf(struct SessionHandle *data, const char *fmt, ...)
  131. {
  132. va_list ap;
  133. va_start(ap, fmt);
  134. if(data->set.errorbuffer && !data->state.errorbuf) {
  135. vsnprintf(data->set.errorbuffer, CURL_ERROR_SIZE, fmt, ap);
  136. data->state.errorbuf = TRUE; /* wrote error string */
  137. if(data->set.verbose) {
  138. size_t len = strlen(data->set.errorbuffer);
  139. bool doneit=FALSE;
  140. if(len < CURL_ERROR_SIZE - 1) {
  141. doneit = TRUE;
  142. data->set.errorbuffer[len] = '\n';
  143. data->set.errorbuffer[++len] = '\0';
  144. }
  145. Curl_debug(data, CURLINFO_TEXT, data->set.errorbuffer, len, NULL);
  146. if(doneit)
  147. /* cut off the newline again */
  148. data->set.errorbuffer[--len]=0;
  149. }
  150. }
  151. va_end(ap);
  152. }
  153. /* Curl_sendf() sends formated data to the server */
  154. CURLcode Curl_sendf(curl_socket_t sockfd, struct connectdata *conn,
  155. const char *fmt, ...)
  156. {
  157. struct SessionHandle *data = conn->data;
  158. ssize_t bytes_written;
  159. size_t write_len;
  160. CURLcode res;
  161. char *s;
  162. char *sptr;
  163. va_list ap;
  164. va_start(ap, fmt);
  165. s = vaprintf(fmt, ap); /* returns an allocated string */
  166. va_end(ap);
  167. if(!s)
  168. return CURLE_OUT_OF_MEMORY; /* failure */
  169. bytes_written=0;
  170. write_len = strlen(s);
  171. sptr = s;
  172. while (1) {
  173. /* Write the buffer to the socket */
  174. res = Curl_write(conn, sockfd, sptr, write_len, &bytes_written);
  175. if(CURLE_OK != res)
  176. break;
  177. if(data->set.verbose)
  178. Curl_debug(data, CURLINFO_DATA_OUT, sptr, bytes_written,
  179. conn->host.dispname);
  180. if((size_t)bytes_written != write_len) {
  181. /* if not all was written at once, we must advance the pointer, decrease
  182. the size left and try again! */
  183. write_len -= bytes_written;
  184. sptr += bytes_written;
  185. }
  186. else
  187. break;
  188. }
  189. free(s); /* free the output string */
  190. return res;
  191. }
  192. /*
  193. * Curl_write() is an internal write function that sends plain (binary) data
  194. * to the server. Works with plain sockets, SSL or kerberos.
  195. */
  196. CURLcode Curl_write(struct connectdata *conn,
  197. curl_socket_t sockfd,
  198. void *mem,
  199. size_t len,
  200. ssize_t *written)
  201. {
  202. ssize_t bytes_written;
  203. CURLcode retcode;
  204. #ifdef USE_SSLEAY
  205. /* Set 'num' to 0 or 1, depending on which socket that has been sent here.
  206. If it is the second socket, we set num to 1. Otherwise to 0. This lets
  207. us use the correct ssl handle. */
  208. int num = (sockfd == conn->sock[SECONDARYSOCKET]);
  209. /* SSL_write() is said to return 'int' while write() and send() returns
  210. 'size_t' */
  211. if (conn->ssl[num].use) {
  212. int err;
  213. char error_buffer[120]; /* OpenSSL documents that this must be at least
  214. 120 bytes long. */
  215. unsigned long sslerror;
  216. int rc = SSL_write(conn->ssl[num].handle, mem, (int)len);
  217. if(rc < 0) {
  218. err = SSL_get_error(conn->ssl[num].handle, rc);
  219. switch(err) {
  220. case SSL_ERROR_WANT_READ:
  221. case SSL_ERROR_WANT_WRITE:
  222. /* The operation did not complete; the same TLS/SSL I/O function
  223. should be called again later. This is basicly an EWOULDBLOCK
  224. equivalent. */
  225. *written = 0;
  226. return CURLE_OK;
  227. case SSL_ERROR_SYSCALL:
  228. failf(conn->data, "SSL_write() returned SYSCALL, errno = %d\n",
  229. Curl_ourerrno());
  230. return CURLE_SEND_ERROR;
  231. case SSL_ERROR_SSL:
  232. /* A failure in the SSL library occurred, usually a protocol error.
  233. The OpenSSL error queue contains more information on the error. */
  234. sslerror = ERR_get_error();
  235. failf(conn->data, "SSL_write() error: %s\n",
  236. ERR_error_string(sslerror, error_buffer));
  237. return CURLE_SEND_ERROR;
  238. }
  239. /* a true error */
  240. failf(conn->data, "SSL_write() return error %d\n", err);
  241. return CURLE_SEND_ERROR;
  242. }
  243. bytes_written = rc;
  244. }
  245. else {
  246. #else
  247. (void)conn;
  248. #endif
  249. #ifdef HAVE_KRB4
  250. if(conn->sec_complete) {
  251. bytes_written = Curl_sec_write(conn, sockfd, mem, len);
  252. }
  253. else
  254. #endif /* HAVE_KRB4 */
  255. {
  256. bytes_written = (ssize_t)swrite(sockfd, mem, len);
  257. }
  258. if(-1 == bytes_written) {
  259. int err = Curl_ourerrno();
  260. if(
  261. #ifdef WSAEWOULDBLOCK
  262. /* This is how Windows does it */
  263. (WSAEWOULDBLOCK == err)
  264. #else
  265. /* As pointed out by Christophe Demory on March 11 2003, errno
  266. may be EWOULDBLOCK or on some systems EAGAIN when it returned
  267. due to its inability to send off data without blocking. We
  268. therefor treat both error codes the same here */
  269. (EWOULDBLOCK == err) || (EAGAIN == err) || (EINTR == err)
  270. #endif
  271. )
  272. /* this is just a case of EWOULDBLOCK */
  273. bytes_written=0;
  274. }
  275. #ifdef USE_SSLEAY
  276. }
  277. #endif
  278. *written = bytes_written;
  279. retcode = (-1 != bytes_written)?CURLE_OK:CURLE_SEND_ERROR;
  280. return retcode;
  281. }
  282. /* client_write() sends data to the write callback(s)
  283. The bit pattern defines to what "streams" to write to. Body and/or header.
  284. The defines are in sendf.h of course.
  285. */
  286. CURLcode Curl_client_write(struct SessionHandle *data,
  287. int type,
  288. char *ptr,
  289. size_t len)
  290. {
  291. size_t wrote;
  292. if(0 == len)
  293. len = strlen(ptr);
  294. if(type & CLIENTWRITE_BODY) {
  295. wrote = data->set.fwrite(ptr, 1, len, data->set.out);
  296. if(wrote != len) {
  297. failf (data, "Failed writing body");
  298. return CURLE_WRITE_ERROR;
  299. }
  300. }
  301. if((type & CLIENTWRITE_HEADER) &&
  302. (data->set.fwrite_header || data->set.writeheader) ) {
  303. /*
  304. * Write headers to the same callback or to the especially setup
  305. * header callback function (added after version 7.7.1).
  306. */
  307. curl_write_callback writeit=
  308. data->set.fwrite_header?data->set.fwrite_header:data->set.fwrite;
  309. wrote = writeit(ptr, 1, len, data->set.writeheader);
  310. if(wrote != len) {
  311. failf (data, "Failed writing header");
  312. return CURLE_WRITE_ERROR;
  313. }
  314. }
  315. return CURLE_OK;
  316. }
  317. /*
  318. * Internal read-from-socket function. This is meant to deal with plain
  319. * sockets, SSL sockets and kerberos sockets.
  320. *
  321. * If the read would block (EWOULDBLOCK) we return -1. Otherwise we return
  322. * a regular CURLcode value.
  323. */
  324. int Curl_read(struct connectdata *conn, /* connection data */
  325. curl_socket_t sockfd, /* read from this socket */
  326. char *buf, /* store read data here */
  327. size_t buffersize, /* max amount to read */
  328. ssize_t *n) /* amount bytes read */
  329. {
  330. ssize_t nread;
  331. #ifdef USE_SSLEAY
  332. /* Set 'num' to 0 or 1, depending on which socket that has been sent here.
  333. If it is the second socket, we set num to 1. Otherwise to 0. This lets
  334. us use the correct ssl handle. */
  335. int num = (sockfd == conn->sock[SECONDARYSOCKET]);
  336. *n=0; /* reset amount to zero */
  337. if (conn->ssl[num].use) {
  338. nread = (ssize_t)SSL_read(conn->ssl[num].handle, buf, (int)buffersize);
  339. if(nread < 0) {
  340. /* failed SSL_read */
  341. int err = SSL_get_error(conn->ssl[num].handle, (int)nread);
  342. switch(err) {
  343. case SSL_ERROR_NONE: /* this is not an error */
  344. case SSL_ERROR_ZERO_RETURN: /* no more data */
  345. break;
  346. case SSL_ERROR_WANT_READ:
  347. case SSL_ERROR_WANT_WRITE:
  348. /* there's data pending, re-invoke SSL_read() */
  349. return -1; /* basicly EWOULDBLOCK */
  350. default:
  351. /* openssl/ssl.h says "look at error stack/return value/errno" */
  352. {
  353. char error_buffer[120]; /* OpenSSL documents that this must be at
  354. least 120 bytes long. */
  355. unsigned long sslerror = ERR_get_error();
  356. failf(conn->data, "SSL read: %s, errno %d",
  357. ERR_error_string(sslerror, error_buffer),
  358. Curl_ourerrno() );
  359. }
  360. return CURLE_RECV_ERROR;
  361. }
  362. }
  363. }
  364. else {
  365. #else
  366. (void)conn;
  367. #endif
  368. *n=0; /* reset amount to zero */
  369. #ifdef HAVE_KRB4
  370. if(conn->sec_complete)
  371. nread = Curl_sec_read(conn, sockfd, buf, buffersize);
  372. else
  373. #endif
  374. nread = sread(sockfd, buf, buffersize);
  375. if(-1 == nread) {
  376. int err = Curl_ourerrno();
  377. #ifdef WIN32
  378. if(WSAEWOULDBLOCK == err)
  379. #else
  380. if((EWOULDBLOCK == err) || (EAGAIN == err) || (EINTR == err))
  381. #endif
  382. return -1;
  383. }
  384. #ifdef USE_SSLEAY
  385. }
  386. #endif /* USE_SSLEAY */
  387. *n = nread;
  388. return CURLE_OK;
  389. }
  390. /* return 0 on success */
  391. static int showit(struct SessionHandle *data, curl_infotype type,
  392. char *ptr, size_t size)
  393. {
  394. static const char * const s_infotype[CURLINFO_END] = {
  395. "* ", "< ", "> ", "{ ", "} ", "{ ", "} " };
  396. if(data->set.fdebug)
  397. return (*data->set.fdebug)(data, type, ptr, size,
  398. data->set.debugdata);
  399. switch(type) {
  400. case CURLINFO_TEXT:
  401. case CURLINFO_HEADER_OUT:
  402. case CURLINFO_HEADER_IN:
  403. fwrite(s_infotype[type], 2, 1, data->set.err);
  404. fwrite(ptr, size, 1, data->set.err);
  405. break;
  406. default: /* nada */
  407. break;
  408. }
  409. return 0;
  410. }
  411. int Curl_debug(struct SessionHandle *data, curl_infotype type,
  412. char *ptr, size_t size, char *host)
  413. {
  414. int rc;
  415. if(data->set.printhost && host) {
  416. char buffer[160];
  417. const char *t=NULL;
  418. switch (type) {
  419. case CURLINFO_HEADER_IN:
  420. case CURLINFO_DATA_IN:
  421. t = "from";
  422. break;
  423. case CURLINFO_HEADER_OUT:
  424. case CURLINFO_DATA_OUT:
  425. t = "to";
  426. break;
  427. default:
  428. break;
  429. }
  430. if(t) {
  431. snprintf(buffer, sizeof(buffer), "[Data %s %s]", t, host);
  432. rc = showit(data, CURLINFO_TEXT, buffer, strlen(buffer));
  433. if(rc)
  434. return rc;
  435. }
  436. }
  437. rc = showit(data, type, ptr, size);
  438. return rc;
  439. }