~ubuntu-branches/ubuntu/raring/curl/raring-updates

« back to all changes in this revision

Viewing changes to lib/multi.c

  • Committer: Package Import Robot
  • Author(s): Marc Deslauriers
  • Date: 2013-02-12 08:54:32 UTC
  • mfrom: (3.4.34 sid)
  • Revision ID: package-import@ubuntu.com-20130212085432-r1fyi0b37enr93pp
Tags: 7.29.0-1ubuntu1
* Resynchronise with Debian. Remaining changes:
  - Drop dependencies not in main:
    + Build-Depends: Drop stunnel4 and libssh2-1-dev.
    + Drop libssh2-1-dev from binary package Depends.
  - Add new libcurl3-udeb package.
  - Add new curl-udeb package.
* Add warning to debian/patches/series.

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 *                            | (__| |_| |  _ <| |___
6
6
 *                             \___|\___/|_| \_\_____|
7
7
 *
8
 
 * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
 
8
 * Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
9
9
 *
10
10
 * This software is licensed as described in the file COPYING, which
11
11
 * you should have received as part of this distribution. The terms
20
20
 *
21
21
 ***************************************************************************/
22
22
 
23
 
#include "setup.h"
24
 
 
25
 
#ifdef HAVE_SYS_SOCKET_H
26
 
#include <sys/socket.h>
27
 
#endif
28
 
#ifdef HAVE_UNISTD_H
29
 
#include <unistd.h>
30
 
#endif
 
23
#include "curl_setup.h"
31
24
 
32
25
#include <curl/curl.h>
33
26
 
44
37
#include "select.h"
45
38
#include "warnless.h"
46
39
#include "speedcheck.h"
 
40
#include "conncache.h"
 
41
#include "bundles.h"
 
42
#include "multihandle.h"
47
43
 
48
44
#define _MPRINTF_REPLACE /* use our functions only */
49
45
#include <curl/mprintf.h>
61
57
#define CURL_SOCKET_HASH_TABLE_SIZE 911
62
58
#endif
63
59
 
64
 
struct Curl_message {
65
 
  /* the 'CURLMsg' is the part that is visible to the external user */
66
 
  struct CURLMsg extmsg;
67
 
};
68
 
 
69
 
/* NOTE: if you add a state here, add the name to the statename[] array as
70
 
   well!
71
 
*/
72
 
typedef enum {
73
 
  CURLM_STATE_INIT,        /* 0 - start in this state */
74
 
  CURLM_STATE_CONNECT,     /* 1 - resolve/connect has been sent off */
75
 
  CURLM_STATE_WAITRESOLVE, /* 2 - awaiting the resolve to finalize */
76
 
  CURLM_STATE_WAITCONNECT, /* 3 - awaiting the connect to finalize */
77
 
  CURLM_STATE_WAITPROXYCONNECT, /* 4 - awaiting proxy CONNECT to finalize */
78
 
  CURLM_STATE_PROTOCONNECT, /* 5 - completing the protocol-specific connect
79
 
                               phase */
80
 
  CURLM_STATE_WAITDO,      /* 6 - wait for our turn to send the request */
81
 
  CURLM_STATE_DO,          /* 7 - start send off the request (part 1) */
82
 
  CURLM_STATE_DOING,       /* 8 - sending off the request (part 1) */
83
 
  CURLM_STATE_DO_MORE,     /* 9 - send off the request (part 2) */
84
 
  CURLM_STATE_DO_DONE,     /* 10 - done sending off request */
85
 
  CURLM_STATE_WAITPERFORM, /* 11 - wait for our turn to read the response */
86
 
  CURLM_STATE_PERFORM,     /* 12 - transfer data */
87
 
  CURLM_STATE_TOOFAST,     /* 13 - wait because limit-rate exceeded */
88
 
  CURLM_STATE_DONE,        /* 14 - post data transfer operation */
89
 
  CURLM_STATE_COMPLETED,   /* 15 - operation complete */
90
 
  CURLM_STATE_MSGSENT,     /* 16 - the operation complete message is sent */
91
 
  CURLM_STATE_LAST         /* 17 - not a true state, never use this */
92
 
} CURLMstate;
93
 
 
94
 
/* we support N sockets per easy handle. Set the corresponding bit to what
95
 
   action we should wait for */
96
 
#define MAX_SOCKSPEREASYHANDLE 5
97
 
#define GETSOCK_READABLE (0x00ff)
98
 
#define GETSOCK_WRITABLE (0xff00)
99
 
 
100
 
struct closure {
101
 
  struct closure *next; /* a simple one-way list of structs */
102
 
  struct SessionHandle *easy_handle;
103
 
};
104
 
 
105
 
struct Curl_one_easy {
106
 
  /* first, two fields for the linked list of these */
107
 
  struct Curl_one_easy *next;
108
 
  struct Curl_one_easy *prev;
109
 
 
110
 
  struct SessionHandle *easy_handle; /* the easy handle for this unit */
111
 
  struct connectdata *easy_conn;     /* the "unit's" connection */
112
 
 
113
 
  CURLMstate state;  /* the handle's state */
114
 
  CURLcode result;   /* previous result */
115
 
 
116
 
  struct Curl_message msg; /* A single posted message. */
117
 
 
118
 
  /* Array with the plain socket numbers this handle takes care of, in no
119
 
     particular order. Note that all sockets are added to the sockhash, where
120
 
     the state etc are also kept. This array is mostly used to detect when a
121
 
     socket is to be removed from the hash. See singlesocket(). */
122
 
  curl_socket_t sockets[MAX_SOCKSPEREASYHANDLE];
123
 
  int numsocks;
124
 
};
125
60
 
126
61
#define CURL_MULTI_HANDLE 0x000bab1e
127
62
 
130
65
#define GOOD_EASY_HANDLE(x) \
131
66
  ((x) && (((struct SessionHandle *)(x))->magic == CURLEASY_MAGIC_NUMBER))
132
67
 
133
 
/* This is the struct known as CURLM on the outside */
134
 
struct Curl_multi {
135
 
  /* First a simple identifier to easier detect if a user mix up
136
 
     this multi handle with an easy handle. Set this to CURL_MULTI_HANDLE. */
137
 
  long type;
138
 
 
139
 
  /* We have a doubly-linked circular list with easy handles */
140
 
  struct Curl_one_easy easy;
141
 
 
142
 
  int num_easy; /* amount of entries in the linked list above. */
143
 
  int num_alive; /* amount of easy handles that are added but have not yet
144
 
                    reached COMPLETE state */
145
 
 
146
 
  struct curl_llist *msglist; /* a list of messages from completed transfers */
147
 
 
148
 
  /* callback function and user data pointer for the *socket() API */
149
 
  curl_socket_callback socket_cb;
150
 
  void *socket_userp;
151
 
 
152
 
  /* Hostname cache */
153
 
  struct curl_hash *hostcache;
154
 
 
155
 
  /* timetree points to the splay-tree of time nodes to figure out expire
156
 
     times of all currently set timers */
157
 
  struct Curl_tree *timetree;
158
 
 
159
 
  /* 'sockhash' is the lookup hash for socket descriptor => easy handles (note
160
 
     the pluralis form, there can be more than one easy handle waiting on the
161
 
     same actual socket) */
162
 
  struct curl_hash *sockhash;
163
 
 
164
 
  /* Whether pipelining is enabled for this multi handle */
165
 
  bool pipelining_enabled;
166
 
 
167
 
  /* shared connection cache */
168
 
  struct conncache *connc;
169
 
  long maxconnects; /* if >0, a fixed limit of the maximum number of entries
170
 
                       we're allowed to grow the connection cache to */
171
 
 
172
 
  /* list of easy handles kept around for doing nice connection closures */
173
 
  struct closure *closure;
174
 
 
175
 
  /* timer callback and user data pointer for the *socket() API */
176
 
  curl_multi_timer_callback timer_cb;
177
 
  void *timer_userp;
178
 
  struct timeval timer_lastcall; /* the fixed time for the timeout for the
179
 
                                    previous callback */
180
 
};
181
 
 
182
 
static void multi_connc_remove_handle(struct Curl_multi *multi,
183
 
                                      struct SessionHandle *data);
184
68
static void singlesocket(struct Curl_multi *multi,
185
69
                         struct Curl_one_easy *easy);
186
 
static CURLMcode add_closure(struct Curl_multi *multi,
187
 
                             struct SessionHandle *data);
188
70
static int update_timer(struct Curl_multi *multi);
189
71
 
190
72
static CURLcode addHandleToSendOrPendPipeline(struct SessionHandle *handle,
225
107
static void multi_freetimeout(void *a, void *b);
226
108
 
227
109
/* always use this function to change state, to make debugging easier */
228
 
static void multistate(struct Curl_one_easy *easy, CURLMstate state)
 
110
static void mstate(struct Curl_one_easy *easy, CURLMstate state
 
111
#ifdef DEBUGBUILD
 
112
                   , int lineno
 
113
#endif
 
114
)
229
115
{
230
116
#ifdef DEBUGBUILD
231
 
  long connectindex = -5000;
 
117
  long connection_id = -5000;
232
118
#endif
233
119
  CURLMstate oldstate = easy->state;
234
120
 
242
128
  if(easy->easy_conn) {
243
129
    if(easy->state > CURLM_STATE_CONNECT &&
244
130
       easy->state < CURLM_STATE_COMPLETED)
245
 
      connectindex = easy->easy_conn->connectindex;
 
131
      connection_id = easy->easy_conn->connection_id;
246
132
 
247
133
    infof(easy->easy_handle,
248
 
          "STATE: %s => %s handle %p; (connection #%ld) \n",
 
134
          "STATE: %s => %s handle %p; line %d (connection #%ld) \n",
249
135
          statename[oldstate], statename[easy->state],
250
 
          (char *)easy, connectindex);
 
136
          (char *)easy, lineno, connection_id);
251
137
  }
252
138
#endif
253
139
  if(state == CURLM_STATE_COMPLETED)
255
141
    easy->easy_handle->multi->num_alive--;
256
142
}
257
143
 
 
144
#ifndef DEBUGBUILD
 
145
#define multistate(x,y) mstate(x,y)
 
146
#else
 
147
#define multistate(x,y) mstate(x,y, __LINE__)
 
148
#endif
 
149
 
258
150
/*
259
151
 * We add one of these structs to the sockhash for a particular socket
260
152
 */
391
283
  (void)b;
392
284
}
393
285
 
