00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "setup.h"
00025 #include <stdarg.h>
00026 #include <stdlib.h>
00027 #include <string.h>
00028 #include <curl/curl.h>
00029 #include "urldata.h"
00030 #include "share.h"
00031 #include "memory.h"
00032
00033
00034 #include "memdebug.h"
00035
00036 CURLSH *
00037 curl_share_init(void)
00038 {
00039 struct Curl_share *share =
00040 (struct Curl_share *)malloc(sizeof(struct Curl_share));
00041 if (share) {
00042 memset (share, 0, sizeof(struct Curl_share));
00043 share->specifier |= (1<<CURL_LOCK_DATA_SHARE);
00044 }
00045
00046 return share;
00047 }
00048
00049 CURLSHcode
00050 curl_share_setopt(CURLSH *sh, CURLSHoption option, ...)
00051 {
00052 struct Curl_share *share = (struct Curl_share *)sh;
00053 va_list param;
00054 int type;
00055 curl_lock_function lockfunc;
00056 curl_unlock_function unlockfunc;
00057 void *ptr;
00058
00059 if (share->dirty)
00060
00061
00062 return CURLSHE_IN_USE;
00063
00064 va_start(param, option);
00065
00066 switch(option) {
00067 case CURLSHOPT_SHARE:
00068
00069 type = va_arg(param, int);
00070 share->specifier |= (1<<type);
00071 switch( type ) {
00072 case CURL_LOCK_DATA_DNS:
00073 if (!share->hostcache) {
00074 share->hostcache = Curl_mk_dnscache();
00075 if(!share->hostcache)
00076 return CURLSHE_NOMEM;
00077 }
00078 break;
00079
00080 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
00081 case CURL_LOCK_DATA_COOKIE:
00082 if (!share->cookies) {
00083 share->cookies = Curl_cookie_init(NULL, NULL, NULL, TRUE );
00084 if(!share->cookies)
00085 return CURLSHE_NOMEM;
00086 }
00087 break;
00088 #endif
00089
00090 case CURL_LOCK_DATA_SSL_SESSION:
00091 case CURL_LOCK_DATA_CONNECT:
00092
00093 default:
00094 return CURLSHE_BAD_OPTION;
00095 }
00096 break;
00097
00098 case CURLSHOPT_UNSHARE:
00099
00100 type = va_arg(param, int);
00101 share->specifier &= ~(1<<type);
00102 switch( type )
00103 {
00104 case CURL_LOCK_DATA_DNS:
00105 if (share->hostcache) {
00106 Curl_hash_destroy(share->hostcache);
00107 share->hostcache = NULL;
00108 }
00109 break;
00110
00111 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
00112 case CURL_LOCK_DATA_COOKIE:
00113 if (share->cookies) {
00114 Curl_cookie_cleanup(share->cookies);
00115 share->cookies = NULL;
00116 }
00117 break;
00118 #endif
00119
00120 case CURL_LOCK_DATA_SSL_SESSION:
00121 break;
00122
00123 case CURL_LOCK_DATA_CONNECT:
00124 break;
00125
00126 default:
00127 return CURLSHE_BAD_OPTION;
00128 }
00129 break;
00130
00131 case CURLSHOPT_LOCKFUNC:
00132 lockfunc = va_arg(param, curl_lock_function);
00133 share->lockfunc = lockfunc;
00134 break;
00135
00136 case CURLSHOPT_UNLOCKFUNC:
00137 unlockfunc = va_arg(param, curl_unlock_function);
00138 share->unlockfunc = unlockfunc;
00139 break;
00140
00141 case CURLSHOPT_USERDATA:
00142 ptr = va_arg(param, void *);
00143 share->clientdata = ptr;
00144 break;
00145
00146 default:
00147 return CURLSHE_BAD_OPTION;
00148 }
00149
00150 return CURLSHE_OK;
00151 }
00152
00153 CURLSHcode
00154 curl_share_cleanup(CURLSH *sh)
00155 {
00156 struct Curl_share *share = (struct Curl_share *)sh;
00157
00158 if (share == NULL)
00159 return CURLSHE_INVALID;
00160
00161 if(share->lockfunc)
00162 share->lockfunc(NULL, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE,
00163 share->clientdata);
00164
00165 if (share->dirty) {
00166 if(share->unlockfunc)
00167 share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
00168 return CURLSHE_IN_USE;
00169 }
00170
00171 if(share->hostcache)
00172 Curl_hash_destroy(share->hostcache);
00173
00174 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
00175 if(share->cookies)
00176 Curl_cookie_cleanup(share->cookies);
00177 #endif
00178
00179 if(share->unlockfunc)
00180 share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
00181 free(share);
00182
00183 return CURLSHE_OK;
00184 }
00185
00186
00187 CURLSHcode
00188 Curl_share_lock(struct SessionHandle *data, curl_lock_data type,
00189 curl_lock_access accesstype)
00190 {
00191 struct Curl_share *share = data->share;
00192
00193 if (share == NULL)
00194 return CURLSHE_INVALID;
00195
00196 if(share->specifier & (1<<type)) {
00197 if(share->lockfunc)
00198 share->lockfunc(data, type, accesstype, share->clientdata);
00199 }
00200
00201
00202 return CURLSHE_OK;
00203 }
00204
00205 CURLSHcode
00206 Curl_share_unlock(struct SessionHandle *data, curl_lock_data type)
00207 {
00208 struct Curl_share *share = data->share;
00209
00210 if (share == NULL)
00211 return CURLSHE_INVALID;
00212
00213 if(share->specifier & (1<<type)) {
00214 if(share->unlockfunc)
00215 share->unlockfunc (data, type, share->clientdata);
00216 }
00217
00218 return CURLSHE_OK;
00219 }