Artifactspublic-v0

curl

master
ply4 git calls38 ms323 KiB3 cachedprotocol v2
4278 lines130 KiBRaw

Showing 4000 of 4278 lines.

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"
26#include "urldata.h"
27#include "transfer.h"
28#include "url.h"
29#include "cfilters.h"
30#include "connect.h"
31#include "progress.h"
32#include "curl_share.h"
33#include "psl.h"
34#include "multiif.h"
35#include "multi_ev.h"
36#include "sendf.h"
37#include "curl_trc.h"
38#include "http.h"
39#include "select.h"
40#include "curlx/wait.h"
41#include "conncache.h"
42#include "multihandle.h"
43#include "sigpipe.h"
44#include "vtls/vtls.h"
45#include "vtls/vtls_scache.h"
46#include "http_proxy.h"
47#include "http2.h"
48#include "socketpair.h"
49#include "bufref.h"
50#include "curlx/strparse.h"
52/* initial multi->xfers table size for a full multi */
53#define CURL_XFER_TABLE_SIZE 128
55/* CURL_SOCKET_HASH_TABLE_SIZE should be a prime number. Increasing it from 97
56 to 911 takes on a 32-bit machine 4 x 804 = 3211 more bytes. Still, every
57 curl handle takes 6K memory, therefore this 3K are not significant. */
58#ifndef CURL_SOCKET_HASH_TABLE_SIZE
59#define CURL_SOCKET_HASH_TABLE_SIZE 911
60#endif
62#ifndef CURL_CONNECTION_HASH_SIZE
63#define CURL_CONNECTION_HASH_SIZE 97
64#endif
66#ifndef CURL_DNS_HASH_SIZE
67#define CURL_DNS_HASH_SIZE 71
68#endif
70#ifndef CURL_TLS_SESSION_SIZE
71#define CURL_TLS_SESSION_SIZE 25
72#endif
74static void move_pending_to_connect(struct Curl_multi *multi,
75 struct Curl_easy *data);
76static CURLMcode add_next_timeout(const struct curltime *pnow,
77 struct Curl_multi *multi,
78 struct Curl_easy *data);
79static void multi_timeout(struct Curl_multi *multi,
80 timediff_t *pexire_offset_us,
81 int *timeout_ms);
82static void multi_schedule_pending(struct Curl_multi *multi);
83static void multi_xfer_bufs_free(struct Curl_multi *multi);
84#ifdef DEBUGBUILD
85static void multi_xfer_tbl_dump(struct Curl_multi *multi);
86#endif
87/* Get the # of transfers current in process/pending. */
88static uint32_t multi_xfers_running(struct Curl_multi *multi);
90/* function pointer called once when entering a state */
91typedef void (*mstate_enter_func)(struct Curl_easy *data,
92 CURLMstate from_state);
94static void mstate_enter_connect(struct Curl_easy *data,
95 CURLMstate from_state)
97 (void)from_state;
98 Curl_init_CONNECT(data);
101static void mstate_enter_did(struct Curl_easy *data,
102 CURLMstate from_state)
104 (void)from_state;
105 data->req.chunk = FALSE;
106 Curl_pgrsTime(data, TIMER_PRETRANSFER);
107 if(!CURL_REQ_WANT_SEND(data))
108 Curl_pgrsTime(data, TIMER_POSTRANSFER);
111static void mstate_enter_done(struct Curl_easy *data,
112 CURLMstate from_state)
114 (void)from_state;
115 CURLM_NTFY(data, CURLMNOTIFY_EASY_DONE);
118static void mstate_enter_completed(struct Curl_easy *data,
119 CURLMstate from_state)
121 /* we sometimes directly jump to COMPLETED, trigger things
122 * we then missed. */
123 if(from_state < MSTATE_DID) {
124 Curl_pgrsTime(data, TIMER_PRETRANSFER);
125 Curl_pgrsTime(data, TIMER_POSTRANSFER);
126 Curl_pgrsTime(data, TIMER_STARTTRANSFER);
127 }
128 /* multi_done() should have run and detached any connection already */
129 DEBUGASSERT(!data->conn);
130 Curl_pgrsCompleted(data);
131 if(from_state < MSTATE_DONE)
132 CURLM_NTFY(data, CURLMNOTIFY_EASY_DONE);
133 /* changing to COMPLETED means it is in process and needs to go */
134 DEBUGASSERT(Curl_uint32_bset_contains(&data->multi->process, data->mid));
135 Curl_uint32_bset_remove(&data->multi->process, data->mid);
136 Curl_uint32_bset_remove(&data->multi->pending, data->mid); /* to be sure */
138 if(Curl_uint32_bset_empty(&data->multi->process)) {
139 /* free the transfer buffer when we have no more active transfers */
140 multi_xfer_bufs_free(data->multi);
141 }
142 /* Important: reset the conn pointer so that we do not point to memory
143 that could be freed anytime */
144 Curl_detach_connection(data);
145 Curl_expire_clear_all(data); /* stop all timers */
148/* always use this function to change state, to make debugging easier */
149static void mstate(struct Curl_easy *data, CURLMstate state
150#ifdef DEBUGBUILD
151 , int lineno
152#endif
155 CURLMstate oldstate = data->mstate;
156 static const mstate_enter_func state_enter[MSTATE_LAST] = {
157 NULL, /* INIT */
158 NULL, /* PENDING */
159 NULL, /* SETUP */
160 mstate_enter_connect, /* CONNECT */
161 NULL, /* CONNECTING */
162 NULL, /* PROTOCONNECT */
163 NULL, /* PROTOCONNECTING */
164 NULL, /* DO */
165 NULL, /* DOING */
166 NULL, /* DOING_MORE */
167 mstate_enter_did, /* DID */
168 NULL, /* PERFORMING */
169 NULL, /* RATELIMITING */
170 mstate_enter_done, /* DONE */
171 mstate_enter_completed, /* COMPLETED */
172 NULL /* MSGSENT */
173 };
175 if(oldstate == state)
176 /* do not bother when the new state is the same as the old state */
177 return;
179#ifdef DEBUGBUILD
180 NOVERBOSE((void)lineno);
181 CURL_TRC_M(data, "-> [%s] (line %d)", CURL_MSTATE_NAME(state), lineno);
182#else
183 CURL_TRC_M(data, "-> [%s]", CURL_MSTATE_NAME(state));
184#endif
186 /* really switching state */
187 data->mstate = state;
188 if(state_enter[state])
189 state_enter[state](data, oldstate);
192#ifndef DEBUGBUILD
193#define multistate(x, y) mstate(x, y)
194#else
195#define multistate(x, y) mstate(x, y, __LINE__)
196#endif
198/* multi->proto_hash destructor. Should never be called as elements
199 * MUST be added with their own destructor */
200static void ph_freeentry(void *p)
202 (void)p;
203 /* Always FALSE. Cannot use a 0 assert here since compilers
204 * are not in agreement if they then want a NORETURN attribute or
205 * not. *sigh* */
206 DEBUGASSERT(!p);
209/*
210 * multi_addmsg()
211 *
212 * Called when a transfer is completed. Marks its message as unread.
213 */
214static void multi_addmsg(struct Curl_multi *multi, struct Curl_easy *data)
216 if(Curl_uint32_bset_empty(&multi->msgsent))
217 CURLM_NTFY(multi->admin, CURLMNOTIFY_INFO_READ);
218 Curl_uint32_bset_add(&multi->msgsent, data->mid);
221static void multi_timeouts_init(struct Curl_easy *data);
223struct Curl_multi *Curl_multi_handle(uint32_t xfer_table_size,
224 size_t ev_hashsize, /* event hash */
225 size_t chashsize, /* connection hash */
226 size_t dnssize, /* dns hash */
227 size_t sesssize) /* TLS session cache */
229 struct curltime created;
230 struct Curl_multi *multi = curlx_calloc(1, sizeof(struct Curl_multi));
232 if(!multi)
233 return NULL;
235 multi->magic = CURLMULTI_MAGIC_NUMBER;
237 /* Initialisation order is important here!
238 * easy_init() does a lazy check on curl_global_init() which sets
239 * up platform specific things we need. For example calling curlx_pnow()
240 * before this is not safe. */
241 multi->admin = curl_easy_init();
242 if(!multi->admin) {
243 curlx_free(multi);
244 return NULL;
245 }
246 multi->admin->multi = multi;
247 multi->admin->state.internal = TRUE;
249 /* Now we can use curlx_* things safely */
250 curlx_pnow(&created);
251 Curl_timeouts_init(&multi->timeouts, &created);
252 multi_timeouts_init(multi->admin);
254 Curl_dnscache_init(&multi->dnscache, dnssize);
255 Curl_mntfy_init(multi);
256 Curl_multi_ev_init(multi, ev_hashsize);
257 Curl_uint32_tbl_init(&multi->xfers);
258 Curl_uint32_bset_init(&multi->process);
259 Curl_uint32_bset_init(&multi->dirty);
260 Curl_uint32_bset_init(&multi->pending);
261 Curl_uint32_bset_init(&multi->msgsent);
262 Curl_hash_init(&multi->proto_hash, 23, CURL_HASH_TYPE_BYTES, ph_freeentry);
263 Curl_cshutdn_init(&multi->cshutdn);
265 multi->multiplexing = TRUE;
266 multi->max_concurrent_streams = 100;
267#ifdef ENABLE_WAKEUP
268 multi->wakeup_pair[0] = CURL_SOCKET_BAD;
269 multi->wakeup_pair[1] = CURL_SOCKET_BAD;
270#endif
271#ifdef ENABLE_INTERNAL_WAKEUP
272 multi->wakeup_internal[0] = CURL_SOCKET_BAD;
273 multi->wakeup_internal[1] = CURL_SOCKET_BAD;
274#endif
276 if(Curl_uint32_bset_resize(&multi->process, xfer_table_size) ||
277 Curl_uint32_bset_resize(&multi->pending, xfer_table_size) ||
278 Curl_uint32_bset_resize(&multi->dirty, xfer_table_size) ||
279 Curl_uint32_bset_resize(&multi->msgsent, xfer_table_size) ||
280 Curl_uint32_tbl_resize(&multi->xfers, xfer_table_size))
281 goto error;
283#ifdef DEBUGBUILD
284 {
285 const char *p;
286 if(getenv("CURL_DEBUG"))
287 multi->admin->set.verbose = TRUE;
288 p = getenv("CURL_DBG_MAX_TOTAL_CONNECTIONS");
289 if(p) {
290 curl_off_t l;
291 if(!curlx_str_number(&p, &l, UINT32_MAX)) {
292 multi->max_total_connections = (uint32_t)l;
293 }
294 }
295 }
296#endif
297 Curl_uint32_tbl_add(&multi->xfers, multi->admin, &multi->admin->mid);
298 Curl_uint32_bset_add(&multi->process, multi->admin->mid);
300 Curl_cpool_init(&multi->cpool, NULL, chashsize);
302#ifdef USE_SSL
303 if(Curl_ssl_scache_create(sesssize, 2, &multi->ssl_scache))
304 goto error;
305#else
306 (void)sesssize;
307#endif
309#ifdef USE_WINSOCK
310 multi->wsa_event = WSACreateEvent();
311 if(multi->wsa_event == WSA_INVALID_EVENT)
312 goto error;
313#endif
314#ifdef ENABLE_WAKEUP
315 /* When enabled, rely on this to work. We ignore this in previous
316 * versions, but that seems an unnecessary complication. */
317 if(Curl_wakeup_init(multi->wakeup_pair, TRUE) < 0)
318 goto error;
319#endif
320#ifdef ENABLE_INTERNAL_WAKEUP
321 if(Curl_wakeup_init(multi->wakeup_internal, TRUE) < 0)
322 goto error;
323#endif
325 if(Curl_probeipv6(multi))
326 goto error;
328#ifdef USE_RESOLV_THREADED
329 if(xfer_table_size < CURL_XFER_TABLE_SIZE) { /* easy multi */
330 if(Curl_async_thrdd_multi_init(multi, 0, 2, 10))
331 goto error;
332 }
333 else { /* real multi handle */
334 if(Curl_async_thrdd_multi_init(multi, 0, 20, 2000))
335 goto error;
336 }
337#endif
339 return multi;
341error:
343#ifdef USE_RESOLV_THREADED
344 Curl_async_thrdd_multi_destroy(multi, TRUE);
345#endif
346 Curl_multi_ev_cleanup(multi);
347 Curl_hash_destroy(&multi->proto_hash);
348 Curl_dnscache_destroy(&multi->dnscache);
349 Curl_cpool_destroy(&multi->cpool, multi->admin);
350 Curl_cshutdn_destroy(&multi->cshutdn, multi->admin);
351#ifdef USE_SSL
352 Curl_ssl_scache_destroy(multi->ssl_scache);
353#endif
354 if(multi->admin) {
355 Curl_multi_ev_xfer_done(multi, multi->admin);
356 multi->admin->multi = NULL;
357 Curl_close(&multi->admin);
358 }
359 Curl_mntfy_cleanup(multi);
361 Curl_uint32_bset_destroy(&multi->process);
362 Curl_uint32_bset_destroy(&multi->dirty);
363 Curl_uint32_bset_destroy(&multi->pending);
364 Curl_uint32_bset_destroy(&multi->msgsent);
365 Curl_uint32_tbl_destroy(&multi->xfers);
366#ifdef ENABLE_WAKEUP
367 Curl_wakeup_destroy(multi->wakeup_pair);
368#endif
369#ifdef ENABLE_INTERNAL_WAKEUP
370 Curl_wakeup_destroy(multi->wakeup_internal);
371#endif
373 curlx_free(multi);
374 return NULL;
377CURLM *curl_multi_init(void)
379 return Curl_multi_handle(CURL_XFER_TABLE_SIZE,
380 CURL_SOCKET_HASH_TABLE_SIZE,
381 CURL_CONNECTION_HASH_SIZE,
382 CURL_DNS_HASH_SIZE,
383 CURL_TLS_SESSION_SIZE);
386#if defined(DEBUGBUILD) && defined(CURLVERBOSE)
387static void multi_warn_debug(struct Curl_multi *multi, struct Curl_easy *data)
389 if(!multi->warned) {
390 infof(data, "!!! WARNING !!!");
391 infof(data, "This is a debug build of libcurl, "
392 "do not use in production.");
393 multi->warned = TRUE;
394 }
396#else
397#define multi_warn_debug(x, y) Curl_nop_stmt
398#endif
400bool Curl_is_connecting(struct Curl_easy *data)
402 return data->mstate < MSTATE_DO;
405static CURLMcode multi_assess_wakeup(struct Curl_multi *multi)
407#ifdef ENABLE_INTERNAL_WAKEUP
408 if(multi->socket_cb)
409 return Curl_multi_ev_assess_xfer(multi, multi->admin);
410#else
411 (void)multi;
412#endif
413 return CURLM_OK;
416static CURLMcode multi_xfers_add(struct Curl_multi *multi,
417 struct Curl_easy *data)
419 uint32_t capacity = Curl_uint32_tbl_capacity(&multi->xfers);
420 uint32_t new_size = 0;
421 /* Prepare to make this into a CURLMOPT_MAX_TRANSFERS, because some
422 * applications may want to prevent a run-away of their memory use. */
423 /* UINT_MAX is our "invalid" id, do not let the table grow up to that. */
424 const uint32_t max_capacity = UINT_MAX - 1;
426 if(capacity < max_capacity) {
427 /* We want `multi->xfers` to have "sufficient" free rows, so that we do
428 * not have to reuse the `mid` from a removed easy right away.
429 * Check if an 8th of the capacity is still free */
430 uint32_t used = Curl_uint32_tbl_count(&multi->xfers);
431 uint32_t unused = capacity - used;
432 uint32_t min_unused = CURLMAX(capacity >> 3, 4);
433 if(unused < min_unused) {
434 /* Grow by 50% of current capacity, in range of [128, 2048],
435 * which means the table grows max by 16kb on 64-bit arch. */
436 uint32_t growth = CURLMIN(CURLMAX(capacity >> 1, 128), 2048);
437 /* Make sure the uint arithmetic here works on the corner
438 * cases where we are close to max_capacity or UINT_MAX */
439 if((max_capacity - growth) <= capacity)
440 new_size = max_capacity;
441 else
442 new_size = capacity + growth;
443 }
444 }
446 if(new_size > capacity) {
447 /* Grow the bitsets first. Should one fail, we do not need
448 * to downsize the already resized ones. The sets continue
449 * to work properly when larger than the table, but not
450 * the other way around. */
451 CURL_TRC_M(data, "increasing xfer table size to %u", new_size);
452 if(Curl_uint32_bset_resize(&multi->process, new_size) ||
453 Curl_uint32_bset_resize(&multi->dirty, new_size) ||
454 Curl_uint32_bset_resize(&multi->pending, new_size) ||
455 Curl_uint32_bset_resize(&multi->msgsent, new_size) ||
456 Curl_uint32_tbl_resize(&multi->xfers, new_size))
457 return CURLM_OUT_OF_MEMORY;
458 }
460 /* Insert the easy into the table now */
461 if(!Curl_uint32_tbl_add(&multi->xfers, data, &data->mid)) {
462 /* MUST only happen when table is full */
463 DEBUGASSERT(Curl_uint32_tbl_capacity(&multi->xfers) <=
464 Curl_uint32_tbl_count(&multi->xfers));
465 return CURLM_OUT_OF_MEMORY;
466 }
467 return CURLM_OK;
470CURLMcode Curl_multi_add_handle(struct Curl_multi *multi,
471 struct Curl_easy *data)
473 CURLMcode mresult;
475 /* Prevent users from adding same easy handle more than once and prevent
476 adding to more than one multi stack */
477 if(data->multi)
478 return CURLM_ADDED_ALREADY;
480 if(multi->dead) {
481 /* a "dead" handle cannot get added transfers while any existing easy
482 handles are still alive - but if there are none alive anymore, it is
483 fine to start over and unmark the "deadness" of this handle.
484 This means only the admin handle MUST be present. */
485 if((Curl_uint32_tbl_count(&multi->xfers) != 1) ||
486 !Curl_uint32_tbl_contains(&multi->xfers, 0))
487 return CURLM_ABORTED_BY_CALLBACK;
488 multi->dead = FALSE;
489 Curl_uint32_bset_clear(&multi->process);
490 Curl_uint32_bset_clear(&multi->dirty);
491 Curl_uint32_bset_clear(&multi->pending);
492 Curl_uint32_bset_clear(&multi->msgsent);
493 }
495 if(data->multi_easy) {
496 /* if this easy handle was previously used for curl_easy_perform(), there
497 is a private multi handle here that we can kill */
498 curl_multi_cleanup(data->multi_easy);
499 data->multi_easy = NULL;
500 }
502 /* Insert the easy into the multi->xfers table, assigning it a `mid`. */
503 if(multi_xfers_add(multi, data))
504 return CURLM_OUT_OF_MEMORY;
506 /* Initialize timeouts for this handle */
507 multi_timeouts_init(data);
509 /*
510 * No failure allowed in this function beyond this point. No modification
511 * of easy nor multi handle allowed before this except for potential
512 * multi's connection pool growing which will not be undone in this
513 * function no matter what.
514 */
515 if(data->set.errorbuffer)
516 data->set.errorbuffer[0] = 0;
518 data->state.os_errno = 0;
520 /* make the Curl_easy refer back to this multi handle - before
521 Curl_expire() is called. */
522 data->multi = multi;
524 /* set the easy handle */
525 multistate(data, MSTATE_INIT);
526 /* not yet passed INIT state */
527 data->state.really_alive = FALSE;
529#ifdef USE_LIBPSL
530 /* Do the same for PSL. */
531 if(data->share && (data->share->specifier & (1 << CURL_LOCK_DATA_PSL)))
532 data->psl = &data->share->psl;
533 else
534 data->psl = &multi->psl;
535#endif
537 /* add the easy handle to the process set */
538 Curl_uint32_bset_add(&multi->process, data->mid);
539 ++multi->xfers_alive;
540 ++multi->xfers_total_ever;
542 Curl_cpool_xfer_init(data);
543 multi_warn_debug(multi, data);
545 /* Make sure the new handle will run */
546 Curl_multi_mark_dirty(data);
548 /* Necessary in event based processing, where dirty handles trigger
549 * a timeout callback invocation. */
550 mresult = Curl_update_timer(multi);
551 if(mresult) {
552 /* undo the additions above */
553 Curl_uint32_bset_remove(&multi->process, data->mid);
554 Curl_uint32_bset_remove(&multi->dirty, data->mid);
555 --multi->xfers_alive;
556 data->multi = NULL; /* not anymore */
557 Curl_uint32_tbl_remove(&multi->xfers, data->mid);
558 data->mid = UINT32_MAX;
559 return mresult;
560 }
562 /* The admin handle only ever has default timeouts set. To improve the
563 state somewhat we clone the timeouts from each added handle so that the
564 admin handle always has the same timeouts as the most recently added
565 easy handle. */
566 multi->admin->set.timeout = data->set.timeout;
567 multi->admin->set.server_response_timeout =
568 data->set.server_response_timeout;
569 multi->admin->set.no_signal = data->set.no_signal;
571 CURL_TRC_M(data, "added to multi, mid=%u, running=%u, total=%u",
572 data->mid, multi_xfers_running(multi),
573 Curl_uint32_tbl_count(&multi->xfers));
574 return CURLM_OK;
577CURLMcode curl_multi_add_handle(CURLM *m, CURL *curl)
579 struct Curl_mapi_guard guard;
580 CURLMcode mresult;
582 if(CURL_MAPI_ENTER(&guard, m, multi_add_handle, &mresult)) {
583 struct Curl_easy *data = curl;
584 /* Verify that we got a somewhat good easy handle too */
585 if(!GOOD_EASY_HANDLE(data) || data->state.internal)
586 mresult = CURLM_BAD_EASY_HANDLE;
587 else
588 mresult = Curl_multi_add_handle(m, data);
589 }
590 CURL_MAPI_LEAVE(&guard);
591 return mresult;
594struct multi_done_ctx {
595 BIT(premature);
596};
598static bool multi_conn_should_close(struct connectdata *conn,
599 struct Curl_easy *data,
600 bool premature)
602 /* if conn->bits.close is TRUE, it means that the connection should be
603 closed in spite of everything else. */
604 if(conn->bits.close)
605 return TRUE;
607 /* if data->set.reuse_forbid is TRUE, it means the libcurl client has forced
608 us to close this connection. This is ignored for requests taking place in
609 a NEGOTIATE authentication handshake. */
610 if(data->set.reuse_forbid
611#ifdef USE_SPNEGO
612 && !(conn->http_negotiate_state == CURL_NEGO_AUTH_RECV ||
613 conn->proxy_negotiate_state == CURL_NEGO_AUTH_RECV)
614#endif
615 )
616 return TRUE;
618 /* Unless this connection is for a "connect-only" transfer, it
619 * needs to be closed if the protocol handler does not support reuse. */
620 if(!data->set.connect_only && conn->scheme &&
621 !(conn->scheme->flags & PROTOPT_CONN_REUSE))
622 return TRUE;
624 /* if premature is TRUE, it means this connection was said to be DONE before
625 the entire request operation is complete and thus we cannot know in what
626 state it is for reusing, so we are forced to close it. In a perfect world
627 we can add code that keep track of if we really must close it here or not,
628 but currently we have no such detail knowledge. */
629 if(premature && !Curl_conn_is_multiplex(conn, FIRSTSOCKET))
630 return TRUE;
632 return FALSE;
635static cpool_do_result multi_done_locked(struct Curl_easy *data,
636 struct connectdata *conn,
637 void *userdata,
638 const struct curltime *pnow)
640 struct multi_done_ctx *mdctx = userdata;
642 (void)pnow;
643 CURL_TRC_M(data, "multi_done_locked, in use=%u", conn->attached_xfers);
644 if(CONN_INUSE(conn)) {
645 /* Stop if still used. */
646 CURL_TRC_M(data, "Connection still in use %u, no more multi_done now!",
647 conn->attached_xfers);
648 return CPOOL_DO_KEEP;
649 }
651 if(multi_conn_should_close(conn, data, (bool)mdctx->premature)) {
652 CURL_TRC_M(data, "multi_done, terminating conn #%" FMT_OFF_T " to %s:%u, "
653 "forbid=%d, close=%d, premature=%d, conn_multiplex=%d",
654 conn->connection_id, conn->origin->user_hostname,
655 conn->origin->port,
656 data->set.reuse_forbid, conn->bits.close, mdctx->premature,
657 Curl_conn_is_multiplex(conn, FIRSTSOCKET));
658 connclose(conn);
659 return mdctx->premature ? CPOOL_DO_TERMINATE : CPOOL_DO_CLOSE;
660 }
661 else if(!Curl_conn_get_max_concurrent(data, conn, FIRSTSOCKET)) {
662 CURL_TRC_M(data, "multi_done, conn #%" FMT_OFF_T " to %s:%u was shutdown"
663 " by server, not reusing", conn->connection_id,
664 conn->origin->user_hostname, conn->origin->port);
665 connclose(conn);
666 return mdctx->premature ? CPOOL_DO_TERMINATE : CPOOL_DO_CLOSE;
667 }
668 else {
669 return CPOOL_DO_IDLE;
670 }
673static CURLcode multi_done(struct Curl_easy *data,
674 CURLcode status, /* an error if this is called
675 after an error was detected */
676 bool premature)
678 CURLcode result;
679 struct connectdata *conn = data->conn;
680 const struct curltime *pnow = Curl_pgrs_now(data);
682 CURL_TRC_M(data, "multi_done: status: %d prem: %d done: %d",
683 (int)status, (int)premature, data->state.done);
685 if(data->state.done) {
686 /* Stop if multi_done() has already been called */
687 return CURLE_OK;
688 }
689 data->state.done = TRUE; /* called now! */
691 /* Shut down any ongoing async resolver operation. */
692 Curl_resolv_shutdown_all(data);
694 /* Cleanup possible redirect junk */
695 curlx_safefree(data->req.newurl);
696 curlx_safefree(data->req.location);
698 switch(status) {
699 case CURLE_ABORTED_BY_CALLBACK:
700 case CURLE_READ_ERROR:
701 case CURLE_WRITE_ERROR:
702 /* When we are aborted due to a callback return code it has to be counted
703 as premature as there is trouble ahead if we do not. We have many
704 callbacks and protocols work differently, we could potentially do this
705 more fine-grained in the future. */
706 premature = TRUE;
707 FALLTHROUGH();
708 default:
709 break;
710 }
712 /* this calls the protocol-specific function pointer previously set */
713 if(conn && conn->scheme->run->done && (data->mstate >= MSTATE_PROTOCONNECT))
714 result = conn->scheme->run->done(data, status, premature);
715 else
716 result = status;
718 if(data->mstate > MSTATE_CONNECTING &&
719 (result != CURLE_ABORTED_BY_CALLBACK)) {
720 /* avoid this if
721 * - the transfer has not connected
722 * - we already aborted by callback to avoid this calling another callback
723 */
724 int rc = Curl_pgrsDone(data, pnow);
725 if(!result && rc)
726 result = CURLE_ABORTED_BY_CALLBACK;
727 }
729 /* Make sure that transfer client writes are really done now. */
730 result = Curl_1st_fatal(result, Curl_xfer_write_done(data, premature));
732 if(!result)
733 result = Curl_req_done(&data->req, data, premature);
735 if(conn) {
736 /* Detach connection and decide what to do with it. */
737 struct multi_done_ctx mdctx;
739 /* Inform connection filters that this transfer is done and they
740 * need to give up any references to `data`. */
741 Curl_conn_ev_data_done(data, premature);
742 Curl_detach_connection(data);
743 Curl_dnscache_prune(data, pnow);
745 memset(&mdctx, 0, sizeof(mdctx));
746 mdctx.premature = premature;
747 Curl_cpool_return(data, conn, multi_done_locked, &mdctx, pnow);
749 if(data->multi)
750 multi_schedule_pending(data->multi);
751 }
753 /* flush the netrc cache */
754 Curl_netrc_cleanup(&data->state.netrc);
755 return result;
758CURLMcode Curl_multi_remove_handle(struct Curl_multi *multi,
759 struct Curl_easy *data)
761 CURLMcode mresult;
762 bool premature;
763 uint32_t mid;
765 /* Prevent users from trying to remove same easy handle more than once */
766 if(!data->multi)
767 return CURLM_OK; /* it is already removed so let's say it is fine! */
769 /* Prevent users from trying to remove an easy handle from the wrong multi */
770 if(data->multi != multi)
771 return CURLM_BAD_EASY_HANDLE;
773 if(data->mid == UINT32_MAX) {
774 DEBUGASSERT(0);
775 return CURLM_INTERNAL_ERROR;
776 }
777 if(Curl_uint32_tbl_get(&multi->xfers, data->mid) != data) {
778 DEBUGASSERT(0);
779 return CURLM_INTERNAL_ERROR;
780 }
782 premature = (data->mstate < MSTATE_COMPLETED);
784 if(data->conn) {
785 /* If the 'state' is not INIT or COMPLETED, we might need to do something
786 nice to put the easy_handle in a good known state when this returns. */
787 if(premature && (data->mstate > MSTATE_DO))
788 streamclose(data->conn);
790 /* multi_done() clears the association between the easy handle and the
791 connection.
792 Note that this ignores the return code because there is
793 nothing really useful to do with it anyway! */
794 (void)multi_done(data, data->result, premature);
795 }
797 /* The timer must be shut down before data->multi is set to NULL, else
798 data's splaynode would remain in the splay tree after curl_easy_cleanup is
799 called. Do it after multi_done() in case that sets another time! */
800 Curl_expire_clear_all(data);
802 /* In MSGSENT, it was deducted from `multi->xfers_alive` already. */
803 if(data->mstate != MSTATE_MSGSENT)
804 --multi->xfers_alive;
806 if(data->state.really_alive) {
807 data->state.really_alive = FALSE;
808 --multi->xfers_really_alive;
809 if(!multi->xfers_really_alive)
810 (void)multi_assess_wakeup(multi);
811 }
813 Curl_wildcard_dtor(&data->wildcard);
815 data->mstate = MSTATE_COMPLETED;
817 /* Remove the association between the connection and the handle */
818 Curl_detach_connection(data);
820 /* Tell event handling that this transfer is definitely going away */
821 Curl_multi_ev_xfer_done(multi, data);
823 if(data->set.connect_only) {
824 if(data->multi_easy) {
825 if(data->state.last_conn_id != -1) {
826 /* Mark any connect-only connection for closure */
827 struct connectdata *conn;
828 (void)Curl_getconnectinfo(data, &conn);
829 if(conn && conn->bits.connect_only)
830 connclose(conn);
831 }
832 }
833 else {
834 /* This removes a handle that was part the multi interface that used
835 CONNECT_ONLY, that connection is now left alive but since this handle
836 has bits.close set nothing can use that connection anymore and it is
837 forbidden from reuse. This easy handle cannot find the connection
838 anymore once removed from the multi handle
840 Better close the connection here, at once. */
841 struct connectdata *conn;
842 (void)Curl_getconnectinfo(data, &conn);
843 if(conn)
844 Curl_conn_close(data, conn, TRUE);
845 }
846 }
848#ifdef USE_LIBPSL
849 /* Remove the PSL association. */
850 if(data->psl == &multi->psl)
851 data->psl = NULL;
852#endif
854 /* clear the association to this multi handle */
855 mid = data->mid;
856 DEBUGASSERT(Curl_uint32_tbl_contains(&multi->xfers, mid));
857 Curl_uint32_tbl_remove(&multi->xfers, mid);
858 Curl_uint32_bset_remove(&multi->process, mid);
859 Curl_uint32_bset_remove(&multi->dirty, mid);
860 Curl_uint32_bset_remove(&multi->pending, mid);
861 Curl_uint32_bset_remove(&multi->msgsent, mid);
862 data->multi = NULL;
863 data->mid = UINT32_MAX;
864 data->master_mid = UINT32_MAX;
866 /* A pending transfer *might* be able to run now. */
867 multi_schedule_pending(multi);
868 mresult = Curl_update_timer(multi);
869 if(mresult)
870 return mresult;
872 mresult = multi_assess_wakeup(multi);
873 if(mresult) {
874 failf(data, "error enabling wakeup listening: %d", mresult);
875 return mresult;
876 }
878 CURL_TRC_M(data, "removed from multi, mid=%u, running=%u, total=%u",
879 mid, multi_xfers_running(multi),
880 Curl_uint32_tbl_count(&multi->xfers));
881 return CURLM_OK;
884CURLMcode curl_multi_remove_handle(CURLM *m, CURL *curl)
886 struct Curl_mapi_guard guard;
887 CURLMcode mresult;
889 if(CURL_MAPI_ENTER(&guard, m, multi_remove_handle, &mresult)) {
890 struct Curl_easy *data = curl;
891 if(!GOOD_EASY_HANDLE(data) || data->state.internal)
892 mresult = CURLM_BAD_EASY_HANDLE;
893 else
894 mresult = Curl_multi_remove_handle(m, data);
895 }
896 CURL_MAPI_LEAVE(&guard);
897 return mresult;
900/* Return TRUE if the application asked for multiplexing */
901bool Curl_multiplex_wanted(const struct Curl_multi *multi)
903 return multi && multi->multiplexing;
906/*
907 * Curl_detach_connection() removes the given transfer from the connection.
908 *
909 * This is the only function that should clear data->conn. This will
910 * occasionally be called with the data->conn pointer already cleared.
911 */
912void Curl_detach_connection(struct Curl_easy *data)
914 struct connectdata *conn = data->conn;
915 if(conn) {
916 /* this should never happen, prevent underflow */
917 DEBUGASSERT(conn->attached_xfers);
918 if(conn->attached_xfers) {
919 conn->attached_xfers--;
920 if(!conn->attached_xfers)
921 conn->attached_multi = NULL;
922 }
923 }
924 data->conn = NULL;
927/*
928 * Curl_attach_connection() attaches this transfer to this connection.
929 *
930 * This is the only function that should assign data->conn.
931 * `matched == TRUE` means the transfer's properties match this
932 * connection and it is not a temporary attach for maintenance.
933 */
934void Curl_attach_connection(struct Curl_easy *data,
935 struct connectdata *conn,
936 bool matched)
938 DEBUGASSERT(data);
939 DEBUGASSERT(!data->conn);
940 DEBUGASSERT(conn);
941 DEBUGASSERT(conn->attached_xfers < UINT32_MAX);
942 data->conn = conn;
943 if(matched) {
944 data->state.last_conn_id = conn->connection_id;
945 data->state.last_cpid = conn->cpid;
946 }
947 else
948 DEBUGASSERT(!data->mid); /* admin handle */
949 conn->attached_xfers++;
950 /* all attached transfers must be from the same multi */
951 if(!conn->attached_multi)
952 conn->attached_multi = data->multi;
953 DEBUGASSERT(conn->attached_multi == data->multi);
955 if(conn->scheme && conn->scheme->run->attach)
956 conn->scheme->run->attach(data, conn);
959/* adjust pollset for rate limits/pauses */
960static CURLcode multi_adjust_pollset(struct Curl_easy *data,
961 struct easy_pollset *ps)
963 CURLcode result = CURLE_OK;
965 if(ps->n) {
966 bool send_blocked, recv_blocked;
968 recv_blocked = (Curl_rlimit_avail(&data->progress.dl.rlimit, NULL) <= 0);
969 send_blocked = (Curl_rlimit_avail(&data->progress.ul.rlimit, NULL) <= 0);
970 if(send_blocked || recv_blocked) {
971 int i;
972 for(i = 0; i <= SECONDARYSOCKET; ++i) {
973 curl_socket_t sock = data->conn->sock[i];
974 if(sock == CURL_SOCKET_BAD)
975 continue;
976 if(recv_blocked && Curl_pollset_want_recv(data, ps, sock)) {
977 result = Curl_pollset_remove_in(data, ps, sock);
978 if(result)
979 break;
980 }
981 if(send_blocked && Curl_pollset_want_send(data, ps, sock)) {
982 result = Curl_pollset_remove_out(data, ps, sock);
983 if(result)
984 break;
985 }
986 }
987 }
989 /* Not blocked and wanting to receive. If there is data pending
990 * in the connection filters, make transfer run again. */
991 if(!recv_blocked &&
992 ((Curl_pollset_want_recv(data, ps, data->conn->sock[FIRSTSOCKET]) &&
993 Curl_conn_data_pending(data, FIRSTSOCKET)) ||
994 (Curl_pollset_want_recv(data, ps, data->conn->sock[SECONDARYSOCKET]) &&
995 Curl_conn_data_pending(data, SECONDARYSOCKET)))) {
996 CURL_TRC_M(data, "pollset[] has POLLIN, but there is still "
997 "buffered input -> mark as dirty");
998 Curl_multi_mark_dirty(data);
999 }
1001 return result;
1004static CURLcode mstate_connecting_pollset(struct Curl_easy *data,
1005 struct easy_pollset *ps)
1007 struct connectdata *conn = data->conn;
1008 curl_socket_t sockfd;
1009 CURLcode result = CURLE_OK;
1011 if(Curl_xfer_recv_is_paused(data))
1012 return CURLE_OK;
1013 /* If a socket is set, receiving is default. If the socket
1014 * has not been determined yet (eyeballing), always ask the
1015 * connection filters for what to monitor. */
1016 sockfd = Curl_conn_get_first_socket(data);
1017 if(sockfd != CURL_SOCKET_BAD) {
1018 result = Curl_pollset_change(data, ps, sockfd, CURL_POLL_IN, 0);
1019 if(!result)
1020 result = multi_adjust_pollset(data, ps);
1022 if(!result)
1023 result = Curl_conn_adjust_pollset(data, conn, ps);
1024 return result;
1027static CURLcode mstate_protocol_pollset(struct Curl_easy *data,
1028 struct easy_pollset *ps)
1030 struct connectdata *conn = data->conn;
1031 CURLcode result = CURLE_OK;
1033 if(conn->scheme->run->proto_pollset)
1034 result = conn->scheme->run->proto_pollset(data, ps);
1035 else {
1036 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
1037 if(sockfd != CURL_SOCKET_BAD) {
1038 /* Default is to wait to something from the server */
1039 result = Curl_pollset_change(data, ps, sockfd, CURL_POLL_IN, 0);
1042 if(!result)
1043 result = multi_adjust_pollset(data, ps);
1044 if(!result)
1045 result = Curl_conn_adjust_pollset(data, conn, ps);
1046 return result;
1049static CURLcode mstate_do_pollset(struct Curl_easy *data,
1050 struct easy_pollset *ps)
1052 struct connectdata *conn = data->conn;
1053 CURLcode result = CURLE_OK;
1055 if(conn->scheme->run->doing_pollset)
1056 result = conn->scheme->run->doing_pollset(data, ps);
1057 else if(CONN_SOCK_IDX_VALID(conn->send_idx)) {
1058 /* Default is that we want to send something to the server */
1059 result = Curl_pollset_add_out(data, ps, conn->sock[conn->send_idx]);
1061 if(!result)
1062 result = multi_adjust_pollset(data, ps);
1063 if(!result)
1064 result = Curl_conn_adjust_pollset(data, conn, ps);
1065 return result;
1068static CURLcode mstate_domore_pollset(struct Curl_easy *data,
1069 struct easy_pollset *ps)
1071 struct connectdata *conn = data->conn;
1072 CURLcode result = CURLE_OK;
1074 if(conn->scheme->run->domore_pollset)
1075 result = conn->scheme->run->domore_pollset(data, ps);
1076 else if(CONN_SOCK_IDX_VALID(conn->send_idx)) {
1077 /* Default is that we want to send something to the server */
1078 result = Curl_pollset_add_out(data, ps, conn->sock[conn->send_idx]);
1080 if(!result)
1081 result = multi_adjust_pollset(data, ps);
1082 if(!result)
1083 result = Curl_conn_adjust_pollset(data, conn, ps);
1084 return result;
1087static CURLcode mstate_perform_pollset(struct Curl_easy *data,
1088 struct easy_pollset *ps)
1090 struct connectdata *conn = data->conn;
1091 CURLcode result = CURLE_OK;
1093 if(conn->scheme->run->perform_pollset)
1094 result = conn->scheme->run->perform_pollset(data, ps);
1095 else {
1096 /* Default is to obey the request flags for send/recv */
1097 if(Curl_req_want_recv(data) && CONN_SOCK_IDX_VALID(conn->recv_idx)) {
1098 result = Curl_pollset_add_in(data, ps, conn->sock[conn->recv_idx]);
1100 if(!result && Curl_req_want_send(data) &&
1101 CONN_SOCK_IDX_VALID(conn->send_idx)) {
1102 result = Curl_pollset_add_out(data, ps, conn->sock[conn->send_idx]);
1105 if(!result)
1106 result = multi_adjust_pollset(data, ps);
1107 if(!result)
1108 result = Curl_conn_adjust_pollset(data, conn, ps);
1109 return result;
1112#ifdef CURLVERBOSE
1113static size_t multi_timeouts_count(struct expire_timers *timeouts)
1115 size_t n = 0;
1116 uint8_t eid = timeouts->first;
1117 for(; eid < EXPIRE_LAST; eid = timeouts->next[eid])
1118 ++n;
1119 return n;
1121#endif
1123/* Initializes `poll_set` with the current socket poll actions needed
1124 * for transfer `data`. */
1125CURLMcode Curl_multi_pollset(struct Curl_easy *data,
1126 struct easy_pollset *ps)
1128 CURLcode result = CURLE_OK;
1130 Curl_pollset_reset(ps);
1131#ifdef ENABLE_INTERNAL_WAKEUP
1132 /* The admin handle always listens on the wakeup socket when there
1133 * are transfers alive. */
1134 if(data->multi && (data == data->multi->admin) &&
1135 data->multi->xfers_really_alive) {
1136 CURL_TRC_M(data, "adding wakeup, %u xfers really alive",
1137 data->multi->xfers_really_alive);
1138 result = Curl_pollset_add_in(data, ps, data->multi->wakeup_internal[0]);
1140#endif
1141 /* If the transfer has no connection, this is fine. Happens when
1142 called via curl_multi_remove_handle() => Curl_multi_ev_assess() =>
1143 Curl_multi_pollset(). */
1144 if(!result && data->conn) {
1145 switch(data->mstate) {
1146 case MSTATE_INIT:
1147 case MSTATE_PENDING:
1148 case MSTATE_SETUP:
1149 case MSTATE_CONNECT:
1150 /* nothing to poll for yet */
1151 break;
1153 case MSTATE_CONNECTING:
1154 result = mstate_connecting_pollset(data, ps);
1155 break;
1157 case MSTATE_PROTOCONNECT:
1158 case MSTATE_PROTOCONNECTING:
1159 result = mstate_protocol_pollset(data, ps);
1160 break;
1162 case MSTATE_DO:
1163 case MSTATE_DOING:
1164 result = mstate_do_pollset(data, ps);
1165 break;
1167 case MSTATE_DOING_MORE:
1168 result = mstate_domore_pollset(data, ps);
1169 break;
1171 case MSTATE_DID: /* same as PERFORMING in regard to polling */
1172 case MSTATE_PERFORMING:
1173 result = mstate_perform_pollset(data, ps);
1174 break;
1176 case MSTATE_RATELIMITING:
1177 /* we need to let time pass, ignore socket(s) */
1178 break;
1180 case MSTATE_DONE:
1181 case MSTATE_COMPLETED:
1182 case MSTATE_MSGSENT:
1183 /* nothing more to poll for */
1184 break;
1186 default:
1187 failf(data, "multi_getsock: unexpected multi state %d",
1188 (int)data->mstate);
1189 DEBUGASSERT(0);
1190 break;
1194 if(result) {
1195 if(result == CURLE_OUT_OF_MEMORY)
1196 return CURLM_OUT_OF_MEMORY;
1197 failf(data, "error determining pollset: %d", (int)result);
1198 return CURLM_INTERNAL_ERROR;
1201#ifdef CURLVERBOSE
1202 if(CURL_TRC_M_is_verbose(data)) {
1203 size_t timeout_count = multi_timeouts_count(&data->state.timeouts);
1204 switch(ps->n) {
1205 case 0:
1206 CURL_TRC_M(data, "pollset[], timeouts=%zu, paused %d/%d (r/w)",
1207 timeout_count,
1208 Curl_xfer_send_is_paused(data),
1209 Curl_xfer_recv_is_paused(data));
1210 break;
1211 case 1:
1212 CURL_TRC_M(data, "pollset[fd=%" FMT_SOCKET_T " %s%s], timeouts=%zu",
1213 ps->sockets[0],
1214 (ps->actions[0] & CURL_POLL_IN) ? "IN" : "",
1215 (ps->actions[0] & CURL_POLL_OUT) ? "OUT" : "",
1216 timeout_count);
1217 break;
1218 case 2:
1219 CURL_TRC_M(data, "pollset[fd=%" FMT_SOCKET_T " %s%s, "
1220 "fd=%" FMT_SOCKET_T " %s%s], timeouts=%zu",
1221 ps->sockets[0],
1222 (ps->actions[0] & CURL_POLL_IN) ? "IN" : "",
1223 (ps->actions[0] & CURL_POLL_OUT) ? "OUT" : "",
1224 ps->sockets[1],
1225 (ps->actions[1] & CURL_POLL_IN) ? "IN" : "",
1226 (ps->actions[1] & CURL_POLL_OUT) ? "OUT" : "",
1227 timeout_count);
1228 break;
1229 default:
1230 CURL_TRC_M(data, "pollset[fds=%u], timeouts=%zu", ps->n, timeout_count);
1231 break;
1233 CURL_TRC_EASY_TIMERS(data);
1235#endif
1237 return CURLM_OK;
1240CURLMcode curl_multi_fdset(CURLM *m,
1241 fd_set *read_fd_set, fd_set *write_fd_set,
1242 fd_set *exc_fd_set, int *max_fd)
1244 struct Curl_mapi_guard guard;
1245 CURLMcode mresult;
1247 if(CURL_MAPI_ENTER(&guard, m, multi_fdset, &mresult)) {
1248 /* Scan through all the easy handles to get the file descriptors set.
1249 Some easy handles may not have connected to the remote host yet,
1250 and then we must make sure that is done. */
1251 struct Curl_multi *multi = m;
1252 struct easy_pollset ps;
1253 int this_max_fd = -1;
1254 unsigned int i;
1255 uint32_t mid;
1256 (void)exc_fd_set;
1258 Curl_pollset_init(&ps);
1259 if(Curl_uint32_bset_first(&multi->process, &mid)) {
1260 do {
1261 struct Curl_easy *data = Curl_multi_get_easy(multi, mid);
1263 if(!data) {
1264 DEBUGASSERT(0);
1265 continue;
1268 Curl_multi_pollset(data, &ps);
1269 for(i = 0; i < ps.n; i++) {
1270 if(!FDSET_SOCK(ps.sockets[i]))
1271 /* pretend it does not exist */
1272 continue;
1273 if(ps.actions[i] & CURL_POLL_IN)
1274 FD_SET(ps.sockets[i], read_fd_set);
1275 if(ps.actions[i] & CURL_POLL_OUT)
1276 FD_SET(ps.sockets[i], write_fd_set);
1277 if((int)ps.sockets[i] > this_max_fd)
1278 this_max_fd = (int)ps.sockets[i];
1280 } while(Curl_uint32_bset_next(&multi->process, mid, &mid));
1283 Curl_cshutdn_setfds(&multi->cshutdn, multi->admin,
1284 read_fd_set, write_fd_set, &this_max_fd);
1286 *max_fd = this_max_fd;
1287 Curl_pollset_cleanup(&ps);
1289 mresult = CURLM_OK;
1291 CURL_MAPI_LEAVE(&guard);
1292 return mresult;
1295CURLMcode curl_multi_waitfds(CURLM *m,
1296 struct curl_waitfd *ufds,
1297 unsigned int size,
1298 unsigned int *fd_count)
1300 struct Curl_mapi_guard guard;
1301 CURLMcode mresult;
1303 if(CURL_MAPI_ENTER(&guard, m, multi_waitfds, &mresult)) {
1304 struct Curl_waitfds cwfds;
1305 struct Curl_multi *multi = m;
1306 struct easy_pollset ps;
1307 unsigned int need = 0;
1308 uint32_t mid;
1310 if(!ufds && (size || !fd_count)) {
1311 mresult = CURLM_BAD_FUNCTION_ARGUMENT;
1312 goto out;
1315 Curl_pollset_init(&ps);
1316 Curl_waitfds_init(&cwfds, ufds, size);
1317 mresult = CURLM_OK;
1318 if(Curl_uint32_bset_first(&multi->process, &mid)) {
1319 do {
1320 struct Curl_easy *data = Curl_multi_get_easy(multi, mid);
1321 if(!data) {
1322 DEBUGASSERT(0);
1323 Curl_uint32_bset_remove(&multi->process, mid);
1324 Curl_uint32_bset_remove(&multi->dirty, mid);
1325 continue;
1327 Curl_multi_pollset(data, &ps);
1328 need += Curl_waitfds_add_ps(&cwfds, &ps);
1329 } while(Curl_uint32_bset_next(&multi->process, mid, &mid));
1332 need += Curl_cshutdn_add_waitfds(&multi->cshutdn, multi->admin, &cwfds);
1334 if(need != cwfds.n && ufds)
1335 mresult = CURLM_OUT_OF_MEMORY;
1337 if(fd_count)
1338 *fd_count = need;
1339 Curl_pollset_cleanup(&ps);
1341out:
1342 CURL_MAPI_LEAVE(&guard);
1343 return mresult;
1346#ifdef USE_WINSOCK
1347/* Reset FD_WRITE for TCP sockets. Nothing is actually sent. UDP sockets cannot
1348 * be reset this way because an empty datagram would be sent. #9203
1350 * "On Windows the internal state of FD_WRITE as returned from
1351 * WSAEnumNetworkEvents is only reset after successful send()."
1352 */
1353static void reset_socket_fdwrite(curl_socket_t s)
1355 int t;
1356 int l = (int)sizeof(t);
1357 if(!getsockopt(s, SOL_SOCKET, SO_TYPE, (char *)&t, &l) && t == SOCK_STREAM)
1358 swrite(s, NULL, 0);
1361static CURLMcode multi_winsock_select(struct Curl_multi *multi,
1362 struct curl_pollfds *cpfds,
1363 unsigned int curl_nfds,
1364 struct curl_waitfd extra_fds[],
1365 unsigned int extra_nfds,
1366 int timeout_ms,
1367 bool extrawait,
1368 int *pnevents)
1370 CURLMcode mresult = CURLM_OK;
1371 WSANETWORKEVENTS wsa_events;
1372 int nevents = 0;
1373 size_t i;
1375 DEBUGASSERT(multi->wsa_event != WSA_INVALID_EVENT);
1377 /* Set the WSA events based on the collected pollds */
1378 for(i = 0; i < cpfds->n; i++) {
1379 long mask = 0;
1380 if(cpfds->pfds[i].events & POLLIN)
1381 mask |= FD_READ | FD_ACCEPT | FD_CLOSE;
1382 if(cpfds->pfds[i].events & POLLPRI)
1383 mask |= FD_OOB;
1384 if(cpfds->pfds[i].events & POLLOUT) {
1385 mask |= FD_WRITE | FD_CONNECT | FD_CLOSE;
1386 reset_socket_fdwrite(cpfds->pfds[i].fd);
1388 if(mask && WSAEventSelect(cpfds->pfds[i].fd, multi->wsa_event, mask)) {
1389 mresult = CURLM_OUT_OF_MEMORY;
1390 goto out;
1394 if(cpfds->n || extrawait) {
1395 int pollrc = 0;
1397 if(cpfds->n) { /* pre-check with Winsock */
1398 pollrc = Curl_poll(cpfds->pfds, cpfds->n, 0);
1399 if(pollrc < 0) {
1400 mresult = CURLM_UNRECOVERABLE_POLL;
1401 goto out;
1403 nevents = pollrc;
1406 if(!nevents) {
1407 /* now wait... if not ready during the pre-check (pollrc == 0) */
1408 WSAWaitForMultipleEvents(1, &multi->wsa_event, FALSE, (DWORD)timeout_ms,
1409 FALSE);
1412 /* With Winsock, we have to run the following section unconditionally
1413 to call WSAEventSelect(fd, event, 0) on all the sockets */
1414 /* copy revents results from the poll to the curl_multi_wait poll
1415 struct, the bit values of the actual underlying poll() implementation
1416 may not be the same as the ones in the public libcurl API! */
1417 for(i = 0; i < extra_nfds; i++) {
1418 unsigned short mask = 0;
1419 curl_socket_t s = extra_fds[i].fd;
1421 wsa_events.lNetworkEvents = 0;
1422 if(WSAEnumNetworkEvents(s, NULL, &wsa_events) == 0) {
1423 if(wsa_events.lNetworkEvents & (FD_READ | FD_ACCEPT | FD_CLOSE))
1424 mask |= CURL_WAIT_POLLIN;
1425 if(wsa_events.lNetworkEvents & (FD_WRITE | FD_CONNECT | FD_CLOSE))
1426 mask |= CURL_WAIT_POLLOUT;
1427 if(wsa_events.lNetworkEvents & FD_OOB)
1428 mask |= CURL_WAIT_POLLPRI;
1429 if(!pollrc && wsa_events.lNetworkEvents)
1430 nevents++;
1432 WSAEventSelect(s, multi->wsa_event, 0);
1433 if(!pollrc) {
1434 extra_fds[i].revents = (short)mask;
1435 continue;
1437 else {
1438 unsigned r = (unsigned)cpfds->pfds[curl_nfds + i].revents;
1439 if(r & POLLIN)
1440 mask |= CURL_WAIT_POLLIN;
1441 if(r & POLLOUT)
1442 mask |= CURL_WAIT_POLLOUT;
1443 if(r & POLLPRI)
1444 mask |= CURL_WAIT_POLLPRI;
1445 extra_fds[i].revents = (short)mask;
1449 /* Count up all our own sockets that had activity,
1450 and remove them from the event. */
1451 for(i = 0; i < curl_nfds; ++i) {
1452 wsa_events.lNetworkEvents = 0;
1453 if(WSAEnumNetworkEvents(cpfds->pfds[i].fd, NULL, &wsa_events) == 0) {
1454 if(!pollrc && wsa_events.lNetworkEvents)
1455 nevents++;
1457 WSAEventSelect(cpfds->pfds[i].fd, multi->wsa_event, 0);
1459 WSAResetEvent(multi->wsa_event);
1462out:
1463 *pnevents = nevents;
1464 return mresult;
1467#else /* USE_WINSOCK */
1469static CURLMcode multi_posix_poll(struct Curl_multi *multi,
1470 struct curl_pollfds *cpfds,
1471 unsigned int curl_nfds,
1472 struct curl_waitfd extra_fds[],
1473 unsigned int extra_nfds,
1474 int timeout_ms,
1475 bool extrawait,
1476 int *pnevents)
1478 CURLMcode mresult = CURLM_OK;
1479 int nevents = 0;
1480 size_t i;
1482 (void)multi;
1483 if(cpfds->n) {
1484 int pollrc = Curl_poll(cpfds->pfds, cpfds->n, timeout_ms); /* wait... */
1485 if(pollrc < 0) {
1486 mresult = CURLM_UNRECOVERABLE_POLL;
1487 goto out;
1489 nevents = pollrc;
1491 /* copy revents results from the poll to the curl_multi_wait poll
1492 struct, the bit values of the actual underlying poll() implementation
1493 may not be the same as the ones in the public libcurl API! */
1494 for(i = 0; i < extra_nfds; i++) {
1495 unsigned r = (unsigned)cpfds->pfds[curl_nfds + i].revents;
1496 unsigned short mask = 0;
1497 if(r & POLLIN)
1498 mask |= CURL_WAIT_POLLIN;
1499 if(r & POLLOUT)
1500 mask |= CURL_WAIT_POLLOUT;
1501 if(r & POLLPRI)
1502 mask |= CURL_WAIT_POLLPRI;
1503 extra_fds[i].revents = (short)mask;
1506 else if(extrawait) {
1507 /* No fds to poll, but asked to obey timeout_ms anyway. We cannot
1508 * use Curl_poll() as it, on some platforms, returns immediately
1509 * without fds. */
1510 curlx_wait_ms(timeout_ms);
1513out:
1514 *pnevents = nevents;
1515 return mresult;
1518#endif /* !USE_WINSOCK */
1520#define NUM_POLLS_ON_STACK 10
1522static CURLMcode multi_wait(struct Curl_multi *multi,
1523 struct curl_waitfd extra_fds[],
1524 unsigned int extra_nfds,
1525 int timeout_ms,
1526 int *ret,
1527 bool extrawait) /* when no socket, wait */
1529 size_t i;
1530 int timeout_internal;
1531 int nevents = 0;
1532 struct easy_pollset ps;
1533 struct pollfd a_few_on_stack[NUM_POLLS_ON_STACK];
1534 struct curl_pollfds cpfds;
1535 unsigned int curl_nfds = 0; /* how many pfds are for curl transfers */
1536 struct Curl_easy *data = NULL;
1537 CURLMcode mresult = CURLM_OK;
1538 uint32_t mid;
1539#ifdef ENABLE_WAKEUP
1540 int wakeup_idx = -1;
1541#endif
1543 if(timeout_ms < 0)
1544 return CURLM_BAD_FUNCTION_ARGUMENT;
1546 Curl_pollset_init(&ps);
1547 Curl_pollfds_init(&cpfds, a_few_on_stack, NUM_POLLS_ON_STACK);
1549 /* Add the curl handles to our pollfds first */
1550 if(Curl_uint32_bset_first(&multi->process, &mid)) {
1551 do {
1552 data = Curl_multi_get_easy(multi, mid);
1553 if(!data) {
1554 DEBUGASSERT(0);
1555 Curl_uint32_bset_remove(&multi->process, mid);
1556 Curl_uint32_bset_remove(&multi->dirty, mid);
1557 continue;
1559 Curl_multi_pollset(data, &ps);
1560 if(Curl_pollfds_add_ps(&cpfds, &ps)) {
1561 mresult = CURLM_OUT_OF_MEMORY;
1562 goto out;
1564 } while(Curl_uint32_bset_next(&multi->process, mid, &mid));
1567 if(Curl_cshutdn_add_pollfds(&multi->cshutdn, multi->admin, &cpfds)) {
1568 mresult = CURLM_OUT_OF_MEMORY;
1569 goto out;
1572#ifdef ENABLE_WAKEUP
1573 /* If `extrawait` is TRUE *or* we have `extra_fds`to poll *or* we
1574 * have transfer sockets to poll, we obey `timeout_ms`.
1575 * Then we need to also monitor the multi's wakeup
1576 * socket to catch calls to `curl_multi_wakeup()` during the wait. */
1577 if(extrawait || cpfds.n || extra_nfds) {
1578 wakeup_idx = cpfds.n;
1579 if(Curl_pollfds_add_sock(&cpfds, multi->wakeup_pair[0], POLLIN)) {
1580 mresult = CURLM_OUT_OF_MEMORY;
1581 goto out;
1584#endif
1586 curl_nfds = cpfds.n; /* what curl internally uses in cpfds */
1587 /* Add external file descriptions from poll-like struct curl_waitfd */
1588 for(i = 0; i < extra_nfds; i++) {
1589 unsigned short events = 0;
1590 if(extra_fds[i].events & CURL_WAIT_POLLIN)
1591 events |= POLLIN;
1592 if(extra_fds[i].events & CURL_WAIT_POLLPRI)
1593 events |= POLLPRI;
1594 if(extra_fds[i].events & CURL_WAIT_POLLOUT)
1595 events |= POLLOUT;
1596 if(Curl_pollfds_add_sock(&cpfds, extra_fds[i].fd, events)) {
1597 mresult = CURLM_OUT_OF_MEMORY;
1598 goto out;
1602 /* We check the internal timeout *AFTER* we collected all sockets to
1603 * poll. Collecting the sockets may install new timers by protocols
1604 * and connection filters.
1605 * Use the shorter one of the internal and the caller requested timeout.
1606 * If we are called with `!extrawait` and multi_timeout() reports no
1607 * timeouts exist, do not wait. */
1608 multi_timeout(multi, NULL, &timeout_internal);
1609 if((timeout_internal >= 0) && (timeout_internal < timeout_ms))
1610 timeout_ms = timeout_internal;
1612 if(data)
1613 CURL_TRC_M(data, "multi_wait(fds=%u, timeout=%d) tinternal=%d",
1614 cpfds.n, timeout_ms, timeout_internal);
1616#ifdef USE_WINSOCK
1617 mresult = multi_winsock_select(multi, &cpfds, curl_nfds,
1618 extra_fds, extra_nfds,
1619 timeout_ms, extrawait, &nevents);
1620#else
1621 mresult = multi_posix_poll(multi, &cpfds, curl_nfds,
1622 extra_fds, extra_nfds,
1623 timeout_ms, extrawait, &nevents);
1624#endif
1626#ifdef ENABLE_WAKEUP
1627 if(nevents && (wakeup_idx >= 0)) {
1628 if(cpfds.pfds[wakeup_idx].revents & POLLIN) {
1629 (void)Curl_wakeup_consume(multi->wakeup_pair, TRUE);
1630 /* do not count the wakeup socket into the returned value */
1631 nevents--;
1634#endif
1636out:
1637 Curl_pollset_cleanup(&ps);
1638 Curl_pollfds_cleanup(&cpfds);
1639 if(ret)
1640 *ret = nevents;
1641 return mresult;
1644CURLMcode curl_multi_wait(CURLM *m,
1645 struct curl_waitfd extra_fds[],
1646 unsigned int extra_nfds,
1647 int timeout_ms,
1648 int *ret)
1650 struct Curl_mapi_guard guard;
1651 CURLMcode mresult;
1653 if(CURL_MAPI_ENTER(&guard, m, multi_wait, &mresult)) {
1654 mresult = multi_wait(m, extra_fds, extra_nfds, timeout_ms, ret, FALSE);
1656 CURL_MAPI_LEAVE(&guard);
1657 return mresult;
1660CURLMcode curl_multi_poll(CURLM *m,
1661 struct curl_waitfd extra_fds[],
1662 unsigned int extra_nfds,
1663 int timeout_ms,
1664 int *ret)
1666 struct Curl_mapi_guard guard;
1667 CURLMcode mresult;
1669 if(CURL_MAPI_ENTER(&guard, m, multi_poll, &mresult)) {
1670 mresult = multi_wait(m, extra_fds, extra_nfds, timeout_ms, ret, TRUE);
1672 CURL_MAPI_LEAVE(&guard);
1673 return mresult;
1676CURLMcode curl_multi_wakeup(CURLM *m)
1678 /* this function is usually called from another thread,
1679 it has to be careful only to access parts of the
1680 Curl_multi struct that are constant */
1681 struct Curl_multi *multi = m;
1682 CURLMcode mresult = CURLM_WAKEUP_FAILURE;
1684 /* GOOD_MULTI_HANDLE can be safely called */
1685 if(!GOOD_MULTI_HANDLE(multi))
1686 return CURLM_BAD_HANDLE;
1688#ifdef ENABLE_WAKEUP
1689 /* the wakeup_pair variable is only written during init and cleanup,
1690 making it safe to access from another thread after the init part
1691 and before cleanup */
1692 if(!Curl_wakeup_signal(multi->wakeup_pair))
1693 mresult = CURLM_OK;
1694#endif
1695#ifdef USE_WINSOCK
1696 if(WSASetEvent(multi->wsa_event))
1697 mresult = CURLM_OK;
1698#endif
1699 return mresult;
1702#ifdef ENABLE_INTERNAL_WAKEUP
1703void Curl_multi_wakeup_internal(struct Curl_multi *multi)
1705 /* This is expected to be invocable from another thread which
1706 * does NOT outlive the multi handle. Check for sanity. */
1707 if(GOOD_MULTI_HANDLE(multi))
1708 Curl_wakeup_signal(multi->wakeup_internal);
1709 else
1710 DEBUGASSERT(0);
1712#endif
1715 * multi_ischanged() is called
1717 * Returns TRUE/FALSE whether the state is changed to trigger a CONNECT_PEND
1718 * => CONNECT action.
1720 * Set 'clear' to TRUE to have it also clear the state variable.
1721 */
1722static bool multi_ischanged(struct Curl_multi *multi, bool clear)
1724 bool retval = FALSE;
1725 DEBUGASSERT(multi);
1726 if(multi) {
1727 retval = (bool)multi->recheckstate;
1728 if(clear)
1729 multi->recheckstate = FALSE;
1731 return retval;
1735 * Curl_multi_connchanged() is called to tell that there is a connection in
1736 * this multi handle that has changed state (multiplexing become possible, the
1737 * number of allowed streams changed or similar), and a subsequent use of this
1738 * multi handle should move CONNECT_PEND handles back to CONNECT to have them
1739 * retry.
1740 */
1741void Curl_multi_connchanged(struct Curl_multi *multi)
1743 /* May be invoked for a `data->multi` when no multi is present. */
1744 if(multi)
1745 multi->recheckstate = TRUE;
1748CURLMcode Curl_multi_add_perform(struct Curl_multi *multi,
1749 struct Curl_easy *data,
1750 struct connectdata *conn)
1752 CURLMcode mresult;
1754 mresult = Curl_multi_add_handle(multi, data);
1755 if(!mresult) {
1756 CURLcode result;
1758 /* pass in NULL for 'conn' here since we do not want to init the
1759 connection, only this transfer */
1760 result = Curl_init_transfer(data, NULL);
1761 if(result) {
1762 Curl_multi_remove_handle(multi, data);
1763 return CURLM_INTERNAL_ERROR;
1766 /* take this handle to the perform state right away */
1767 multistate(data, MSTATE_PERFORMING);
1768 Curl_attach_connection(data, conn, TRUE);
1769 CURL_REQ_SET_RECV(data);
1771 return mresult;
1774static CURLcode multi_do(struct Curl_easy *data, bool *done)
1776 CURLcode result = CURLE_OK;
1777 struct connectdata *conn = data->conn;
1779 DEBUGASSERT(conn);
1780 DEBUGASSERT(conn->scheme);
1782 if(conn->scheme->run->do_it)
1783 result = conn->scheme->run->do_it(data, done);
1785 return result;
1789 * multi_do_more() is called during the DO_MORE multi state. It is a second
1790 * stage DO state which (wrongly) was introduced to support FTP's second
1791 * connection.
1793 * 'complete' can return DOMORE_INCOMPLETE, DOMORE_DONE or DOMORE_GOBACK
1794 * (to DOING state when there is more work to do)
1795 */
1797static CURLcode multi_do_more(struct Curl_easy *data, domore *complete)
1799 CURLcode result = CURLE_OK;
1800 struct connectdata *conn = data->conn;
1802 *complete = DOMORE_INCOMPLETE;
1804 if(conn->scheme->run->do_more)
1805 result = conn->scheme->run->do_more(data, complete);
1807 return result;
1811 * Check whether a timeout occurred, and handle it if it did
1812 */
1813static bool multi_handle_timeout(struct Curl_easy *data,
1814 const struct curltime *pnow,
1815 bool *stream_error,
1816 CURLcode *result)
1818 timediff_t timeout_ms;
1820 timeout_ms = Curl_timeleft_now_ms(data, pnow);
1821 if(timeout_ms < 0) {
1822 /* Handle timed out */
1823 timerid base_timer = Curl_is_connecting(data) ?
1824 TIMER_STARTSINGLE : TIMER_STARTOP;
1825 timediff_t elapsed_ms = Curl_pgrs_since_ms(data, NULL, base_timer);
1826 if(data->mstate == MSTATE_CONNECTING) {
1827 failf(data, "%s timed out after %" FMT_TIMEDIFF_T " milliseconds",
1828 data->conn->bits.dns_resolved ? "Connection" : "Resolving",
1829 elapsed_ms);
1830 /* keep what we know about a partly done connect, e.g. TCP done
1831 * but TLS handshake still ongoing */
1832 Curl_conn_report_failed(data, FIRSTSOCKET);
1834 else {
1835 struct SingleRequest *k = &data->req;
1836 if(k->size != -1) {
1837 failf(data, "Operation timed out after %" FMT_TIMEDIFF_T
1838 " milliseconds with %" FMT_OFF_T " out of %"
1839 FMT_OFF_T " bytes received",
1840 elapsed_ms, k->bytecount, k->size);
1842 else {
1843 failf(data, "Operation timed out after %" FMT_TIMEDIFF_T
1844 " milliseconds with %" FMT_OFF_T " bytes received",
1845 elapsed_ms, k->bytecount);
1848 *result = CURLE_OPERATION_TIMEDOUT;
1849 if(data->conn) {
1850 /* Force connection closed if the connection has indeed been used */
1851 if(data->mstate > MSTATE_DO) {
1852 streamclose(data->conn);
1853 *stream_error = TRUE;
1855 (void)multi_done(data, *result, TRUE);
1857 return TRUE;
1860 return FALSE;
1864 * We are doing protocol-specific connecting and this is being called over and
1865 * over from the multi interface until the connection phase is done on
1866 * protocol layer.
1867 */
1869static CURLcode protocol_connecting(struct Curl_easy *data, bool *done)
1871 CURLcode result = CURLE_OK;
1872 struct connectdata *conn = data->conn;
1874 if(conn && conn->scheme->run->connecting) {
1875 *done = FALSE;
1876 result = conn->scheme->run->connecting(data, done);
1878 else
1879 *done = TRUE;
1881 return result;
1885 * We are DOING this is being called over and over from the multi interface
1886 * until the DOING phase is done on protocol layer.
1887 */
1889static CURLcode protocol_doing(struct Curl_easy *data, bool *done)
1891 CURLcode result = CURLE_OK;
1892 struct connectdata *conn = data->conn;
1894 if(conn && conn->scheme->run->doing) {
1895 *done = FALSE;
1896 result = conn->scheme->run->doing(data, done);
1898 else
1899 *done = TRUE;
1901 return result;
1905 * We have discovered that the TCP connection has been successful, we can now
1906 * proceed with some action.
1908 */
1909static CURLcode protocol_connect(struct Curl_easy *data, bool *protocol_done)
1911 struct connectdata *conn = data->conn;
1912 CURLcode result = CURLE_OK;
1914 DEBUGASSERT(conn);
1915 DEBUGASSERT(protocol_done);
1916 DEBUGASSERT(Curl_conn_is_connected(conn, FIRSTSOCKET));
1918 *protocol_done = FALSE;
1919 if(!conn->bits.protoconnstart) {
1920 if(conn->scheme->run->connect_it) {
1921 /* Call the protocol-specific connect function */
1922 result = conn->scheme->run->connect_it(data, protocol_done);
1923 if(result)
1924 return result;
1926 conn->bits.protoconnstart = TRUE;
1929 /* Unless this protocol does not have any protocol-connect callback, as
1930 then we know we are done. */
1931 if(!conn->scheme->run->connecting)
1932 *protocol_done = TRUE;
1933 return CURLE_OK;
1937 * posttransfer() is called immediately after a transfer ends
1938 */
1939static void multi_posttransfer(struct Curl_easy *data)
1941#if defined(HAVE_SIGNAL) && defined(SIGPIPE) && !defined(MSG_NOSIGNAL)
1942 /* restore the signal handler for SIGPIPE before we get back */
1943 if(!data->set.no_signal)
1944 signal(SIGPIPE, data->state.prev_signal);
1945#else
1946 (void)data;
1947#endif
1951 * multi_follow() handles the URL redirect magic. Pass in the 'newurl' string
1952 * as given by the remote server and set up the new URL to request.
1954 * This function DOES NOT FREE the given URL.
1955 */
1956static CURLcode multi_follow(struct Curl_easy *data,
1957 const struct Curl_scheme *handler,
1958 const char *newurl, /* the Location: string */
1959 followtype type) /* see transfer.h */
1961 if(handler && handler->run->follow)
1962 return handler->run->follow(data, newurl, type);
1964 if(type == FOLLOW_RETRY)
1965 /* Retries are generic and do not require protocol-specific redirect
1966 handling. */
1967 return CURLE_OK;
1969 return CURLE_TOO_MANY_REDIRECTS;
1972static CURLcode mspeed_check(struct Curl_easy *data)
1974 if(Curl_rlimit_active(&data->progress.dl.rlimit) ||
1975 Curl_rlimit_active(&data->progress.ul.rlimit)) {
1976 /* check if our send/recv limits require idle waits */
1977 const struct curltime *pnow = Curl_pgrs_now(data);
1978 timediff_t recv_ms, send_ms;
1980 send_ms = Curl_rlimit_wait_ms(&data->progress.ul.rlimit, pnow);
1981 recv_ms = Curl_rlimit_wait_ms(&data->progress.dl.rlimit, pnow);
1983 if(send_ms || recv_ms) {
1984 if(data->mstate != MSTATE_RATELIMITING) {
1985 multistate(data, MSTATE_RATELIMITING);
1987 Curl_expire_set(data, EXPIRE_TOOFAST, CURLMAX(send_ms, recv_ms), pnow);
1988 Curl_multi_clear_dirty(data);
1989 CURL_TRC_M(data, "[RLIMIT] waiting %" FMT_TIMEDIFF_T "ms",
1990 CURLMAX(send_ms, recv_ms));
1991 return CURLE_AGAIN;
1993 else {
1994 /* when will the rate limits increase next? The transfer needs
1995 * to run again at that time or it may stall. */
1996 send_ms = Curl_rlimit_next_step_ms(&data->progress.ul.rlimit, pnow);
1997 recv_ms = Curl_rlimit_next_step_ms(&data->progress.dl.rlimit, pnow);
1998 if(send_ms || recv_ms) {
1999 timediff_t next_ms = CURLMIN(send_ms, recv_ms);
2000 if(!next_ms)
2001 next_ms = CURLMAX(send_ms, recv_ms);
2002 Curl_expire_set(data, EXPIRE_TOOFAST, next_ms, pnow);
2003 CURL_TRC_M(data, "[RLIMIT] next token update in %" FMT_TIMEDIFF_T "ms",
2004 next_ms);
2009 if(data->mstate != MSTATE_PERFORMING) {
2010 CURL_TRC_M(data, "[RLIMIT] wait over, continue");
2011 multistate(data, MSTATE_PERFORMING);
2013 return CURLE_OK;
2016static CURLMcode multistate_performing(struct Curl_easy *data,
2017 bool *stream_errorp,
2018 CURLcode *resultp)
2020 char *newurl = NULL;
2021 bool retry = FALSE;
2022 CURLMcode mresult = CURLM_OK;
2023 CURLcode result = *resultp = CURLE_OK;
2024 *stream_errorp = FALSE;
2026 if(mspeed_check(data) == CURLE_AGAIN)
2027 return CURLM_OK;
2029 /* read/write data if it is ready to do so */
2030 result = Curl_sendrecv(data);
2032 if(data->req.done || (result == CURLE_RECV_ERROR)) {
2033 /* If CURLE_RECV_ERROR happens early enough, we assume it was a race
2034 * condition and the server closed the reused connection exactly when we
2035 * wanted to use it, so figure out if that is indeed the case.
2036 */
2037 CURLcode ret = Curl_retry_request(data, &newurl);
2038 if(!ret)
2039 retry = !!newurl;
2040 else if(!result)
2041 result = ret;
2043 if(retry) {
2044 /* if we are to retry, set the result to OK and consider the
2045 request as done */
2046 result = CURLE_OK;
2047 data->req.done = TRUE;
2050#ifndef CURL_DISABLE_HTTP
2051 else if((result == CURLE_HTTP2_STREAM) &&
2052 Curl_h2_http_1_1_error(data)) {
2053 CURLcode ret = Curl_retry_request(data, &newurl);
2055 if(!ret) {
2056 infof(data, "Downgrades to HTTP/1.1");
2057 streamclose(data->conn);
2058 data->state.http_neg.wanted = CURL_HTTP_V1x;
2059 data->state.http_neg.allowed = CURL_HTTP_V1x;
2060 /* clear the error message bit too as we ignore the one we got */
2061 data->state.errorbuf = FALSE;
2062 if(!newurl)
2063 /* typically for HTTP_1_1_REQUIRED error on first flight */
2064 newurl = Curl_bufref_dup(&data->state.url);
2065 if(!newurl) {
2066 result = CURLE_OUT_OF_MEMORY;
2068 else {
2069 /* if we are to retry, set the result to OK and consider the request
2070 as done */
2071 retry = TRUE;
2072 result = CURLE_OK;
2073 data->req.done = TRUE;
2076 else
2077 result = ret;
2079#endif
2081 if(result) {
2082 /*
2083 * The transfer phase returned error, we mark the connection to get closed
2084 * to prevent being reused. This is because we cannot possibly know if the
2085 * connection is in a good shape or not now. Unless it is a protocol which
2086 * uses two "channels" like FTP, as then the error happened in the data
2087 * connection.
2088 */
2090 if(!(data->conn->scheme->flags & PROTOPT_DUAL) &&
2091 result != CURLE_HTTP2_STREAM)
2092 streamclose(data->conn);
2094 multi_posttransfer(data);
2095 multi_done(data, result, TRUE);
2097 else if(data->req.done && !Curl_cwriter_is_paused(data)) {
2098 const struct Curl_scheme *handler = data->conn->scheme;
2100 /* call this even if the readwrite function returned error */
2101 multi_posttransfer(data);
2103 /* When we follow redirects or is set to retry the connection, we must to
2104 go back to the CONNECT state */
2105 if(data->req.newurl || retry) {
2106 followtype follow = FOLLOW_NONE;
2107 if(!retry) {
2108 /* if the URL is a follow-location and not a retried request then
2109 figure out the URL here */
2110 curlx_free(newurl);
2111 newurl = data->req.newurl;
2112 data->req.newurl = NULL;
2113 follow = FOLLOW_REDIR;
2115 else
2116 follow = FOLLOW_RETRY;
2117 (void)multi_done(data, CURLE_OK, FALSE);
2118 /* multi_done() might return CURLE_GOT_NOTHING */
2119 result = multi_follow(data, handler, newurl, follow);
2120 if(!result) {
2121 multistate(data, MSTATE_SETUP);
2122 mresult = CURLM_CALL_MULTI_PERFORM;
2125 else {
2126 /* after the transfer is done, go DONE */
2128 /* but first check to see if we got a location info even though we are
2129 not following redirects */
2130 if(data->req.location) {
2131 curlx_free(newurl);
2132 newurl = data->req.location;
2133 data->req.location = NULL;
2134 result = multi_follow(data, handler, newurl, FOLLOW_FAKE);
2135 if(result) {
2136 *stream_errorp = TRUE;
2137 result = multi_done(data, result, TRUE);
2141 if(!result) {
2142 multistate(data, MSTATE_DONE);
2143 mresult = CURLM_CALL_MULTI_PERFORM;
2147 else { /* not errored, not done */
2148 mspeed_check(data);
2150 curlx_free(newurl);
2151 *resultp = result;
2152 return mresult;
2155static CURLMcode multistate_do(struct Curl_easy *data,
2156 bool *stream_errorp,
2157 CURLcode *resultp)
2159 CURLMcode mresult = CURLM_OK;
2160 CURLcode result = CURLE_OK;
2161 if(data->set.fprereq) {
2162 struct Curl_mapi_guard guard;
2163 int prereq_rc;
2165 /* call the prerequest callback function */
2166 CURL_CBAPI_START(&guard, data, easy_fprereq);
2167 prereq_rc = data->set.fprereq(data->set.prereq_userp,
2168 data->info.primary.remote_ip,
2169 data->info.primary.local_ip,
2170 data->info.primary.remote_port,
2171 data->info.primary.local_port);
2172 CURL_CBAPI_END(&guard);
2173 if(prereq_rc != CURL_PREREQFUNC_OK) {
2174 failf(data, "operation aborted by pre-request callback");
2175 /* failure in pre-request callback - do not do any other processing */
2176 result = CURLE_ABORTED_BY_CALLBACK;
2177 multi_posttransfer(data);
2178 multi_done(data, result, FALSE);
2179 *stream_errorp = TRUE;
2180 goto end;
2184 if(data->set.connect_only && !data->set.connect_only_ws) {
2185 multistate(data, MSTATE_DONE);
2186 mresult = CURLM_CALL_MULTI_PERFORM;
2188 else {
2189 bool dophase_done = FALSE;
2190 /* Perform the protocol's DO action */
2191 result = multi_do(data, &dophase_done);
2193 if(!result) {
2194 if(!dophase_done) {
2195#ifndef CURL_DISABLE_FTP
2196 /* some steps needed for wildcard matching */
2197 if(data->state.wildcardmatch) {
2198 struct WildcardData *wc = data->wildcard;
2199 if(wc->state == CURLWC_DONE || wc->state == CURLWC_SKIP) {
2200 /* skip some states if it is important */
2201 multi_done(data, CURLE_OK, FALSE);
2203 /* if there is no connection left, skip the DONE state */
2204 multistate(data, data->conn ? MSTATE_DONE : MSTATE_COMPLETED);
2205 mresult = CURLM_CALL_MULTI_PERFORM;
2206 goto end;
2209#endif
2210 /* DO was not completed in one function call, we must continue
2211 DOING... */
2212 multistate(data, MSTATE_DOING);
2213 mresult = CURLM_CALL_MULTI_PERFORM;
2216 /* after DO, go DO_DONE... or DO_MORE */
2217 else if(data->conn->bits.do_more) {
2218 /* we are supposed to do more, but we need to sit down, relax and wait
2219 a little while first */
2220 multistate(data, MSTATE_DOING_MORE);
2221 mresult = CURLM_CALL_MULTI_PERFORM;
2223 else {
2224 /* we are done with the DO, now DID */
2225 multistate(data, MSTATE_DID);
2226 mresult = CURLM_CALL_MULTI_PERFORM;
2229 else if((result == CURLE_SEND_ERROR) &&
2230 data->conn->bits.reuse) {
2231 /*
2232 * In this situation, a connection that we were trying to use may have
2233 * unexpectedly died. If possible, send the connection back to the
2234 * CONNECT phase so we can try again.
2235 */
2236 const struct Curl_scheme *handler = data->conn->scheme;
2237 char *newurl = NULL;
2238 followtype follow = FOLLOW_NONE;
2239 CURLcode drc;
2241 drc = Curl_retry_request(data, &newurl);
2242 if(drc) {
2243 /* a failure here pretty much implies an out of memory */
2244 result = drc;
2245 *stream_errorp = TRUE;
2248 multi_posttransfer(data);
2249 drc = multi_done(data, result, FALSE);
2251 /* When set to retry the connection, we must go back to the CONNECT
2252 * state */
2253 if(newurl) {
2254 if(!drc || (drc == CURLE_SEND_ERROR)) {
2255 follow = FOLLOW_RETRY;
2256 drc = multi_follow(data, handler, newurl, follow);
2257 if(!drc) {
2258 multistate(data, MSTATE_SETUP);
2259 mresult = CURLM_CALL_MULTI_PERFORM;
2260 result = CURLE_OK;
2262 else {
2263 /* Follow failed */
2264 result = drc;
2267 else {
2268 /* done did not return OK or SEND_ERROR */
2269 result = drc;
2272 else {
2273 /* Have error handler disconnect conn if we cannot retry */
2274 *stream_errorp = TRUE;
2276 curlx_free(newurl);
2278 else {
2279 /* failure detected */
2280 multi_posttransfer(data);
2281 if(data->conn)
2282 multi_done(data, result, FALSE);
2283 *stream_errorp = TRUE;
2286end:
2287 *resultp = result;
2288 return mresult;
2291static CURLMcode multistate_ratelimiting(struct Curl_easy *data,
2292 CURLcode *resultp)
2294 CURLcode result = CURLE_OK;
2295 CURLMcode mresult = CURLM_OK;
2296 DEBUGASSERT(data->conn);
2297 /* if both rates are within spec, resume transfer */
2298 result = Curl_pgrsCheck(data);
2300 if(result) {
2301 if(!(data->conn->scheme->flags & PROTOPT_DUAL) &&
2302 result != CURLE_HTTP2_STREAM)
2303 streamclose(data->conn);
2305 multi_posttransfer(data);
2306 multi_done(data, result, TRUE);
2308 else {
2309 if(!mspeed_check(data))
2310 mresult = CURLM_CALL_MULTI_PERFORM;
2312 *resultp = result;
2313 return mresult;
2316static CURLMcode multistate_connect(struct Curl_multi *multi,
2317 struct Curl_easy *data,
2318 CURLcode *resultp)
2320 /* Connect. We want to get a connection identifier filled in. This state can
2321 be entered from SETUP and from PENDING. */
2322 bool connected;
2323 CURLMcode mresult = CURLM_OK;
2324 CURLcode result = Curl_connect(data, &connected);
2325 if(result == CURLE_NO_CONNECTION_AVAILABLE) {
2326 /* There was no connection available. We will go to the pending state and
2327 wait for an available connection. */
2328 multistate(data, MSTATE_PENDING);
2329 /* move from process to pending set */
2330 Curl_uint32_bset_remove(&multi->process, data->mid);
2331 Curl_uint32_bset_remove(&multi->dirty, data->mid);
2332 Curl_uint32_bset_add(&multi->pending, data->mid);
2333 *resultp = CURLE_OK;
2334 return mresult;
2336 else
2337 multi_schedule_pending(data->multi);
2339 if(!result) {
2340 /* after the connect has been sent off, go WAITCONNECT unless the
2341 protocol connect is already done and we can go directly to WAITDO or
2342 DO! */
2343 mresult = CURLM_CALL_MULTI_PERFORM;
2345 if(connected) {
2346 if(!data->conn->bits.reuse &&
2347 Curl_conn_is_multiplex(data->conn, FIRSTSOCKET)) {
2348 /* new connection, can multiplex, wake pending handles */
2349 multi_schedule_pending(data->multi);
2351 multistate(data, MSTATE_PROTOCONNECT);
2353 else {
2354 multistate(data, MSTATE_CONNECTING);
2357 *resultp = result;
2358 return mresult;
2361/* returns the possibly updated result */
2362static CURLcode is_finished(struct Curl_multi *multi,
2363 struct Curl_easy *data,
2364 const struct curltime *pnow,
2365 bool stream_error,
2366 CURLcode result)
2368 if(data->mstate < MSTATE_COMPLETED) {
2369 if(result) {
2370 /*
2371 * If an error was returned, and we are not in completed state now,
2372 * then we go to completed and consider this transfer aborted.
2373 */
2375 /* No attempt to disconnect connections must be made before this -
2376 connection detach and termination happens only here */
2378 /* Check if we can move pending requests to send pipe */
2379 multi_schedule_pending(multi); /* connection */
2381 if(data->conn) {
2382 if(stream_error) {
2383 /* Do not attempt to send data over a connection that timed out */
2384 bool dead_connection = result == CURLE_OPERATION_TIMEDOUT;
2385 struct connectdata *conn = data->conn;
2387 /* This is where we make sure that the conn pointer is reset.
2388 We do not have to do this in every case block above where a
2389 failure is detected */
2390 Curl_detach_connection(data);
2391 Curl_conn_close(data, conn, dead_connection);
2394 else if(data->mstate == MSTATE_CONNECT) {
2395 /* Curl_connect() failed */
2396 multi_posttransfer(data);
2397 Curl_pgrsUpdate_nometer(data);
2400 multistate(data, MSTATE_COMPLETED);
2401 return result;
2403 /* if there is still a connection to use, call the progress function */
2404 else if(data->conn && Curl_conn_is_connected(data->conn, FIRSTSOCKET)) {
2405 result = Curl_pgrsUpdateX(data, pnow);
2406 if(result) {
2407 /* aborted due to progress callback return code must close the
2408 connection */
2409 streamclose(data->conn);
2411 /* if not yet in DONE state, go there. */
2412 if(data->mstate < MSTATE_DONE)
2413 multistate(data, MSTATE_DONE);
2414 return result;
2418 return result;
2421static void handle_completed(struct Curl_multi *multi,
2422 struct Curl_easy *data,
2423 CURLcode result)
2425 bool msg_to_app = data->master_mid == UINT32_MAX;
2427 if(!msg_to_app) {
2428 /* A sub transfer, not reported to the application. Is anyone still
2429 * interested in processing its results? */
2430 if(data->sub_xfer_done) {
2431 struct Curl_easy *master = Curl_multi_get_easy(multi, data->master_mid);
2433 CURL_TRC_M(data, "sub xfer done for master %u", data->master_mid);
2434 if(master)
2435 data->sub_xfer_done(data, master, result);
2436 else
2437 CURL_TRC_M(data, "master easy %u already gone.", data->master_mid);
2440 else {
2441 /* now fill in the CURLMsg with this info */
2442 struct CURLMsg *msg = &data->msg;
2444 msg->msg = CURLMSG_DONE;
2445 msg->easy_handle = data;
2446 msg->data.result = result;
2448 DEBUGASSERT(!data->conn);
2450 multistate(data, MSTATE_MSGSENT);
2452 /* remove from the other sets */
2453 Curl_uint32_bset_remove(&multi->process, data->mid);
2454 Curl_uint32_bset_remove(&multi->dirty, data->mid);
2455 Curl_uint32_bset_remove(&multi->pending, data->mid);
2456 if(msg_to_app)
2457 multi_addmsg(multi, data);
2458 if(data->state.really_alive) {
2459 data->state.really_alive = FALSE;
2460 --multi->xfers_really_alive;
2461 if(!multi->xfers_really_alive)
2462 (void)multi_assess_wakeup(multi);
2464 --multi->xfers_alive;
2465 if(!multi->xfers_alive)
2466 multi_assess_wakeup(multi);
2469static CURLMcode multistate_init(struct Curl_easy *data, CURLcode *result)
2471 if(!data->state.really_alive) {
2472 data->state.really_alive = TRUE;
2473 ++data->multi->xfers_really_alive;
2474 if(data->multi->xfers_really_alive == 1) {
2475 CURLMcode mresult = multi_assess_wakeup(data->multi);
2476 if(mresult) {
2477 failf(data, "error enabling wakeup listening: %d", mresult);
2478 return mresult;
2483 *result = Curl_pretransfer(data);
2484 if(*result)
2485 return CURLM_OK;
2487 /* after init, go SETUP */
2488 multistate(data, MSTATE_SETUP);
2489 Curl_pgrsTime(data, TIMER_STARTOP);
2490 return CURLM_CALL_MULTI_PERFORM;
2493static CURLMcode multistate_setup(struct Curl_easy *data)
2495 const struct curltime *pnow = Curl_pgrs_now(data);
2496 Curl_pgrsTimeWas(data, TIMER_STARTSINGLE, *pnow);
2497 if(data->set.timeout)
2498 Curl_expire_set(data, EXPIRE_TIMEOUT, data->set.timeout, pnow);
2499 if(data->set.connecttimeout)
2500 /* Since a connection might go to pending and back to CONNECT several
2501 times before it actually takes off, we need to set the timeout once
2502 in SETUP before we enter CONNECT the first time. */
2503 Curl_expire_set(data, EXPIRE_CONNECTTIMEOUT,
2504 data->set.connecttimeout, pnow);
2506 multistate(data, MSTATE_CONNECT);
2507 return CURLM_CALL_MULTI_PERFORM;
2510static CURLMcode multistate_connecting(struct Curl_easy *data,
2511 bool *stream_error,
2512 CURLcode *result)
2514 bool connected;
2516 if(!data->conn) {
2517 DEBUGASSERT(0);
2518 *result = CURLE_FAILED_INIT;
2519 return CURLM_OK;
2521 if(!Curl_xfer_recv_is_paused(data)) {
2522 *result = Curl_conn_connect(data, FIRSTSOCKET, FALSE, &connected);
2523 if(connected && !*result) {
2524 if(!data->conn->bits.reuse &&
2525 Curl_conn_is_multiplex(data->conn, FIRSTSOCKET)) {
2526 /* new connection, can multiplex, wake pending handles */
2527 multi_schedule_pending(data->multi);
2529 multistate(data, MSTATE_PROTOCONNECT);
2530 return CURLM_CALL_MULTI_PERFORM;
2532 else if(*result) {
2533 /* failure detected */
2534 CURL_TRC_M(data, "connect failed -> %d", (int)*result);
2535 multi_posttransfer(data);
2536 multi_done(data, *result, TRUE);
2537 *stream_error = TRUE;
2538 return CURLM_OK;
2541 return CURLM_OK;
2544static CURLMcode multistate_protoconnect(struct Curl_easy *data,
2545 bool *stream_error,
2546 CURLcode *result)
2548 bool protocol_connected = FALSE;
2550 if(!*result && data->conn->bits.reuse) {
2551 /* ftp seems to hang when protoconnect on reused connection since we
2552 * handle PROTOCONNECT in general inside the filters, it seems wrong to
2553 * restart this on a reused connection.
2554 */
2555 multistate(data, MSTATE_DO);
2556 return CURLM_CALL_MULTI_PERFORM;
2558 if(!*result)
2559 *result = protocol_connect(data, &protocol_connected);
2560 if(!*result && !protocol_connected) {
2561 /* switch to waiting state */
2562 multistate(data, MSTATE_PROTOCONNECTING);
2563 return CURLM_CALL_MULTI_PERFORM;
2565 else if(!*result) {
2566 /* protocol connect has completed, go WAITDO or DO */
2567 multistate(data, MSTATE_DO);
2568 return CURLM_CALL_MULTI_PERFORM;
2571 /* failure detected */
2572 multi_posttransfer(data);
2573 multi_done(data, *result, TRUE);
2574 *stream_error = TRUE;
2575 return CURLM_OK;
2578static CURLMcode multistate_protoconnecting(struct Curl_easy *data,
2579 bool *stream_error,
2580 CURLcode *result)
2582 bool protocol_connected = FALSE;
2584 /* protocol-specific connect phase */
2585 *result = protocol_connecting(data, &protocol_connected);
2586 if(!*result && protocol_connected) {
2587 /* after the connect has completed, go WAITDO or DO */
2588 multistate(data, MSTATE_DO);
2589 return CURLM_CALL_MULTI_PERFORM;
2591 else if(*result) {
2592 /* failure detected */
2593 multi_posttransfer(data);
2594 multi_done(data, *result, TRUE);
2595 *stream_error = TRUE;
2597 return CURLM_OK;
2600static CURLMcode multistate_doing(struct Curl_easy *data,
2601 bool *stream_error,
2602 CURLcode *result)
2604 bool dophase_done = FALSE;
2606 /* we continue DOING until the DO phase is complete */
2607 DEBUGASSERT(data->conn);
2608 *result = protocol_doing(data, &dophase_done);
2609 if(!*result) {
2610 if(dophase_done) {
2611 /* after DO, go DO_DONE or DO_MORE */
2612 multistate(data, data->conn->bits.do_more ?
2613 MSTATE_DOING_MORE : MSTATE_DID);
2614 return CURLM_CALL_MULTI_PERFORM;
2615 } /* dophase_done */
2617 else {
2618 /* failure detected */
2619 multi_posttransfer(data);
2620 multi_done(data, *result, FALSE);
2621 *stream_error = TRUE;
2623 return CURLM_OK;
2626static CURLMcode multistate_doing_more(struct Curl_easy *data,
2627 bool *stream_error,
2628 CURLcode *result)
2630 domore control;
2632 /*
2633 * When we are connected, DOING MORE and then go DID
2634 */
2635 DEBUGASSERT(data->conn);
2636 *result = multi_do_more(data, &control);
2638 if(!*result) {
2639 if(control != DOMORE_INCOMPLETE) {
2640 /* if DONE, advance to DO_DONE
2641 if GOBACK, go back to DOING */
2642 multistate(data, control == DOMORE_DONE ? MSTATE_DID : MSTATE_DOING);
2643 return CURLM_CALL_MULTI_PERFORM;
2645 /* else
2646 stay in DO_MORE */
2648 else {
2649 /* failure detected */
2650 multi_posttransfer(data);
2651 multi_done(data, *result, FALSE);
2652 *stream_error = TRUE;
2654 return CURLM_OK;
2657static CURLMcode multistate_did(struct Curl_multi *multi,
2658 struct Curl_easy *data)
2660 DEBUGASSERT(data->conn);
2661 if(data->conn->bits.multiplex)
2662 /* Check if we can move pending requests to send pipe */
2663 multi_schedule_pending(multi); /* multiplexed */
2665 /* Only perform the transfer if there is a good socket to work with.
2666 Having both BAD is a signal to skip immediately to DONE */
2667 if(CONN_SOCK_IDX_VALID(data->conn->recv_idx) ||
2668 CONN_SOCK_IDX_VALID(data->conn->send_idx)) {
2669 multistate(data, MSTATE_PERFORMING);
2670 /* Do not return CURLM_CALL_MULTI_PERFORM to give other transfers
2671 * a chance to send off their requests.
2672 * Note: Some SFTP handlers do not seem to like this.
2673 * Restrict it to HTTP families. */
2674 return ((multi->xfers_alive > 1) &&
2675 (data->conn->scheme->protocol & PROTO_FAMILY_HTTP)) ?
2676 CURLM_OK : CURLM_CALL_MULTI_PERFORM;
2678 else {
2679#ifndef CURL_DISABLE_FTP
2680 if(data->state.wildcardmatch &&
2681 ((data->conn->scheme->flags & PROTOPT_WILDCARD) == 0)) {
2682 data->wildcard->state = CURLWC_DONE;
2684#endif
2685 multistate(data, MSTATE_DONE);
2686 return CURLM_CALL_MULTI_PERFORM;
2690static CURLMcode multistate_done(struct Curl_easy *data, CURLcode *presult)
2692 if(data->conn) {
2693 CURLcode result;
2695 /* post-transfer command */
2696 result = multi_done(data, *presult, FALSE);
2698 /* allow a previously set error code take precedence */
2699 if(!(*presult))
2700 *presult = result;
2703#ifndef CURL_DISABLE_FTP
2704 if(data->state.wildcardmatch) {
2705 if(data->wildcard->state != CURLWC_DONE) {
2706 /* if a wildcard is set and we are not ending -> lets start again
2707 with MSTATE_INIT */
2708 multistate(data, MSTATE_INIT);
2709 return CURLM_CALL_MULTI_PERFORM;
2712#endif
2713 /* after we have DONE what we are supposed to do, go COMPLETED, and
2714 it does not matter what the multi_done() returned! */
2715 multistate(data, MSTATE_COMPLETED);
2716 return CURLM_CALL_MULTI_PERFORM;
2719static CURLMcode multi_runsingle(struct Curl_multi *multi,
2720 struct Curl_easy *data,
2721 struct Curl_sigpipe_ctx *sigpipe_ctx)
2723 CURLMcode mresult = CURLM_OK;
2724 CURLcode result = CURLE_OK;
2725 const struct curltime *pnow = NULL;
2727 if(multi->dead) {
2728 /* a multi-level callback returned error before, meaning every individual
2729 transfer now has failed */
2730 result = CURLE_ABORTED_BY_CALLBACK;
2731 multi_posttransfer(data);
2732 multi_done(data, result, FALSE);
2733 multistate(data, MSTATE_COMPLETED);
2736 multi_warn_debug(multi, data);
2738 /* transfer runs now, clear the dirty bit. This may be set
2739 * again during processing, triggering a re-run later. */
2740 Curl_uint32_bset_remove(&multi->dirty, data->mid);
2742 if(data == multi->admin) {
2743#ifdef ENABLE_INTERNAL_WAKEUP
2744 /* Consume any pending wakeup signals before processing.
2745 * This is necessary for event based processing. See #21547 */
2746 (void)Curl_wakeup_consume(multi->wakeup_internal, TRUE);
2747#endif
2748#ifdef USE_RESOLV_THREADED
2749 Curl_async_thrdd_multi_process(multi);
2750#endif
2751 Curl_cshutdn_perform(&multi->cshutdn, multi->admin, sigpipe_ctx);
2752 goto out;
2755 sigpipe_apply(data, sigpipe_ctx);
2756 do {
2757 /* A "stream" here is a logical stream if the protocol can handle that
2758 (HTTP/2), or the full connection for older protocols */
2759 bool stream_error = FALSE;
2760 mresult = CURLM_OK;
2761 pnow = NULL;
2763 if(multi_ischanged(multi, TRUE)) {
2764 CURL_TRC_M(data, "multi changed, check CONNECT_PEND queue");
2765 multi_schedule_pending(multi); /* multiplexed */
2768 if(data->mstate > MSTATE_CONNECT &&
2769 data->mstate < MSTATE_COMPLETED) {
2770 /* Make sure we set the connection's current owner */
2771 DEBUGASSERT(data->conn);
2772 if(!data->conn) {
2773 mresult = CURLM_INTERNAL_ERROR;
2774 goto out;
2778 /* Wait for the connect state as only then is the start time stored, but
2779 we must not check already completed handles */
2780 if((data->mstate >= MSTATE_CONNECT) && (data->mstate < MSTATE_COMPLETED)) {
2781 pnow = Curl_pgrs_now(data);
2782 if(multi_handle_timeout(data, pnow, &stream_error, &result))
2783 /* Skip the statemachine and go directly to error handling section. */
2784 goto statemachine_end;
2785 pnow = NULL;
2788 switch(data->mstate) {
2789 case MSTATE_INIT:
2790 /* Transitional state. init this transfer. A handle never comes back to
2791 this state. */
2792 mresult = multistate_init(data, &result);
2793 break;
2795 case MSTATE_SETUP:
2796 /* Transitional state. Setup things for a new transfer. The handle
2797 can come back to this state on a redirect. */
2798 mresult = multistate_setup(data);
2799 break;
2801 case MSTATE_CONNECT:
2802 mresult = multistate_connect(multi, data, &result);
2803 break;
2805 case MSTATE_CONNECTING:
2806 /* awaiting a completion of an asynch TCP connect */
2807 mresult = multistate_connecting(data, &stream_error, &result);
2808 break;
2810 case MSTATE_PROTOCONNECT:
2811 mresult = multistate_protoconnect(data, &stream_error, &result);
2812 break;
2814 case MSTATE_PROTOCONNECTING:
2815 /* protocol-specific connect phase */
2816 mresult = multistate_protoconnecting(data, &stream_error, &result);
2817 break;
2819 case MSTATE_DO:
2820 mresult = multistate_do(data, &stream_error, &result);
2821 break;
2823 case MSTATE_DOING:
2824 /* we continue DOING until the DO phase is complete */
2825 mresult = multistate_doing(data, &stream_error, &result);
2826 break;
2828 case MSTATE_DOING_MORE:
2829 /*
2830 * When we are connected, DOING MORE and then go DID
2831 */
2832 mresult = multistate_doing_more(data, &stream_error, &result);
2833 break;
2835 case MSTATE_DID:
2836 mresult = multistate_did(multi, data);
2837 break;
2839 case MSTATE_RATELIMITING: /* limit-rate exceeded in either direction */
2840 mresult = multistate_ratelimiting(data, &result);
2841 break;
2843 case MSTATE_PERFORMING:
2844 mresult = multistate_performing(data, &stream_error, &result);
2845 break;
2847 case MSTATE_DONE:
2848 mresult = multistate_done(data, &result);
2849 break;
2851 case MSTATE_COMPLETED:
2852 break;
2854 case MSTATE_PENDING:
2855 case MSTATE_MSGSENT:
2856 /* handles in these states should NOT be in this list */
2857 break;
2859 default:
2860 mresult = CURLM_INTERNAL_ERROR;
2861 goto out;
2864 if(data->mstate >= MSTATE_CONNECT &&
2865 data->mstate < MSTATE_DO &&
2866 mresult != CURLM_CALL_MULTI_PERFORM &&
2867 !multi_ischanged(multi, FALSE)) {
2868 /* We now handle stream timeouts if and only if this will be the last
2869 * loop iteration. We only check this on the last iteration to ensure
2870 * that if we know we have additional work to do immediately
2871 * (i.e. CURLM_CALL_MULTI_PERFORM == TRUE) then we should do that before
2872 * declaring the connection timed out as we may almost have a completed
2873 * connection. */
2874 pnow = Curl_pgrs_now(data);
2875 multi_handle_timeout(data, pnow, &stream_error, &result);
2878statemachine_end:
2879 if(!pnow)
2880 pnow = Curl_pgrs_now(data);
2881 result = is_finished(multi, data, pnow, stream_error, result);
2882 if(result)
2883 mresult = CURLM_CALL_MULTI_PERFORM;
2885 if(MSTATE_COMPLETED == data->mstate) {
2886 handle_completed(multi, data, result);
2887 mresult = CURLM_OK;
2888 goto out;
2890 } while((mresult == CURLM_CALL_MULTI_PERFORM) ||
2891 multi_ischanged(multi, FALSE));
2893out:
2894 data->result = result;
2895 return mresult;
2898static CURLMcode multi_perform(struct Curl_multi *multi,
2899 int *running_handles)
2901 CURLMcode returncode = CURLM_OK;
2902 struct curltime start;
2903 uint32_t mid;
2904 struct Curl_sigpipe_ctx sigpipe_ctx;
2906 curlx_pnow(&start);
2907 sigpipe_init(&sigpipe_ctx);
2909 if(Curl_uint32_bset_first(&multi->process, &mid)) {
2910 CURL_TRC_M(multi->admin, "multi_perform(running=%u)",
2911 multi_xfers_running(multi));
2912 do {
2913 struct Curl_easy *data = Curl_multi_get_easy(multi, mid);
2914 CURLMcode mresult;
2915 if(!data) {
2916 DEBUGASSERT(0);
2917 Curl_uint32_bset_remove(&multi->process, mid);
2918 Curl_uint32_bset_remove(&multi->dirty, mid);
2919 continue;
2921 mresult = multi_runsingle(multi, data, &sigpipe_ctx);
2922 if(mresult)
2923 returncode = mresult;
2924 } while(Curl_uint32_bset_next(&multi->process, mid, &mid));
2926 sigpipe_restore(&sigpipe_ctx);
2928 if(multi_ischanged(multi, TRUE))
2929 multi_schedule_pending(multi);
2931 if(!returncode && CURL_MNTFY_HAS_ENTRIES(multi))
2932 returncode = Curl_mntfy_dispatch_all(multi);
2934 /*
2935 * Remove all expired timers from the splay since handles are dealt
2936 * with unconditionally by this function and curl_multi_timeout() requires
2937 * that already passed/handled expire times are removed from the splay.
2939 * It is important that the 'now' value is set at the entry of this function
2940 * and not for the current time as it may have ticked a little while since
2941 * then and then we risk this loop to remove timers that actually have not
2942 * been handled!
2943 */
2944 while(Curl_timeouts_remove_expired(&multi->timeouts, &start, &mid)) {
2945 /* the removed may have another timeout in queue */
2946 struct Curl_easy *data = Curl_multi_get_easy(multi, mid);
2947 if(!data) {
2948 DEBUGASSERT(0);
2949 continue;
2951 (void)add_next_timeout(&start, multi, data);
2952 if(data->mstate == MSTATE_PENDING) {
2953 bool stream_unused;
2954 CURLcode result_unused;
2955 if(multi_handle_timeout(data, Curl_pgrs_now(data),
2956 &stream_unused, &result_unused)) {
2957 infof(data, "PENDING handle timeout");
2958 move_pending_to_connect(multi, data);
2963 if(running_handles) {
2964 uint32_t running = multi_xfers_running(multi);
2965 *running_handles = (running < INT_MAX) ? (int)running : INT_MAX;
2968 if(CURLM_OK >= returncode)
2969 returncode = Curl_update_timer(multi);
2971 return returncode;
2974CURLMcode curl_multi_perform(CURLM *m, int *running_handles)
2976 struct Curl_mapi_guard guard;
2977 CURLMcode mresult;
2979 if(CURL_MAPI_ENTER(&guard, m, multi_perform, &mresult)) {
2980 mresult = multi_perform(m, running_handles);
2982 CURL_MAPI_LEAVE(&guard);
2983 return mresult;
2986CURLMcode curl_multi_cleanup(CURLM *m)
2988 struct Curl_mapi_guard guard;
2989 CURLMcode mresult;
2991 if(CURL_MAPI_ENTER(&guard, m, multi_cleanup, &mresult)) {
2992 struct Curl_multi *multi = m;
2993 void *entry;
2994 uint32_t mid;
2996 /* First remove all remaining easy handles,
2997 * close internal ones. admin handle is special */
2998 if(Curl_uint32_tbl_first(&multi->xfers, &mid, &entry)) {
2999 do {
3000 struct Curl_easy *data = entry;
3001 if(!GOOD_EASY_HANDLE(data)) {
3002 mresult = CURLM_BAD_HANDLE;
3003 goto out;
3006#ifdef DEBUGBUILD
3007 if(mid != data->mid) {
3008 CURL_TRC_M(data, "multi_cleanup: still present with mid=%u, "
3009 "but unexpected data->mid=%u\n", mid, data->mid);
3010 DEBUGASSERT(0);
3012#endif
3014 if(data == multi->admin)
3015 continue;
3017 if(!data->state.done && data->conn)
3018 /* if DONE was never called for this handle */
3019 (void)multi_done(data, CURLE_OK, TRUE);
3021 data->multi = NULL; /* clear the association */
3022 Curl_uint32_tbl_remove(&multi->xfers, mid);
3023 data->mid = UINT32_MAX;
3025#ifdef USE_LIBPSL
3026 if(data->psl == &multi->psl)
3027 data->psl = NULL;
3028#endif
3029 if(data->state.internal)
3030 Curl_close(&data);
3031 } while(Curl_uint32_tbl_next(&multi->xfers, mid, &mid, &entry));
3034#ifdef USE_RESOLV_THREADED
3035 Curl_async_thrdd_multi_destroy(multi, !multi->quick_exit);
3036#endif
3037 Curl_cpool_destroy(&multi->cpool, multi->admin);
3038 Curl_cshutdn_destroy(&multi->cshutdn, multi->admin);
3039 if(multi->admin) {
3040 CURL_TRC_M(multi->admin, "multi_cleanup, closing admin handle, done");
3041 multi->admin->multi = NULL;
3042 Curl_uint32_tbl_remove(&multi->xfers, multi->admin->mid);
3043 Curl_close(&multi->admin);
3046 multi->magic = 0; /* not good anymore */
3048 Curl_multi_ev_cleanup(multi);
3049 Curl_hash_destroy(&multi->proto_hash);
3050 Curl_dnscache_destroy(&multi->dnscache);
3051 Curl_psl_destroy(&multi->psl);
3052#ifdef USE_SSL
3053 Curl_ssl_scache_destroy(multi->ssl_scache);
3054#endif
3056#ifdef USE_WINSOCK
3057 WSACloseEvent(multi->wsa_event);
3058#endif
3059#ifdef ENABLE_WAKEUP
3060 Curl_wakeup_destroy(multi->wakeup_pair);
3061#endif
3062#ifdef ENABLE_INTERNAL_WAKEUP
3063 Curl_wakeup_destroy(multi->wakeup_internal);
3064#endif
3066 multi_xfer_bufs_free(multi);
3067 Curl_mntfy_cleanup(multi);
3068#ifdef DEBUGBUILD
3069 if(Curl_uint32_tbl_count(&multi->xfers)) {
3070 multi_xfer_tbl_dump(multi);
3071 DEBUGASSERT(0);
3073#endif
3074 Curl_uint32_bset_destroy(&multi->process);
3075 Curl_uint32_bset_destroy(&multi->dirty);
3076 Curl_uint32_bset_destroy(&multi->pending);
3077 Curl_uint32_bset_destroy(&multi->msgsent);
3078 Curl_uint32_tbl_destroy(&multi->xfers);
3079 curlx_memzero(multi, sizeof(*multi));
3080 curlx_free(multi);
3082 mresult = CURLM_OK;
3084out:
3085 CURL_MAPI_LEAVE(&guard);
3086 return mresult;
3090 * curl_multi_info_read()
3092 * This function is the primary way for a multi/multi_socket application to
3093 * figure out if a transfer has ended.
3094 */
3096CURLMsg *curl_multi_info_read(CURLM *m, int *msgs_in_queue)
3098 struct Curl_mapi_guard guard;
3099 CURLMsg *msg_result = NULL;
3101 *msgs_in_queue = 0; /* default to none */
3102 if(CURL_MAPI_ENTER(&guard, m, multi_info_read, NULL)) {
3103 struct Curl_multi *multi = m;
3104 uint32_t mid;
3105 if(Curl_uint32_bset_first(&multi->msgsent, &mid)) {
3106 struct Curl_easy *data = Curl_multi_get_easy(multi, mid);
3108 DEBUGASSERT(data);
3109 Curl_uint32_bset_remove(&multi->msgsent, mid);
3110 *msgs_in_queue =
3111 curlx_uztosi(Curl_uint32_bset_count(&multi->msgsent));
3112 if(data)
3113 msg_result = &data->msg;
3116 CURL_MAPI_LEAVE(&guard);
3117 return msg_result;
3120void Curl_multi_will_close(struct Curl_easy *data, curl_socket_t s)
3122 if(data) {
3123 struct Curl_multi *multi = data->multi;
3124 if(multi) {
3125 CURL_TRC_M(data, "Curl_multi_will_close fd=%" FMT_SOCKET_T, s);
3126 Curl_multi_ev_socket_done(multi, data, s);
3131static void multi_timeouts_init(struct Curl_easy *data)
3133 data->state.timeouts.first = EXPIRE_LAST;
3134 data->state.timeouts.splaynode.registered = FALSE;
3138 * Each Curl_easy has a list of timeouts. The add_next_timeout() is called
3139 * when it has been removed from the splay tree because the timeout has
3140 * expired. This function is then to advance in the list to pick the next
3141 * timeout to use (skip the already expired ones) and add this node back to
3142 * the splay tree again.
3144 * The splay tree only has each Curl_easy as a single node and the nearest
3145 * timeout is used to sort it on.
3146 */
3147static CURLMcode add_next_timeout(const struct curltime *pnow,
3148 struct Curl_multi *multi,
3149 struct Curl_easy *data)
3151 struct expire_timers *timeouts = &data->state.timeouts;
3152 timediff_t now_us = Curl_timeouts_offset_us(&multi->timeouts, pnow);
3154 while(timeouts->first < EXPIRE_LAST) {
3155 if(timeouts->offset_us[timeouts->first] <= now_us) /* already expired */
3156 timeouts->first = timeouts->next[timeouts->first];
3157 else /* timeouts are sorted, first is first in the future now */
3158 break;
3161 if(timeouts->first < EXPIRE_LAST) {
3162 /* Insert this node again into the splay. Keep the timer in the list in
3163 case we need to recompute future timers. */
3164 Curl_timeouts_add(&multi->timeouts, data,
3165 timeouts->offset_us[timeouts->first]);
3167 return CURLM_OK;
3170static void multi_mark_expired_as_dirty(struct Curl_multi *multi,
3171 const struct curltime *ts)
3173 struct Curl_easy *data = NULL;
3174 uint32_t mid;
3176 /*
3177 * The loop following here will go on as long as there are expire-times left
3178 * to process (compared to `ts`) in the splay and 'data' will be
3179 * re-assigned for every expired handle we deal with.
3180 */
3181 while(Curl_timeouts_remove_expired(&multi->timeouts, ts, &mid)) {
3182 /* Check if there is one (more) expired timer to deal with! This function
3183 extracts a matching node if there is one */
3184 data = Curl_multi_get_easy(multi, mid);
3185 if(!data) {
3186 DEBUGASSERT(0);
3187 continue;
3189#ifdef CURLVERBOSE
3190 if(CURL_TRC_TIMER_is_verbose(data)) {
3191 if(data->state.timeouts.first < EXPIRE_LAST) {
3192 CURL_TRC_TIMER(data, data->state.timeouts.first, "has expired");
3195#endif
3196 (void)add_next_timeout(ts, multi, data);
3197 Curl_multi_mark_dirty(data);
3201static CURLMcode multi_run_dirty(struct Curl_multi *multi,
3202 struct Curl_sigpipe_ctx *sigpipe_ctx,
3203 uint32_t *pnum)
3205 CURLMcode mresult = CURLM_OK;
3206 uint32_t mid;
3208 *pnum = 0;
3209 if(Curl_uint32_bset_first(&multi->dirty, &mid)) {
3210 do {
3211 struct Curl_easy *data = Curl_multi_get_easy(multi, mid);
3212 if(data) {
3213 CURL_TRC_M(data, "multi_run_dirty");
3215 if(!Curl_uint32_bset_contains(&multi->process, mid)) {
3216 if(Curl_uint32_bset_contains(&multi->pending, mid)) {
3217 /* Something happened on the pending transfer, act on it. */
3218 move_pending_to_connect(multi, data);
3220 else {
3221 /* We are no longer processing this transfer */
3222 Curl_uint32_bset_remove(&multi->dirty, mid);
3223 continue;
3227 (*pnum)++;
3228 /* runsingle() clears the dirty mid */
3229 mresult = multi_runsingle(multi, data, sigpipe_ctx);
3231 if(CURLM_OK >= mresult) {
3232 /* reassess event handling of data */
3233 mresult = Curl_multi_ev_assess_xfer(multi, data);
3234 if(mresult)
3235 goto out;
3238 else {
3239 CURL_TRC_M(multi->admin, "multi_run_dirty, %u no longer found", mid);
3240 Curl_uint32_bset_remove(&multi->dirty, mid);
3242 } while(Curl_uint32_bset_next(&multi->dirty, mid, &mid));
3245out:
3246 return mresult;
3249static CURLMcode multi_socket(struct Curl_multi *multi,
3250 bool checkall,
3251 curl_socket_t s,
3252 int ev_bitmask,
3253 int *running_handles)
3255 CURLMcode mresult = CURLM_OK;
3256 struct Curl_sigpipe_ctx pipe_ctx;
3257 struct curltime now;
3258 uint32_t run_xfers;
3260 (void)ev_bitmask;
3261 sigpipe_init(&pipe_ctx);
3263 if(checkall) {
3264 /* *perform() deals with running_handles on its own */
3265 mresult = multi_perform(multi, running_handles);
3267 if(mresult != CURLM_BAD_HANDLE) {
3268 /* Reassess event status of all active transfers, but keep any error
3269 from multi_perform() */
3270 CURLMcode mresult2 =
3271 Curl_multi_ev_assess_xfer_bset(multi, &multi->process);
3272 if(!mresult)
3273 mresult = mresult2;
3275 goto out;
3278 if(s != CURL_SOCKET_TIMEOUT) {
3279 /* Mark all transfers of that socket as dirty */
3280 Curl_multi_ev_dirty_xfers(multi, s);
3282 else {
3283 /* Asked to run due to time-out. Clear the 'last_expire_ts' variable to
3284 force Curl_update_timer() to trigger a callback to the app again even
3285 if the same timeout is still the one to run after this call. That
3286 handles the case when the application asks libcurl to run the timeout
3287 prematurely. */
3288 multi->last_expire_offset_us = 0;
3290 /* Applications may set `socket_cb` *after* having added transfers
3291 * first. *Then* kick off processing with a
3292 * curl_multi_socket_action(TIMEOUT) afterwards. Make sure our
3293 * admin handle registers its pollset with the callbacks present. */
3294 mresult = multi_assess_wakeup(multi);
3295 if(mresult)
3296 goto out;
3299 curlx_pnow(&now);
3300 multi_mark_expired_as_dirty(multi, &now);
3301 mresult = multi_run_dirty(multi, &pipe_ctx, &run_xfers);
3302 if(mresult)
3303 goto out;
3305 if(run_xfers) {
3306 /* Running transfers takes time. With a new timestamp, we might catch
3307 * other expires which are due now. Instead of telling the application
3308 * to set a 0 timeout and call us again, we run them here.
3309 * Do that only once or it might be unfair to transfers on other
3310 * sockets. */
3311 curlx_pnow(&now);
3312 multi_mark_expired_as_dirty(multi, &now);
3313 mresult = multi_run_dirty(multi, &pipe_ctx, &run_xfers);
3316out:
3317 sigpipe_restore(&pipe_ctx);
3319 if(multi_ischanged(multi, TRUE))
3320 multi_schedule_pending(multi);
3322 if(!mresult && CURL_MNTFY_HAS_ENTRIES(multi))
3323 mresult = Curl_mntfy_dispatch_all(multi);
3325 if(running_handles) {
3326 uint32_t running = multi_xfers_running(multi);
3327 *running_handles = (running < INT_MAX) ? (int)running : INT_MAX;
3330 if(CURLM_OK >= mresult)
3331 mresult = Curl_update_timer(multi);
3332 return mresult;
3335#undef curl_multi_setopt
3336CURLMcode curl_multi_setopt(CURLM *m, CURLMoption option, ...)
3338 struct Curl_mapi_guard guard;
3339 CURLMcode mresult = CURLM_OK;
3341 if(CURL_MAPI_ENTER(&guard, m, multi_setopt, &mresult)) {
3342 struct Curl_multi *multi = m;
3343 va_list param;
3344 unsigned long uarg;
3345 size_t szarg;
3347 va_start(param, option);
3349 switch(option) {
3350 case CURLMOPT_SOCKETFUNCTION:
3351 multi->socket_cb = va_arg(param, curl_socket_callback);
3352 break;
3353 case CURLMOPT_SOCKETDATA:
3354 multi->socket_userp = va_arg(param, void *);
3355 break;
3356 case CURLMOPT_PUSHFUNCTION:
3357 multi->push_cb = va_arg(param, curl_push_callback);
3358 break;
3359 case CURLMOPT_PUSHDATA:
3360 multi->push_userp = va_arg(param, void *);
3361 break;
3362 case CURLMOPT_PIPELINING:
3363 multi->multiplexing = va_arg(param, long) & CURLPIPE_MULTIPLEX ? 1 : 0;
3364 break;
3365 case CURLMOPT_TIMERFUNCTION:
3366 multi->timer_cb = va_arg(param, curl_multi_timer_callback);
3367 break;
3368 case CURLMOPT_TIMERDATA:
3369 multi->timer_userp = va_arg(param, void *);
3370 break;
3371 case CURLMOPT_MAXCONNECTS:
3372 uarg = va_arg(param, unsigned long);
3373 if(uarg <= UINT32_MAX)
3374 multi->maxconnects = (uint32_t)uarg;
3375 break;
3376 case CURLMOPT_MAX_HOST_CONNECTIONS:
3377 if(!curlx_sltouz(va_arg(param, long), &szarg))
3378 mresult = CURLM_BAD_FUNCTION_ARGUMENT;
3379 multi->max_host_connections = (szarg < UINT32_MAX) ?
3380 (uint32_t)szarg : UINT32_MAX;
3381 break;
3382 case CURLMOPT_MAX_TOTAL_CONNECTIONS:
3383 if(!curlx_sltouz(va_arg(param, long), &szarg))
3384 mresult = CURLM_BAD_FUNCTION_ARGUMENT;
3385 multi->max_total_connections = (szarg < UINT32_MAX) ?
3386 (uint32_t)szarg : UINT32_MAX;
3387 break;
3388 /* options formerly used for pipelining */
3389 case CURLMOPT_MAX_PIPELINE_LENGTH:
3390 break;
3391 case CURLMOPT_CONTENT_LENGTH_PENALTY_SIZE:
3392 break;
3393 case CURLMOPT_CHUNK_LENGTH_PENALTY_SIZE:
3394 break;
3395 case CURLMOPT_PIPELINING_SITE_BL:
3396 break;
3397 case CURLMOPT_PIPELINING_SERVER_BL:
3398 break;
3399 case CURLMOPT_MAX_CONCURRENT_STREAMS: {
3400 if(!curlx_sltouz(va_arg(param, long), &szarg) ||
3401 !szarg || (szarg > (size_t)INT_MAX)) /* preserve previous cutoff */
3402 multi->max_concurrent_streams = 100;
3403 else
3404 multi->max_concurrent_streams = (szarg < UINT32_MAX) ?
3405 (uint32_t)szarg : UINT32_MAX;
3406 break;
3408 case CURLMOPT_NETWORK_CHANGED: {
3409 long val = va_arg(param, long);
3410 if(val & CURLMNWC_CLEAR_ALL)
3411 /* In the beginning, all values available to set were 1 by mistake. We
3412 converted this to mean "all", thus setting all the bits
3413 automatically */
3414 val = CURLMNWC_CLEAR_DNS | CURLMNWC_CLEAR_CONNS;
3415 if(val & CURLMNWC_CLEAR_DNS) {
3416 Curl_dnscache_clear(multi->admin);
3418 if(val & CURLMNWC_CLEAR_CONNS) {
3419 Curl_cpool_nw_changed(&multi->cpool, multi->admin);
3421 break;
3423 case CURLMOPT_NOTIFYFUNCTION:
3424 multi->ntfy.ntfy_cb = va_arg(param, curl_notify_callback);
3425 break;
3426 case CURLMOPT_NOTIFYDATA:
3427 multi->ntfy.ntfy_cb_data = va_arg(param, void *);
3428 break;
3429 case CURLMOPT_RESOLVE_THREADS_MAX:
3430#ifdef USE_RESOLV_THREADED
3431 uarg = va_arg(param, long);
3432 if((uarg <= 0) || (uarg > UINT32_MAX))
3433 mresult = CURLM_BAD_FUNCTION_ARGUMENT;
3434 else {
3435 CURLcode result = Curl_async_thrdd_multi_set_props(
3436 multi, 0, (uint32_t)uarg, 2000);
3437 switch(result) {
3438 case CURLE_OK:
3439 mresult = CURLM_OK;
3440 break;
3441 case CURLE_BAD_FUNCTION_ARGUMENT:
3442 mresult = CURLM_BAD_FUNCTION_ARGUMENT;
3443 break;
3444 case CURLE_OUT_OF_MEMORY:
3445 mresult = CURLM_OUT_OF_MEMORY;
3446 break;
3447 default:
3448 mresult = CURLM_INTERNAL_ERROR;
3449 break;
3452#endif
3453 break;
3454 case CURLMOPT_QUICK_EXIT:
3455 multi->quick_exit = va_arg(param, long) ? 1 : 0;
3456 break;
3457 default:
3458 mresult = CURLM_UNKNOWN_OPTION;
3459 break;
3461 va_end(param);
3463 CURL_MAPI_LEAVE(&guard);
3464 return mresult;
3467/* we define curl_multi_socket() in the public multi.h header */
3468#undef curl_multi_socket
3470CURLMcode curl_multi_socket(CURLM *m, curl_socket_t s, int *running_handles)
3472 struct Curl_mapi_guard guard;
3473 CURLMcode mresult;
3475 if(CURL_MAPI_ENTER(&guard, m, multi_socket, &mresult)) {
3476 mresult = multi_socket(m, FALSE, s, 0, running_handles);
3478 CURL_MAPI_LEAVE(&guard);
3479 return mresult;
3482CURLMcode curl_multi_socket_action(CURLM *m, curl_socket_t s,
3483 int ev_bitmask, int *running_handles)
3485 struct Curl_mapi_guard guard;
3486 CURLMcode mresult;
3488 if(CURL_MAPI_ENTER(&guard, m, multi_socket_action, &mresult)) {
3489 mresult = multi_socket(m, FALSE, s, ev_bitmask, running_handles);
3491 CURL_MAPI_LEAVE(&guard);
3492 return mresult;
3495CURLMcode curl_multi_socket_all(CURLM *m, int *running_handles)
3497 struct Curl_mapi_guard guard;
3498 CURLMcode mresult;
3500 if(CURL_MAPI_ENTER(&guard, m, multi_socket_all, &mresult)) {
3501 mresult = multi_socket(m, TRUE, CURL_SOCKET_BAD, 0, running_handles);
3503 CURL_MAPI_LEAVE(&guard);
3504 return mresult;
3507static bool multi_has_dirties(struct Curl_multi *multi)
3509 uint32_t mid;
3510 if(Curl_uint32_bset_first(&multi->dirty, &mid)) {
3511 do {
3512 struct Curl_easy *data = Curl_multi_get_easy(multi, mid);
3513 if(data) {
3514 if(Curl_uint32_bset_contains(&multi->process, mid))
3515 return TRUE;
3516 /* We are no longer processing this transfer */
3517 Curl_uint32_bset_remove(&multi->dirty, mid);
3519 else {
3520 CURL_TRC_M(multi->admin, "dirty transfer %u no longer found", mid);
3521 Curl_uint32_bset_remove(&multi->dirty, mid);
3523 } while(Curl_uint32_bset_next(&multi->dirty, mid, &mid));
3525 return FALSE;
3528static void multi_timeout(struct Curl_multi *multi,
3529 timediff_t *pexire_offset_us,
3530 int *timeout_ms)
3532 if(multi->dead) {
3533 if(pexire_offset_us)
3534 *pexire_offset_us = 0;
3535 *timeout_ms = 0;
3536 return;
3539 if(multi_has_dirties(multi)) {
3540 if(pexire_offset_us) {
3541 struct curltime now;
3542 curlx_pnow(&now);
3543 *pexire_offset_us = Curl_timeouts_offset_us(&multi->timeouts, &now);
3545 *timeout_ms = 0;
3546 return;
3548 else {
3549 struct curltime now;
3550 uint32_t mid;
3552 curlx_pnow(&now);
3553 *timeout_ms = Curl_timeouts_next_ms(&multi->timeouts, &now,
3554 pexire_offset_us, &mid);
3555#ifdef CURLVERBOSE
3556 if(mid != UINT32_MAX) {
3557 struct Curl_easy *data = Curl_multi_get_easy(multi, mid);
3558 if(data && CURL_TRC_TIMER_is_verbose(data) &&
3559 (data->state.timeouts.first < EXPIRE_LAST)) {
3560 CURL_TRC_TIMER(data, data->state.timeouts.first,
3561 "gives multi timeout in %dms", *timeout_ms);
3564#endif
3568CURLMcode curl_multi_timeout(CURLM *m,
3569 long *timeout_ms)
3571 struct Curl_mapi_guard guard;
3572 CURLMcode mresult;
3574 if(CURL_MAPI_ENTER(&guard, m, multi_timeout, &mresult)) {
3575 int itimeout_ms;
3577 multi_timeout(m, NULL, &itimeout_ms);
3578 *timeout_ms = (long)itimeout_ms;
3579 mresult = CURLM_OK;
3581 CURL_MAPI_LEAVE(&guard);
3582 return mresult;
3586 * Tell the application it should update its timers, if it subscribes to the
3587 * update timer callback.
3588 */
3589CURLMcode Curl_update_timer(struct Curl_multi *multi)
3591 timediff_t timeouts_offset_us = 0;
3592 int timeout_ms;
3593 int rc;
3594 bool set_value = FALSE;
3596 if(!multi->timer_cb || multi->dead)
3597 return CURLM_OK;
3598 multi_timeout(multi, &timeouts_offset_us, &timeout_ms);
3600 if(timeout_ms < 0 && !multi->last_timeout_set) {
3601 /* nothing to do */
3603 else if(timeout_ms < 0) {
3604 /* there is no timeout now but there was one previously */
3605 CURL_TRC_M(multi->admin, "[TIMER] clear");
3606 timeout_ms = -1; /* normalize */
3607 set_value = TRUE;
3609 else if(!multi->last_timeout_set) {
3610 CURL_TRC_M(multi->admin, "[TIMER] set %dms, none before", timeout_ms);
3611 set_value = TRUE;
3613 else if(multi->last_expire_offset_us != timeouts_offset_us) {
3614 /* We had a timeout before and have one now, the absolute timestamp
3615 * differs. The relative timeout_ms may be the same, but the starting
3616 * point differs. Let the application restart its timer. */
3617 CURL_TRC_M(multi->admin, "[TIMER] set %dms, replace previous",
3618 timeout_ms);
3619 set_value = TRUE;
3621 else {
3622 /* We have same expire time as previously. Our relative 'timeout_ms'
3623 * may be different now, but the application has the timer running
3624 * and we do not to tell it to start this again. */
3627 if(set_value) {
3628 struct Curl_mapi_guard guard;
3630 multi->last_expire_offset_us = timeouts_offset_us;
3631 multi->last_timeout_set = timeout_ms >= 0;
3632 CURL_CBAPI_MULTI_START(&guard, multi, multi_timer_cb);
3633 rc = multi->timer_cb(multi, timeout_ms, multi->timer_userp);
3634 CURL_CBAPI_MULTI_END(&guard);
3635 if(rc == -1) {
3636 multi->dead = TRUE;
3637 return CURLM_ABORTED_BY_CALLBACK;
3640 return CURLM_OK;
3643#ifdef DEBUGBUILD
3644static bool multi_timeouts_check(struct Curl_easy *data)
3646 struct expire_timers *timeouts = &data->state.timeouts;
3647 uint8_t id;
3648 int i = 0;
3649 for(id = timeouts->first; id < EXPIRE_LAST; id = timeouts->next[id]) {
3650 if(++i >= EXPIRE_LAST) {
3651 failf(data, "expire timeouts looped: %d iterations and no end", i);
3652 return FALSE;
3654 if(id == timeouts->next[id]) {
3655 failf(data, "expire timeouts wrong: %d points to itself", (int)id);
3656 return FALSE;
3658 if((timeouts->next[id] < EXPIRE_LAST) &&
3659 (timeouts->offset_us[id] > timeouts->offset_us[timeouts->next[id]])) {
3660 failf(data, "expire timeouts not sorted: %d happens after %d but "
3661 "is listed before", (int)id, (int)timeouts->next[id]);
3662 return FALSE;
3665 return TRUE;
3667#endif
3670 * Remove a given timestamp from the list of timeouts.
3671 */
3672static void multi_clear_timeout(struct Curl_easy *data, expire_id eid)
3674 struct expire_timers *timeouts = &data->state.timeouts;
3675 uint8_t orig_first = timeouts->first;
3676 uint8_t *anchor = &timeouts->first;
3677 uint8_t id = (uint8_t)eid;
3679 if((unsigned)eid >= EXPIRE_LAST) {
3680 DEBUGASSERT(0);
3681 return;
3684 while(*anchor < EXPIRE_LAST) {
3685 if(*anchor == id) {
3686 *anchor = timeouts->next[id];
3687 break;
3689 anchor = &timeouts->next[*anchor];
3691 DEBUGASSERT(multi_timeouts_check(data));
3692 if(Curl_timeouts_has(data)) {
3693 struct Curl_multi *multi = data->multi;
3695 if(!multi) {
3696 DEBUGASSERT(0);
3697 return;
3699 if((timeouts->first >= EXPIRE_LAST) || /* no more timeouts */
3700 (timeouts->first != orig_first)) { /* active timeout changed */
3701 Curl_timeouts_remove(&multi->timeouts, data);
3703 if((timeouts->first < EXPIRE_LAST) && !Curl_timeouts_has(data)) {
3704 Curl_timeouts_add(&multi->timeouts, data,
3705 timeouts->offset_us[timeouts->first]);
3711 * Add a timestamp to the list of timeouts. Keep the list sorted so that head
3712 * of list is always the timeout nearest in time.
3713 */
3714static CURLMcode multi_set_timeout(struct Curl_easy *data,
3715 const struct curltime *stamp,
3716 expire_id eid)
3718 struct expire_timers *timeouts = &data->state.timeouts;
3719 uint8_t *anchor = &timeouts->first;
3720 uint8_t id = (uint8_t)eid;
3722 if((unsigned)eid >= EXPIRE_LAST) {
3723 DEBUGASSERT(0);
3724 return CURLM_BAD_FUNCTION_ARGUMENT;
3726 /* remove from list, store time and re-insert */
3727 multi_clear_timeout(data, eid);
3728 timeouts->offset_us[id] =
3729 Curl_timeouts_offset_us(&data->multi->timeouts, stamp);
3731 while(*anchor < EXPIRE_LAST) {
3732 if(timeouts->offset_us[*anchor] > timeouts->offset_us[id])
3733 break;
3734 anchor = &timeouts->next[*anchor];
3736 timeouts->next[eid] = *anchor;
3737 timeouts->next[eid] = *anchor;
3738 *anchor = id;
3739 DEBUGASSERT(multi_timeouts_check(data));
3740 CURL_TRC_TIMER(data, eid, "set for %" FMT_TIMEDIFF_T "us",
3741 curlx_ptimediff_us(stamp, Curl_pgrs_now(data)));
3742 return CURLM_OK;
3746 * given a number of milliseconds from now to use to set the 'act before
3747 * this'-time for the transfer, to be extracted by curl_multi_timeout()
3749 * The timeout will be added to a queue of timeouts if it defines a moment in
3750 * time that is later than the current head of queue.
3752 * Expire replaces a former timeout using the same id if already set.
3753 */
3754void Curl_expire_set(struct Curl_easy *data,
3755 expire_id eid, timediff_t ms,
3756 const struct curltime *pnow)
3758 struct Curl_multi *multi = data->multi;
3759 struct expire_timers *timeouts = &data->state.timeouts;
3760 uint8_t prev_id = timeouts->first;
3761 struct curltime set;
3763 /* this is only interesting while there is still an associated multi struct
3764 remaining! */
3765 if(!multi)
3766 return;
3767 DEBUGASSERT(eid < EXPIRE_LAST);
3768 if(ms > INT_MAX)
3769 /* Cap ridiculous timeouts, 31-bit ms is still 3.5 weeks. When the time
3770 goes to the user, it must fit in this size. */
3771 ms = INT_MAX;
3773 set = *pnow;
3774 set.tv_sec += (time_t)(ms / 1000); /* may be a 64 to 32-bit conversion */
3775 set.tv_usec += (int)(ms % 1000) * 1000;
3776 if(set.tv_usec >= 1000000) {
3777 set.tv_sec++;
3778 set.tv_usec -= 1000000;
3781 /* Add the timeout, will replace any previous value for this timer. */
3782 multi_set_timeout(data, &set, eid);
3783 DEBUGASSERT(timeouts->first < EXPIRE_LAST);
3785 if(Curl_timeouts_has(data)) {
3786 /* data has already a timeout registered. If the first timer
3787 * was NOT the one we just set AND is still the first one,
3788 * nothing changed from the timeouts point of view. The
3789 * set timer triggers after the one already registered. Leave. */
3790 if((prev_id != eid) && (prev_id == timeouts->first))
3791 return;
3793 /* Since this is an updated time, we must remove data from
3794 * timeouts and then add it again. */
3795 Curl_timeouts_remove(&multi->timeouts, data);
3798 /* Insert the new timer expiry since it is our local minimum. */
3799 Curl_timeouts_add(&multi->timeouts, data,
3800 timeouts->offset_us[timeouts->first]);
3803void Curl_expire(struct Curl_easy *data,
3804 timediff_t milli, expire_id eid)
3806 Curl_expire_set(data, eid, milli, Curl_pgrs_now(data));
3810 * Removes the expire timer. Marks it as done.
3811 */
3812void Curl_expire_clear(struct Curl_easy *data, expire_id eid)
3814 /* remove the timer, if there */
3815 multi_clear_timeout(data, eid);
3816 CURL_TRC_TIMER(data, eid, "cleared");
3820 * Clear ALL timeout values for this handle.
3821 */
3822void Curl_expire_clear_all(struct Curl_easy *data)
3824 struct Curl_multi *multi = data->multi;
3826 /* this is only interesting while there is still an associated multi struct
3827 remaining! */
3828 if(!multi)
3829 return;
3831 if(Curl_timeouts_remove(&multi->timeouts, data)) {
3832 /* Since this is an cleared time, we must remove the previous entry from
3833 the splay tree */
3834 multi_timeouts_init(data);
3836 if(data->id >= 0)
3837 CURL_TRC_M(data, "[TIMEOUT] all cleared");
3841CURLMcode curl_multi_assign(CURLM *m, curl_socket_t sockfd,
3842 void *sockp)
3844 struct Curl_mapi_guard guard;
3845 CURLMcode mresult;
3847 if(CURL_MAPI_ENTER(&guard, m, multi_assign, &mresult)) {
3848 mresult = Curl_multi_ev_assign(m, sockfd, sockp);
3850 CURL_MAPI_LEAVE(&guard);
3851 return mresult;
3854static void move_pending_to_connect(struct Curl_multi *multi,
3855 struct Curl_easy *data)
3857 DEBUGASSERT(data->mstate == MSTATE_PENDING);
3859 /* Remove this node from the pending set, add into process set */
3860 Curl_uint32_bset_remove(&multi->pending, data->mid);
3861 Curl_uint32_bset_add(&multi->process, data->mid);
3863 multistate(data, MSTATE_CONNECT);
3864 Curl_multi_mark_dirty(data); /* make it run */
3867/* multi_schedule_pending() moves a handle from PENDING back into the process
3868 list and change state to CONNECT.
3870 We do not move all transfers because that can be a significant amount.
3871 Since this is tried every now and then doing too many too often becomes a
3872 performance problem.
3874 When there is a change for connection limits like max host connections etc,
3875 this likely only allows one new transfer. When there is a pipewait change,
3876 it can potentially allow hundreds of new transfers.
3878 We could consider an improvement where we store the queue reason and allow
3879 more pipewait rechecks than others. */
3880static void multi_schedule_pending(struct Curl_multi *multi)
3882 uint32_t mid = multi->last_pending_mid;
3884 if(mid) {
3885 while(Curl_uint32_bset_next(&multi->pending, mid, &mid)) {
3886 struct Curl_easy *data = Curl_multi_get_easy(multi, mid);
3887 if(data) {
3888 move_pending_to_connect(multi, data);
3889 multi->last_pending_mid = mid;
3890 return;
3892 /* transfer no longer known, should not happen */
3893 Curl_uint32_bset_remove(&multi->pending, mid);
3894 DEBUGASSERT(0);
3896 /* found no pending transfers with `mid` larger than `last_pending_mid`.
3897 * Start at the beginning of the pending set again. */
3898 multi->last_pending_mid = 0;
3901 if(Curl_uint32_bset_first(&multi->pending, &mid)) {
3902 do {
3903 struct Curl_easy *data = Curl_multi_get_easy(multi, mid);
3904 if(data) {
3905 move_pending_to_connect(multi, data);
3906 multi->last_pending_mid = mid;
3907 return;
3909 /* transfer no longer known, should not happen */
3910 Curl_uint32_bset_remove(&multi->pending, mid);
3911 DEBUGASSERT(0);
3912 } while(Curl_uint32_bset_next(&multi->pending, mid, &mid));
3916uint32_t Curl_multi_max_concurrent_streams(struct Curl_multi *multi)
3918 DEBUGASSERT(multi);
3919 return multi->max_concurrent_streams;
3922CURL **curl_multi_get_handles(CURLM *m)
3924 struct Curl_mapi_guard guard;
3925 CURL **a = NULL;
3927 if(CURL_MAPI_ENTER(&guard, m, multi_get_handles, NULL)) {
3928 struct Curl_multi *multi = m;
3929 void *entry;
3930 size_t count = Curl_uint32_tbl_count(&multi->xfers);
3932 a = curlx_malloc(sizeof(struct Curl_easy *) * (count + 1));
3933 if(a) {
3934 unsigned int i = 0;
3935 uint32_t mid;
3937 if(Curl_uint32_tbl_first(&multi->xfers, &mid, &entry)) {
3938 do {
3939 struct Curl_easy *data = entry;
3940 DEBUGASSERT(i < count);
3941 if(!data->state.internal)
3942 a[i++] = data;
3943 } while(Curl_uint32_tbl_next(&multi->xfers, mid, &mid, &entry));
3945 a[i] = NULL; /* last entry is a NULL */
3948 CURL_MAPI_LEAVE(&guard);
3949 return a;
3952CURLMcode curl_multi_get_offt(CURLM *m,
3953 CURLMinfo_offt info,
3954 curl_off_t *pvalue)
3956 struct Curl_mapi_guard guard;
3957 CURLMcode mresult = CURLM_OK;
3959 if(CURL_MAPI_ENTER(&guard, m, multi_get_offt, &mresult)) {
3960 struct Curl_multi *multi = m;
3961 uint32_t n;
3963 if(!pvalue) {
3964 mresult = CURLM_BAD_FUNCTION_ARGUMENT;
3965 goto out;
3968 switch(info) {
3969 case CURLMINFO_XFERS_CURRENT:
3970 n = Curl_uint32_tbl_count(&multi->xfers);
3971 if(n && multi->admin)
3972 --n;
3973 *pvalue = (curl_off_t)n;
3974 break;
3975 case CURLMINFO_XFERS_RUNNING:
3976 n = Curl_uint32_bset_count(&multi->process);
3977 if(n && Curl_uint32_bset_contains(&multi->process, multi->admin->mid))
3978 --n;
3979 *pvalue = (curl_off_t)n;
3980 break;
3981 case CURLMINFO_XFERS_PENDING:
3982 *pvalue = (curl_off_t)Curl_uint32_bset_count(&multi->pending);
3983 break;
3984 case CURLMINFO_XFERS_DONE:
3985 *pvalue = (curl_off_t)Curl_uint32_bset_count(&multi->msgsent);
3986 break;
3987 case CURLMINFO_XFERS_ADDED:
3988 *pvalue = multi->xfers_total_ever;
3989 break;
3990 default:
3991 *pvalue = -1;
3992 mresult = CURLM_UNKNOWN_OPTION;
3993 break;
3996out:
3997 CURL_MAPI_LEAVE(&guard);
3998 return mresult;
Diagnostics4 git calls · 38 ms
CallStatusSizeTime038 ms
discover200152 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=152
git.server.agent=gitty/1.0
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch object-format
git.server.fetch=shallow filter sideband-all
git.protocol.version=2
git.pkt.frames=8
git.pkt.data=6
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=152
git.pkt.payload.max=34
ls-refs200149 B · hit3 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=149
git.response.cache=HIT
git.response.duration_ms=3
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=149
git.pkt.payload.max=78
git.ls_refs.count=2
git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=32
git.response.bytes=293470
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=blob:none
git.fetch.filter.requested=tree:0
git.fetch.filter.fallback=blob:none
git.fetch.filter.learned=false
git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=10
git.pkt.data=8
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=293470
git.pkt.payload.max=65516
git.sideband.band1.bytes=293354
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=87
git.pack.bytes=293354
git.pack.ofs_delta=0
git.pack.ref_delta=1
git.pack.chain.max=1
git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0
git.pack.full=86
Open pack map
fetch20036 KiB · hit33 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=36584
git.response.cache=HIT
git.response.duration_ms=33
git.fetch.wants=5062dea992458274365fbb26344e9dff7251e2f2
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=36584
git.pkt.payload.max=36563
git.sideband.band1.bytes=36562
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
Ref
refs/heads/master
Commit
31c911efe5652725e930f4d2e60c9060e5f6646d
cf-ray
a5e21c35ca2decd4
Request
7e425995…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format