394
 
 
395
286
CURLM *curl_multi_init(void)
396
287
{
397
288
  struct Curl_multi *multi = calloc(1, sizeof(struct Curl_multi));
409
300
  if(!multi->sockhash)
410
301
    goto error;
411
302
 
412
 
  multi->connc = Curl_mk_connc(CONNCACHE_MULTI, -1L);
413
 
  if(!multi->connc)
 
303
  multi->conn_cache = Curl_conncache_init();
 
304
  if(!multi->conn_cache)
414
305
    goto error;
415
306
 
416
307
  multi->msglist = Curl_llist_alloc(multi_freeamsg);
431
322
  multi->sockhash = NULL;
432
323
  Curl_hash_destroy(multi->hostcache);
433
324
  multi->hostcache = NULL;
434
 
  Curl_rm_connc(multi->connc);
435
 
  multi->connc = NULL;
 
325
  Curl_conncache_destroy(multi->conn_cache);
 
326
  multi->conn_cache = NULL;
436
327
 
437
328
  free(multi);
438
329
  return NULL;
443
334
{
444
335
  struct curl_llist *timeoutlist;
445
336
  struct Curl_one_easy *easy;
446
 
  struct closure *cl;
447
 
  struct closure *prev = NULL;
448
337
  struct Curl_multi *multi = (struct Curl_multi *)multi_handle;
449
338
  struct SessionHandle *data = (struct SessionHandle *)easy_handle;
 
339
  struct SessionHandle *new_closure = NULL;
 
340
  struct curl_hash *hostcache = NULL;
450
341
 
451
342
  /* First, make some basic checks that the CURLM handle is a good handle */
452
343
  if(!GOOD_MULTI_HANDLE(multi))
462
353
    /* possibly we should create a new unique error code for this condition */
463
354
    return CURLM_BAD_EASY_HANDLE;
464
355
 
465
 
  /* We want the connection cache to have plenty of room. Before we supported
466
 
     the shared cache every single easy handle had 5 entries in their cache
467
 
     by default. */
468
 
  if(((multi->num_easy + 1) * 4) > multi->connc->num) {
469
 
    long newmax = (multi->num_easy + 1) * 4;
470
 
 
471
 
    if(multi->maxconnects && (newmax > multi->maxconnects))
472
 
      /* don't grow beyond the allowed size */
473
 
      newmax = multi->maxconnects;
474
 
 
475
 
    if(newmax > multi->connc->num) {
476
 
      /* we only do this is we can in fact grow the cache */
477
 
      CURLcode res = Curl_ch_connc(data, multi->connc, newmax);
478
 
      if(res)
479
 
        return CURLM_OUT_OF_MEMORY;
480
 
    }
481
 
  }
482
 
 
483
356
  /* Allocate and initialize timeout list for easy handle */
484
357
  timeoutlist = Curl_llist_alloc(multi_freetimeout);
485
358
  if(!timeoutlist)
493
366
    return CURLM_OUT_OF_MEMORY;
494
367
  }
495
368
 
 
369
  /* In case multi handle has no hostcache yet, allocate one */
 
370
  if(!multi->hostcache) {
 
371
    hostcache = Curl_mk_dnscache();
 
372
    if(!hostcache) {
 
373
      free(easy);
 
374
      Curl_llist_destroy(timeoutlist, NULL);
 
375
      return CURLM_OUT_OF_MEMORY;
 
376
    }
 
377
  }
 
378
 
 
379
  /* In case multi handle has no closure_handle yet, allocate
 
380
     a new easy handle to use when closing cached connections */
 
381
  if(!multi->closure_handle) {
 
382
    new_closure = (struct SessionHandle *)curl_easy_init();
 
383
    if(!new_closure) {
 
384
      Curl_hash_destroy(hostcache);
 
385
      free(easy);
 
386
      Curl_llist_destroy(timeoutlist, NULL);
 
387
      return CURLM_OUT_OF_MEMORY;
 
388
    }
 
389
  }
 
390
 
496
391
  /*
497
392
  ** No failure allowed in this function beyond this point. And
498
393
  ** no modification of easy nor multi handle allowed before this
500
395
  ** won't be undone in this function no matter what.
501
396
  */
502
397
 
 
398
  /* In case a new closure handle has been initialized above, it
 
399
     is associated now with the multi handle which lacked one. */
 
400
  if(new_closure) {
 
401
    multi->closure_handle = new_closure;
 
402
    Curl_easy_addmulti(multi->closure_handle, multi_handle);
 
403
    multi->closure_handle->state.conn_cache = multi->conn_cache;
 
404
  }
 
405
 
 
406
  /* In case hostcache has been allocated above,
 
407
     it is associated now with the multi handle. */
 
408
  if(hostcache)
 
409
    multi->hostcache = hostcache;
 
410
 
503
411
  /* Make easy handle use timeout list initialized above */
504
412
  data->state.timeoutlist = timeoutlist;
505
413
  timeoutlist = NULL;
506
414
 
507
 
  /* Remove handle from the list of 'closure handles' in case it is there */
508
 
  cl = multi->closure;
509
 
  while(cl) {
510
 
    struct closure *next = cl->next;
511
 
    if(cl->easy_handle == data) {
512
 
      /* Remove node from list */
513
 
      free(cl);
514
 
      if(prev)
515
 
        prev->next = next;
516
 
      else
517
 
        multi->closure = next;
518
 
      /* removed from closure list now, this might reuse an existing
519
 
         existing connection but we don't know that at this point */
520
 
      data->state.shared_conn = NULL;
521
 
      /* No need to continue, handle can only be present once in the list */
522
 
      break;
523
 
    }
524
 
    prev = cl;
525
 
    cl = next;
526
 
  }
527
 
 
528
415
  /* set the easy handle */
529
416
  easy->easy_handle = data;
530
417
  multistate(easy, CURLM_STATE_INIT);
533
420
  easy->easy_handle->multi_pos =  easy;
534
421
 
535
422
  /* for multi interface connections, we share DNS cache automatically if the
536
 
     easy handle's one is currently private. */
537
 
  if(easy->easy_handle->dns.hostcache &&
538
 
     (easy->easy_handle->dns.hostcachetype == HCACHE_PRIVATE)) {
539
 
    Curl_hash_destroy(easy->easy_handle->dns.hostcache);
540
 
    easy->easy_handle->dns.hostcache = NULL;
541
 
    easy->easy_handle->dns.hostcachetype = HCACHE_NONE;
542
 
  }
543
 
 
 
423
     easy handle's one is currently not set. */
544
424
  if(!easy->easy_handle->dns.hostcache ||
545
425
     (easy->easy_handle->dns.hostcachetype == HCACHE_NONE)) {
546
426
    easy->easy_handle->dns.hostcache = multi->hostcache;
547
427
    easy->easy_handle->dns.hostcachetype = HCACHE_MULTI;
548
428
  }
549
429
 
550
 
  /* On a multi stack the connection cache, owned by the multi handle,
551
 
     is shared between all easy handles within the multi handle. */
552
 
  if(easy->easy_handle->state.connc &&
553
 
     (easy->easy_handle->state.connc->type == CONNCACHE_PRIVATE)) {
554
 
    /* kill old private connection cache */
555
 
    Curl_rm_connc(easy->easy_handle->state.connc);
556
 
    easy->easy_handle->state.connc = NULL;
557
 
  }
558
 
  /* Point now to this multi's connection cache */
559
 
  easy->easy_handle->state.connc = multi->connc;
560
 
  easy->easy_handle->state.connc->type = CONNCACHE_MULTI;
 
430
  /* Point to the multi's connection cache */
 
431
  easy->easy_handle->state.conn_cache = multi->conn_cache;
561
432
 
562
433
  /* This adds the new entry at the 'end' of the doubly-linked circular
563
434
     list of Curl_one_easy structs to try and maintain a FIFO queue so
684
555
    }
685
556
 
686
557
    if(easy->easy_handle->dns.hostcachetype == HCACHE_MULTI) {
687
 
      /* clear out the usage of the shared DNS cache */
 
558
      /* stop using the multi handle's DNS cache */
688
559
      easy->easy_handle->dns.hostcache = NULL;
689
560
      easy->easy_handle->dns.hostcachetype = HCACHE_NONE;
690
561
    }
