easy.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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. /* -- WIN32 approved -- */
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <stdarg.h>
  28. #include <stdlib.h>
  29. #include <ctype.h>
  30. #ifdef HAVE_SYS_TYPES_H
  31. #include <sys/types.h>
  32. #endif
  33. #ifdef HAVE_SYS_STAT_H
  34. #include <sys/stat.h>
  35. #endif
  36. #include <errno.h>
  37. #include "strequal.h"
  38. #ifdef WIN32
  39. #include <time.h>
  40. #include <io.h>
  41. #else
  42. #ifdef HAVE_SYS_SOCKET_H
  43. #include <sys/socket.h>
  44. #endif
  45. #include <netinet/in.h>
  46. #ifdef HAVE_SYS_TIME_H
  47. #include <sys/time.h>
  48. #endif
  49. #ifdef HAVE_UNISTD_H
  50. #include <unistd.h>
  51. #endif
  52. #include <netdb.h>
  53. #ifdef HAVE_ARPA_INET_H
  54. #include <arpa/inet.h>
  55. #endif
  56. #ifdef HAVE_NET_IF_H
  57. #include <net/if.h>
  58. #endif
  59. #include <sys/ioctl.h>
  60. #include <signal.h>
  61. #ifdef HAVE_SYS_PARAM_H
  62. #include <sys/param.h>
  63. #endif
  64. #ifdef HAVE_SYS_SELECT_H
  65. #include <sys/select.h>
  66. #endif
  67. #endif /* WIN32 ... */
  68. #include "urldata.h"
  69. #include <curl/curl.h>
  70. #include "transfer.h"
  71. #include "sslgen.h"
  72. #include "url.h"
  73. #include "getinfo.h"
  74. #include "hostip.h"
  75. #include "share.h"
  76. #include "strdup.h"
  77. #include "memory.h"
  78. #include "progress.h"
  79. #include "easyif.h"
  80. #include "sendf.h" /* for failf function prototype */
  81. #include <ca-bundle.h>
  82. #define _MPRINTF_REPLACE /* use our functions only */
  83. #include <curl/mprintf.h>
  84. #if defined(CURL_DOES_CONVERSIONS) && defined(HAVE_ICONV)
  85. #include <iconv.h>
  86. /* set default codesets for iconv */
  87. #ifndef CURL_ICONV_CODESET_OF_NETWORK
  88. #define CURL_ICONV_CODESET_OF_NETWORK "ISO8859-1"
  89. #endif
  90. #ifndef CURL_ICONV_CODESET_FOR_UTF8
  91. #define CURL_ICONV_CODESET_FOR_UTF8 "UTF-8"
  92. #endif
  93. #define ICONV_ERROR (size_t)-1
  94. #endif /* CURL_DOES_CONVERSIONS && HAVE_ICONV */
  95. /* The last #include file should be: */
  96. #include "memdebug.h"
  97. #ifdef USE_WINSOCK
  98. /* win32_cleanup() is for win32 socket cleanup functionality, the opposite
  99. of win32_init() */
  100. static void win32_cleanup(void)
  101. {
  102. WSACleanup();
  103. }
  104. /* win32_init() performs win32 socket initialization to properly setup the
  105. stack to allow networking */
  106. static CURLcode win32_init(void)
  107. {
  108. WORD wVersionRequested;
  109. WSADATA wsaData;
  110. int err;
  111. #if defined(ENABLE_IPV6) && (USE_WINSOCK < 2)
  112. Error IPV6_requires_winsock2
  113. #endif
  114. wVersionRequested = MAKEWORD(USE_WINSOCK, USE_WINSOCK);
  115. err = WSAStartup(wVersionRequested, &wsaData);
  116. if (err != 0)
  117. /* Tell the user that we couldn't find a useable */
  118. /* winsock.dll. */
  119. return CURLE_FAILED_INIT;
  120. /* Confirm that the Windows Sockets DLL supports what we need.*/
  121. /* Note that if the DLL supports versions greater */
  122. /* than wVersionRequested, it will still return */
  123. /* wVersionRequested in wVersion. wHighVersion contains the */
  124. /* highest supported version. */
  125. if ( LOBYTE( wsaData.wVersion ) != LOBYTE(wVersionRequested) ||
  126. HIBYTE( wsaData.wVersion ) != HIBYTE(wVersionRequested) ) {
  127. /* Tell the user that we couldn't find a useable */
  128. /* winsock.dll. */
  129. WSACleanup();
  130. return CURLE_FAILED_INIT;
  131. }
  132. /* The Windows Sockets DLL is acceptable. Proceed. */
  133. return CURLE_OK;
  134. }
  135. #else
  136. /* These functions exist merely to prevent compiler warnings */
  137. static CURLcode win32_init(void) { return CURLE_OK; }
  138. static void win32_cleanup(void) { }
  139. #endif
  140. #ifdef USE_LIBIDN
  141. /*
  142. * Initialise use of IDNA library.
  143. * It falls back to ASCII if $CHARSET isn't defined. This doesn't work for
  144. * idna_to_ascii_lz().
  145. */
  146. static void idna_init (void)
  147. {
  148. #ifdef WIN32
  149. char buf[60];
  150. UINT cp = GetACP();
  151. if (!getenv("CHARSET") && cp > 0) {
  152. snprintf(buf, sizeof(buf), "CHARSET=cp%u", cp);
  153. putenv(buf);
  154. }
  155. #else
  156. /* to do? */
  157. #endif
  158. }
  159. #endif /* USE_LIBIDN */
  160. /* true globals -- for curl_global_init() and curl_global_cleanup() */
  161. static unsigned int initialized;
  162. static long init_flags;
  163. /*
  164. * strdup (and other memory functions) is redefined in complicated
  165. * ways, but at this point it must be defined as the system-supplied strdup
  166. * so the callback pointer is initialized correctly.
  167. */
  168. #if defined(_WIN32_WCE)
  169. #define system_strdup _strdup
  170. #elif !defined(HAVE_STRDUP)
  171. #define system_strdup curlx_strdup
  172. #else
  173. #define system_strdup strdup
  174. #endif
  175. /*
  176. * If a memory-using function (like curl_getenv) is used before
  177. * curl_global_init() is called, we need to have these pointers set already.
  178. */
  179. curl_malloc_callback Curl_cmalloc = (curl_malloc_callback)malloc;
  180. curl_free_callback Curl_cfree = (curl_free_callback)free;
  181. curl_realloc_callback Curl_crealloc = (curl_realloc_callback)realloc;
  182. curl_strdup_callback Curl_cstrdup = (curl_strdup_callback)system_strdup;
  183. curl_calloc_callback Curl_ccalloc = (curl_calloc_callback)calloc;
  184. /**
  185. * curl_global_init() globally initializes cURL given a bitwise set of the
  186. * different features of what to initialize.
  187. */
  188. CURLcode curl_global_init(long flags)
  189. {
  190. if (initialized++)
  191. return CURLE_OK;
  192. /* Setup the default memory functions here (again) */
  193. Curl_cmalloc = (curl_malloc_callback)malloc;
  194. Curl_cfree = (curl_free_callback)free;
  195. Curl_crealloc = (curl_realloc_callback)realloc;
  196. Curl_cstrdup = (curl_strdup_callback)system_strdup;
  197. Curl_ccalloc = (curl_calloc_callback)calloc;
  198. if (flags & CURL_GLOBAL_SSL)
  199. if (!Curl_ssl_init())
  200. return CURLE_FAILED_INIT;
  201. if (flags & CURL_GLOBAL_WIN32)
  202. if (win32_init() != CURLE_OK)
  203. return CURLE_FAILED_INIT;
  204. #ifdef _AMIGASF
  205. if(!amiga_init())
  206. return CURLE_FAILED_INIT;
  207. #endif
  208. #ifdef USE_LIBIDN
  209. idna_init();
  210. #endif
  211. init_flags = flags;
  212. return CURLE_OK;
  213. }
  214. /*
  215. * curl_global_init_mem() globally initializes cURL and also registers the
  216. * user provided callback routines.
  217. */
  218. CURLcode curl_global_init_mem(long flags, curl_malloc_callback m,
  219. curl_free_callback f, curl_realloc_callback r,
  220. curl_strdup_callback s, curl_calloc_callback c)
  221. {
  222. CURLcode code = CURLE_OK;
  223. /* Invalid input, return immediately */
  224. if (!m || !f || !r || !s || !c)
  225. return CURLE_FAILED_INIT;
  226. /* Already initialized, don't do it again */
  227. if ( initialized )
  228. return CURLE_OK;
  229. /* Call the actual init function first */
  230. code = curl_global_init(flags);
  231. if (code == CURLE_OK) {
  232. Curl_cmalloc = m;
  233. Curl_cfree = f;
  234. Curl_cstrdup = s;
  235. Curl_crealloc = r;
  236. Curl_ccalloc = c;
  237. }
  238. return code;
  239. }
  240. /**
  241. * curl_global_cleanup() globally cleanups cURL, uses the value of
  242. * "init_flags" to determine what needs to be cleaned up and what doesn't.
  243. */
  244. void curl_global_cleanup(void)
  245. {
  246. if (!initialized)
  247. return;
  248. if (--initialized)
  249. return;
  250. Curl_global_host_cache_dtor();
  251. if (init_flags & CURL_GLOBAL_SSL)
  252. Curl_ssl_cleanup();
  253. if (init_flags & CURL_GLOBAL_WIN32)
  254. win32_cleanup();
  255. #ifdef _AMIGASF
  256. amiga_cleanup();
  257. #endif
  258. init_flags = 0;
  259. }
  260. /*
  261. * curl_easy_init() is the external interface to alloc, setup and init an
  262. * easy handle that is returned. If anything goes wrong, NULL is returned.
  263. */
  264. CURL *curl_easy_init(void)
  265. {
  266. CURLcode res;
  267. struct SessionHandle *data;
  268. /* Make sure we inited the global SSL stuff */
  269. if (!initialized) {
  270. res = curl_global_init(CURL_GLOBAL_DEFAULT);
  271. if(res)
  272. /* something in the global init failed, return nothing */
  273. return NULL;
  274. }
  275. /* We use curl_open() with undefined URL so far */
  276. res = Curl_open(&data);
  277. if(res != CURLE_OK)
  278. return NULL;
  279. return data;
  280. }
  281. /*
  282. * curl_easy_setopt() is the external interface for setting options on an
  283. * easy handle.
  284. */
  285. CURLcode curl_easy_setopt(CURL *curl, CURLoption tag, ...)
  286. {
  287. va_list arg;
  288. struct SessionHandle *data = curl;
  289. CURLcode ret;
  290. if(!curl)
  291. return CURLE_BAD_FUNCTION_ARGUMENT;
  292. va_start(arg, tag);
  293. ret = Curl_setopt(data, tag, arg);
  294. va_end(arg);
  295. return ret;
  296. }
  297. #ifdef CURL_MULTIEASY
  298. /***************************************************************************
  299. * This function is still only for testing purposes. It makes a great way
  300. * to run the full test suite on the multi interface instead of the easy one.
  301. ***************************************************************************
  302. *
  303. * The *new* curl_easy_perform() is the external interface that performs a
  304. * transfer previously setup.
  305. *
  306. * Wrapper-function that: creates a multi handle, adds the easy handle to it,
  307. * runs curl_multi_perform() until the transfer is done, then detaches the
  308. * easy handle, destroys the multi handle and returns the easy handle's return
  309. * code. This will make everything internally use and assume multi interface.
  310. */
  311. CURLcode curl_easy_perform(CURL *easy)
  312. {
  313. CURLM *multi;
  314. CURLMcode mcode;
  315. CURLcode code = CURLE_OK;
  316. int still_running;
  317. struct timeval timeout;
  318. int rc;
  319. CURLMsg *msg;
  320. fd_set fdread;
  321. fd_set fdwrite;
  322. fd_set fdexcep;
  323. int maxfd;
  324. if(!easy)
  325. return CURLE_BAD_FUNCTION_ARGUMENT;
  326. multi = curl_multi_init();
  327. if(!multi)
  328. return CURLE_OUT_OF_MEMORY;
  329. mcode = curl_multi_add_handle(multi, easy);
  330. if(mcode) {
  331. curl_multi_cleanup(multi);
  332. return CURLE_FAILED_INIT;
  333. }
  334. /* we start some action by calling perform right away */
  335. do {
  336. while(CURLM_CALL_MULTI_PERFORM ==
  337. curl_multi_perform(multi, &still_running));
  338. if(!still_running)
  339. break;
  340. FD_ZERO(&fdread);
  341. FD_ZERO(&fdwrite);
  342. FD_ZERO(&fdexcep);
  343. /* timeout once per second */
  344. timeout.tv_sec = 1;
  345. timeout.tv_usec = 0;
  346. /* get file descriptors from the transfers */
  347. curl_multi_fdset(multi, &fdread, &fdwrite, &fdexcep, &maxfd);
  348. rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
  349. if(rc == -1)
  350. /* select error */
  351. break;
  352. /* timeout or data to send/receive => loop! */
  353. } while(still_running);
  354. msg = curl_multi_info_read(multi, &rc);
  355. if(msg)
  356. code = msg->data.result;
  357. mcode = curl_multi_remove_handle(multi, easy);
  358. /* what to do if it fails? */
  359. mcode = curl_multi_cleanup(multi);
  360. /* what to do if it fails? */
  361. return code;
  362. }
  363. #else
  364. /*
  365. * curl_easy_perform() is the external interface that performs a transfer
  366. * previously setup.
  367. */
  368. CURLcode curl_easy_perform(CURL *curl)
  369. {
  370. struct SessionHandle *data = (struct SessionHandle *)curl;
  371. if(!data)
  372. return CURLE_BAD_FUNCTION_ARGUMENT;
  373. if ( ! (data->share && data->share->hostcache) ) {
  374. if (Curl_global_host_cache_use(data) &&
  375. (data->dns.hostcachetype != HCACHE_GLOBAL)) {
  376. if (data->dns.hostcachetype == HCACHE_PRIVATE)
  377. Curl_hash_destroy(data->dns.hostcache);
  378. data->dns.hostcache = Curl_global_host_cache_get();
  379. data->dns.hostcachetype = HCACHE_GLOBAL;
  380. }
  381. if (!data->dns.hostcache) {
  382. data->dns.hostcachetype = HCACHE_PRIVATE;
  383. data->dns.hostcache = Curl_mk_dnscache();
  384. if(!data->dns.hostcache)
  385. /* While we possibly could survive and do good without a host cache,
  386. the fact that creating it failed indicates that things are truly
  387. screwed up and we should bail out! */
  388. return CURLE_OUT_OF_MEMORY;
  389. }
  390. }
  391. if(!data->state.connc) {
  392. /* oops, no connection cache, make one up */
  393. data->state.connc = Curl_mk_connc(CONNCACHE_PRIVATE, -1);
  394. if(!data->state.connc)
  395. return CURLE_OUT_OF_MEMORY;
  396. }
  397. return Curl_perform(data);
  398. }
  399. #endif
  400. /*
  401. * curl_easy_cleanup() is the external interface to cleaning/freeing the given
  402. * easy handle.
  403. */
  404. void curl_easy_cleanup(CURL *curl)
  405. {
  406. struct SessionHandle *data = (struct SessionHandle *)curl;
  407. if(!data)
  408. return;
  409. Curl_close(data);
  410. }
  411. /*
  412. * Store a pointed to the multi handle within the easy handle's data struct.
  413. */
  414. void Curl_easy_addmulti(struct SessionHandle *data,
  415. void *multi)
  416. {
  417. data->multi = multi;
  418. }
  419. void Curl_easy_initHandleData(struct SessionHandle *data)
  420. {
  421. memset(&data->reqdata, 0, sizeof(struct HandleData));
  422. data->reqdata.maxdownload = -1;
  423. }
  424. /*
  425. * curl_easy_getinfo() is an external interface that allows an app to retrieve
  426. * information from a performed transfer and similar.
  427. */
  428. CURLcode curl_easy_getinfo(CURL *curl, CURLINFO info, ...)
  429. {
  430. va_list arg;
  431. void *paramp;
  432. struct SessionHandle *data = (struct SessionHandle *)curl;
  433. va_start(arg, info);
  434. paramp = va_arg(arg, void *);
  435. return Curl_getinfo(data, info, paramp);
  436. }
  437. /*
  438. * curl_easy_duphandle() is an external interface to allow duplication of a
  439. * given input easy handle. The returned handle will be a new working handle
  440. * with all options set exactly as the input source handle.
  441. */
  442. CURL *curl_easy_duphandle(CURL *incurl)
  443. {
  444. bool fail = TRUE;
  445. struct SessionHandle *data=(struct SessionHandle *)incurl;
  446. struct SessionHandle *outcurl = (struct SessionHandle *)
  447. calloc(sizeof(struct SessionHandle), 1);
  448. if(NULL == outcurl)
  449. return NULL; /* failure */
  450. do {
  451. /*
  452. * We setup a few buffers we need. We should probably make them
  453. * get setup on-demand in the code, as that would probably decrease
  454. * the likeliness of us forgetting to init a buffer here in the future.
  455. */
  456. outcurl->state.headerbuff=(char*)malloc(HEADERSIZE);
  457. if(!outcurl->state.headerbuff) {
  458. break;
  459. }
  460. outcurl->state.headersize=HEADERSIZE;
  461. /* copy all userdefined values */
  462. outcurl->set = data->set;
  463. if(data->state.used_interface == Curl_if_multi)
  464. outcurl->state.connc = data->state.connc;
  465. else
  466. outcurl->state.connc = Curl_mk_connc(CONNCACHE_PRIVATE, -1);
  467. if(!outcurl->state.connc)
  468. break;
  469. outcurl->state.lastconnect = -1;
  470. outcurl->progress.flags = data->progress.flags;
  471. outcurl->progress.callback = data->progress.callback;
  472. #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
  473. if(data->cookies) {
  474. /* If cookies are enabled in the parent handle, we enable them
  475. in the clone as well! */
  476. outcurl->cookies = Curl_cookie_init(data,
  477. data->cookies->filename,
  478. outcurl->cookies,
  479. data->set.cookiesession);
  480. if(!outcurl->cookies) {
  481. break;
  482. }
  483. }
  484. #endif /* CURL_DISABLE_HTTP */
  485. /* duplicate all values in 'change' */
  486. if(data->change.url) {
  487. outcurl->change.url = strdup(data->change.url);
  488. if(!outcurl->change.url)
  489. break;
  490. outcurl->change.url_alloc = TRUE;
  491. }
  492. if(data->change.referer) {
  493. outcurl->change.referer = strdup(data->change.referer);
  494. if(!outcurl->change.referer)
  495. break;
  496. outcurl->change.referer_alloc = TRUE;
  497. }
  498. #ifdef USE_ARES
  499. /* If we use ares, we setup a new ares channel for the new handle */
  500. if(ARES_SUCCESS != ares_init(&outcurl->state.areschannel))
  501. break;
  502. #endif
  503. #if defined(CURL_DOES_CONVERSIONS) && defined(HAVE_ICONV)
  504. outcurl->inbound_cd = iconv_open(CURL_ICONV_CODESET_OF_HOST,
  505. CURL_ICONV_CODESET_OF_NETWORK);
  506. outcurl->outbound_cd = iconv_open(CURL_ICONV_CODESET_OF_NETWORK,
  507. CURL_ICONV_CODESET_OF_HOST);
  508. outcurl->utf8_cd = iconv_open(CURL_ICONV_CODESET_OF_HOST,
  509. CURL_ICONV_CODESET_FOR_UTF8);
  510. #endif
  511. Curl_easy_initHandleData(outcurl);
  512. outcurl->magic = CURLEASY_MAGIC_NUMBER;
  513. fail = FALSE; /* we reach this point and thus we are OK */
  514. } while(0);
  515. if(fail) {
  516. if(outcurl) {
  517. if(outcurl->state.connc->type == CONNCACHE_PRIVATE)
  518. Curl_rm_connc(outcurl->state.connc);
  519. if(outcurl->state.headerbuff)
  520. free(outcurl->state.headerbuff);
  521. if(outcurl->change.url)
  522. free(outcurl->change.url);
  523. if(outcurl->change.referer)
  524. free(outcurl->change.referer);
  525. free(outcurl); /* free the memory again */
  526. outcurl = NULL;
  527. }
  528. }
  529. return outcurl;
  530. }
  531. /*
  532. * curl_easy_reset() is an external interface that allows an app to re-
  533. * initialize a session handle to the default values.
  534. */
  535. void curl_easy_reset(CURL *curl)
  536. {
  537. struct SessionHandle *data = (struct SessionHandle *)curl;
  538. Curl_safefree(data->reqdata.pathbuffer);
  539. data->reqdata.pathbuffer=NULL;
  540. Curl_safefree(data->reqdata.proto.generic);
  541. data->reqdata.proto.generic=NULL;
  542. /* zero out UserDefined data: */
  543. memset(&data->set, 0, sizeof(struct UserDefined));
  544. /* zero out Progress data: */
  545. memset(&data->progress, 0, sizeof(struct Progress));
  546. /* init Handle data */
  547. Curl_easy_initHandleData(data);
  548. /* The remainder of these calls have been taken from Curl_open() */
  549. data->set.out = stdout; /* default output to stdout */
  550. data->set.in = stdin; /* default input from stdin */
  551. data->set.err = stderr; /* default stderr to stderr */
  552. /* use fwrite as default function to store output */
  553. data->set.fwrite = (curl_write_callback)fwrite;
  554. /* use fread as default function to read input */
  555. data->set.fread = (curl_read_callback)fread;
  556. data->set.infilesize = -1; /* we don't know any size */
  557. data->set.postfieldsize = -1;
  558. data->state.current_speed = -1; /* init to negative == impossible */
  559. data->set.httpreq = HTTPREQ_GET; /* Default HTTP request */
  560. data->set.ftp_use_epsv = TRUE; /* FTP defaults to EPSV operations */
  561. data->set.ftp_use_eprt = TRUE; /* FTP defaults to EPRT operations */
  562. data->set.dns_cache_timeout = 60; /* Timeout every 60 seconds by default */
  563. /* make libcurl quiet by default: */
  564. data->set.hide_progress = TRUE; /* CURLOPT_NOPROGRESS changes these */
  565. data->progress.flags |= PGRS_HIDE;
  566. /* Set the default size of the SSL session ID cache */
  567. data->set.ssl.numsessions = 5;
  568. data->set.proxyport = 1080;
  569. data->set.proxytype = CURLPROXY_HTTP; /* defaults to HTTP proxy */
  570. data->set.httpauth = CURLAUTH_BASIC; /* defaults to basic */
  571. data->set.proxyauth = CURLAUTH_BASIC; /* defaults to basic */
  572. /*
  573. * libcurl 7.10 introduced SSL verification *by default*! This needs to be
  574. * switched off unless wanted.
  575. */
  576. data->set.ssl.verifypeer = TRUE;
  577. data->set.ssl.verifyhost = 2;
  578. #ifdef CURL_CA_BUNDLE
  579. /* This is our prefered CA cert bundle since install time */
  580. data->set.ssl.CAfile = (char *)CURL_CA_BUNDLE;
  581. #endif
  582. data->set.ssh_auth_types = CURLSSH_AUTH_DEFAULT; /* defaults to any auth
  583. type */
  584. }
  585. #ifdef CURL_DOES_CONVERSIONS
  586. /*
  587. * Curl_convert_to_network() is an internal function
  588. * for performing ASCII conversions on non-ASCII platforms.
  589. */
  590. CURLcode Curl_convert_to_network(struct SessionHandle *data,
  591. char *buffer, size_t length)
  592. {
  593. CURLcode rc;
  594. if(data->set.convtonetwork) {
  595. /* use translation callback */
  596. rc = data->set.convtonetwork(buffer, length);
  597. if(rc != CURLE_OK) {
  598. failf(data,
  599. "CURLOPT_CONV_TO_NETWORK_FUNCTION callback returned %i: %s",
  600. rc, curl_easy_strerror(rc));
  601. }
  602. return(rc);
  603. } else {
  604. #ifdef HAVE_ICONV
  605. /* do the translation ourselves */
  606. char *input_ptr, *output_ptr;
  607. size_t in_bytes, out_bytes, rc;
  608. /* open an iconv conversion descriptor if necessary */
  609. if(data->outbound_cd == (iconv_t)-1) {
  610. data->outbound_cd = iconv_open(CURL_ICONV_CODESET_OF_NETWORK,
  611. CURL_ICONV_CODESET_OF_HOST);
  612. if(data->outbound_cd == (iconv_t)-1) {
  613. failf(data,
  614. "The iconv_open(\"%s\", \"%s\") call failed with errno %i: %s",
  615. CURL_ICONV_CODESET_OF_NETWORK,
  616. CURL_ICONV_CODESET_OF_HOST,
  617. errno, strerror(errno));
  618. return CURLE_CONV_FAILED;
  619. }
  620. }
  621. /* call iconv */
  622. input_ptr = output_ptr = buffer;
  623. in_bytes = out_bytes = length;
  624. rc = iconv(data->outbound_cd, (const char**)&input_ptr, &in_bytes,
  625. &output_ptr, &out_bytes);
  626. if ((rc == ICONV_ERROR) || (in_bytes != 0)) {
  627. failf(data,
  628. "The Curl_convert_to_network iconv call failed with errno %i: %s",
  629. errno, strerror(errno));
  630. return CURLE_CONV_FAILED;
  631. }
  632. #else
  633. failf(data, "CURLOPT_CONV_TO_NETWORK_FUNCTION callback required");
  634. return CURLE_CONV_REQD;
  635. #endif /* HAVE_ICONV */
  636. }
  637. return CURLE_OK;
  638. }
  639. /*
  640. * Curl_convert_from_network() is an internal function
  641. * for performing ASCII conversions on non-ASCII platforms.
  642. */
  643. CURLcode Curl_convert_from_network(struct SessionHandle *data,
  644. char *buffer, size_t length)
  645. {
  646. CURLcode rc;
  647. if(data->set.convfromnetwork) {
  648. /* use translation callback */
  649. rc = data->set.convfromnetwork(buffer, length);
  650. if(rc != CURLE_OK) {
  651. failf(data,
  652. "CURLOPT_CONV_FROM_NETWORK_FUNCTION callback returned %i: %s",
  653. rc, curl_easy_strerror(rc));
  654. }
  655. return(rc);
  656. } else {
  657. #ifdef HAVE_ICONV
  658. /* do the translation ourselves */
  659. char *input_ptr, *output_ptr;
  660. size_t in_bytes, out_bytes, rc;
  661. /* open an iconv conversion descriptor if necessary */
  662. if(data->inbound_cd == (iconv_t)-1) {
  663. data->inbound_cd = iconv_open(CURL_ICONV_CODESET_OF_HOST,
  664. CURL_ICONV_CODESET_OF_NETWORK);
  665. if(data->inbound_cd == (iconv_t)-1) {
  666. failf(data,
  667. "The iconv_open(\"%s\", \"%s\") call failed with errno %i: %s",
  668. CURL_ICONV_CODESET_OF_HOST,
  669. CURL_ICONV_CODESET_OF_NETWORK,
  670. errno, strerror(errno));
  671. return CURLE_CONV_FAILED;
  672. }
  673. }
  674. /* call iconv */
  675. input_ptr = output_ptr = buffer;
  676. in_bytes = out_bytes = length;
  677. rc = iconv(data->inbound_cd, (const char **)&input_ptr, &in_bytes,
  678. &output_ptr, &out_bytes);
  679. if ((rc == ICONV_ERROR) || (in_bytes != 0)) {
  680. failf(data,
  681. "The Curl_convert_from_network iconv call failed with errno %i: %s",
  682. errno, strerror(errno));
  683. return CURLE_CONV_FAILED;
  684. }
  685. #else
  686. failf(data, "CURLOPT_CONV_FROM_NETWORK_FUNCTION callback required");
  687. return CURLE_CONV_REQD;
  688. #endif /* HAVE_ICONV */
  689. }
  690. return CURLE_OK;
  691. }
  692. /*
  693. * Curl_convert_from_utf8() is an internal function
  694. * for performing UTF-8 conversions on non-ASCII platforms.
  695. */
  696. CURLcode Curl_convert_from_utf8(struct SessionHandle *data,
  697. char *buffer, size_t length)
  698. {
  699. CURLcode rc;
  700. if(data->set.convfromutf8) {
  701. /* use translation callback */
  702. rc = data->set.convfromutf8(buffer, length);
  703. if(rc != CURLE_OK) {
  704. failf(data,
  705. "CURLOPT_CONV_FROM_UTF8_FUNCTION callback returned %i: %s",
  706. rc, curl_easy_strerror(rc));
  707. }
  708. return(rc);
  709. } else {
  710. #ifdef HAVE_ICONV
  711. /* do the translation ourselves */
  712. char *input_ptr, *output_ptr;
  713. size_t in_bytes, out_bytes, rc;
  714. /* open an iconv conversion descriptor if necessary */
  715. if(data->utf8_cd == (iconv_t)-1) {
  716. data->utf8_cd = iconv_open(CURL_ICONV_CODESET_OF_HOST,
  717. CURL_ICONV_CODESET_FOR_UTF8);
  718. if(data->utf8_cd == (iconv_t)-1) {
  719. failf(data,
  720. "The iconv_open(\"%s\", \"%s\") call failed with errno %i: %s",
  721. CURL_ICONV_CODESET_OF_HOST,
  722. CURL_ICONV_CODESET_FOR_UTF8,
  723. errno, strerror(errno));
  724. return CURLE_CONV_FAILED;
  725. }
  726. }
  727. /* call iconv */
  728. input_ptr = output_ptr = buffer;
  729. in_bytes = out_bytes = length;
  730. rc = iconv(data->utf8_cd, (const char**)&input_ptr, &in_bytes,
  731. &output_ptr, &out_bytes);
  732. if ((rc == ICONV_ERROR) || (in_bytes != 0)) {
  733. failf(data,
  734. "The Curl_convert_from_utf8 iconv call failed with errno %i: %s",
  735. errno, strerror(errno));
  736. return CURLE_CONV_FAILED;
  737. }
  738. if (output_ptr < input_ptr) {
  739. /* null terminate the now shorter output string */
  740. *output_ptr = 0x00;
  741. }
  742. #else
  743. failf(data, "CURLOPT_CONV_FROM_UTF8_FUNCTION callback required");
  744. return CURLE_CONV_REQD;
  745. #endif /* HAVE_ICONV */
  746. }
  747. return CURLE_OK;
  748. }
  749. #endif /* CURL_DOES_CONVERSIONS */