share.c 5.3 KB

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