701
572
           Note that this ignores the return code simply because there's
702
573
           nothing really useful to do with it anyway! */
703
574
        (void)Curl_done(&easy->easy_conn, easy->result, premature);
704
 
 
705
 
        if(easy->easy_conn)
706
 
          /* the connection is still alive, set back the association to enable
707
 
             the check below to trigger TRUE */
708
 
          easy->easy_conn->data = easy->easy_handle;
709
575
      }
710
576
      else
711
577
        /* Clear connection pipelines, if Curl_done above was not called */
712
578
        Curl_getoff_all_pipelines(easy->easy_handle, easy->easy_conn);
713
579
    }
714
580
 
715
 
    /* figure out if the easy handle is used by one or more connections in the
716
 
       cache */
717
 
    multi_connc_remove_handle(multi, easy->easy_handle);
718
 
 
719
 
    if(easy->easy_handle->state.connc->type == CONNCACHE_MULTI) {
720
 
      /* if this was using the shared connection cache we clear the pointer
721
 
         to that since we're not part of that handle anymore */
722
 
      easy->easy_handle->state.connc = NULL;
723
 
 
724
 
      /* Since we return the connection back to the communal connection pool
725
 
         we mark the last connection as inaccessible */
726
 
      easy->easy_handle->state.lastconnect = -1;
727
 
 
728
 
      /* Modify the connectindex since this handle can't point to the
729
 
         connection cache anymore.
730
 
 
731
 
         TODO: consider if this is really what we want. The connection cache
732
 
         is within the multi handle and that owns the connections so we should
733
 
         not need to touch connections like this when we just remove an easy
734
 
         handle...
735
 
      */
736
 
      if(easy->easy_conn && easy_owns_conn &&
737
 
         (easy->easy_conn->send_pipe->size +
738
 
          easy->easy_conn->recv_pipe->size == 0))
739
 
        easy->easy_conn->connectindex = -1;
740
 
    }
 
