easy.c 31 KB

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