Git init
[external/curl.git] / lib / share.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2009, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
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.
13  *
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.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  ***************************************************************************/
22
23 #include "setup.h"
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <curl/curl.h>
28 #include "urldata.h"
29 #include "share.h"
30 #include "curl_memory.h"
31
32 /* The last #include file should be: */
33 #include "memdebug.h"
34
35 CURLSH *
36 curl_share_init(void)
37 {
38   struct Curl_share *share = calloc(1, sizeof(struct Curl_share));
39   if(share)
40     share->specifier |= (1<<CURL_LOCK_DATA_SHARE);
41
42   return share;
43 }
44
45 #undef curl_share_setopt
46 CURLSHcode
47 curl_share_setopt(CURLSH *sh, CURLSHoption option, ...)
48 {
49   struct Curl_share *share = (struct Curl_share *)sh;
50   va_list param;
51   int type;
52   curl_lock_function lockfunc;
53   curl_unlock_function unlockfunc;
54   void *ptr;
55
56   if(share->dirty)
57     /* don't allow setting options while one or more handles are already
58        using this share */
59     return CURLSHE_IN_USE;
60
61   va_start(param, option);
62
63   switch(option) {
64   case CURLSHOPT_SHARE:
65     /* this is a type this share will share */
66     type = va_arg(param, int);
67     share->specifier |= (1<<type);
68     switch( type ) {
69     case CURL_LOCK_DATA_DNS:
70       if(!share->hostcache) {
71         share->hostcache = Curl_mk_dnscache();
72         if(!share->hostcache)
73           return CURLSHE_NOMEM;
74       }
75       break;
76
77 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
78     case CURL_LOCK_DATA_COOKIE:
79       if(!share->cookies) {
80         share->cookies = Curl_cookie_init(NULL, NULL, NULL, TRUE );
81         if(!share->cookies)
82           return CURLSHE_NOMEM;
83       }
84       break;
85 #endif   /* CURL_DISABLE_HTTP */
86
87     case CURL_LOCK_DATA_SSL_SESSION: /* not supported (yet) */
88     case CURL_LOCK_DATA_CONNECT:     /* not supported (yet) */
89
90     default:
91       return CURLSHE_BAD_OPTION;
92     }
93     break;
94
95   case CURLSHOPT_UNSHARE:
96     /* this is a type this share will no longer share */
97     type = va_arg(param, int);
98     share->specifier &= ~(1<<type);
99     switch( type )
100     {
101       case CURL_LOCK_DATA_DNS:
102         if(share->hostcache) {
103           Curl_hash_destroy(share->hostcache);
104           share->hostcache = NULL;
105         }
106         break;
107
108 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
109       case CURL_LOCK_DATA_COOKIE:
110         if(share->cookies) {
111           Curl_cookie_cleanup(share->cookies);
112           share->cookies = NULL;
113         }
114         break;
115 #endif   /* CURL_DISABLE_HTTP */
116
117       case CURL_LOCK_DATA_SSL_SESSION:
118         break;
119
120       case CURL_LOCK_DATA_CONNECT:
121         break;
122
123       default:
124         return CURLSHE_BAD_OPTION;
125     }
126     break;
127
128   case CURLSHOPT_LOCKFUNC:
129     lockfunc = va_arg(param, curl_lock_function);
130     share->lockfunc = lockfunc;
131     break;
132
133   case CURLSHOPT_UNLOCKFUNC:
134     unlockfunc = va_arg(param, curl_unlock_function);
135     share->unlockfunc = unlockfunc;
136     break;
137
138   case CURLSHOPT_USERDATA:
139     ptr = va_arg(param, void *);
140     share->clientdata = ptr;
141     break;
142
143   default:
144     return CURLSHE_BAD_OPTION;
145   }
146
147   return CURLSHE_OK;
148 }
149
150 CURLSHcode
151 curl_share_cleanup(CURLSH *sh)
152 {
153   struct Curl_share *share = (struct Curl_share *)sh;
154
155   if(share == NULL)
156     return CURLSHE_INVALID;
157
158   if(share->lockfunc)
159     share->lockfunc(NULL, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE,
160                     share->clientdata);
161
162   if(share->dirty) {
163     if(share->unlockfunc)
164       share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
165     return CURLSHE_IN_USE;
166   }
167
168   if(share->hostcache) {
169     Curl_hash_destroy(share->hostcache);
170     share->hostcache = NULL;
171   }
172
173 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
174   if(share->cookies)
175     Curl_cookie_cleanup(share->cookies);
176 #endif   /* CURL_DISABLE_HTTP */
177
178   if(share->unlockfunc)
179     share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
180   free(share);
181
182   return CURLSHE_OK;
183 }
184
185
186 CURLSHcode
187 Curl_share_lock(struct SessionHandle *data, curl_lock_data type,
188                 curl_lock_access accesstype)
189 {
190   struct Curl_share *share = data->share;
191
192   if(share == NULL)
193     return CURLSHE_INVALID;
194
195   if(share->specifier & (1<<type)) {
196     if(share->lockfunc) /* only call this if set! */
197       share->lockfunc(data, type, accesstype, share->clientdata);
198   }
199   /* else if we don't share this, pretend successful lock */
200
201   return CURLSHE_OK;
202 }
203
204 CURLSHcode
205 Curl_share_unlock(struct SessionHandle *data, curl_lock_data type)
206 {
207   struct Curl_share *share = data->share;
208
209   if(share == NULL)
210     return CURLSHE_INVALID;
211
212   if(share->specifier & (1<<type)) {
213     if(share->unlockfunc) /* only call this if set! */
214       share->unlockfunc (data, type, share->clientdata);
215   }
216
217   return CURLSHE_OK;
218 }