2
0

easy.c 24 KB

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