easy.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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 https://curl.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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #ifdef HAVE_NETINET_IN_H
  26. #include <netinet/in.h>
  27. #endif
  28. #ifdef HAVE_NETDB_H
  29. #include <netdb.h>
  30. #endif
  31. #ifdef HAVE_ARPA_INET_H
  32. #include <arpa/inet.h>
  33. #endif
  34. #ifdef HAVE_NET_IF_H
  35. #include <net/if.h>
  36. #endif
  37. #ifdef HAVE_SYS_IOCTL_H
  38. #include <sys/ioctl.h>
  39. #endif
  40. #ifdef HAVE_SYS_PARAM_H
  41. #include <sys/param.h>
  42. #endif
  43. #include "urldata.h"
  44. #include <curl/curl.h>
  45. #include "transfer.h"
  46. #include "vtls/vtls.h"
  47. #include "url.h"
  48. #include "getinfo.h"
  49. #include "hostip.h"
  50. #include "share.h"
  51. #include "strdup.h"
  52. #include "progress.h"
  53. #include "easyif.h"
  54. #include "multiif.h"
  55. #include "select.h"
  56. #include "cfilters.h"
  57. #include "sendf.h" /* for failf function prototype */
  58. #include "connect.h" /* for Curl_getconnectinfo */
  59. #include "slist.h"
  60. #include "mime.h"
  61. #include "amigaos.h"
  62. #include "macos.h"
  63. #include "warnless.h"
  64. #include "sigpipe.h"
  65. #include "vssh/ssh.h"
  66. #include "setopt.h"
  67. #include "http_digest.h"
  68. #include "system_win32.h"
  69. #include "http2.h"
  70. #include "dynbuf.h"
  71. #include "altsvc.h"
  72. #include "hsts.h"
  73. #include "easy_lock.h"
  74. /* The last 3 #include files should be in this order */
  75. #include "curl_printf.h"
  76. #include "curl_memory.h"
  77. #include "memdebug.h"
  78. /* true globals -- for curl_global_init() and curl_global_cleanup() */
  79. static unsigned int initialized;
  80. static long easy_init_flags;
  81. #ifdef GLOBAL_INIT_IS_THREADSAFE
  82. static curl_simple_lock s_lock = CURL_SIMPLE_LOCK_INIT;
  83. #define global_init_lock() curl_simple_lock_lock(&s_lock)
  84. #define global_init_unlock() curl_simple_lock_unlock(&s_lock)
  85. #else
  86. #define global_init_lock()
  87. #define global_init_unlock()
  88. #endif
  89. /*
  90. * strdup (and other memory functions) is redefined in complicated
  91. * ways, but at this point it must be defined as the system-supplied strdup
  92. * so the callback pointer is initialized correctly.
  93. */
  94. #if defined(_WIN32_WCE)
  95. #define system_strdup _strdup
  96. #elif !defined(HAVE_STRDUP)
  97. #define system_strdup Curl_strdup
  98. #else
  99. #define system_strdup strdup
  100. #endif
  101. #if defined(_MSC_VER) && defined(_DLL) && !defined(__POCC__)
  102. # pragma warning(disable:4232) /* MSVC extension, dllimport identity */
  103. #endif
  104. /*
  105. * If a memory-using function (like curl_getenv) is used before
  106. * curl_global_init() is called, we need to have these pointers set already.
  107. */
  108. curl_malloc_callback Curl_cmalloc = (curl_malloc_callback)malloc;
  109. curl_free_callback Curl_cfree = (curl_free_callback)free;
  110. curl_realloc_callback Curl_crealloc = (curl_realloc_callback)realloc;
  111. curl_strdup_callback Curl_cstrdup = (curl_strdup_callback)system_strdup;
  112. curl_calloc_callback Curl_ccalloc = (curl_calloc_callback)calloc;
  113. #if defined(WIN32) && defined(UNICODE)
  114. curl_wcsdup_callback Curl_cwcsdup = Curl_wcsdup;
  115. #endif
  116. #if defined(_MSC_VER) && defined(_DLL) && !defined(__POCC__)
  117. # pragma warning(default:4232) /* MSVC extension, dllimport identity */
  118. #endif
  119. #ifdef DEBUGBUILD
  120. static char *leakpointer;
  121. #endif
  122. /**
  123. * curl_global_init() globally initializes curl given a bitwise set of the
  124. * different features of what to initialize.
  125. */
  126. static CURLcode global_init(long flags, bool memoryfuncs)
  127. {
  128. if(initialized++)
  129. return CURLE_OK;
  130. if(memoryfuncs) {
  131. /* Setup the default memory functions here (again) */
  132. Curl_cmalloc = (curl_malloc_callback)malloc;
  133. Curl_cfree = (curl_free_callback)free;
  134. Curl_crealloc = (curl_realloc_callback)realloc;
  135. Curl_cstrdup = (curl_strdup_callback)system_strdup;
  136. Curl_ccalloc = (curl_calloc_callback)calloc;
  137. #if defined(WIN32) && defined(UNICODE)
  138. Curl_cwcsdup = (curl_wcsdup_callback)_wcsdup;
  139. #endif
  140. }
  141. if(Curl_log_init()) {
  142. DEBUGF(fprintf(stderr, "Error: Curl_log_init failed\n"));
  143. goto fail;
  144. }
  145. if(!Curl_ssl_init()) {
  146. DEBUGF(fprintf(stderr, "Error: Curl_ssl_init failed\n"));
  147. goto fail;
  148. }
  149. #ifdef WIN32
  150. if(Curl_win32_init(flags)) {
  151. DEBUGF(fprintf(stderr, "Error: win32_init failed\n"));
  152. goto fail;
  153. }
  154. #endif
  155. #ifdef __AMIGA__
  156. if(Curl_amiga_init()) {
  157. DEBUGF(fprintf(stderr, "Error: Curl_amiga_init failed\n"));
  158. goto fail;
  159. }
  160. #endif
  161. if(Curl_macos_init()) {
  162. DEBUGF(fprintf(stderr, "Error: Curl_macos_init failed\n"));
  163. goto fail;
  164. }
  165. if(Curl_resolver_global_init()) {
  166. DEBUGF(fprintf(stderr, "Error: resolver_global_init failed\n"));
  167. goto fail;
  168. }
  169. #if defined(USE_SSH)
  170. if(Curl_ssh_init()) {
  171. goto fail;
  172. }
  173. #endif
  174. #ifdef USE_WOLFSSH
  175. if(WS_SUCCESS != wolfSSH_Init()) {
  176. DEBUGF(fprintf(stderr, "Error: wolfSSH_Init failed\n"));
  177. return CURLE_FAILED_INIT;
  178. }
  179. #endif
  180. easy_init_flags = flags;
  181. #ifdef DEBUGBUILD
  182. if(getenv("CURL_GLOBAL_INIT"))
  183. /* alloc data that will leak if *cleanup() is not called! */
  184. leakpointer = malloc(1);
  185. #endif
  186. return CURLE_OK;
  187. fail:
  188. initialized--; /* undo the increase */
  189. return CURLE_FAILED_INIT;
  190. }
  191. /**
  192. * curl_global_init() globally initializes curl given a bitwise set of the
  193. * different features of what to initialize.
  194. */
  195. CURLcode curl_global_init(long flags)
  196. {
  197. CURLcode result;
  198. global_init_lock();
  199. result = global_init(flags, TRUE);
  200. global_init_unlock();
  201. return result;
  202. }
  203. /*
  204. * curl_global_init_mem() globally initializes curl and also registers the
  205. * user provided callback routines.
  206. */
  207. CURLcode curl_global_init_mem(long flags, curl_malloc_callback m,
  208. curl_free_callback f, curl_realloc_callback r,
  209. curl_strdup_callback s, curl_calloc_callback c)
  210. {
  211. CURLcode result;
  212. /* Invalid input, return immediately */
  213. if(!m || !f || !r || !s || !c)
  214. return CURLE_FAILED_INIT;
  215. global_init_lock();
  216. if(initialized) {
  217. /* Already initialized, don't do it again, but bump the variable anyway to
  218. work like curl_global_init() and require the same amount of cleanup
  219. calls. */
  220. initialized++;
  221. global_init_unlock();
  222. return CURLE_OK;
  223. }
  224. /* set memory functions before global_init() in case it wants memory
  225. functions */
  226. Curl_cmalloc = m;
  227. Curl_cfree = f;
  228. Curl_cstrdup = s;
  229. Curl_crealloc = r;
  230. Curl_ccalloc = c;
  231. /* Call the actual init function, but without setting */
  232. result = global_init(flags, FALSE);
  233. global_init_unlock();
  234. return result;
  235. }
  236. /**
  237. * curl_global_cleanup() globally cleanups curl, uses the value of
  238. * "easy_init_flags" to determine what needs to be cleaned up and what doesn't.
  239. */
  240. void curl_global_cleanup(void)
  241. {
  242. global_init_lock();
  243. if(!initialized) {
  244. global_init_unlock();
  245. return;
  246. }
  247. if(--initialized) {
  248. global_init_unlock();
  249. return;
  250. }
  251. Curl_ssl_cleanup();
  252. Curl_resolver_global_cleanup();
  253. #ifdef WIN32
  254. Curl_win32_cleanup(easy_init_flags);
  255. #endif
  256. Curl_amiga_cleanup();
  257. Curl_ssh_cleanup();
  258. #ifdef USE_WOLFSSH
  259. (void)wolfSSH_Cleanup();
  260. #endif
  261. #ifdef DEBUGBUILD
  262. free(leakpointer);
  263. #endif
  264. easy_init_flags = 0;
  265. global_init_unlock();
  266. }
  267. /*
  268. * curl_global_sslset() globally initializes the SSL backend to use.
  269. */
  270. CURLsslset curl_global_sslset(curl_sslbackend id, const char *name,
  271. const curl_ssl_backend ***avail)
  272. {
  273. CURLsslset rc;
  274. global_init_lock();
  275. rc = Curl_init_sslset_nolock(id, name, avail);
  276. global_init_unlock();
  277. return rc;
  278. }
  279. /*
  280. * curl_easy_init() is the external interface to alloc, setup and init an
  281. * easy handle that is returned. If anything goes wrong, NULL is returned.
  282. */
  283. struct Curl_easy *curl_easy_init(void)
  284. {
  285. CURLcode result;
  286. struct Curl_easy *data;
  287. /* Make sure we inited the global SSL stuff */
  288. global_init_lock();
  289. if(!initialized) {
  290. result = global_init(CURL_GLOBAL_DEFAULT, TRUE);
  291. if(result) {
  292. /* something in the global init failed, return nothing */
  293. DEBUGF(fprintf(stderr, "Error: curl_global_init failed\n"));
  294. global_init_unlock();
  295. return NULL;
  296. }
  297. }
  298. global_init_unlock();
  299. /* We use curl_open() with undefined URL so far */
  300. result = Curl_open(&data);
  301. if(result) {
  302. DEBUGF(fprintf(stderr, "Error: Curl_open failed\n"));
  303. return NULL;
  304. }
  305. return data;
  306. }
  307. #ifdef CURLDEBUG
  308. struct socketmonitor {
  309. struct socketmonitor *next; /* the next node in the list or NULL */
  310. struct pollfd socket; /* socket info of what to monitor */
  311. };
  312. struct events {
  313. long ms; /* timeout, run the timeout function when reached */
  314. bool msbump; /* set TRUE when timeout is set by callback */
  315. int num_sockets; /* number of nodes in the monitor list */
  316. struct socketmonitor *list; /* list of sockets to monitor */
  317. int running_handles; /* store the returned number */
  318. };
  319. /* events_timer
  320. *
  321. * Callback that gets called with a new value when the timeout should be
  322. * updated.
  323. */
  324. static int events_timer(struct Curl_multi *multi, /* multi handle */
  325. long timeout_ms, /* see above */
  326. void *userp) /* private callback pointer */
  327. {
  328. struct events *ev = userp;
  329. (void)multi;
  330. if(timeout_ms == -1)
  331. /* timeout removed */
  332. timeout_ms = 0;
  333. else if(timeout_ms == 0)
  334. /* timeout is already reached! */
  335. timeout_ms = 1; /* trigger asap */
  336. ev->ms = timeout_ms;
  337. ev->msbump = TRUE;
  338. return 0;
  339. }
  340. /* poll2cselect
  341. *
  342. * convert from poll() bit definitions to libcurl's CURL_CSELECT_* ones
  343. */
  344. static int poll2cselect(int pollmask)
  345. {
  346. int omask = 0;
  347. if(pollmask & POLLIN)
  348. omask |= CURL_CSELECT_IN;
  349. if(pollmask & POLLOUT)
  350. omask |= CURL_CSELECT_OUT;
  351. if(pollmask & POLLERR)
  352. omask |= CURL_CSELECT_ERR;
  353. return omask;
  354. }
  355. /* socketcb2poll
  356. *
  357. * convert from libcurl' CURL_POLL_* bit definitions to poll()'s
  358. */
  359. static short socketcb2poll(int pollmask)
  360. {
  361. short omask = 0;
  362. if(pollmask & CURL_POLL_IN)
  363. omask |= POLLIN;
  364. if(pollmask & CURL_POLL_OUT)
  365. omask |= POLLOUT;
  366. return omask;
  367. }
  368. /* events_socket
  369. *
  370. * Callback that gets called with information about socket activity to
  371. * monitor.
  372. */
  373. static int events_socket(struct Curl_easy *easy, /* easy handle */
  374. curl_socket_t s, /* socket */
  375. int what, /* see above */
  376. void *userp, /* private callback
  377. pointer */
  378. void *socketp) /* private socket
  379. pointer */
  380. {
  381. struct events *ev = userp;
  382. struct socketmonitor *m;
  383. struct socketmonitor *prev = NULL;
  384. #if defined(CURL_DISABLE_VERBOSE_STRINGS)
  385. (void) easy;
  386. #endif
  387. (void)socketp;
  388. m = ev->list;
  389. while(m) {
  390. if(m->socket.fd == s) {
  391. if(what == CURL_POLL_REMOVE) {
  392. struct socketmonitor *nxt = m->next;
  393. /* remove this node from the list of monitored sockets */
  394. if(prev)
  395. prev->next = nxt;
  396. else
  397. ev->list = nxt;
  398. free(m);
  399. m = nxt;
  400. infof(easy, "socket cb: socket %d REMOVED", s);
  401. }
  402. else {
  403. /* The socket 's' is already being monitored, update the activity
  404. mask. Convert from libcurl bitmask to the poll one. */
  405. m->socket.events = socketcb2poll(what);
  406. infof(easy, "socket cb: socket %d UPDATED as %s%s", s,
  407. (what&CURL_POLL_IN)?"IN":"",
  408. (what&CURL_POLL_OUT)?"OUT":"");
  409. }
  410. break;
  411. }
  412. prev = m;
  413. m = m->next; /* move to next node */
  414. }
  415. if(!m) {
  416. if(what == CURL_POLL_REMOVE) {
  417. /* this happens a bit too often, libcurl fix perhaps? */
  418. /* fprintf(stderr,
  419. "%s: socket %d asked to be REMOVED but not present!\n",
  420. __func__, s); */
  421. }
  422. else {
  423. m = malloc(sizeof(struct socketmonitor));
  424. if(m) {
  425. m->next = ev->list;
  426. m->socket.fd = s;
  427. m->socket.events = socketcb2poll(what);
  428. m->socket.revents = 0;
  429. ev->list = m;
  430. infof(easy, "socket cb: socket %d ADDED as %s%s", s,
  431. (what&CURL_POLL_IN)?"IN":"",
  432. (what&CURL_POLL_OUT)?"OUT":"");
  433. }
  434. else
  435. return CURLE_OUT_OF_MEMORY;
  436. }
  437. }
  438. return 0;
  439. }
  440. /*
  441. * events_setup()
  442. *
  443. * Do the multi handle setups that only event-based transfers need.
  444. */
  445. static void events_setup(struct Curl_multi *multi, struct events *ev)
  446. {
  447. /* timer callback */
  448. curl_multi_setopt(multi, CURLMOPT_TIMERFUNCTION, events_timer);
  449. curl_multi_setopt(multi, CURLMOPT_TIMERDATA, ev);
  450. /* socket callback */
  451. curl_multi_setopt(multi, CURLMOPT_SOCKETFUNCTION, events_socket);
  452. curl_multi_setopt(multi, CURLMOPT_SOCKETDATA, ev);
  453. }
  454. /* wait_or_timeout()
  455. *
  456. * waits for activity on any of the given sockets, or the timeout to trigger.
  457. */
  458. static CURLcode wait_or_timeout(struct Curl_multi *multi, struct events *ev)
  459. {
  460. bool done = FALSE;
  461. CURLMcode mcode = CURLM_OK;
  462. CURLcode result = CURLE_OK;
  463. while(!done) {
  464. CURLMsg *msg;
  465. struct socketmonitor *m;
  466. struct pollfd *f;
  467. struct pollfd fds[4];
  468. int numfds = 0;
  469. int pollrc;
  470. int i;
  471. struct curltime before;
  472. struct curltime after;
  473. /* populate the fds[] array */
  474. for(m = ev->list, f = &fds[0]; m; m = m->next) {
  475. f->fd = m->socket.fd;
  476. f->events = m->socket.events;
  477. f->revents = 0;
  478. /* fprintf(stderr, "poll() %d check socket %d\n", numfds, f->fd); */
  479. f++;
  480. numfds++;
  481. }
  482. /* get the time stamp to use to figure out how long poll takes */
  483. before = Curl_now();
  484. /* wait for activity or timeout */
  485. pollrc = Curl_poll(fds, numfds, ev->ms);
  486. if(pollrc < 0)
  487. return CURLE_UNRECOVERABLE_POLL;
  488. after = Curl_now();
  489. ev->msbump = FALSE; /* reset here */
  490. if(!pollrc) {
  491. /* timeout! */
  492. ev->ms = 0;
  493. /* fprintf(stderr, "call curl_multi_socket_action(TIMEOUT)\n"); */
  494. mcode = curl_multi_socket_action(multi, CURL_SOCKET_TIMEOUT, 0,
  495. &ev->running_handles);
  496. }
  497. else {
  498. /* here pollrc is > 0 */
  499. /* loop over the monitored sockets to see which ones had activity */
  500. for(i = 0; i< numfds; i++) {
  501. if(fds[i].revents) {
  502. /* socket activity, tell libcurl */
  503. int act = poll2cselect(fds[i].revents); /* convert */
  504. infof(multi->easyp, "call curl_multi_socket_action(socket %d)",
  505. fds[i].fd);
  506. mcode = curl_multi_socket_action(multi, fds[i].fd, act,
  507. &ev->running_handles);
  508. }
  509. }
  510. if(!ev->msbump) {
  511. /* If nothing updated the timeout, we decrease it by the spent time.
  512. * If it was updated, it has the new timeout time stored already.
  513. */
  514. timediff_t timediff = Curl_timediff(after, before);
  515. if(timediff > 0) {
  516. if(timediff > ev->ms)
  517. ev->ms = 0;
  518. else
  519. ev->ms -= (long)timediff;
  520. }
  521. }
  522. }
  523. if(mcode)
  524. return CURLE_URL_MALFORMAT;
  525. /* we don't really care about the "msgs_in_queue" value returned in the
  526. second argument */
  527. msg = curl_multi_info_read(multi, &pollrc);
  528. if(msg) {
  529. result = msg->data.result;
  530. done = TRUE;
  531. }
  532. }
  533. return result;
  534. }
  535. /* easy_events()
  536. *
  537. * Runs a transfer in a blocking manner using the events-based API
  538. */
  539. static CURLcode easy_events(struct Curl_multi *multi)
  540. {
  541. /* this struct is made static to allow it to be used after this function
  542. returns and curl_multi_remove_handle() is called */
  543. static struct events evs = {2, FALSE, 0, NULL, 0};
  544. /* if running event-based, do some further multi inits */
  545. events_setup(multi, &evs);
  546. return wait_or_timeout(multi, &evs);
  547. }
  548. #else /* CURLDEBUG */
  549. /* when not built with debug, this function doesn't exist */
  550. #define easy_events(x) CURLE_NOT_BUILT_IN
  551. #endif
  552. static CURLcode easy_transfer(struct Curl_multi *multi)
  553. {
  554. bool done = FALSE;
  555. CURLMcode mcode = CURLM_OK;
  556. CURLcode result = CURLE_OK;
  557. while(!done && !mcode) {
  558. int still_running = 0;
  559. mcode = curl_multi_poll(multi, NULL, 0, 1000, NULL);
  560. if(!mcode)
  561. mcode = curl_multi_perform(multi, &still_running);
  562. /* only read 'still_running' if curl_multi_perform() return OK */
  563. if(!mcode && !still_running) {
  564. int rc;
  565. CURLMsg *msg = curl_multi_info_read(multi, &rc);
  566. if(msg) {
  567. result = msg->data.result;
  568. done = TRUE;
  569. }
  570. }
  571. }
  572. /* Make sure to return some kind of error if there was a multi problem */
  573. if(mcode) {
  574. result = (mcode == CURLM_OUT_OF_MEMORY) ? CURLE_OUT_OF_MEMORY :
  575. /* The other multi errors should never happen, so return
  576. something suitably generic */
  577. CURLE_BAD_FUNCTION_ARGUMENT;
  578. }
  579. return result;
  580. }
  581. /*
  582. * easy_perform() is the external interface that performs a blocking
  583. * transfer as previously setup.
  584. *
  585. * CONCEPT: This function creates a multi handle, adds the easy handle to it,
  586. * runs curl_multi_perform() until the transfer is done, then detaches the
  587. * easy handle, destroys the multi handle and returns the easy handle's return
  588. * code.
  589. *
  590. * REALITY: it can't just create and destroy the multi handle that easily. It
  591. * needs to keep it around since if this easy handle is used again by this
  592. * function, the same multi handle must be re-used so that the same pools and
  593. * caches can be used.
  594. *
  595. * DEBUG: if 'events' is set TRUE, this function will use a replacement engine
  596. * instead of curl_multi_perform() and use curl_multi_socket_action().
  597. */
  598. static CURLcode easy_perform(struct Curl_easy *data, bool events)
  599. {
  600. struct Curl_multi *multi;
  601. CURLMcode mcode;
  602. CURLcode result = CURLE_OK;
  603. SIGPIPE_VARIABLE(pipe_st);
  604. if(!data)
  605. return CURLE_BAD_FUNCTION_ARGUMENT;
  606. if(data->set.errorbuffer)
  607. /* clear this as early as possible */
  608. data->set.errorbuffer[0] = 0;
  609. if(data->multi) {
  610. failf(data, "easy handle already used in multi handle");
  611. return CURLE_FAILED_INIT;
  612. }
  613. if(data->multi_easy)
  614. multi = data->multi_easy;
  615. else {
  616. /* this multi handle will only ever have a single easy handled attached
  617. to it, so make it use minimal hashes */
  618. multi = Curl_multi_handle(1, 3, 7);
  619. if(!multi)
  620. return CURLE_OUT_OF_MEMORY;
  621. data->multi_easy = multi;
  622. }
  623. if(multi->in_callback)
  624. return CURLE_RECURSIVE_API_CALL;
  625. /* Copy the MAXCONNECTS option to the multi handle */
  626. curl_multi_setopt(multi, CURLMOPT_MAXCONNECTS, data->set.maxconnects);
  627. mcode = curl_multi_add_handle(multi, data);
  628. if(mcode) {
  629. curl_multi_cleanup(multi);
  630. data->multi_easy = NULL;
  631. if(mcode == CURLM_OUT_OF_MEMORY)
  632. return CURLE_OUT_OF_MEMORY;
  633. return CURLE_FAILED_INIT;
  634. }
  635. sigpipe_ignore(data, &pipe_st);
  636. /* run the transfer */
  637. result = events ? easy_events(multi) : easy_transfer(multi);
  638. /* ignoring the return code isn't nice, but atm we can't really handle
  639. a failure here, room for future improvement! */
  640. (void)curl_multi_remove_handle(multi, data);
  641. sigpipe_restore(&pipe_st);
  642. /* The multi handle is kept alive, owned by the easy handle */
  643. return result;
  644. }
  645. /*
  646. * curl_easy_perform() is the external interface that performs a blocking
  647. * transfer as previously setup.
  648. */
  649. CURLcode curl_easy_perform(struct Curl_easy *data)
  650. {
  651. return easy_perform(data, FALSE);
  652. }
  653. #ifdef CURLDEBUG
  654. /*
  655. * curl_easy_perform_ev() is the external interface that performs a blocking
  656. * transfer using the event-based API internally.
  657. */
  658. CURLcode curl_easy_perform_ev(struct Curl_easy *data)
  659. {
  660. return easy_perform(data, TRUE);
  661. }
  662. #endif
  663. /*
  664. * curl_easy_cleanup() is the external interface to cleaning/freeing the given
  665. * easy handle.
  666. */
  667. void curl_easy_cleanup(struct Curl_easy *data)
  668. {
  669. if(GOOD_EASY_HANDLE(data)) {
  670. SIGPIPE_VARIABLE(pipe_st);
  671. sigpipe_ignore(data, &pipe_st);
  672. Curl_close(&data);
  673. sigpipe_restore(&pipe_st);
  674. }
  675. }
  676. /*
  677. * curl_easy_getinfo() is an external interface that allows an app to retrieve
  678. * information from a performed transfer and similar.
  679. */
  680. #undef curl_easy_getinfo
  681. CURLcode curl_easy_getinfo(struct Curl_easy *data, CURLINFO info, ...)
  682. {
  683. va_list arg;
  684. void *paramp;
  685. CURLcode result;
  686. va_start(arg, info);
  687. paramp = va_arg(arg, void *);
  688. result = Curl_getinfo(data, info, paramp);
  689. va_end(arg);
  690. return result;
  691. }
  692. static CURLcode dupset(struct Curl_easy *dst, struct Curl_easy *src)
  693. {
  694. CURLcode result = CURLE_OK;
  695. enum dupstring i;
  696. enum dupblob j;
  697. /* Copy src->set into dst->set first, then deal with the strings
  698. afterwards */
  699. dst->set = src->set;
  700. Curl_mime_initpart(&dst->set.mimepost);
  701. /* clear all string pointers first */
  702. memset(dst->set.str, 0, STRING_LAST * sizeof(char *));
  703. /* duplicate all strings */
  704. for(i = (enum dupstring)0; i< STRING_LASTZEROTERMINATED; i++) {
  705. result = Curl_setstropt(&dst->set.str[i], src->set.str[i]);
  706. if(result)
  707. return result;
  708. }
  709. /* clear all blob pointers first */
  710. memset(dst->set.blobs, 0, BLOB_LAST * sizeof(struct curl_blob *));
  711. /* duplicate all blobs */
  712. for(j = (enum dupblob)0; j < BLOB_LAST; j++) {
  713. result = Curl_setblobopt(&dst->set.blobs[j], src->set.blobs[j]);
  714. if(result)
  715. return result;
  716. }
  717. /* duplicate memory areas pointed to */
  718. i = STRING_COPYPOSTFIELDS;
  719. if(src->set.postfieldsize && src->set.str[i]) {
  720. /* postfieldsize is curl_off_t, Curl_memdup() takes a size_t ... */
  721. dst->set.str[i] = Curl_memdup(src->set.str[i],
  722. curlx_sotouz(src->set.postfieldsize));
  723. if(!dst->set.str[i])
  724. return CURLE_OUT_OF_MEMORY;
  725. /* point to the new copy */
  726. dst->set.postfields = dst->set.str[i];
  727. }
  728. /* Duplicate mime data. */
  729. result = Curl_mime_duppart(dst, &dst->set.mimepost, &src->set.mimepost);
  730. if(src->set.resolve)
  731. dst->state.resolve = dst->set.resolve;
  732. return result;
  733. }
  734. /*
  735. * curl_easy_duphandle() is an external interface to allow duplication of a
  736. * given input easy handle. The returned handle will be a new working handle
  737. * with all options set exactly as the input source handle.
  738. */
  739. struct Curl_easy *curl_easy_duphandle(struct Curl_easy *data)
  740. {
  741. struct Curl_easy *outcurl = calloc(1, sizeof(struct Curl_easy));
  742. if(!outcurl)
  743. goto fail;
  744. /*
  745. * We setup a few buffers we need. We should probably make them
  746. * get setup on-demand in the code, as that would probably decrease
  747. * the likeliness of us forgetting to init a buffer here in the future.
  748. */
  749. outcurl->set.buffer_size = data->set.buffer_size;
  750. /* copy all userdefined values */
  751. if(dupset(outcurl, data))
  752. goto fail;
  753. Curl_dyn_init(&outcurl->state.headerb, CURL_MAX_HTTP_HEADER);
  754. /* the connection cache is setup on demand */
  755. outcurl->state.conn_cache = NULL;
  756. outcurl->state.lastconnect_id = -1;
  757. outcurl->state.recent_conn_id = -1;
  758. outcurl->id = -1;
  759. outcurl->progress.flags = data->progress.flags;
  760. outcurl->progress.callback = data->progress.callback;
  761. #ifndef CURL_DISABLE_COOKIES
  762. if(data->cookies) {
  763. /* If cookies are enabled in the parent handle, we enable them
  764. in the clone as well! */
  765. outcurl->cookies = Curl_cookie_init(data,
  766. data->cookies->filename,
  767. outcurl->cookies,
  768. data->set.cookiesession);
  769. if(!outcurl->cookies)
  770. goto fail;
  771. }
  772. if(data->set.cookielist) {
  773. outcurl->set.cookielist = Curl_slist_duplicate(data->set.cookielist);
  774. if(!outcurl->set.cookielist)
  775. goto fail;
  776. }
  777. #endif
  778. if(data->state.url) {
  779. outcurl->state.url = strdup(data->state.url);
  780. if(!outcurl->state.url)
  781. goto fail;
  782. outcurl->state.url_alloc = TRUE;
  783. }
  784. if(data->state.referer) {
  785. outcurl->state.referer = strdup(data->state.referer);
  786. if(!outcurl->state.referer)
  787. goto fail;
  788. outcurl->state.referer_alloc = TRUE;
  789. }
  790. /* Reinitialize an SSL engine for the new handle
  791. * note: the engine name has already been copied by dupset */
  792. if(outcurl->set.str[STRING_SSL_ENGINE]) {
  793. if(Curl_ssl_set_engine(outcurl, outcurl->set.str[STRING_SSL_ENGINE]))
  794. goto fail;
  795. }
  796. #ifndef CURL_DISABLE_ALTSVC
  797. if(data->asi) {
  798. outcurl->asi = Curl_altsvc_init();
  799. if(!outcurl->asi)
  800. goto fail;
  801. if(outcurl->set.str[STRING_ALTSVC])
  802. (void)Curl_altsvc_load(outcurl->asi, outcurl->set.str[STRING_ALTSVC]);
  803. }
  804. #endif
  805. #ifndef CURL_DISABLE_HSTS
  806. if(data->hsts) {
  807. outcurl->hsts = Curl_hsts_init();
  808. if(!outcurl->hsts)
  809. goto fail;
  810. if(outcurl->set.str[STRING_HSTS])
  811. (void)Curl_hsts_loadfile(outcurl,
  812. outcurl->hsts, outcurl->set.str[STRING_HSTS]);
  813. (void)Curl_hsts_loadcb(outcurl, outcurl->hsts);
  814. }
  815. #endif
  816. /* Clone the resolver handle, if present, for the new handle */
  817. if(Curl_resolver_duphandle(outcurl,
  818. &outcurl->state.async.resolver,
  819. data->state.async.resolver))
  820. goto fail;
  821. #ifdef USE_ARES
  822. {
  823. CURLcode rc;
  824. rc = Curl_set_dns_servers(outcurl, data->set.str[STRING_DNS_SERVERS]);
  825. if(rc && rc != CURLE_NOT_BUILT_IN)
  826. goto fail;
  827. rc = Curl_set_dns_interface(outcurl, data->set.str[STRING_DNS_INTERFACE]);
  828. if(rc && rc != CURLE_NOT_BUILT_IN)
  829. goto fail;
  830. rc = Curl_set_dns_local_ip4(outcurl, data->set.str[STRING_DNS_LOCAL_IP4]);
  831. if(rc && rc != CURLE_NOT_BUILT_IN)
  832. goto fail;
  833. rc = Curl_set_dns_local_ip6(outcurl, data->set.str[STRING_DNS_LOCAL_IP6]);
  834. if(rc && rc != CURLE_NOT_BUILT_IN)
  835. goto fail;
  836. }
  837. #endif /* USE_ARES */
  838. Curl_initinfo(outcurl);
  839. outcurl->magic = CURLEASY_MAGIC_NUMBER;
  840. /* we reach this point and thus we are OK */
  841. return outcurl;
  842. fail:
  843. if(outcurl) {
  844. #ifndef CURL_DISABLE_COOKIES
  845. curl_slist_free_all(outcurl->set.cookielist);
  846. outcurl->set.cookielist = NULL;
  847. #endif
  848. Curl_safefree(outcurl->state.buffer);
  849. Curl_dyn_free(&outcurl->state.headerb);
  850. Curl_safefree(outcurl->state.url);
  851. Curl_safefree(outcurl->state.referer);
  852. Curl_altsvc_cleanup(&outcurl->asi);
  853. Curl_hsts_cleanup(&outcurl->hsts);
  854. Curl_freeset(outcurl);
  855. free(outcurl);
  856. }
  857. return NULL;
  858. }
  859. /*
  860. * curl_easy_reset() is an external interface that allows an app to re-
  861. * initialize a session handle to the default values.
  862. */
  863. void curl_easy_reset(struct Curl_easy *data)
  864. {
  865. Curl_free_request_state(data);
  866. /* zero out UserDefined data: */
  867. Curl_freeset(data);
  868. memset(&data->set, 0, sizeof(struct UserDefined));
  869. (void)Curl_init_userdefined(data);
  870. /* zero out Progress data: */
  871. memset(&data->progress, 0, sizeof(struct Progress));
  872. /* zero out PureInfo data: */
  873. Curl_initinfo(data);
  874. data->progress.flags |= PGRS_HIDE;
  875. data->state.current_speed = -1; /* init to negative == impossible */
  876. data->state.retrycount = 0; /* reset the retry counter */
  877. /* zero out authentication data: */
  878. memset(&data->state.authhost, 0, sizeof(struct auth));
  879. memset(&data->state.authproxy, 0, sizeof(struct auth));
  880. #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_CRYPTO_AUTH)
  881. Curl_http_auth_cleanup_digest(data);
  882. #endif
  883. }
  884. /*
  885. * curl_easy_pause() allows an application to pause or unpause a specific
  886. * transfer and direction. This function sets the full new state for the
  887. * current connection this easy handle operates on.
  888. *
  889. * NOTE: if you have the receiving paused and you call this function to remove
  890. * the pausing, you may get your write callback called at this point.
  891. *
  892. * Action is a bitmask consisting of CURLPAUSE_* bits in curl/curl.h
  893. *
  894. * NOTE: This is one of few API functions that are allowed to be called from
  895. * within a callback.
  896. */
  897. CURLcode curl_easy_pause(struct Curl_easy *data, int action)
  898. {
  899. struct SingleRequest *k;
  900. CURLcode result = CURLE_OK;
  901. int oldstate;
  902. int newstate;
  903. if(!GOOD_EASY_HANDLE(data) || !data->conn)
  904. /* crazy input, don't continue */
  905. return CURLE_BAD_FUNCTION_ARGUMENT;
  906. k = &data->req;
  907. oldstate = k->keepon & (KEEP_RECV_PAUSE| KEEP_SEND_PAUSE);
  908. /* first switch off both pause bits then set the new pause bits */
  909. newstate = (k->keepon &~ (KEEP_RECV_PAUSE| KEEP_SEND_PAUSE)) |
  910. ((action & CURLPAUSE_RECV)?KEEP_RECV_PAUSE:0) |
  911. ((action & CURLPAUSE_SEND)?KEEP_SEND_PAUSE:0);
  912. if((newstate & (KEEP_RECV_PAUSE| KEEP_SEND_PAUSE)) == oldstate) {
  913. /* Not changing any pause state, return */
  914. DEBUGF(infof(data, "pause: no change, early return"));
  915. return CURLE_OK;
  916. }
  917. /* Unpause parts in active mime tree. */
  918. if((k->keepon & ~newstate & KEEP_SEND_PAUSE) &&
  919. (data->mstate == MSTATE_PERFORMING ||
  920. data->mstate == MSTATE_RATELIMITING) &&
  921. data->state.fread_func == (curl_read_callback) Curl_mime_read) {
  922. Curl_mime_unpause(data->state.in);
  923. }
  924. /* put it back in the keepon */
  925. k->keepon = newstate;
  926. if(!(newstate & KEEP_RECV_PAUSE)) {
  927. Curl_conn_ev_data_pause(data, FALSE);
  928. if(data->state.tempcount) {
  929. /* there are buffers for sending that can be delivered as the receive
  930. pausing is lifted! */
  931. unsigned int i;
  932. unsigned int count = data->state.tempcount;
  933. struct tempbuf writebuf[3]; /* there can only be three */
  934. /* copy the structs to allow for immediate re-pausing */
  935. for(i = 0; i < data->state.tempcount; i++) {
  936. writebuf[i] = data->state.tempwrite[i];
  937. Curl_dyn_init(&data->state.tempwrite[i].b, DYN_PAUSE_BUFFER);
  938. }
  939. data->state.tempcount = 0;
  940. for(i = 0; i < count; i++) {
  941. /* even if one function returns error, this loops through and frees
  942. all buffers */
  943. if(!result)
  944. result = Curl_client_write(data, writebuf[i].type,
  945. Curl_dyn_ptr(&writebuf[i].b),
  946. Curl_dyn_len(&writebuf[i].b));
  947. Curl_dyn_free(&writebuf[i].b);
  948. }
  949. if(result)
  950. return result;
  951. }
  952. }
  953. #ifdef USE_HYPER
  954. if(!(newstate & KEEP_SEND_PAUSE)) {
  955. /* need to wake the send body waker */
  956. if(data->hyp.send_body_waker) {
  957. hyper_waker_wake(data->hyp.send_body_waker);
  958. data->hyp.send_body_waker = NULL;
  959. }
  960. }
  961. #endif
  962. /* if there's no error and we're not pausing both directions, we want
  963. to have this handle checked soon */
  964. if((newstate & (KEEP_RECV_PAUSE|KEEP_SEND_PAUSE)) !=
  965. (KEEP_RECV_PAUSE|KEEP_SEND_PAUSE)) {
  966. Curl_expire(data, 0, EXPIRE_RUN_NOW); /* get this handle going again */
  967. /* reset the too-slow time keeper */
  968. data->state.keeps_speed.tv_sec = 0;
  969. if(!data->state.tempcount)
  970. /* if not pausing again, force a recv/send check of this connection as
  971. the data might've been read off the socket already */
  972. data->conn->cselect_bits = CURL_CSELECT_IN | CURL_CSELECT_OUT;
  973. if(data->multi) {
  974. if(Curl_update_timer(data->multi))
  975. return CURLE_ABORTED_BY_CALLBACK;
  976. }
  977. }
  978. if(!data->state.done)
  979. /* This transfer may have been moved in or out of the bundle, update the
  980. corresponding socket callback, if used */
  981. result = Curl_updatesocket(data);
  982. return result;
  983. }
  984. static CURLcode easy_connection(struct Curl_easy *data, curl_socket_t *sfd,
  985. struct connectdata **connp)
  986. {
  987. if(!data)
  988. return CURLE_BAD_FUNCTION_ARGUMENT;
  989. /* only allow these to be called on handles with CURLOPT_CONNECT_ONLY */
  990. if(!data->set.connect_only) {
  991. failf(data, "CONNECT_ONLY is required");
  992. return CURLE_UNSUPPORTED_PROTOCOL;
  993. }
  994. *sfd = Curl_getconnectinfo(data, connp);
  995. if(*sfd == CURL_SOCKET_BAD) {
  996. failf(data, "Failed to get recent socket");
  997. return CURLE_UNSUPPORTED_PROTOCOL;
  998. }
  999. return CURLE_OK;
  1000. }
  1001. /*
  1002. * Receives data from the connected socket. Use after successful
  1003. * curl_easy_perform() with CURLOPT_CONNECT_ONLY option.
  1004. * Returns CURLE_OK on success, error code on error.
  1005. */
  1006. CURLcode curl_easy_recv(struct Curl_easy *data, void *buffer, size_t buflen,
  1007. size_t *n)
  1008. {
  1009. curl_socket_t sfd;
  1010. CURLcode result;
  1011. ssize_t n1;
  1012. struct connectdata *c;
  1013. if(Curl_is_in_callback(data))
  1014. return CURLE_RECURSIVE_API_CALL;
  1015. result = easy_connection(data, &sfd, &c);
  1016. if(result)
  1017. return result;
  1018. if(!data->conn)
  1019. /* on first invoke, the transfer has been detached from the connection and
  1020. needs to be reattached */
  1021. Curl_attach_connection(data, c);
  1022. *n = 0;
  1023. result = Curl_read(data, sfd, buffer, buflen, &n1);
  1024. if(result)
  1025. return result;
  1026. *n = (size_t)n1;
  1027. return CURLE_OK;
  1028. }
  1029. #ifdef USE_WEBSOCKETS
  1030. CURLcode Curl_connect_only_attach(struct Curl_easy *data)
  1031. {
  1032. curl_socket_t sfd;
  1033. CURLcode result;
  1034. struct connectdata *c = NULL;
  1035. result = easy_connection(data, &sfd, &c);
  1036. if(result)
  1037. return result;
  1038. if(!data->conn)
  1039. /* on first invoke, the transfer has been detached from the connection and
  1040. needs to be reattached */
  1041. Curl_attach_connection(data, c);
  1042. return CURLE_OK;
  1043. }
  1044. #endif /* USE_WEBSOCKETS */
  1045. /*
  1046. * Sends data over the connected socket.
  1047. *
  1048. * This is the private internal version of curl_easy_send()
  1049. */
  1050. CURLcode Curl_senddata(struct Curl_easy *data, const void *buffer,
  1051. size_t buflen, ssize_t *n)
  1052. {
  1053. curl_socket_t sfd;
  1054. CURLcode result;
  1055. ssize_t n1;
  1056. struct connectdata *c = NULL;
  1057. SIGPIPE_VARIABLE(pipe_st);
  1058. result = easy_connection(data, &sfd, &c);
  1059. if(result)
  1060. return result;
  1061. if(!data->conn)
  1062. /* on first invoke, the transfer has been detached from the connection and
  1063. needs to be reattached */
  1064. Curl_attach_connection(data, c);
  1065. *n = 0;
  1066. sigpipe_ignore(data, &pipe_st);
  1067. result = Curl_write(data, sfd, buffer, buflen, &n1);
  1068. sigpipe_restore(&pipe_st);
  1069. if(n1 == -1)
  1070. return CURLE_SEND_ERROR;
  1071. /* detect EAGAIN */
  1072. if(!result && !n1)
  1073. return CURLE_AGAIN;
  1074. *n = n1;
  1075. return result;
  1076. }
  1077. /*
  1078. * Sends data over the connected socket. Use after successful
  1079. * curl_easy_perform() with CURLOPT_CONNECT_ONLY option.
  1080. */
  1081. CURLcode curl_easy_send(struct Curl_easy *data, const void *buffer,
  1082. size_t buflen, size_t *n)
  1083. {
  1084. ssize_t written = 0;
  1085. CURLcode result;
  1086. if(Curl_is_in_callback(data))
  1087. return CURLE_RECURSIVE_API_CALL;
  1088. result = Curl_senddata(data, buffer, buflen, &written);
  1089. *n = (size_t)written;
  1090. return result;
  1091. }
  1092. /*
  1093. * Wrapper to call functions in Curl_conncache_foreach()
  1094. *
  1095. * Returns always 0.
  1096. */
  1097. static int conn_upkeep(struct Curl_easy *data,
  1098. struct connectdata *conn,
  1099. void *param)
  1100. {
  1101. struct curltime *now = param;
  1102. if(Curl_timediff(*now, conn->keepalive) <= data->set.upkeep_interval_ms)
  1103. return 0;
  1104. /* briefly attach for action */
  1105. Curl_attach_connection(data, conn);
  1106. if(conn->handler->connection_check) {
  1107. /* Do a protocol-specific keepalive check on the connection. */
  1108. conn->handler->connection_check(data, conn, CONNCHECK_KEEPALIVE);
  1109. }
  1110. else {
  1111. /* Do the generic action on the FIRSTSOCKE filter chain */
  1112. Curl_conn_keep_alive(data, conn, FIRSTSOCKET);
  1113. }
  1114. Curl_detach_connection(data);
  1115. conn->keepalive = *now;
  1116. return 0; /* continue iteration */
  1117. }
  1118. static CURLcode upkeep(struct conncache *conn_cache, void *data)
  1119. {
  1120. struct curltime now = Curl_now();
  1121. /* Loop over every connection and make connection alive. */
  1122. Curl_conncache_foreach(data,
  1123. conn_cache,
  1124. &now,
  1125. conn_upkeep);
  1126. return CURLE_OK;
  1127. }
  1128. /*
  1129. * Performs connection upkeep for the given session handle.
  1130. */
  1131. CURLcode curl_easy_upkeep(struct Curl_easy *data)
  1132. {
  1133. /* Verify that we got an easy handle we can work with. */
  1134. if(!GOOD_EASY_HANDLE(data))
  1135. return CURLE_BAD_FUNCTION_ARGUMENT;
  1136. if(data->multi_easy) {
  1137. /* Use the common function to keep connections alive. */
  1138. return upkeep(&data->multi_easy->conn_cache, data);
  1139. }
  1140. else {
  1141. /* No connections, so just return success */
  1142. return CURLE_OK;
  1143. }
  1144. }