581
    /* as this was using a shared connection cache we clear the pointer
 
582
       to that since we're not part of that multi handle anymore */
 
583
    easy->easy_handle->state.conn_cache = NULL;
741
584
 
742
585
    /* change state without using multistate(), only to make singlesocket() do
743
586
       what we want */
745
588
    singlesocket(multi, easy); /* to let the application know what sockets
746
589
                                  that vanish with this handle */
747
590
 
 
591
    /* Remove the association between the connection and the handle */
 
592
    if(easy->easy_conn) {
 
593
      easy->easy_conn->data = NULL;
 
594
      easy->easy_conn = NULL;
 
595
    }
 
596
 
748
597
    Curl_easy_addmulti(easy->easy_handle, NULL); /* clear the association
749
598
                                                    to this multi handle */
750
599
 
954
803
  int bitmap;
955
804
  unsigned int i;
956
805
  unsigned int nfds = extra_nfds;
957
 
  struct pollfd *ufds;
 
806
  struct pollfd *ufds = NULL;
958
807
 
959
808
  if(!GOOD_MULTI_HANDLE(multi))
960
809
    return CURLM_BAD_HANDLE;
983
832
    easy = easy->next; /* check next handle */
984
833
  }
985
834
 
986
 
  ufds = (struct pollfd *)malloc(nfds * sizeof(struct pollfd));
 
