easy.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2010, 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. ***************************************************************************/
  22. #include "setup.h"
  23. /* -- WIN32 approved -- */
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <stdarg.h>
  27. #include <stdlib.h>
  28. #include <ctype.h>
  29. #include <errno.h>
  30. #include "strequal.h"
  31. #ifdef WIN32
  32. #include <time.h>
  33. #include <io.h>
  34. #else
  35. #ifdef HAVE_SYS_SOCKET_H
  36. #include <sys/socket.h>
  37. #endif
  38. #ifdef HAVE_NETINET_IN_H
  39. #include <netinet/in.h>
  40. #endif
  41. #ifdef HAVE_SYS_TIME_H
  42. #include <sys/time.h>
  43. #endif
  44. #ifdef HAVE_UNISTD_H
  45. #include <unistd.h>
  46. #endif
  47. #ifdef HAVE_NETDB_H
  48. #include <netdb.h>
  49. #endif
  50. #ifdef HAVE_ARPA_INET_H
  51. #include <arpa/inet.h>
  52. #endif
  53. #ifdef HAVE_NET_IF_H
  54. #include <net/if.h>
  55. #endif
  56. #ifdef HAVE_SYS_IOCTL_H
  57. #include <sys/ioctl.h>
  58. #endif
  59. #ifdef HAVE_SYS_PARAM_H
  60. #include <sys/param.h>
  61. #endif
  62. #endif /* WIN32 ... */
  63. #include "urldata.h"
  64. #include <curl/curl.h>
  65. #include "transfer.h"
  66. #include "sslgen.h"
  67. #include "url.h"
  68. #include "getinfo.h"
  69. #include "hostip.h"
  70. #include "share.h"
  71. #include "strdup.h"
  72. #include "curl_memory.h"
  73. #include "progress.h"
  74. #include "easyif.h"
  75. #include "select.h"
  76. #include "sendf.h" /* for failf function prototype */
  77. #include "http_ntlm.h"
  78. #include "connect.h" /* for Curl_getconnectinfo */
  79. #include "slist.h"
  80. #include "curl_rand.h"
  81. #define _MPRINTF_REPLACE /* use our functions only */
  82. #include <curl/mprintf.h>
  83. #if defined(CURL_DOES_CONVERSIONS) && defined(HAVE_ICONV)
  84. #include <iconv.h>
  85. /* set default codesets for iconv */
  86. #ifndef CURL_ICONV_CODESET_OF_NETWORK
  87. #define CURL_ICONV_CODESET_OF_NETWORK "ISO8859-1"
  88. #endif
  89. #ifndef CURL_ICONV_CODESET_FOR_UTF8
  90. #define CURL_ICONV_CODESET_FOR_UTF8 "UTF-8"
  91. #endif
  92. #define ICONV_ERROR (size_t)-1
  93. #endif /* CURL_DOES_CONVERSIONS && HAVE_ICONV */
  94. /* The last #include file should be: */
  95. #include "memdebug.h"
  96. /* win32_cleanup() is for win32 socket cleanup functionality, the opposite
  97. of win32_init() */
  98. static void win32_cleanup(void)
  99. {
  100. #ifdef USE_WINSOCK
  101. WSACleanup();
  102. #endif
  103. #ifdef USE_WINDOWS_SSPI
  104. Curl_sspi_global_cleanup();
  105. #endif
  106. }
  107. /* win32_init() performs win32 socket initialization to properly setup the
  108. stack to allow networking */
  109. static CURLcode win32_init(void)
  110. {
  111. #ifdef USE_WINSOCK
  112. WORD wVersionRequested;
  113. WSADATA wsaData;
  114. int res;
  115. #if defined(ENABLE_IPV6) && (USE_WINSOCK < 2)
  116. Error IPV6_requires_winsock2
  117. #endif
  118. wVersionRequested = MAKEWORD(USE_WINSOCK, USE_WINSOCK);
  119. res = WSAStartup(wVersionRequested, &wsaData);
  120. if(res != 0)
  121. /* Tell the user that we couldn't find a useable */
  122. /* winsock.dll. */
  123. return CURLE_FAILED_INIT;
  124. /* Confirm that the Windows Sockets DLL supports what we need.*/
  125. /* Note that if the DLL supports versions greater */
  126. /* than wVersionRequested, it will still return */
  127. /* wVersionRequested in wVersion. wHighVersion contains the */
  128. /* highest supported version. */
  129. if( LOBYTE( wsaData.wVersion ) != LOBYTE(wVersionRequested) ||
  130. HIBYTE( wsaData.wVersion ) != HIBYTE(wVersionRequested) ) {
  131. /* Tell the user that we couldn't find a useable */
  132. /* winsock.dll. */
  133. WSACleanup();
  134. return CURLE_FAILED_INIT;
  135. }
  136. /* The Windows Sockets DLL is acceptable. Proceed. */
  137. #endif
  138. #ifdef USE_WINDOWS_SSPI
  139. {
  140. CURLcode err = Curl_sspi_global_init();
  141. if (err != CURLE_OK)
  142. return err;
  143. }
  144. #endif
  145. return CURLE_OK;
  146. }
  147. #ifdef USE_LIBIDN
  148. /*
  149. * Initialise use of IDNA library.
  150. * It falls back to ASCII if $CHARSET isn't defined. This doesn't work for
  151. * idna_to_ascii_lz().
  152. */
  153. static void idna_init (void)
  154. {
  155. #ifdef WIN32
  156. char buf[60];
  157. UINT cp = GetACP();
  158. if(!getenv("CHARSET") && cp > 0) {
  159. snprintf(buf, sizeof(buf), "CHARSET=cp%u", cp);
  160. putenv(buf);
  161. }
  162. #else
  163. /* to do? */
  164. #endif
  165. }
  166. #endif /* USE_LIBIDN */
  167. /* true globals -- for curl_global_init() and curl_global_cleanup() */
  168. static unsigned int initialized;
  169. static long init_flags;
  170. /*
  171. * strdup (and other memory functions) is redefined in complicated
  172. * ways, but at this point it must be defined as the system-supplied strdup
  173. * so the callback pointer is initialized correctly.
  174. */
  175. #if defined(_WIN32_WCE)
  176. #define system_strdup _strdup
  177. #elif !defined(HAVE_STRDUP)
  178. #define system_strdup curlx_strdup
  179. #else
  180. #define system_strdup strdup
  181. #endif
  182. #if defined(_MSC_VER) && defined(_DLL) && !defined(__POCC__)
  183. # pragma warning(disable:4232) /* MSVC extension, dllimport identity */
  184. #endif
  185. #ifndef __SYMBIAN32__
  186. /*
  187. * If a memory-using function (like curl_getenv) is used before
  188. * curl_global_init() is called, we need to have these pointers set already.
  189. */
  190. curl_malloc_callback Curl_cmalloc = (curl_malloc_callback)malloc;
  191. curl_free_callback Curl_cfree = (curl_free_callback)free;
  192. curl_realloc_callback Curl_crealloc = (curl_realloc_callback)realloc;
  193. curl_strdup_callback Curl_cstrdup = (curl_strdup_callback)system_strdup;
  194. curl_calloc_callback Curl_ccalloc = (curl_calloc_callback)calloc;
  195. #else
  196. /*
  197. * Symbian OS doesn't support initialization to code in writeable static data.
  198. * Initialization will occur in the curl_global_init() call.
  199. */
  200. curl_malloc_callback Curl_cmalloc;
  201. curl_free_callback Curl_cfree;
  202. curl_realloc_callback Curl_crealloc;
  203. curl_strdup_callback Curl_cstrdup;
  204. curl_calloc_callback Curl_ccalloc;
  205. #endif
  206. #if defined(_MSC_VER) && defined(_DLL) && !defined(__POCC__)
  207. # pragma warning(default:4232) /* MSVC extension, dllimport identity */
  208. #endif
  209. /**
  210. * curl_global_init() globally initializes cURL given a bitwise set of the
  211. * different features of what to initialize.
  212. */
  213. CURLcode curl_global_init(long flags)
  214. {
  215. if(initialized++)
  216. return CURLE_OK;
  217. /* Setup the default memory functions here (again) */
  218. Curl_cmalloc = (curl_malloc_callback)malloc;
  219. Curl_cfree = (curl_free_callback)free;
  220. Curl_crealloc = (curl_realloc_callback)realloc;
  221. Curl_cstrdup = (curl_strdup_callback)system_strdup;
  222. Curl_ccalloc = (curl_calloc_callback)calloc;
  223. if(flags & CURL_GLOBAL_SSL)
  224. if(!Curl_ssl_init()) {
  225. DEBUGF(fprintf(stderr, "Error: Curl_ssl_init failed\n"));
  226. return CURLE_FAILED_INIT;
  227. }
  228. if(flags & CURL_GLOBAL_WIN32)
  229. if(win32_init() != CURLE_OK) {
  230. DEBUGF(fprintf(stderr, "Error: win32_init failed\n"));
  231. return CURLE_FAILED_INIT;
  232. }
  233. #ifdef __AMIGA__
  234. if(!amiga_init()) {
  235. DEBUGF(fprintf(stderr, "Error: amiga_init failed\n"));
  236. return CURLE_FAILED_INIT;
  237. }
  238. #endif
  239. #ifdef NETWARE
  240. if(netware_init()) {
  241. DEBUGF(fprintf(stderr, "Warning: LONG namespace not available\n"));
  242. }
  243. #endif
  244. #ifdef USE_LIBIDN
  245. idna_init();
  246. #endif
  247. #ifdef CARES_HAVE_ARES_LIBRARY_INIT
  248. if(ares_library_init(ARES_LIB_INIT_ALL)) {
  249. DEBUGF(fprintf(stderr, "Error: ares_library_init failed\n"));
  250. return CURLE_FAILED_INIT;
  251. }
  252. #endif
  253. #if defined(USE_LIBSSH2) && defined(HAVE_LIBSSH2_INIT)
  254. if(libssh2_init(0)) {
  255. DEBUGF(fprintf(stderr, "Error: libssh2_init failed\n"));
  256. return CURLE_FAILED_INIT;
  257. }
  258. #endif
  259. init_flags = flags;
  260. /* Preset pseudo-random number sequence. */
  261. Curl_srand();
  262. return CURLE_OK;
  263. }
  264. /*
  265. * curl_global_init_mem() globally initializes cURL and also registers the
  266. * user provided callback routines.
  267. */
  268. CURLcode curl_global_init_mem(long flags, curl_malloc_callback m,
  269. curl_free_callback f, curl_realloc_callback r,
  270. curl_strdup_callback s, curl_calloc_callback c)
  271. {
  272. CURLcode code = CURLE_OK;
  273. /* Invalid input, return immediately */
  274. if(!m || !f || !r || !s || !c)
  275. return CURLE_FAILED_INIT;
  276. /* Already initialized, don't do it again */
  277. if( initialized )
  278. return CURLE_OK;
  279. /* Call the actual init function first */
  280. code = curl_global_init(flags);
  281. if(code == CURLE_OK) {
  282. Curl_cmalloc = m;
  283. Curl_cfree = f;
  284. Curl_cstrdup = s;
  285. Curl_crealloc = r;
  286. Curl_ccalloc = c;
  287. }
  288. return code;
  289. }
  290. /**
  291. * curl_global_cleanup() globally cleanups cURL, uses the value of
  292. * "init_flags" to determine what needs to be cleaned up and what doesn't.
  293. */
  294. void curl_global_cleanup(void)
  295. {
  296. if(!initialized)
  297. return;
  298. if(--initialized)
  299. return;
  300. Curl_global_host_cache_dtor();
  301. if(init_flags & CURL_GLOBAL_SSL)
  302. Curl_ssl_cleanup();
  303. #ifdef CARES_HAVE_ARES_LIBRARY_CLEANUP
  304. ares_library_cleanup();
  305. #endif
  306. if(init_flags & CURL_GLOBAL_WIN32)
  307. win32_cleanup();
  308. #ifdef __AMIGA__
  309. amiga_cleanup();
  310. #endif
  311. #if defined(USE_LIBSSH2) && defined(HAVE_LIBSSH2_EXIT)
  312. (void)libssh2_exit();
  313. #endif
  314. init_flags = 0;
  315. }
  316. /*
  317. * curl_easy_init() is the external interface to alloc, setup and init an
  318. * easy handle that is returned. If anything goes wrong, NULL is returned.
  319. */
  320. CURL *curl_easy_init(void)
  321. {
  322. CURLcode res;
  323. struct SessionHandle *data;
  324. /* Make sure we inited the global SSL stuff */
  325. if(!initialized) {
  326. res = curl_global_init(CURL_GLOBAL_DEFAULT);
  327. if(res) {
  328. /* something in the global init failed, return nothing */
  329. DEBUGF(fprintf(stderr, "Error: curl_global_init failed\n"));
  330. return NULL;
  331. }
  332. }
  333. /* We use curl_open() with undefined URL so far */
  334. res = Curl_open(&data);
  335. if(res != CURLE_OK) {
  336. DEBUGF(fprintf(stderr, "Error: Curl_open failed\n"));
  337. return NULL;
  338. }
  339. return data;
  340. }
  341. /*
  342. * curl_easy_setopt() is the external interface for setting options on an
  343. * easy handle.
  344. */
  345. #undef curl_easy_setopt
  346. CURLcode curl_easy_setopt(CURL *curl, CURLoption tag, ...)
  347. {
  348. va_list arg;
  349. struct SessionHandle *data = curl;
  350. CURLcode ret;
  351. if(!curl)
  352. return CURLE_BAD_FUNCTION_ARGUMENT;
  353. va_start(arg, tag);
  354. ret = Curl_setopt(data, tag, arg);
  355. va_end(arg);
  356. return ret;
  357. }
  358. #ifdef CURL_MULTIEASY
  359. /***************************************************************************
  360. * This function is still only for testing purposes. It makes a great way
  361. * to run the full test suite on the multi interface instead of the easy one.
  362. ***************************************************************************
  363. *
  364. * The *new* curl_easy_perform() is the external interface that performs a
  365. * transfer previously setup.
  366. *
  367. * Wrapper-function that: creates a multi handle, adds the easy handle to it,
  368. * runs curl_multi_perform() until the transfer is done, then detaches the
  369. * easy handle, destroys the multi handle and returns the easy handle's return
  370. * code. This will make everything internally use and assume multi interface.
  371. */
  372. CURLcode curl_easy_perform(CURL *easy)
  373. {
  374. CURLM *multi;
  375. CURLMcode mcode;
  376. CURLcode code = CURLE_OK;
  377. int still_running;
  378. struct timeval timeout;
  379. int rc;
  380. CURLMsg *msg;
  381. fd_set fdread;
  382. fd_set fdwrite;
  383. fd_set fdexcep;
  384. int maxfd;
  385. if(!easy)
  386. return CURLE_BAD_FUNCTION_ARGUMENT;
  387. multi = curl_multi_init();
  388. if(!multi)
  389. return CURLE_OUT_OF_MEMORY;
  390. mcode = curl_multi_add_handle(multi, easy);
  391. if(mcode) {
  392. curl_multi_cleanup(multi);
  393. if(mcode == CURLM_OUT_OF_MEMORY)
  394. return CURLE_OUT_OF_MEMORY;
  395. else
  396. return CURLE_FAILED_INIT;
  397. }
  398. /* we start some action by calling perform right away */
  399. do {
  400. while(CURLM_CALL_MULTI_PERFORM ==
  401. curl_multi_perform(multi, &still_running));
  402. if(!still_running)
  403. break;
  404. FD_ZERO(&fdread);
  405. FD_ZERO(&fdwrite);
  406. FD_ZERO(&fdexcep);
  407. /* timeout once per second */
  408. timeout.tv_sec = 1;
  409. timeout.tv_usec = 0;
  410. /* Old deprecated style: get file descriptors from the transfers */
  411. curl_multi_fdset(multi, &fdread, &fdwrite, &fdexcep, &maxfd);
  412. rc = Curl_select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
  413. /* The way is to extract the sockets and wait for them without using
  414. select. This whole alternative version should probably rather use the
  415. curl_multi_socket() approach. */
  416. if(rc == -1)
  417. /* select error */
  418. break;
  419. /* timeout or data to send/receive => loop! */
  420. } while(still_running);
  421. msg = curl_multi_info_read(multi, &rc);
  422. if(msg)
  423. code = msg->data.result;
  424. mcode = curl_multi_remove_handle(multi, easy);
  425. /* what to do if it fails? */
  426. mcode = curl_multi_cleanup(multi);
  427. /* what to do if it fails? */
  428. return code;
  429. }
  430. #else
  431. /*
  432. * curl_easy_perform() is the external interface that performs a transfer
  433. * previously setup.
  434. */
  435. CURLcode curl_easy_perform(CURL *curl)
  436. {
  437. struct SessionHandle *data = (struct SessionHandle *)curl;
  438. if(!data)
  439. return CURLE_BAD_FUNCTION_ARGUMENT;
  440. if( ! (data->share && data->share->hostcache) ) {
  441. /* this handle is not using a shared dns cache */
  442. if(data->set.global_dns_cache &&
  443. (data->dns.hostcachetype != HCACHE_GLOBAL)) {
  444. /* global dns cache was requested but still isn't */
  445. struct curl_hash *ptr;
  446. if(data->dns.hostcachetype == HCACHE_PRIVATE) {
  447. /* if the current cache is private, kill it first */
  448. Curl_hash_destroy(data->dns.hostcache);
  449. data->dns.hostcachetype = HCACHE_NONE;
  450. data->dns.hostcache = NULL;
  451. }
  452. ptr = Curl_global_host_cache_init();
  453. if(ptr) {
  454. /* only do this if the global cache init works */
  455. data->dns.hostcache = ptr;
  456. data->dns.hostcachetype = HCACHE_GLOBAL;
  457. }
  458. }
  459. if(!data->dns.hostcache) {
  460. data->dns.hostcachetype = HCACHE_PRIVATE;
  461. data->dns.hostcache = Curl_mk_dnscache();
  462. if(!data->dns.hostcache)
  463. /* While we possibly could survive and do good without a host cache,
  464. the fact that creating it failed indicates that things are truly
  465. screwed up and we should bail out! */
  466. return CURLE_OUT_OF_MEMORY;
  467. }
  468. }
  469. if(!data->state.connc) {
  470. /* oops, no connection cache, make one up */
  471. data->state.connc = Curl_mk_connc(CONNCACHE_PRIVATE, -1L);
  472. if(!data->state.connc)
  473. return CURLE_OUT_OF_MEMORY;
  474. }
  475. return Curl_perform(data);
  476. }
  477. #endif
  478. /*
  479. * curl_easy_cleanup() is the external interface to cleaning/freeing the given
  480. * easy handle.
  481. */
  482. void curl_easy_cleanup(CURL *curl)
  483. {
  484. struct SessionHandle *data = (struct SessionHandle *)curl;
  485. if(!data)
  486. return;
  487. Curl_close(data);
  488. }
  489. /*
  490. * Store a pointed to the multi handle within the easy handle's data struct.
  491. */
  492. void Curl_easy_addmulti(struct SessionHandle *data,
  493. void *multi)
  494. {
  495. data->multi = multi;
  496. if(multi == NULL)
  497. /* the association is cleared, mark the easy handle as not used by an
  498. interface */
  499. data->state.used_interface = Curl_if_none;
  500. }
  501. void Curl_easy_initHandleData(struct SessionHandle *data)
  502. {
  503. memset(&data->req, 0, sizeof(struct SingleRequest));
  504. data->req.maxdownload = -1;
  505. }
  506. /*
  507. * curl_easy_getinfo() is an external interface that allows an app to retrieve
  508. * information from a performed transfer and similar.
  509. */
  510. #undef curl_easy_getinfo
  511. CURLcode curl_easy_getinfo(CURL *curl, CURLINFO info, ...)
  512. {
  513. va_list arg;
  514. void *paramp;
  515. struct SessionHandle *data = (struct SessionHandle *)curl;
  516. va_start(arg, info);
  517. paramp = va_arg(arg, void *);
  518. return Curl_getinfo(data, info, paramp);
  519. }
  520. /*
  521. * curl_easy_duphandle() is an external interface to allow duplication of a
  522. * given input easy handle. The returned handle will be a new working handle
  523. * with all options set exactly as the input source handle.
  524. */
  525. CURL *curl_easy_duphandle(CURL *incurl)
  526. {
  527. struct SessionHandle *data=(struct SessionHandle *)incurl;
  528. struct SessionHandle *outcurl = calloc(1, sizeof(struct SessionHandle));
  529. if(NULL == outcurl)
  530. goto fail;
  531. /*
  532. * We setup a few buffers we need. We should probably make them
  533. * get setup on-demand in the code, as that would probably decrease
  534. * the likeliness of us forgetting to init a buffer here in the future.
  535. */
  536. outcurl->state.headerbuff = malloc(HEADERSIZE);
  537. if(!outcurl->state.headerbuff)
  538. goto fail;
  539. outcurl->state.headersize = HEADERSIZE;
  540. /* copy all userdefined values */
  541. if(Curl_dupset(outcurl, data) != CURLE_OK)
  542. goto fail;
  543. /* the connection cache is setup on demand */
  544. outcurl->state.connc = NULL;
  545. outcurl->state.lastconnect = -1;
  546. outcurl->progress.flags = data->progress.flags;
  547. outcurl->progress.callback = data->progress.callback;
  548. #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
  549. if(data->cookies) {
  550. /* If cookies are enabled in the parent handle, we enable them
  551. in the clone as well! */
  552. outcurl->cookies = Curl_cookie_init(data,
  553. data->cookies->filename,
  554. outcurl->cookies,
  555. data->set.cookiesession);
  556. if(!outcurl->cookies)
  557. goto fail;
  558. }
  559. #endif /* CURL_DISABLE_HTTP */
  560. /* duplicate all values in 'change' */
  561. #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
  562. if(data->change.cookielist) {
  563. outcurl->change.cookielist =
  564. Curl_slist_duplicate(data->change.cookielist);
  565. if(!outcurl->change.cookielist)
  566. goto fail;
  567. }
  568. #endif /* CURL_DISABLE_HTTP */
  569. if(data->change.url) {
  570. outcurl->change.url = strdup(data->change.url);
  571. if(!outcurl->change.url)
  572. goto fail;
  573. outcurl->change.url_alloc = TRUE;
  574. }
  575. if(data->change.referer) {
  576. outcurl->change.referer = strdup(data->change.referer);
  577. if(!outcurl->change.referer)
  578. goto fail;
  579. outcurl->change.referer_alloc = TRUE;
  580. }
  581. #ifdef USE_ARES
  582. /* If we use ares, we clone the ares channel for the new handle */
  583. if(ARES_SUCCESS != ares_dup(&outcurl->state.areschannel,
  584. data->state.areschannel))
  585. goto fail;
  586. #endif
  587. #if defined(CURL_DOES_CONVERSIONS) && defined(HAVE_ICONV)
  588. outcurl->inbound_cd = iconv_open(CURL_ICONV_CODESET_OF_HOST,
  589. CURL_ICONV_CODESET_OF_NETWORK);
  590. outcurl->outbound_cd = iconv_open(CURL_ICONV_CODESET_OF_NETWORK,
  591. CURL_ICONV_CODESET_OF_HOST);
  592. outcurl->utf8_cd = iconv_open(CURL_ICONV_CODESET_OF_HOST,
  593. CURL_ICONV_CODESET_FOR_UTF8);
  594. #endif
  595. Curl_easy_initHandleData(outcurl);
  596. outcurl->magic = CURLEASY_MAGIC_NUMBER;
  597. /* we reach this point and thus we are OK */
  598. return outcurl;
  599. fail:
  600. if(outcurl) {
  601. if(outcurl->state.connc &&
  602. (outcurl->state.connc->type == CONNCACHE_PRIVATE))
  603. Curl_rm_connc(outcurl->state.connc);
  604. if(outcurl->state.headerbuff)
  605. free(outcurl->state.headerbuff);
  606. #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
  607. if(outcurl->change.cookielist)
  608. curl_slist_free_all(outcurl->change.cookielist);
  609. #endif
  610. if(outcurl->change.url)
  611. free(outcurl->change.url);
  612. if(outcurl->change.referer)
  613. free(outcurl->change.referer);
  614. Curl_freeset(outcurl);
  615. free(outcurl);
  616. }
  617. return NULL;
  618. }
  619. /*
  620. * curl_easy_reset() is an external interface that allows an app to re-
  621. * initialize a session handle to the default values.
  622. */
  623. void curl_easy_reset(CURL *curl)
  624. {
  625. struct SessionHandle *data = (struct SessionHandle *)curl;
  626. Curl_safefree(data->state.pathbuffer);
  627. data->state.pathbuffer=NULL;
  628. Curl_safefree(data->state.proto.generic);
  629. data->state.proto.generic=NULL;
  630. /* zero out UserDefined data: */
  631. Curl_freeset(data);
  632. memset(&data->set, 0, sizeof(struct UserDefined));
  633. (void)Curl_init_userdefined(&data->set);
  634. /* zero out Progress data: */
  635. memset(&data->progress, 0, sizeof(struct Progress));
  636. /* init Handle data */
  637. Curl_easy_initHandleData(data);
  638. data->progress.flags |= PGRS_HIDE;
  639. data->state.current_speed = -1; /* init to negative == impossible */
  640. }
  641. /*
  642. * curl_easy_pause() allows an application to pause or unpause a specific
  643. * transfer and direction. This function sets the full new state for the
  644. * current connection this easy handle operates on.
  645. *
  646. * NOTE: if you have the receiving paused and you call this function to remove
  647. * the pausing, you may get your write callback called at this point.
  648. *
  649. * Action is a bitmask consisting of CURLPAUSE_* bits in curl/curl.h
  650. */
  651. CURLcode curl_easy_pause(CURL *curl, int action)
  652. {
  653. struct SessionHandle *data = (struct SessionHandle *)curl;
  654. struct SingleRequest *k = &data->req;
  655. CURLcode result = CURLE_OK;
  656. /* first switch off both pause bits */
  657. int newstate = k->keepon &~ (KEEP_RECV_PAUSE| KEEP_SEND_PAUSE);
  658. /* set the new desired pause bits */
  659. newstate |= ((action & CURLPAUSE_RECV)?KEEP_RECV_PAUSE:0) |
  660. ((action & CURLPAUSE_SEND)?KEEP_SEND_PAUSE:0);
  661. /* put it back in the keepon */
  662. k->keepon = newstate;
  663. if(!(newstate & KEEP_RECV_PAUSE) && data->state.tempwrite) {
  664. /* we have a buffer for sending that we now seem to be able to deliver since
  665. the receive pausing is lifted! */
  666. /* get the pointer, type and length in local copies since the function may
  667. return PAUSE again and then we'll get a new copy allocted and stored in
  668. the tempwrite variables */
  669. char *tempwrite = data->state.tempwrite;
  670. char *freewrite = tempwrite; /* store this pointer to free it later */
  671. size_t tempsize = data->state.tempwritesize;
  672. int temptype = data->state.tempwritetype;
  673. size_t chunklen;
  674. /* clear tempwrite here just to make sure it gets cleared if there's no
  675. further use of it, and make sure we don't clear it after the function
  676. invoke as it may have been set to a new value by then */
  677. data->state.tempwrite = NULL;
  678. /* since the write callback API is define to never exceed
  679. CURL_MAX_WRITE_SIZE bytes in a single call, and since we may in fact
  680. have more data than that in our buffer here, we must loop sending the
  681. data in multiple calls until there's no data left or we get another
  682. pause returned.
  683. A tricky part is that the function we call will "buffer" the data
  684. itself when it pauses on a particular buffer, so we may need to do some
  685. extra trickery if we get a pause return here.
  686. */
  687. do {
  688. chunklen = (tempsize > CURL_MAX_WRITE_SIZE)?CURL_MAX_WRITE_SIZE:tempsize;
  689. result = Curl_client_write(data->state.current_conn,
  690. temptype, tempwrite, chunklen);
  691. if(result)
  692. /* failures abort the loop at once */
  693. break;
  694. if(data->state.tempwrite && (tempsize - chunklen)) {
  695. /* Ouch, the reading is again paused and the block we send is now
  696. "cached". If this is the final chunk we can leave it like this, but
  697. if we have more chunks that are cached after this, we need to free
  698. the newly cached one and put back a version that is truly the entire
  699. contents that is saved for later
  700. */
  701. char *newptr;
  702. /* note that tempsize is still the size as before the callback was
  703. used, and thus the whole piece of data to keep */
  704. newptr = realloc(data->state.tempwrite, tempsize);
  705. if(!newptr) {
  706. free(data->state.tempwrite); /* free old area */
  707. data->state.tempwrite = NULL;
  708. result = CURLE_OUT_OF_MEMORY;
  709. /* tempwrite will be freed further down */
  710. break;
  711. }
  712. data->state.tempwrite = newptr; /* store new pointer */
  713. memcpy(newptr, tempwrite, tempsize);
  714. data->state.tempwritesize = tempsize; /* store new size */
  715. /* tempwrite will be freed further down */
  716. break; /* go back to pausing until further notice */
  717. }
  718. else {
  719. tempsize -= chunklen; /* left after the call above */
  720. tempwrite += chunklen; /* advance the pointer */
  721. }
  722. } while((result == CURLE_OK) && tempsize);
  723. free(freewrite); /* this is unconditionally no longer used */
  724. }
  725. return result;
  726. }
  727. #ifdef CURL_DOES_CONVERSIONS
  728. /*
  729. * Curl_convert_to_network() is an internal function
  730. * for performing ASCII conversions on non-ASCII platforms.
  731. */
  732. CURLcode Curl_convert_to_network(struct SessionHandle *data,
  733. char *buffer, size_t length)
  734. {
  735. CURLcode rc;
  736. if(data->set.convtonetwork) {
  737. /* use translation callback */
  738. rc = data->set.convtonetwork(buffer, length);
  739. if(rc != CURLE_OK) {
  740. failf(data,
  741. "CURLOPT_CONV_TO_NETWORK_FUNCTION callback returned %d: %s",
  742. (int)rc, curl_easy_strerror(rc));
  743. }
  744. return(rc);
  745. }
  746. else {
  747. #ifdef HAVE_ICONV
  748. /* do the translation ourselves */
  749. char *input_ptr, *output_ptr;
  750. size_t in_bytes, out_bytes, rc;
  751. int error;
  752. /* open an iconv conversion descriptor if necessary */
  753. if(data->outbound_cd == (iconv_t)-1) {
  754. data->outbound_cd = iconv_open(CURL_ICONV_CODESET_OF_NETWORK,
  755. CURL_ICONV_CODESET_OF_HOST);
  756. if(data->outbound_cd == (iconv_t)-1) {
  757. error = ERRNO;
  758. failf(data,
  759. "The iconv_open(\"%s\", \"%s\") call failed with errno %i: %s",
  760. CURL_ICONV_CODESET_OF_NETWORK,
  761. CURL_ICONV_CODESET_OF_HOST,
  762. error, strerror(error));
  763. return CURLE_CONV_FAILED;
  764. }
  765. }
  766. /* call iconv */
  767. input_ptr = output_ptr = buffer;
  768. in_bytes = out_bytes = length;
  769. rc = iconv(data->outbound_cd, (const char**)&input_ptr, &in_bytes,
  770. &output_ptr, &out_bytes);
  771. if((rc == ICONV_ERROR) || (in_bytes != 0)) {
  772. error = ERRNO;
  773. failf(data,
  774. "The Curl_convert_to_network iconv call failed with errno %i: %s",
  775. error, strerror(error));
  776. return CURLE_CONV_FAILED;
  777. }
  778. #else
  779. failf(data, "CURLOPT_CONV_TO_NETWORK_FUNCTION callback required");
  780. return CURLE_CONV_REQD;
  781. #endif /* HAVE_ICONV */
  782. }
  783. return CURLE_OK;
  784. }
  785. /*
  786. * Curl_convert_from_network() is an internal function
  787. * for performing ASCII conversions on non-ASCII platforms.
  788. */
  789. CURLcode Curl_convert_from_network(struct SessionHandle *data,
  790. char *buffer, size_t length)
  791. {
  792. CURLcode rc;
  793. if(data->set.convfromnetwork) {
  794. /* use translation callback */
  795. rc = data->set.convfromnetwork(buffer, length);
  796. if(rc != CURLE_OK) {
  797. failf(data,
  798. "CURLOPT_CONV_FROM_NETWORK_FUNCTION callback returned %d: %s",
  799. (int)rc, curl_easy_strerror(rc));
  800. }
  801. return(rc);
  802. }
  803. else {
  804. #ifdef HAVE_ICONV
  805. /* do the translation ourselves */
  806. char *input_ptr, *output_ptr;
  807. size_t in_bytes, out_bytes, rc;
  808. int error;
  809. /* open an iconv conversion descriptor if necessary */
  810. if(data->inbound_cd == (iconv_t)-1) {
  811. data->inbound_cd = iconv_open(CURL_ICONV_CODESET_OF_HOST,
  812. CURL_ICONV_CODESET_OF_NETWORK);
  813. if(data->inbound_cd == (iconv_t)-1) {
  814. error = ERRNO;
  815. failf(data,
  816. "The iconv_open(\"%s\", \"%s\") call failed with errno %i: %s",
  817. CURL_ICONV_CODESET_OF_HOST,
  818. CURL_ICONV_CODESET_OF_NETWORK,
  819. error, strerror(error));
  820. return CURLE_CONV_FAILED;
  821. }
  822. }
  823. /* call iconv */
  824. input_ptr = output_ptr = buffer;
  825. in_bytes = out_bytes = length;
  826. rc = iconv(data->inbound_cd, (const char **)&input_ptr, &in_bytes,
  827. &output_ptr, &out_bytes);
  828. if((rc == ICONV_ERROR) || (in_bytes != 0)) {
  829. error = ERRNO;
  830. failf(data,
  831. "The Curl_convert_from_network iconv call failed with errno %i: %s",
  832. error, strerror(error));
  833. return CURLE_CONV_FAILED;
  834. }
  835. #else
  836. failf(data, "CURLOPT_CONV_FROM_NETWORK_FUNCTION callback required");
  837. return CURLE_CONV_REQD;
  838. #endif /* HAVE_ICONV */
  839. }
  840. return CURLE_OK;
  841. }
  842. /*
  843. * Curl_convert_from_utf8() is an internal function
  844. * for performing UTF-8 conversions on non-ASCII platforms.
  845. */
  846. CURLcode Curl_convert_from_utf8(struct SessionHandle *data,
  847. char *buffer, size_t length)
  848. {
  849. CURLcode rc;
  850. if(data->set.convfromutf8) {
  851. /* use translation callback */
  852. rc = data->set.convfromutf8(buffer, length);
  853. if(rc != CURLE_OK) {
  854. failf(data,
  855. "CURLOPT_CONV_FROM_UTF8_FUNCTION callback returned %d: %s",
  856. (int)rc, curl_easy_strerror(rc));
  857. }
  858. return(rc);
  859. }
  860. else {
  861. #ifdef HAVE_ICONV
  862. /* do the translation ourselves */
  863. const char *input_ptr;
  864. char *output_ptr;
  865. size_t in_bytes, out_bytes, rc;
  866. int error;
  867. /* open an iconv conversion descriptor if necessary */
  868. if(data->utf8_cd == (iconv_t)-1) {
  869. data->utf8_cd = iconv_open(CURL_ICONV_CODESET_OF_HOST,
  870. CURL_ICONV_CODESET_FOR_UTF8);
  871. if(data->utf8_cd == (iconv_t)-1) {
  872. error = ERRNO;
  873. failf(data,
  874. "The iconv_open(\"%s\", \"%s\") call failed with errno %i: %s",
  875. CURL_ICONV_CODESET_OF_HOST,
  876. CURL_ICONV_CODESET_FOR_UTF8,
  877. error, strerror(error));
  878. return CURLE_CONV_FAILED;
  879. }
  880. }
  881. /* call iconv */
  882. input_ptr = output_ptr = buffer;
  883. in_bytes = out_bytes = length;
  884. rc = iconv(data->utf8_cd, &input_ptr, &in_bytes,
  885. &output_ptr, &out_bytes);
  886. if((rc == ICONV_ERROR) || (in_bytes != 0)) {
  887. error = ERRNO;
  888. failf(data,
  889. "The Curl_convert_from_utf8 iconv call failed with errno %i: %s",
  890. error, strerror(error));
  891. return CURLE_CONV_FAILED;
  892. }
  893. if(output_ptr < input_ptr) {
  894. /* null terminate the now shorter output string */
  895. *output_ptr = 0x00;
  896. }
  897. #else
  898. failf(data, "CURLOPT_CONV_FROM_UTF8_FUNCTION callback required");
  899. return CURLE_CONV_REQD;
  900. #endif /* HAVE_ICONV */
  901. }
  902. return CURLE_OK;
  903. }
  904. #endif /* CURL_DOES_CONVERSIONS */
  905. static CURLcode easy_connection(struct SessionHandle *data,
  906. curl_socket_t *sfd,
  907. struct connectdata **connp)
  908. {
  909. if(data == NULL)
  910. return CURLE_BAD_FUNCTION_ARGUMENT;
  911. /* only allow these to be called on handles with CURLOPT_CONNECT_ONLY */
  912. if(!data->set.connect_only) {
  913. failf(data, "CONNECT_ONLY is required!");
  914. return CURLE_UNSUPPORTED_PROTOCOL;
  915. }
  916. *sfd = Curl_getconnectinfo(data, connp);
  917. if(*sfd == CURL_SOCKET_BAD) {
  918. failf(data, "Failed to get recent socket");
  919. return CURLE_UNSUPPORTED_PROTOCOL;
  920. }
  921. return CURLE_OK;
  922. }
  923. /*
  924. * Receives data from the connected socket. Use after successful
  925. * curl_easy_perform() with CURLOPT_CONNECT_ONLY option.
  926. * Returns CURLE_OK on success, error code on error.
  927. */
  928. CURLcode curl_easy_recv(CURL *curl, void *buffer, size_t buflen, size_t *n)
  929. {
  930. curl_socket_t sfd;
  931. CURLcode ret;
  932. ssize_t n1;
  933. struct connectdata *c;
  934. struct SessionHandle *data = (struct SessionHandle *)curl;
  935. ret = easy_connection(data, &sfd, &c);
  936. if(ret)
  937. return ret;
  938. *n = 0;
  939. ret = Curl_read(c, sfd, buffer, buflen, &n1);
  940. if(ret != CURLE_OK)
  941. return ret;
  942. *n = (size_t)n1;
  943. return CURLE_OK;
  944. }
  945. /*
  946. * Sends data over the connected socket. Use after successful
  947. * curl_easy_perform() with CURLOPT_CONNECT_ONLY option.
  948. */
  949. CURLcode curl_easy_send(CURL *curl, const void *buffer, size_t buflen,
  950. size_t *n)
  951. {
  952. curl_socket_t sfd;
  953. CURLcode ret;
  954. ssize_t n1;
  955. struct connectdata *c = NULL;
  956. struct SessionHandle *data = (struct SessionHandle *)curl;
  957. ret = easy_connection(data, &sfd, &c);
  958. if(ret)
  959. return ret;
  960. *n = 0;
  961. ret = Curl_write(c, sfd, buffer, buflen, &n1);
  962. if(n1 == -1)
  963. return CURLE_SEND_ERROR;
  964. /* detect EAGAIN */
  965. if((CURLE_OK == ret) && (0 == n1))
  966. return CURLE_AGAIN;
  967. *n = (size_t)n1;
  968. return ret;
  969. }