share.c 5.5 KB

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