835
  if(nfds) {
 
836
    ufds = malloc(nfds * sizeof(struct pollfd));
 
837
    if(!ufds)
 
838
      return CURLM_OUT_OF_MEMORY;
 
839
  }
987
840
  nfds = 0;
988
841
 
989
842
  /* Add the curl handles to our pollfds first */
1017
870
  /* Add external file descriptions from poll-like struct curl_waitfd */
1018
871
  for(i = 0; i < extra_nfds; i++) {
1019
872
    ufds[nfds].fd = extra_fds[i].fd;
1020
 
    ufds[nfds].events = (short) (
1021
 
      ((extra_fds[i].events & CURL_WAIT_POLLIN)  ? POLLIN  : 0) |
1022
 
      ((extra_fds[i].events & CURL_WAIT_POLLPRI) ? POLLPRI : 0) |
1023
 
      ((extra_fds[i].events & CURL_WAIT_POLLOUT) ? POLLOUT : 0) );
 
873
    ufds[nfds].events = 0;
 
874
    if(extra_fds[i].events & CURL_WAIT_POLLIN)
 
875
      ufds[nfds].events |= POLLIN;
 
876
    if(extra_fds[i].events & CURL_WAIT_POLLPRI)
 
877
      ufds[nfds].events |= POLLPRI;
 
878
    if(extra_fds[i].events & CURL_WAIT_POLLOUT)
 
879
      ufds[nfds].events |= POLLOUT;
1024
880
    ++nfds;
1025
881
  }
1026
882
 
1030
886
  else
1031
887
    i = 0;
1032
888
 
1033
 
  free(ufds);
 
889
  Curl_safefree(ufds);
1034
890
  if(ret)
1035
891
    *ret = i;
1036
892
  return CURLM_OK;
1044
900
  bool connected;
1045
901
  bool async;
1046
902
  bool protocol_connect = FALSE;
1047
 
  bool dophase_done;
 
903
  bool dophase_done = FALSE;
1048
904
  bool done = FALSE;
1049
905
  CURLMcode result = CURLM_OK;
1050
906
  struct SingleRequest *k;
1139
995
        /* after init, go CONNECT */
1140
996
        multistate(easy, CURLM_STATE_CONNECT);
1141
997
        result = CURLM_CALL_MULTI_PERFORM;
1142
 
 
1143
 
        data->state.used_interface = Curl_if_multi;
1144
998
      }
1145
999
      break;
1146
1000
 
1321
1175
    case CURLM_STATE_WAITDO:
1322
1176
      /* Wait for our turn to DO when we're pipelining requests */
