Dan Fandrich added the --disable-cookies option to configure to build
[platform/upstream/curl.git] / lib / share.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2004, 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  * $Id$
22  ***************************************************************************/
23
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"
32
33 /* The last #include file should be: */
34 #include "memdebug.h"
35
36 CURLSH *
37 curl_share_init(void)
38 {
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);
44   }
45
46   return share;
47 }
48
49 CURLSHcode
50 curl_share_setopt(CURLSH *sh, CURLSHoption option, ...)
51 {
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;
58
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;
63
64   va_start(param, option);
65
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;
77       }
78       break;
79
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;
86       }
87       break;
88 #endif   /* CURL_DISABLE_HTTP */
89
90     case CURL_LOCK_DATA_SSL_SESSION: /* not supported (yet) */
91     case CURL_LOCK_DATA_CONNECT:     /* not supported (yet) */
92
93     default:
94       return CURLSHE_BAD_OPTION;
95     }
96     break;
97
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 )
103     {
104       case CURL_LOCK_DATA_DNS:
105         if (share->hostcache) {
106           Curl_hash_destroy(share->hostcache);
107           share->hostcache = NULL;
108         }
109         break;
110
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;
116         }
117         break;
118 #endif   /* CURL_DISABLE_HTTP */
119
120       case CURL_LOCK_DATA_SSL_SESSION:
121         break;
122
123       case CURL_LOCK_DATA_CONNECT:
124         break;
125
126       default:
127         return CURLSHE_BAD_OPTION;
128     }
129     break;
130
131   case CURLSHOPT_LOCKFUNC:
132     lockfunc = va_arg(param, curl_lock_function);
133     share->lockfunc = lockfunc;
134     break;
135
136   case CURLSHOPT_UNLOCKFUNC:
137     unlockfunc = va_arg(param, curl_unlock_function);
138     share->unlockfunc = unlockfunc;
139     break;
140
141   case CURLSHOPT_USERDATA:
142     ptr = va_arg(param, void *);
143     share->clientdata = ptr;
144     break;
145
146   default:
147     return CURLSHE_BAD_OPTION;
148   }
149
150   return CURLSHE_OK;
151 }
152
153 CURLSHcode
154 curl_share_cleanup(CURLSH *sh)
155 {
156   struct Curl_share *share = (struct Curl_share *)sh;
157
158   if (share == NULL)
159     return CURLSHE_INVALID;
160
161   if(share->lockfunc)
162     share->lockfunc(NULL, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE,
163                     share->clientdata);
164
165   if (share->dirty) {
166     if(share->unlockfunc)
167       share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
168     return CURLSHE_IN_USE;
169   }
170
171   if(share->hostcache)
172     Curl_hash_destroy(share->hostcache);
173
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 */
178
179   if(share->unlockfunc)
180     share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
181   free(share);
182
183   return CURLSHE_OK;
184 }
185
186
187 CURLSHcode
188 Curl_share_lock(struct SessionHandle *data, curl_lock_data type,
189                 curl_lock_access accesstype)
190 {
191   struct Curl_share *share = data->share;
192
193   if (share == NULL)
194     return CURLSHE_INVALID;
195
196   if(share->specifier & (1<<type)) {
197     if(share->lockfunc) /* only call this if set! */
198       share->lockfunc(data, type, accesstype, share->clientdata);
199   }
200   /* else if we don't share this, pretend successful lock */
201
202   return CURLSHE_OK;
203 }
204
205 CURLSHcode
206 Curl_share_unlock(struct SessionHandle *data, curl_lock_data type)
207 {
208   struct Curl_share *share = data->share;
209
210   if (share == NULL)
211     return CURLSHE_INVALID;
212
213   if(share->specifier & (1<<type)) {
214     if(share->unlockfunc) /* only call this if set! */
215       share->unlockfunc (data, type, share->clientdata);
216   }
217
218   return CURLSHE_OK;
219 }