ENH: import of new curl version
[cmake.git] / Utilities / cmcurl-7.19.0 / lib / share.c
blobefe6a6591e4654cb5e0dc53ae021973a6fdf67b1
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2007, Daniel Stenberg, <daniel@haxx.se>, et al.
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 http://curl.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * $Id: share.c,v 1.1.1.1 2008-09-23 16:32:05 hoffman Exp $
22 ***************************************************************************/
24 #include "setup.h"
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <curl/curl.h>
29 #include "urldata.h"
30 #include "share.h"
31 #include "memory.h"
33 /* The last #include file should be: */
34 #include "memdebug.h"
36 CURLSH *
37 curl_share_init(void)
39 struct Curl_share *share =
40 (struct Curl_share *)malloc(sizeof(struct Curl_share));
41 if(share) {
42 memset (share, 0, sizeof(struct Curl_share));
43 share->specifier |= (1<<CURL_LOCK_DATA_SHARE);
46 return share;
49 #undef curl_share_setopt
50 CURLSHcode
51 curl_share_setopt(CURLSH *sh, CURLSHoption option, ...)
53 struct Curl_share *share = (struct Curl_share *)sh;
54 va_list param;
55 int type;
56 curl_lock_function lockfunc;
57 curl_unlock_function unlockfunc;
58 void *ptr;
60 if(share->dirty)
61 /* don't allow setting options while one or more handles are already
62 using this share */
63 return CURLSHE_IN_USE;
65 va_start(param, option);
67 switch(option) {
68 case CURLSHOPT_SHARE:
69 /* this is a type this share will share */
70 type = va_arg(param, int);
71 share->specifier |= (1<<type);
72 switch( type ) {
73 case CURL_LOCK_DATA_DNS:
74 if(!share->hostcache) {
75 share->hostcache = Curl_mk_dnscache();
76 if(!share->hostcache)
77 return CURLSHE_NOMEM;
79 break;
81 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
82 case CURL_LOCK_DATA_COOKIE:
83 if(!share->cookies) {
84 share->cookies = Curl_cookie_init(NULL, NULL, NULL, TRUE );
85 if(!share->cookies)
86 return CURLSHE_NOMEM;
88 break;
89 #endif /* CURL_DISABLE_HTTP */
91 case CURL_LOCK_DATA_SSL_SESSION: /* not supported (yet) */
92 case CURL_LOCK_DATA_CONNECT: /* not supported (yet) */
94 default:
95 return CURLSHE_BAD_OPTION;
97 break;
99 case CURLSHOPT_UNSHARE:
100 /* this is a type this share will no longer share */
101 type = va_arg(param, int);
102 share->specifier &= ~(1<<type);
103 switch( type )
105 case CURL_LOCK_DATA_DNS:
106 if(share->hostcache) {
107 Curl_hash_destroy(share->hostcache);
108 share->hostcache = NULL;
110 break;
112 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
113 case CURL_LOCK_DATA_COOKIE:
114 if(share->cookies) {
115 Curl_cookie_cleanup(share->cookies);
116 share->cookies = NULL;
118 break;
119 #endif /* CURL_DISABLE_HTTP */
121 case CURL_LOCK_DATA_SSL_SESSION:
122 break;
124 case CURL_LOCK_DATA_CONNECT:
125 break;
127 default:
128 return CURLSHE_BAD_OPTION;
130 break;
132 case CURLSHOPT_LOCKFUNC:
133 lockfunc = va_arg(param, curl_lock_function);
134 share->lockfunc = lockfunc;
135 break;
137 case CURLSHOPT_UNLOCKFUNC:
138 unlockfunc = va_arg(param, curl_unlock_function);
139 share->unlockfunc = unlockfunc;
140 break;
142 case CURLSHOPT_USERDATA:
143 ptr = va_arg(param, void *);
144 share->clientdata = ptr;
145 break;
147 default:
148 return CURLSHE_BAD_OPTION;
151 return CURLSHE_OK;
154 CURLSHcode
155 curl_share_cleanup(CURLSH *sh)
157 struct Curl_share *share = (struct Curl_share *)sh;
159 if(share == NULL)
160 return CURLSHE_INVALID;
162 if(share->lockfunc)
163 share->lockfunc(NULL, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE,
164 share->clientdata);
166 if(share->dirty) {
167 if(share->unlockfunc)
168 share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
169 return CURLSHE_IN_USE;
172 if(share->hostcache)
173 Curl_hash_destroy(share->hostcache);
175 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
176 if(share->cookies)
177 Curl_cookie_cleanup(share->cookies);
178 #endif /* CURL_DISABLE_HTTP */
180 if(share->unlockfunc)
181 share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
182 free(share);
184 return CURLSHE_OK;
188 CURLSHcode
189 Curl_share_lock(struct SessionHandle *data, curl_lock_data type,
190 curl_lock_access accesstype)
192 struct Curl_share *share = data->share;
194 if(share == NULL)
195 return CURLSHE_INVALID;
197 if(share->specifier & (1<<type)) {
198 if(share->lockfunc) /* only call this if set! */
199 share->lockfunc(data, type, accesstype, share->clientdata);
201 /* else if we don't share this, pretend successful lock */
203 return CURLSHE_OK;
206 CURLSHcode
207 Curl_share_unlock(struct SessionHandle *data, curl_lock_data type)
209 struct Curl_share *share = data->share;
211 if(share == NULL)
212 return CURLSHE_INVALID;
214 if(share->specifier & (1<<type)) {
215 if(share->unlockfunc) /* only call this if set! */
216 share->unlockfunc (data, type, share->clientdata);
219 return CURLSHE_OK;