2
0

share.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. #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. /* The last #include file should be: */
  31. #ifdef CURLDEBUG
  32. #include "memdebug.h"
  33. #endif
  34. CURLSH *
  35. curl_share_init(void)
  36. {
  37. struct Curl_share *share =
  38. (struct Curl_share *)malloc(sizeof(struct Curl_share));
  39. if (share) {
  40. memset (share, 0, sizeof(struct Curl_share));
  41. share->specifier |= (1<<CURL_LOCK_DATA_SHARE);
  42. }
  43. return share;
  44. }
  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. {
  66. case CURL_LOCK_DATA_DNS:
  67. if (!share->hostcache) {
  68. share->hostcache = Curl_hash_alloc(7, Curl_freednsinfo);
  69. }
  70. break;
  71. case CURL_LOCK_DATA_COOKIE:
  72. if (!share->cookies) {
  73. share->cookies = Curl_cookie_init(NULL, NULL, NULL, TRUE );
  74. }
  75. break;
  76. case CURL_LOCK_DATA_SSL_SESSION:
  77. break;
  78. case CURL_LOCK_DATA_CONNECT:
  79. break;
  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. case CURL_LOCK_DATA_COOKIE:
  97. if (share->cookies) {
  98. Curl_cookie_cleanup(share->cookies);
  99. share->cookies = NULL;
  100. }
  101. break;
  102. case CURL_LOCK_DATA_SSL_SESSION:
  103. break;
  104. case CURL_LOCK_DATA_CONNECT:
  105. break;
  106. default:
  107. return CURLSHE_BAD_OPTION;
  108. }
  109. break;
  110. case CURLSHOPT_LOCKFUNC:
  111. lockfunc = va_arg(param, curl_lock_function);
  112. share->lockfunc = lockfunc;
  113. break;
  114. case CURLSHOPT_UNLOCKFUNC:
  115. unlockfunc = va_arg(param, curl_unlock_function);
  116. share->unlockfunc = unlockfunc;
  117. break;
  118. case CURLSHOPT_USERDATA:
  119. ptr = va_arg(param, void *);
  120. share->clientdata = ptr;
  121. break;
  122. default:
  123. return CURLSHE_BAD_OPTION;
  124. }
  125. return CURLSHE_OK;
  126. }
  127. CURLSHcode
  128. curl_share_cleanup(CURLSH *sh)
  129. {
  130. struct Curl_share *share = (struct Curl_share *)sh;
  131. if (share == NULL)
  132. return CURLSHE_INVALID;
  133. share->lockfunc(NULL, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE,
  134. share->clientdata);
  135. if (share->dirty) {
  136. share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
  137. return CURLSHE_IN_USE;
  138. }
  139. if(share->hostcache)
  140. Curl_hash_destroy(share->hostcache);
  141. if(share->cookies)
  142. Curl_cookie_cleanup(share->cookies);
  143. share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
  144. free (share);
  145. return CURLSHE_OK;
  146. }
  147. CURLSHcode
  148. Curl_share_lock(struct SessionHandle *data, curl_lock_data type,
  149. curl_lock_access accesstype)
  150. {
  151. struct Curl_share *share = data->share;
  152. if (share == NULL)
  153. return CURLSHE_INVALID;
  154. if(share->specifier & (1<<type)) {
  155. if(share->lockfunc) /* only call this if set! */
  156. share->lockfunc(data, type, accesstype, share->clientdata);
  157. }
  158. /* else if we don't share this, pretend successful lock */
  159. return CURLSHE_OK;
  160. }
  161. CURLSHcode
  162. Curl_share_unlock(struct SessionHandle *data, curl_lock_data type)
  163. {
  164. struct Curl_share *share = data->share;
  165. if (share == NULL)
  166. return CURLSHE_INVALID;
  167. if(share->specifier & (1<<type)) {
  168. if(share->unlockfunc) /* only call this if set! */
  169. share->unlockfunc (data, type, share->clientdata);
  170. }
  171. return CURLSHE_OK;
  172. }