easy.c 35 KB

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