2012-12-06 06:12:04 -05:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012, Linus Nielsen Feltzing, <linus@haxx.se>
|
2014-01-04 12:41:10 -05:00
|
|
|
* Copyright (C) 2012 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2012-12-06 06:12:04 -05:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
|
|
|
* are also available at http://curl.haxx.se/docs/copyright.html.
|
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
2013-01-06 13:06:49 -05:00
|
|
|
#include "curl_setup.h"
|
2012-12-06 06:12:04 -05:00
|
|
|
|
|
|
|
#include <curl/curl.h>
|
|
|
|
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "urldata.h"
|
|
|
|
#include "url.h"
|
|
|
|
#include "progress.h"
|
|
|
|
#include "multiif.h"
|
|
|
|
#include "sendf.h"
|
|
|
|
#include "rawstr.h"
|
|
|
|
#include "bundles.h"
|
|
|
|
#include "conncache.h"
|
2012-12-06 06:12:04 -05:00
|
|
|
|
|
|
|
#include "curl_memory.h"
|
|
|
|
/* The last #include file should be: */
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "memdebug.h"
|
2012-12-06 06:12:04 -05:00
|
|
|
|
|
|
|
static void free_bundle_hash_entry(void *freethis)
|
|
|
|
{
|
|
|
|
struct connectbundle *b = (struct connectbundle *) freethis;
|
|
|
|
|
|
|
|
Curl_bundle_destroy(b);
|
|
|
|
}
|
|
|
|
|
2013-04-26 16:23:08 -04:00
|
|
|
struct conncache *Curl_conncache_init(int size)
|
2012-12-06 06:12:04 -05:00
|
|
|
{
|
|
|
|
struct conncache *connc;
|
|
|
|
|
|
|
|
connc = calloc(1, sizeof(struct conncache));
|
|
|
|
if(!connc)
|
|
|
|
return NULL;
|
|
|
|
|
2013-04-26 16:23:08 -04:00
|
|
|
connc->hash = Curl_hash_alloc(size, Curl_hash_str,
|
2012-12-06 06:12:04 -05:00
|
|
|
Curl_str_key_compare, free_bundle_hash_entry);
|
|
|
|
|
|
|
|
if(!connc->hash) {
|
|
|
|
free(connc);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return connc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Curl_conncache_destroy(struct conncache *connc)
|
|
|
|
{
|
2012-12-19 13:52:11 -05:00
|
|
|
if(connc) {
|
|
|
|
Curl_hash_destroy(connc->hash);
|
|
|
|
connc->hash = NULL;
|
|
|
|
free(connc);
|
|
|
|
}
|
2012-12-06 06:12:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct connectbundle *Curl_conncache_find_bundle(struct conncache *connc,
|
|
|
|
char *hostname)
|
|
|
|
{
|
|
|
|
struct connectbundle *bundle = NULL;
|
|
|
|
|
|
|
|
if(connc)
|
|
|
|
bundle = Curl_hash_pick(connc->hash, hostname, strlen(hostname)+1);
|
|
|
|
|
|
|
|
return bundle;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool conncache_add_bundle(struct conncache *connc,
|
|
|
|
char *hostname,
|
|
|
|
struct connectbundle *bundle)
|
|
|
|
{
|
|
|
|
void *p;
|
|
|
|
|
|
|
|
p = Curl_hash_add(connc->hash, hostname, strlen(hostname)+1, bundle);
|
|
|
|
|
|
|
|
return p?TRUE:FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void conncache_remove_bundle(struct conncache *connc,
|
|
|
|
struct connectbundle *bundle)
|
|
|
|
{
|
|
|
|
struct curl_hash_iterator iter;
|
|
|
|
struct curl_hash_element *he;
|
|
|
|
|
|
|
|
if(!connc)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Curl_hash_start_iterate(connc->hash, &iter);
|
|
|
|
|
|
|
|
he = Curl_hash_next_element(&iter);
|
|
|
|
while(he) {
|
|
|
|
if(he->ptr == bundle) {
|
|
|
|
/* The bundle is destroyed by the hash destructor function,
|
|
|
|
free_bundle_hash_entry() */
|
|
|
|
Curl_hash_delete(connc->hash, he->key, he->key_len);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
he = Curl_hash_next_element(&iter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CURLcode Curl_conncache_add_conn(struct conncache *connc,
|
|
|
|
struct connectdata *conn)
|
|
|
|
{
|
|
|
|
CURLcode result;
|
|
|
|
struct connectbundle *bundle;
|
2012-12-19 13:52:11 -05:00
|
|
|
struct connectbundle *new_bundle = NULL;
|
2012-12-06 06:12:04 -05:00
|
|
|
struct SessionHandle *data = conn->data;
|
|
|
|
|
|
|
|
bundle = Curl_conncache_find_bundle(data->state.conn_cache,
|
|
|
|
conn->host.name);
|
|
|
|
if(!bundle) {
|
2012-12-19 13:52:11 -05:00
|
|
|
result = Curl_bundle_create(data, &new_bundle);
|
2014-10-23 16:56:35 -04:00
|
|
|
if(result)
|
2012-12-06 06:12:04 -05:00
|
|
|
return result;
|
|
|
|
|
|
|
|
if(!conncache_add_bundle(data->state.conn_cache,
|
2012-12-19 13:52:11 -05:00
|
|
|
conn->host.name, new_bundle)) {
|
|
|
|
Curl_bundle_destroy(new_bundle);
|
2012-12-06 06:12:04 -05:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2012-12-19 13:52:11 -05:00
|
|
|
}
|
|
|
|
bundle = new_bundle;
|
2012-12-06 06:12:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
result = Curl_bundle_add_conn(bundle, conn);
|
2014-10-23 16:56:35 -04:00
|
|
|
if(result) {
|
2012-12-19 13:52:11 -05:00
|
|
|
if(new_bundle)
|
|
|
|
conncache_remove_bundle(data->state.conn_cache, new_bundle);
|
2012-12-06 06:12:04 -05:00
|
|
|
return result;
|
2012-12-19 13:52:11 -05:00
|
|
|
}
|
2012-12-06 06:12:04 -05:00
|
|
|
|
2014-06-12 14:36:41 -04:00
|
|
|
conn->connection_id = connc->next_connection_id++;
|
2012-12-06 06:12:04 -05:00
|
|
|
connc->num_connections++;
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Curl_conncache_remove_conn(struct conncache *connc,
|
|
|
|
struct connectdata *conn)
|
|
|
|
{
|
|
|
|
struct connectbundle *bundle = conn->bundle;
|
|
|
|
|
|
|
|
/* The bundle pointer can be NULL, since this function can be called
|
|
|
|
due to a failed connection attempt, before being added to a bundle */
|
|
|
|
if(bundle) {
|
|
|
|
Curl_bundle_remove_conn(bundle, conn);
|
|
|
|
if(bundle->num_connections == 0) {
|
|
|
|
conncache_remove_bundle(connc, bundle);
|
|
|
|
}
|
|
|
|
|
2014-01-04 10:34:58 -05:00
|
|
|
if(connc) {
|
|
|
|
connc->num_connections--;
|
|
|
|
|
|
|
|
DEBUGF(infof(conn->data, "The cache now contains %d members\n",
|
|
|
|
connc->num_connections));
|
|
|
|
}
|
2012-12-06 06:12:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This function iterates the entire connection cache and calls the
|
|
|
|
function func() with the connection pointer as the first argument
|
2012-12-11 14:13:29 -05:00
|
|
|
and the supplied 'param' argument as the other,
|
|
|
|
|
|
|
|
Return 0 from func() to continue the loop, return 1 to abort it.
|
|
|
|
*/
|
2012-12-06 06:12:04 -05:00
|
|
|
void Curl_conncache_foreach(struct conncache *connc,
|
|
|
|
void *param,
|
2012-12-11 14:13:29 -05:00
|
|
|
int (*func)(struct connectdata *conn, void *param))
|
2012-12-06 06:12:04 -05:00
|
|
|
{
|
|
|
|
struct curl_hash_iterator iter;
|
|
|
|
struct curl_llist_element *curr;
|
|
|
|
struct curl_hash_element *he;
|
|
|
|
|
|
|
|
if(!connc)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Curl_hash_start_iterate(connc->hash, &iter);
|
|
|
|
|
|
|
|
he = Curl_hash_next_element(&iter);
|
|
|
|
while(he) {
|
|
|
|
struct connectbundle *bundle;
|
|
|
|
|
|
|
|
bundle = he->ptr;
|
2014-08-10 18:06:20 -04:00
|
|
|
he = Curl_hash_next_element(&iter);
|
2012-12-06 06:12:04 -05:00
|
|
|
|
|
|
|
curr = bundle->conn_list->head;
|
|
|
|
while(curr) {
|
|
|
|
/* Yes, we need to update curr before calling func(), because func()
|
|
|
|
might decide to remove the connection */
|
2014-10-14 03:44:06 -04:00
|
|
|
struct connectdata *conn = curr->ptr;
|
2012-12-06 06:12:04 -05:00
|
|
|
curr = curr->next;
|
|
|
|
|
2012-12-11 14:13:29 -05:00
|
|
|
if(1 == func(conn, param))
|
|
|
|
return;
|
2012-12-06 06:12:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the first connection found in the cache. Used when closing all
|
|
|
|
connections */
|
|
|
|
struct connectdata *
|
|
|
|
Curl_conncache_find_first_connection(struct conncache *connc)
|
|
|
|
{
|
|
|
|
struct curl_hash_iterator iter;
|
|
|
|
struct curl_hash_element *he;
|
|
|
|
struct connectbundle *bundle;
|
|
|
|
|
|
|
|
Curl_hash_start_iterate(connc->hash, &iter);
|
|
|
|
|
|
|
|
he = Curl_hash_next_element(&iter);
|
|
|
|
while(he) {
|
2014-10-14 03:44:06 -04:00
|
|
|
struct curl_llist_element *curr;
|
2012-12-06 06:12:04 -05:00
|
|
|
bundle = he->ptr;
|
|
|
|
|
|
|
|
curr = bundle->conn_list->head;
|
|
|
|
if(curr) {
|
|
|
|
return curr->ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
he = Curl_hash_next_element(&iter);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* Useful for debugging the connection cache */
|
|
|
|
void Curl_conncache_print(struct conncache *connc)
|
|
|
|
{
|
|
|
|
struct curl_hash_iterator iter;
|
|
|
|
struct curl_llist_element *curr;
|
|
|
|
struct curl_hash_element *he;
|
|
|
|
|
|
|
|
if(!connc)
|
|
|
|
return;
|
|
|
|
|
|
|
|
fprintf(stderr, "=Bundle cache=\n");
|
|
|
|
|
|
|
|
Curl_hash_start_iterate(connc->hash, &iter);
|
|
|
|
|
|
|
|
he = Curl_hash_next_element(&iter);
|
|
|
|
while(he) {
|
|
|
|
struct connectbundle *bundle;
|
|
|
|
struct connectdata *conn;
|
|
|
|
|
|
|
|
bundle = he->ptr;
|
|
|
|
|
|
|
|
fprintf(stderr, "%s -", he->key);
|
|
|
|
curr = bundle->conn_list->head;
|
|
|
|
while(curr) {
|
|
|
|
conn = curr->ptr;
|
|
|
|
|
|
|
|
fprintf(stderr, " [%p %d]", (void *)conn, conn->inuse);
|
|
|
|
curr = curr->next;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
|
|
|
he = Curl_hash_next_element(&iter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|