sendf.c 13 KB

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