Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl / share.c
blob7f2cb5ebb9962dffa4a4204f46916bf4a5dc85a2
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2004, 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.2 2007/03/15 19:22:13 andy 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 CURLSHcode
50 curl_share_setopt(CURLSH *sh, CURLSHoption option, ...)
52 struct Curl_share *share = (struct Curl_share *)sh;
53 va_list param;
54 int type;
55 curl_lock_function lockfunc;
56 curl_unlock_function unlockfunc;
57 void *ptr;
59 if (share->dirty)
60 /* don't allow setting options while one or more handles are already
61 using this share */
62 return CURLSHE_IN_USE;
64 va_start(param, option);
66 switch(option) {
67 case CURLSHOPT_SHARE:
68 /* this is a type this share will share */
69 type = va_arg(param, int);
70 share->specifier |= (1<<type);
71 switch( type ) {
72 case CURL_LOCK_DATA_DNS:
73 if (!share->hostcache) {
74 share->hostcache = Curl_mk_dnscache();
75 if(!share->hostcache)
76 return CURLSHE_NOMEM;
78 break;
80 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
81 case CURL_LOCK_DATA_COOKIE:
82 if (!share->cookies) {
83 share->cookies = Curl_cookie_init(NULL, NULL, NULL, TRUE );
84 if(!share->cookies)
85 return CURLSHE_NOMEM;
87 break;
88 #endif /* CURL_DISABLE_HTTP */
90 case CURL_LOCK_DATA_SSL_SESSION: /* not supported (yet) */
91 case CURL_LOCK_DATA_CONNECT: /* not supported (yet) */
93 default:
94 return CURLSHE_BAD_OPTION;
96 break;
98 case CURLSHOPT_UNSHARE:
99 /* this is a type this share will no longer share */
100 type = va_arg(param, int);
101 share->specifier &= ~(1<<type);
102 switch( type )
104 case CURL_LOCK_DATA_DNS:
105 if (share->hostcache) {
106 Curl_hash_destroy(share->hostcache);
107 share->hostcache = NULL;
109 break;
111 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
112 case CURL_LOCK_DATA_COOKIE:
113 if (share->cookies) {
114 Curl_cookie_cleanup(share->cookies);
115 share->cookies = NULL;
117 break;
118 #endif /* CURL_DISABLE_HTTP */
120 case CURL_LOCK_DATA_SSL_SESSION:
121 break;
123 case CURL_LOCK_DATA_CONNECT:
124 break;
126 default:
127 return CURLSHE_BAD_OPTION;
129 break;
131 case CURLSHOPT_LOCKFUNC:
132 lockfunc = va_arg(param, curl_lock_function);
133 share->lockfunc = lockfunc;
134 break;
136 case CURLSHOPT_UNLOCKFUNC:
137 unlockfunc = va_arg(param, curl_unlock_function);
138 share->unlockfunc = unlockfunc;
139 break;
141 case CURLSHOPT_USERDATA:
142 ptr = va_arg(param, void *);
143 share->clientdata = ptr;
144 break;
146 default:
147 return CURLSHE_BAD_OPTION;
150 return CURLSHE_OK;
153 CURLSHcode
154 curl_share_cleanup(CURLSH *sh)
156 struct Curl_share *share = (struct Curl_share *)sh;
158 if (share == NULL)
159 return CURLSHE_INVALID;
161 if(share->lockfunc)
162 share->lockfunc(NULL, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE,
163 share->clientdata);
165 if (share->dirty) {
166 if(share->unlockfunc)
167 share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
168 return CURLSHE_IN_USE;
171 if(share->hostcache)
172 Curl_hash_destroy(share->hostcache);
174 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
175 if(share->cookies)
176 Curl_cookie_cleanup(share->cookies);
177 #endif /* CURL_DISABLE_HTTP */
179 if(share->unlockfunc)
180 share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
181 free(share);
183 return CURLSHE_OK;
187 CURLSHcode
188 Curl_share_lock(struct SessionHandle *data, curl_lock_data type,
189 curl_lock_access accesstype)
191 struct Curl_share *share = data->share;
193 if (share == NULL)
194 return CURLSHE_INVALID;
196 if(share->specifier & (1<<type)) {
197 if(share->lockfunc) /* only call this if set! */
198 share->lockfunc(data, type, accesstype, share->clientdata);
200 /* else if we don't share this, pretend successful lock */
202 return CURLSHE_OK;
205 CURLSHcode
206 Curl_share_unlock(struct SessionHandle *data, curl_lock_data type)
208 struct Curl_share *share = data->share;
210 if (share == NULL)
211 return CURLSHE_INVALID;
213 if(share->specifier & (1<<type)) {
214 if(share->unlockfunc) /* only call this if set! */
215 share->unlockfunc (data, type, share->clientdata);
218 return CURLSHE_OK;