181
180
previous callback */
184
static bool multi_conn_using(struct Curl_multi *multi,
183
static void multi_connc_remove_handle(struct Curl_multi *multi,
184
struct SessionHandle *data);
185
static void singlesocket(struct Curl_multi *multi,
186
struct Curl_one_easy *easy);
187
static CURLMcode add_closure(struct Curl_multi *multi,
185
188
struct SessionHandle *data);
186
static void singlesocket(struct Curl_multi *multi,
187
struct Curl_one_easy *easy);
188
static void add_closure(struct Curl_multi *multi,
189
struct SessionHandle *data);
190
189
static int update_timer(struct Curl_multi *multi);
192
191
static CURLcode addHandleToSendOrPendPipeline(struct SessionHandle *handle,
649
647
Curl_getoff_all_pipelines(easy->easy_handle, easy->easy_conn);
652
/* If this easy_handle was the last one in charge for one or more
653
connections in the shared connection cache, we might need to keep this
654
handle around until either A) the connection is closed and killed
655
properly, or B) another easy_handle uses the connection.
657
The reason why we need to have a easy_handle associated with a live
658
connection is simply that some connections will need a handle to get
659
closed down properly. Currently, the only connections that need to keep
660
a easy_handle handle around are using FTP(S). Such connections have
661
the PROT_CLOSEACTION bit set.
663
Thus, we need to check for all connections in the shared cache that
664
points to this handle and are using PROT_CLOSEACTION. If there's any,
665
we need to add this handle to the list of "easy handles kept around for
666
nice connection closures".
668
if(multi_conn_using(multi, easy->easy_handle)) {
669
/* There's at least one connection using this handle so we must keep
670
this handle around. We also keep the connection cache pointer
671
pointing to the shared one since that will be used on close as
673
easy->easy_handle->state.shared_conn = multi;
675
/* this handle is still being used by a shared connection cache and
676
thus we leave it around for now */
677
add_closure(multi, easy->easy_handle);
650
/* figure out if the easy handle is used by one or more connections in the
652
multi_connc_remove_handle(multi, easy->easy_handle);
680
654
if(easy->easy_handle->state.connc->type == CONNCACHE_MULTI) {
681
655
/* if this was using the shared connection cache we clear the pointer
914
888
/* Handle the case when the pipe breaks, i.e., the connection
915
889
we're using gets cleaned up and we're left with nothing. */
916
890
if(easy->easy_handle->state.pipe_broke) {
917
infof(easy->easy_handle, "Pipe broke: handle 0x%x, url = %s\n",
891
infof(easy->easy_handle, "Pipe broke: handle 0x%p, url = %s\n",
918
892
easy, easy->easy_handle->state.path);
920
894
if(easy->state != CURLM_STATE_COMPLETED) {
1122
1096
case CURLM_STATE_WAITDO:
1123
1097
/* Wait for our turn to DO when we're pipelining requests */
1124
1098
#ifdef DEBUGBUILD
1125
infof(easy->easy_handle, "Conn %d send pipe %d inuse %d athead %d\n",
1099
infof(easy->easy_handle, "Conn %ld send pipe %zu inuse %d athead %d\n",
1126
1100
easy->easy_conn->connectindex,
1127
1101
easy->easy_conn->send_pipe->size,
1128
easy->easy_conn->writechannel_inuse,
1102
easy->easy_conn->writechannel_inuse?1:0,
1129
1103
isHandleAtHead(easy->easy_handle,
1130
easy->easy_conn->send_pipe));
1104
easy->easy_conn->send_pipe)?1:0);
1132
1106
if(!easy->easy_conn->writechannel_inuse &&
1133
1107
isHandleAtHead(easy->easy_handle,
1155
1129
if(CURLE_OK == easy->result) {
1156
1130
if(!dophase_done) {
1131
/* some steps needed for wildcard matching */
1132
if(easy->easy_handle->set.wildcardmatch) {
1133
struct WildcardData *wc = &easy->easy_handle->wildcard;
1134
if(wc->state == CURLWC_DONE || wc->state == CURLWC_SKIP) {
1135
/* skip some states if it is important */
1136
Curl_done(&easy->easy_conn, CURLE_OK, FALSE);
1137
multistate(easy, CURLM_STATE_DONE);
1138
result = CURLM_CALL_MULTI_PERFORM;
1157
1142
/* DO was not completed in one function call, we must continue
1159
1144
multistate(easy, CURLM_STATE_DOING);
1311
1296
#ifdef DEBUGBUILD
1313
infof(easy->easy_handle, "Conn %d recv pipe %d inuse %d athead %d\n",
1298
infof(easy->easy_handle, "Conn %ld recv pipe %zu inuse %d athead %d\n",
1314
1299
easy->easy_conn->connectindex,
1315
1300
easy->easy_conn->recv_pipe->size,
1316
easy->easy_conn->readchannel_inuse,
1301
easy->easy_conn->readchannel_inuse?1:0,
1317
1302
isHandleAtHead(easy->easy_handle,
1318
easy->easy_conn->recv_pipe));
1303
easy->easy_conn->recv_pipe)?1:0);
1475
1460
easy->easy_conn = NULL;
1463
if(easy->easy_handle->set.wildcardmatch) {
1464
if(easy->easy_handle->wildcard.state != CURLWC_DONE) {
1465
/* if a wildcard is set and we are not ending -> lets start again
1466
with CURLM_STATE_INIT */
1467
result = CURLM_CALL_MULTI_PERFORM;
1468
multistate(easy, CURLM_STATE_INIT);
1478
1473
/* after we have DONE what we're supposed to do, go COMPLETED, and
1479
1474
it doesn't matter what the Curl_done() returned! */
1480
1475
multistate(easy, CURLM_STATE_COMPLETED);
1559
1554
multi->num_msgs++; /* increase message counter */
1562
if(CURLM_CALL_MULTI_PERFORM == result)
1563
/* Set the timeout for this handle to expire really soon so that it will
1564
be taken care of even when this handle is added in the midst of
1565
operation when only the curl_multi_socket() API is used. During that
1566
flow, only sockets that time-out or have actions will be dealt
1567
with. Since this handle has no action yet, we make sure it times out to
1568
get things to happen. Also, this makes it less important for callers of
1569
the curl_multi_* functions to bother about the CURLM_CALL_MULTI_PERFORM
1570
return code, as long as they deal with the timeouts properly. */
1571
Curl_expire(easy->easy_handle, 1);
1587
1571
easy=multi->easy.next;
1588
1572
while(easy != &multi->easy) {
1589
1573
CURLMcode result;
1591
result = multi_runsingle(multi, easy);
1574
struct WildcardData *wc = &easy->easy_handle->wildcard;
1576
if(easy->easy_handle->set.wildcardmatch) {
1578
CURLcode ret = Curl_wildcard_init(wc); /* init wildcard structures */
1580
return CURLM_OUT_OF_MEMORY;
1585
result = multi_runsingle(multi, easy);
1586
while (CURLM_CALL_MULTI_PERFORM == result);
1588
if(easy->easy_handle->set.wildcardmatch) {
1589
/* destruct wildcard structures if it is needed */
1590
if(wc->state == CURLWC_DONE || result)
1591
Curl_wildcard_dtor(wc);
1593
1595
returncode = result;
2369
2377
return CURLM_OK;
2372
static bool multi_conn_using(struct Curl_multi *multi,
2373
struct SessionHandle *data)
2380
static void multi_connc_remove_handle(struct Curl_multi *multi,
2381
struct SessionHandle *data)
2375
/* any live CLOSEACTION-connections pointing to the give 'data' ? */
2383
/* a connection in the connection cache pointing to the given 'data' ? */
2378
2386
for(i=0; i< multi->connc->num; i++) {
2379
if(multi->connc->connects[i] &&
2380
(multi->connc->connects[i]->data == data) &&
2381
multi->connc->connects[i]->protocol & PROT_CLOSEACTION)
2387
struct connectdata * conn = multi->connc->connects[i];
2389
if(conn && conn->data == data) {
2390
/* If this easy_handle was the last one in charge for one or more
2391
connections in the shared connection cache, we might need to keep
2392
this handle around until either A) the connection is closed and
2393
killed properly, or B) another easy_handle uses the connection.
2395
The reason why we need to have a easy_handle associated with a live
2396
connection is simply that some connections will need a handle to get
2397
closed down properly. Currently, the only connections that need to
2398
keep a easy_handle handle around are using FTP(S). Such connections
2399
have the PROT_CLOSEACTION bit set.
2401
Thus, we need to check for all connections in the shared cache that
2402
points to this handle and are using PROT_CLOSEACTION. If there's any,
2403
we need to add this handle to the list of "easy handles kept around
2404
for nice connection closures".
2407
if(conn->protocol & PROT_CLOSEACTION) {
2408
/* this handle is still being used by a shared connection and
2409
thus we leave it around for now */
2410
if(add_closure(multi, data) == CURLM_OK)
2411
data->state.shared_conn = multi;
2413
/* out of memory - so much for graceful shutdown */
2414
Curl_disconnect(conn);
2415
multi->connc->connects[i] = NULL;
2419
/* disconect the easy handle from the connection since the connection
2420
will now remain but this easy handle is going */
2388
2426
/* Add the given data pointer to the list of 'closure handles' that are kept
2389
2427
around only to be able to close some connections nicely - just make sure
2390
2428
that this handle isn't already added, like for the cases when an easy
2391
2429
handle is removed, added and removed again... */
2392
static void add_closure(struct Curl_multi *multi,
2393
struct SessionHandle *data)
2430
static CURLMcode add_closure(struct Curl_multi *multi,
2431
struct SessionHandle *data)
2396
struct closure *cl = calloc(sizeof(struct closure), 1);
2397
struct closure *p=NULL;
2400
cl->easy_handle = data;
2401
cl->next = multi->closure;
2402
multi->closure = cl;
2406
cl = p->next; /* start immediately on the second since the first is the one
2407
we just added and it is _very_ likely to actually exist
2408
used in the cache since that's the whole purpose of adding
2411
/* When adding, scan through all the other currently kept handles and see if
2412
there are any connections still referring to them and kill them if not. */
2433
struct closure *cl = multi->closure;
2434
struct closure *p = NULL;
2437
/* Before adding, scan through all the other currently kept handles and see
2438
if there are any connections still referring to them and kill them if
2414
2442
bool inuse = FALSE;
2415
2445
for(i=0; i< multi->connc->num; i++) {
2416
2446
if(multi->connc->connects[i] &&
2417
2447
(multi->connc->connects[i]->data == cl->easy_handle)) {
2426
2456
/* cl->easy_handle is now killable */
2427
infof(data, "Delayed kill of easy handle %p\n", cl->easy_handle);
2428
2458
/* unmark it as not having a connection around that uses it anymore */
2429
2459
cl->easy_handle->state.shared_conn= NULL;
2430
Curl_close(cl->easy_handle);
2461
if(cl->easy_handle->state.closed) {
2462
infof(data, "Delayed kill of easy handle %p\n", cl->easy_handle);
2463
/* close handle only if curl_easy_cleanup() already has been called
2464
for this easy handle */
2465
Curl_close(cl->easy_handle);
2434
2470
multi->closure = n;
2473
if(cl->easy_handle == data)
2483
cl = calloc(1, sizeof(struct closure));
2485
return CURLM_OUT_OF_MEMORY;
2487
cl->easy_handle = data;
2488
cl->next = multi->closure;
2489
multi->closure = cl;
2445
2495
#ifdef DEBUGBUILD