1
0

share.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2004, 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 "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 =
  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. CURLSHcode
  45. curl_share_setopt(CURLSH *sh, CURLSHoption option, ...)
  46. {
  47. struct Curl_share *share = (struct Curl_share *)sh;
  48. va_list param;
  49. int type;
  50. curl_lock_function lockfunc;
  51. curl_unlock_function unlockfunc;
  52. void *ptr;
  53. if (share->dirty)
  54. /* don't allow setting options while one or more handles are already
  55. using this share */
  56. return CURLSHE_IN_USE;
  57. va_start(param, option);
  58. switch(option) {
  59. case CURLSHOPT_SHARE:
  60. /* this is a type this share will share */
  61. type = va_arg(param, int);
  62. share->specifier |= (1<<type);
  63. switch( type ) {
  64. case CURL_LOCK_DATA_DNS:
  65. if (!share->hostcache) {
  66. share->hostcache = Curl_mk_dnscache();
  67. if(!share->hostcache)
  68. return CURLSHE_NOMEM;
  69. }
  70. break;
  71. #ifndef CURL_DISABLE_HTTP
  72. case CURL_LOCK_DATA_COOKIE:
  73. if (!share->cookies) {
  74. share->cookies = Curl_cookie_init(NULL, NULL, NULL, TRUE );
  75. if(!share->cookies)
  76. return CURLSHE_NOMEM;
  77. }
  78. break;
  79. #endif /* CURL_DISABLE_HTTP */
  80. case CURL_LOCK_DATA_SSL_SESSION: /* not supported (yet) */
  81. case CURL_LOCK_DATA_CONNECT: /* not supported (yet) */
  82. default:
  83. return CURLSHE_BAD_OPTION;
  84. }
  85. break;
  86. case CURLSHOPT_UNSHARE:
  87. /* this is a type this share will no longer share */
  88. type = va_arg(param, int);
  89. share->specifier &= ~(1<<type);
  90. switch( type )
  91. {
  92. case CURL_LOCK_DATA_DNS:
  93. if (share->hostcache) {
  94. Curl_hash_destroy(share->hostcache);
  95. share->hostcache = NULL;
  96. }
  97. break;
  98. #ifndef CURL_DISABLE_HTTP
  99. case CURL_LOCK_DATA_COOKIE:
  100. if (share->cookies) {
  101. Curl_cookie_cleanup(share->cookies);
  102. share->cookies = NULL;
  103. }
  104. break;
  105. #endif /* CURL_DISABLE_HTTP */
  106. case CURL_LOCK_DATA_SSL_SESSION:
  107. break;
  108. case CURL_LOCK_DATA_CONNECT:
  109. break;
  110. default:
  111. return CURLSHE_BAD_OPTION;
  112. }
  113. break;
  114. case CURLSHOPT_LOCKFUNC:
  115. lockfunc = va_arg(param, curl_lock_function);
  116. share->lockfunc = lockfunc;
  117. break;
  118. case CURLSHOPT_UNLOCKFUNC:
  119. unlockfunc = va_arg(param, curl_unlock_function);
  120. share->unlockfunc = unlockfunc;
  121. break;
  122. case CURLSHOPT_USERDATA:
  123. ptr = va_arg(param, void *);
  124. share->clientdata = ptr;
  125. break;
  126. default:
  127. return CURLSHE_BAD_OPTION;
  128. }
  129. return CURLSHE_OK;
  130. }
  131. CURLSHcode
  132. curl_share_cleanup(CURLSH *sh)
  133. {
  134. struct Curl_share *share = (struct Curl_share *)sh;
  135. if (share == NULL)
  136. return CURLSHE_INVALID;
  137. if(share->lockfunc)
  138. share->lockfunc(NULL, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE,
  139. share->clientdata);
  140. if (share->dirty) {
  141. if(share->unlockfunc)
  142. share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
  143. return CURLSHE_IN_USE;
  144. }
  145. if(share->hostcache)
  146. Curl_hash_destroy(share->hostcache);
  147. #ifndef CURL_DISABLE_HTTP
  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. }