share.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. * $Id$
  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. /* The last #include file should be: */
  32. #include "memdebug.h"
  33. CURLSH *
  34. curl_share_init(void)
  35. {
  36. struct Curl_share *share = calloc(1, sizeof(struct Curl_share));
  37. if(share)
  38. share->specifier |= (1<<CURL_LOCK_DATA_SHARE);
  39. return share;
  40. }
  41. #undef curl_share_setopt
  42. CURLSHcode
  43. curl_share_setopt(CURLSH *sh, CURLSHoption option, ...)
  44. {
  45. struct Curl_share *share = (struct Curl_share *)sh;
  46. va_list param;
  47. int type;
  48. curl_lock_function lockfunc;
  49. curl_unlock_function unlockfunc;
  50. void *ptr;
  51. if(share->dirty)
  52. /* don't allow setting options while one or more handles are already
  53. using this share */
  54. return CURLSHE_IN_USE;
  55. va_start(param, option);
  56. switch(option) {
  57. case CURLSHOPT_SHARE:
  58. /* this is a type this share will share */
  59. type = va_arg(param, int);
  60. share->specifier |= (1<<type);
  61. switch( type ) {
  62. case CURL_LOCK_DATA_DNS:
  63. if(!share->hostcache) {
  64. share->hostcache = Curl_mk_dnscache();
  65. if(!share->hostcache)
  66. return CURLSHE_NOMEM;
  67. }
  68. break;
  69. #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
  70. case CURL_LOCK_DATA_COOKIE:
  71. if(!share->cookies) {
  72. share->cookies = Curl_cookie_init(NULL, NULL, NULL, TRUE );
  73. if(!share->cookies)
  74. return CURLSHE_NOMEM;
  75. }
  76. break;
  77. #endif /* CURL_DISABLE_HTTP */
  78. case CURL_LOCK_DATA_SSL_SESSION: /* not supported (yet) */
  79. case CURL_LOCK_DATA_CONNECT: /* not supported (yet) */
  80. default:
  81. return CURLSHE_BAD_OPTION;
  82. }
  83. break;
  84. case CURLSHOPT_UNSHARE:
  85. /* this is a type this share will no longer share */
  86. type = va_arg(param, int);
  87. share->specifier &= ~(1<<type);
  88. switch( type )
  89. {
  90. case CURL_LOCK_DATA_DNS:
  91. if(share->hostcache) {
  92. Curl_hash_destroy(share->hostcache);
  93. share->hostcache = NULL;
  94. }
  95. break;
  96. #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
  97. case CURL_LOCK_DATA_COOKIE:
  98. if(share->cookies) {
  99. Curl_cookie_cleanup(share->cookies);
  100. share->cookies = NULL;
  101. }
  102. break;
  103. #endif /* CURL_DISABLE_HTTP */
  104. case CURL_LOCK_DATA_SSL_SESSION:
  105. break;
  106. case CURL_LOCK_DATA_CONNECT:
  107. break;
  108. default:
  109. return CURLSHE_BAD_OPTION;
  110. }
  111. break;
  112. case CURLSHOPT_LOCKFUNC:
  113. lockfunc = va_arg(param, curl_lock_function);
  114. share->lockfunc = lockfunc;
  115. break;
  116. case CURLSHOPT_UNLOCKFUNC:
  117. unlockfunc = va_arg(param, curl_unlock_function);
  118. share->unlockfunc = unlockfunc;
  119. break;
  120. case CURLSHOPT_USERDATA:
  121. ptr = va_arg(param, void *);
  122. share->clientdata = ptr;
  123. break;
  124. default:
  125. return CURLSHE_BAD_OPTION;
  126. }
  127. return CURLSHE_OK;
  128. }
  129. CURLSHcode
  130. curl_share_cleanup(CURLSH *sh)
  131. {
  132. struct Curl_share *share = (struct Curl_share *)sh;
  133. if(share == NULL)
  134. return CURLSHE_INVALID;
  135. if(share->lockfunc)
  136. share->lockfunc(NULL, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE,
  137. share->clientdata);
  138. if(share->dirty) {
  139. if(share->unlockfunc)
  140. share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
  141. return CURLSHE_IN_USE;
  142. }
  143. if(share->hostcache) {
  144. Curl_hash_destroy(share->hostcache);
  145. share->hostcache = NULL;
  146. }
  147. #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
  148. if(share->cookies)
  149. Curl_cookie_cleanup(share->cookies);
  150. #endif /* CURL_DISABLE_HTTP */
  151. if(share->unlockfunc)
  152. share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
  153. free(share);
  154. return CURLSHE_OK;
  155. }
  156. CURLSHcode
  157. Curl_share_lock(struct SessionHandle *data, curl_lock_data type,
  158. curl_lock_access accesstype)
  159. {
  160. struct Curl_share *share = data->share;
  161. if(share == NULL)
  162. return CURLSHE_INVALID;
  163. if(share->specifier & (1<<type)) {
  164. if(share->lockfunc) /* only call this if set! */
  165. share->lockfunc(data, type, accesstype, share->clientdata);
  166. }
  167. /* else if we don't share this, pretend successful lock */
  168. return CURLSHE_OK;
  169. }
  170. CURLSHcode
  171. Curl_share_unlock(struct SessionHandle *data, curl_lock_data type)
  172. {
  173. struct Curl_share *share = data->share;
  174. if(share == NULL)
  175. return CURLSHE_INVALID;
  176. if(share->specifier & (1<<type)) {
  177. if(share->unlockfunc) /* only call this if set! */
  178. share->unlockfunc (data, type, share->clientdata);
  179. }
  180. return CURLSHE_OK;
  181. }