easy.c 31 KB

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