1323
1177
#ifdef DEBUGBUILD
1324
 
      infof(data, "Conn %ld send pipe %zu inuse %d athead %d\n",
1325
 
            easy->easy_conn->connectindex,
 
1178
      infof(data, "WAITDO: Conn %ld send pipe %zu inuse %d athead %d\n",
 
1179
            easy->easy_conn->connection_id,
1326
1180
            easy->easy_conn->send_pipe->size,
1327
1181
            easy->easy_conn->writechannel_inuse?1:0,
1328
1182
            isHandleAtHead(data,
1511
1365
      }
1512
1366
#ifdef DEBUGBUILD
1513
1367
      else {
1514
 
        infof(data, "Conn %ld recv pipe %zu inuse %d athead %d\n",
1515
 
              easy->easy_conn->connectindex,
 
1368
        infof(data, "WAITPERFORM: Conn %ld recv pipe %zu inuse %d athead %d\n",
 
1369
              easy->easy_conn->connection_id,
1516
1370
              easy->easy_conn->recv_pipe->size,
1517
1371
              easy->easy_conn->readchannel_inuse?1:0,
1518
1372
              isHandleAtHead(data,
1536
1390
      break;
1537
1391
 
1538
1392
    case CURLM_STATE_PERFORM:
 
1393
      {
 
1394
      char *newurl = NULL;
 
1395
      bool retry = FALSE;
 
1396
 
1539
1397
      /* check if over send speed */
1540
1398
      if((data->set.max_send_speed > 0) &&
1541
1399
         (data->progress.ulspeed > data->set.max_send_speed)) {
1583
1441
        easy->easy_conn->writechannel_inuse = FALSE;
1584
1442
      }
1585
1443
 
 
1444
      if(done || (easy->result == CURLE_RECV_ERROR)) {
 
1445
        /* If CURLE_RECV_ERROR happens early enough, we assume it was a race
 
1446
         * condition and the server closed the re-used connection exactly when
 
1447
         * we wanted to use it, so figure out if that is indeed the case.
 
1448
         */
 
1449
        CURLcode ret = Curl_retry_request(easy->easy_conn, &newurl);
 
1450
        if(!ret)
 
1451
          retry = (newurl)?TRUE:FALSE;
 
1452
 
 
1453
        if(retry) {
 
1454
          /* if we are to retry, set the result to OK and consider the
 
1455
             request as done */
 
1456
          easy->result = CURLE_OK;
 
1457
          done = TRUE;
 
1458
        }
 
1459
      }
 
1460
 
1586
1461
      if(easy->result) {
1587
 
        /* The transfer phase returned error, we mark the connection to get
 
1462
        /*
 
1463
         * The transfer phase returned error, we mark the connection to get
1588
1464
         * closed to prevent being re-used. This is because we can't possibly
1589
1465
         * know if the connection is in a good shape or not now.  Unless it is
1590
1466
         * a protocol which uses two "channels" like FTP, as then the error
1591
1467
         * happened in the data connection.
1592
1468
         */
 
1469
 
1593
1470
        if(!(easy->easy_conn->handler->flags & PROTOPT_DUAL))
1594
1471
          easy->easy_conn->bits.close = TRUE;
1595
1472
 
1597
1474
        Curl_done(&easy->easy_conn, easy->result, FALSE);
1598
1475
      }
1599
1476
      else if(done) {
1600
 
        char *newurl = NULL;
1601
 
        bool retry = FALSE;
1602
1477
        followtype follow=FOLLOW_NONE;
1603
1478
 
1604
 
        easy->result = Curl_retry_request(easy->easy_conn, &newurl);
1605
 
        if(!easy->result)
1606
 
          retry = (newurl)?TRUE:FALSE;
1607
 
 
1608
1479
        /* call this even if the readwrite function returned error */
1609
1480
        Curl_posttransfer(data);
1610
1481
 
1637
1508
          if(CURLE_OK == easy->result) {
1638
1509
            multistate(easy, CURLM_STATE_CONNECT);
1639
1510
            result = CURLM_CALL_MULTI_PERFORM;
 
1511
            newurl = NULL; /* handed over the memory ownership to
 
1512
                              Curl_follow(), make sure we don't free() it
 
1513
                              here */
1640
1514
          }
1641
 
          else if(newurl)
1642
 
            /* Since we "took it", we are in charge of freeing this on
1643
 
               failure */
1644
 
            free(newurl);
1645
1515
        }
1646
1516
        else {
1647
1517
          /* after the transfer is done, go DONE */
1649
1519
          /* but first check to see if we got a location info even though we're
1650
1520
             not following redirects */
1651
1521
          if(data->req.location) {
 
1522
            if(newurl)
 
1523
              free(newurl);
1652
1524
            newurl = data->req.location;
1653
1525
            data->req.location = NULL;
1654
1526
            easy->result = Curl_follow(data, newurl, FOLLOW_FAKE);
1655
 
            if(easy->result) {
 
1527
            if(CURLE_OK == easy->result)
 
1528
              newurl = NULL; /* allocation was handed over Curl_follow() */
 
1529
            else
1656
1530
              disconnect_conn = TRUE;
1657
 
              free(newurl);
1658
 
            }
1659
1531
          }
1660
1532
 
1661
1533
          multistate(easy, CURLM_STATE_DONE);
1663
1535
        }
1664
1536
      }
1665
1537
 
 
1538
      if(newurl)
 
1539
        free(newurl);
1666
1540
      break;
 
1541
      }
1667
1542
 
1668
1543
    case CURLM_STATE_DONE:
1669
1544
 
1789
1664
  } WHILE_FALSE; /* just to break out from! */
1790
1665
 
1791
1666
  if(CURLM_STATE_COMPLETED == easy->state) {
1792
 
    if(data->dns.hostcachetype == HCACHE_MULTI) {
1793
 
      /* clear out the usage of the shared DNS cache */
1794
 
      data->dns.hostcache = NULL;
1795
 
      data->dns.hostcachetype = HCACHE_NONE;
1796
 
    }
1797
 
 
1798
1667
    /* now fill in the Curl_message with this info */
1799
1668
    msg = &easy->msg;
1800
1669
 
1877
1746
  return returncode;
1878
1747
}
1879
1748
 
 
1749
static void close_all_connections(struct Curl_multi *multi)
 
1750
{
 
1751
  struct connectdata *conn;
 
1752
 
 
1753
  conn = Curl_conncache_find_first_connection(multi->conn_cache);
 
1754
  while(conn) {
 
1755
    conn->data = multi->closure_handle;
 
1756
 
 
1757
    /* This will remove the connection from the cache */
 
1758
    (void)Curl_disconnect(conn, FALSE);
 
1759
 
 
1760
    conn = Curl_conncache_find_first_connection(multi->conn_cache);
 
1761
  }
 
1762
}
 
1763
 
1880
1764
CURLMcode curl_multi_cleanup(CURLM *multi_handle)
1881
1765
{
1882
1766
  struct Curl_multi *multi=(struct Curl_multi *)multi_handle;
1883
1767
  struct Curl_one_easy *easy;
1884
1768
  struct Curl_one_easy *nexteasy;
1885
 
  int i;
1886
 
  struct closure *cl;
1887
 
  struct closure *n;
1888
1769
 
1889
1770
  if(GOOD_MULTI_HANDLE(multi)) {
1890
1771
    multi->type = 0; /* not good anymore */
1891
1772
 
1892
 
    /* go over all connections that have close actions */
1893
 
    for(i=0; i< multi->connc->num; i++) {
1894
 
      if(multi->connc->connects[i] &&
1895
 
         multi->connc->connects[i]->handler->flags & PROTOPT_CLOSEACTION) {
1896
 
        Curl_disconnect(multi->connc->connects[i], FALSE);
1897
 
        multi->connc->connects[i] = NULL;
1898
 
      }
1899
 
    }
1900
 
    /* now walk through the list of handles we kept around only to be
1901
 
       able to close connections "properly" */
1902
 
    cl = multi->closure;
1903
 
    while(cl) {
1904
 
      cl->easy_handle->state.shared_conn = NULL; /* allow cleanup */
1905
 
      if(cl->easy_handle->state.closed)
1906
 
        /* close handle only if curl_easy_cleanup() already has been called
1907
 
           for this easy handle */
1908
 
        Curl_close(cl->easy_handle);
1909
 
      n = cl->next;
1910
 
      free(cl);
1911
 
      cl= n;
1912
 
    }
1913
 
 
1914
 
    Curl_hash_destroy(multi->hostcache);
1915
 
    multi->hostcache = NULL;
 
1773
    /* Close all the connections in the connection cache */
 
1774
    close_all_connections(multi);
 
1775
 
 
1776
    multi->closure_handle->dns.hostcache = multi->hostcache;
 
1777
    Curl_hostcache_clean(multi->closure_handle);
 
1778
 
 
1779
    Curl_close(multi->closure_handle);
 
1780
    multi->closure_handle = NULL;
1916
1781
 
1917
1782
    Curl_hash_destroy(multi->sockhash);
1918
1783
    multi->sockhash = NULL;
1919
1784
 
1920
 
    Curl_rm_connc(multi->connc);
1921
 
    multi->connc = NULL;
 
1785
    Curl_conncache_destroy(multi->conn_cache);
 
1786
    multi->conn_cache = NULL;
1922
1787
 
1923
1788
    /* remove the pending list of messages */
1924
1789
    Curl_llist_destroy(multi->msglist, NULL);
1930
1795
      nexteasy=easy->next;
1931
1796
      if(easy->easy_handle->dns.hostcachetype == HCACHE_MULTI) {
1932
1797
        /* clear out the usage of the shared DNS cache */
 
1798
        Curl_hostcache_clean(easy->easy_handle);
1933
1799
        easy->easy_handle->dns.hostcache = NULL;
1934
1800
        easy->easy_handle->dns.hostcachetype = HCACHE_NONE;
1935
1801
      }
1936
1802
 
1937
1803
      /* Clear the pointer to the connection cache */
1938
 
      easy->easy_handle->state.connc = NULL;
 
1804
      easy->easy_handle->state.conn_cache = NULL;
1939
1805
 
1940
1806
      Curl_easy_addmulti(easy->easy_handle, NULL); /* clear the association */
1941
1807
 
1943
1809
      easy = nexteasy;
1944
1810
    }
1945
1811
 
 
1812
    Curl_hash_destroy(multi->hostcache);
 
1813
    multi->hostcache = NULL;
 
1814
 
1946
1815
    free(multi);
1947
1816
 
1948
1817
    return CURLM_OK;
2788
2657
  return CURLM_OK;
2789
2658
}
2790
2659
 
2791
 
static void multi_connc_remove_handle(struct Curl_multi *multi,
2792
 
                                      struct SessionHandle *data)
2793
 
{
2794
 
  /* a connection in the connection cache pointing to the given 'data' ? */
2795
 
  int i;
2796
 
 
2797
 
  for(i=0; i< multi->connc->num; i++) {
2798
 
    struct connectdata * conn = multi->connc->connects[i];
2799
 
 
2800
 
    if(conn && conn->data == data) {
2801
 
      /* If this easy_handle was the last one in charge for one or more
2802
 
         connections in the shared connection cache, we might need to keep
2803
 
         this handle around until either A) the connection is closed and
2804
 
         killed properly, or B) another easy_handle uses the connection.
2805
 
 
2806
 
         The reason why we need to have a easy_handle associated with a live
2807
 
         connection is simply that some connections will need a handle to get
2808
 
         closed down properly. Currently, the only connections that need to
2809
 
         keep a easy_handle handle around are using FTP(S). Such connections
2810
 
         have the PROT_CLOSEACTION bit set.
2811
 
 
2812
 
         Thus, we need to check for all connections in the shared cache that
2813
 
         points to this handle and are using PROT_CLOSEACTION. If there's any,
2814
 
         we need to add this handle to the list of "easy handles kept around
2815
 
         for nice connection closures".
2816
 
      */
2817
 
 
2818
 
      if(conn->handler->flags & PROTOPT_CLOSEACTION) {
2819
 
        /* this handle is still being used by a shared connection and
2820
 
           thus we leave it around for now */
2821
 
        if(add_closure(multi, data) == CURLM_OK)
2822
 
          data->state.shared_conn = multi;
2823
 
        else {
2824
 
          /* out of memory - so much for graceful shutdown */
2825
 
          Curl_disconnect(conn, /* dead_connection */ FALSE);
2826
 
          multi->connc->connects[i] = NULL;
2827
 
          data->state.shared_conn = NULL;
2828
 
        }
2829
 
      }
2830
 
      else {
2831
 
        /* disconect the easy handle from the connection since the connection
2832
 
           will now remain but this easy handle is going */
2833
 
        data->state.shared_conn = NULL;
2834
 
        conn->data = NULL;
2835
 
      }
2836
 
    }
2837
 
  }
2838
 
}
2839
 
 
2840
 
/* Add the given data pointer to the list of 'closure handles' that are kept
2841
 
   around only to be able to close some connections nicely - just make sure
2842
 
   that this handle isn't already added, like for the cases when an easy
2843
 
   handle is removed, added and removed again... */
2844
 
static CURLMcode add_closure(struct Curl_multi *multi,
2845
 
                             struct SessionHandle *data)
2846
 
{
2847
 
  struct closure *cl = multi->closure;
2848
 
  struct closure *p = NULL;
2849
 
  bool add = TRUE;
2850
 
 
2851
 
  /* Before adding, scan through all the other currently kept handles and see
2852
 
     if there are any connections still referring to them and kill them if
2853
 
     not. */
2854
 
  while(cl) {
2855
 
    struct closure *n;
2856
 
    bool inuse = FALSE;
2857
 
    int i;
2858
 
 
2859
 
    for(i=0; i< multi->connc->num; i++) {
2860
 
      if(multi->connc->connects[i] &&
2861
 
         (multi->connc->connects[i]->data == cl->easy_handle)) {
2862
 
        inuse = TRUE;
2863
 
        break;
2864
 
      }
2865
 
    }
2866
 
 
2867
 
    n = cl->next;
2868
 
 
2869
 
    if(!inuse) {
2870
 
      /* cl->easy_handle is now killable */
2871
 
 
2872
 
      /* unmark it as not having a connection around that uses it anymore */
2873
 
      cl->easy_handle->state.shared_conn= NULL;
2874
 
 
2875
 
      if(cl->easy_handle->state.closed) {
2876
 
        infof(data, "Delayed kill of easy handle %p\n", cl->easy_handle);
2877
 
        /* close handle only if curl_easy_cleanup() already has been called
2878
 
           for this easy handle */
2879
 
        Curl_close(cl->easy_handle);
2880
 
      }
2881
 
      if(p)
2882
 
        p->next = n;
2883
 
      else
2884
 
        multi->closure = n;
2885
 
      free(cl);
2886
 
    }
2887
 
    else {
2888
 
      if(cl->easy_handle == data)
2889
 
        add = FALSE;
2890
 
 
2891
 
      p = cl;
2892
 
    }
2893
 
 
2894
 
    cl = n;
2895
 
  }
2896
 
 
2897
 
  if(add) {
2898
 
    cl = calloc(1, sizeof(struct closure));
2899
 
    if(!cl)
2900
 
      return CURLM_OUT_OF_MEMORY;
2901
 
 
2902
 
    cl->easy_handle = data;
2903
 
    cl->next = multi->closure;
2904
 
    multi->closure = cl;
2905
 
  }
2906
 
 
2907
 
  return CURLM_OK;
2908
 
}
2909
 
 
2910
2660
#ifdef DEBUGBUILD
2911
2661
void Curl_multi_dump(const struct Curl_multi *multi_handle)
2912
2662
{