easy.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*****************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2000, Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * In order to be useful for every potential user, curl and libcurl are
  11. * dual-licensed under the MPL and the MIT/X-derivate licenses.
  12. *
  13. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  14. * copies of the Software, and permit persons to whom the Software is
  15. * furnished to do so, under the terms of the MPL or the MIT/X-derivate
  16. * licenses. You may pick one of these licenses.
  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. /* -- WIN32 approved -- */
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <stdarg.h>
  28. #include <stdlib.h>
  29. #include <ctype.h>
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. #include <errno.h>
  33. #include "strequal.h"
  34. #if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
  35. #include <winsock.h>
  36. #include <time.h>
  37. #include <io.h>
  38. #else
  39. #ifdef HAVE_SYS_SOCKET_H
  40. #include <sys/socket.h>
  41. #endif
  42. #include <netinet/in.h>
  43. #include <sys/time.h>
  44. #include <sys/resource.h>
  45. #ifdef HAVE_UNISTD_H
  46. #include <unistd.h>
  47. #endif
  48. #include <netdb.h>
  49. #ifdef HAVE_ARPA_INET_H
  50. #include <arpa/inet.h>
  51. #endif
  52. #ifdef HAVE_NET_IF_H
  53. #include <net/if.h>
  54. #endif
  55. #include <sys/ioctl.h>
  56. #include <signal.h>
  57. #ifdef HAVE_SYS_PARAM_H
  58. #include <sys/param.h>
  59. #endif
  60. #ifdef HAVE_SYS_SELECT_H
  61. #include <sys/select.h>
  62. #endif
  63. #endif
  64. #include "urldata.h"
  65. #include <curl/curl.h>
  66. #include "transfer.h"
  67. #include "ssluse.h"
  68. #include "url.h"
  69. #include "getinfo.h"
  70. #include "hostip.h"
  71. #define _MPRINTF_REPLACE /* use our functions only */
  72. #include <curl/mprintf.h>
  73. /* Silly win32 socket initialization functions */
  74. #if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
  75. static void win32_cleanup(void)
  76. {
  77. WSACleanup();
  78. }
  79. static CURLcode win32_init(void)
  80. {
  81. WORD wVersionRequested;
  82. WSADATA wsaData;
  83. int err;
  84. wVersionRequested = MAKEWORD(2, 0);
  85. err = WSAStartup(wVersionRequested, &wsaData);
  86. if (err != 0)
  87. /* Tell the user that we couldn't find a useable */
  88. /* winsock.dll. */
  89. return CURLE_FAILED_INIT;
  90. /* Confirm that the Windows Sockets DLL supports 2.0.*/
  91. /* Note that if the DLL supports versions greater */
  92. /* than 2.0 in addition to 2.0, it will still return */
  93. /* 2.0 in wVersion since that is the version we */
  94. /* requested. */
  95. if ( LOBYTE( wsaData.wVersion ) != 2 ||
  96. HIBYTE( wsaData.wVersion ) != 0 ) {
  97. /* Tell the user that we couldn't find a useable */
  98. /* winsock.dll. */
  99. WSACleanup();
  100. return CURLE_FAILED_INIT;
  101. }
  102. return CURLE_OK;
  103. }
  104. /* The Windows Sockets DLL is acceptable. Proceed. */
  105. #else
  106. /* These functions exist merely to prevent compiler warnings */
  107. static CURLcode win32_init(void) { return CURLE_OK; }
  108. static void win32_cleanup(void) { }
  109. #endif
  110. /* true globals -- for curl_global_init() and curl_global_cleanup() */
  111. static unsigned int initialized = 0;
  112. static long init_flags = 0;
  113. /**
  114. * Globally initializes cURL given a bitwise set of
  115. * the different features to initialize.
  116. */
  117. CURLcode curl_global_init(long flags)
  118. {
  119. if (initialized)
  120. return CURLE_OK;
  121. if (flags & CURL_GLOBAL_SSL)
  122. Curl_SSL_init();
  123. if (flags & CURL_GLOBAL_WIN32)
  124. if (win32_init() != CURLE_OK)
  125. return CURLE_FAILED_INIT;
  126. initialized = 1;
  127. init_flags = flags;
  128. return CURLE_OK;
  129. }
  130. /**
  131. * Globally cleanup cURL, uses the value of "init_flags" to determine
  132. * what needs to be cleaned up and what doesn't
  133. */
  134. void curl_global_cleanup(void)
  135. {
  136. if (!initialized)
  137. return;
  138. Curl_global_host_cache_dtor();
  139. if (init_flags & CURL_GLOBAL_SSL)
  140. Curl_SSL_cleanup();
  141. if (init_flags & CURL_GLOBAL_WIN32)
  142. win32_cleanup();
  143. initialized = 0;
  144. init_flags = 0;
  145. }
  146. CURL *curl_easy_init(void)
  147. {
  148. CURLcode res;
  149. struct SessionHandle *data;
  150. /* Make sure we inited the global SSL stuff */
  151. if (!initialized)
  152. curl_global_init(CURL_GLOBAL_DEFAULT);
  153. /* We use curl_open() with undefined URL so far */
  154. res = Curl_open(&data);
  155. if(res != CURLE_OK)
  156. return NULL;
  157. return data;
  158. }
  159. typedef int (*func_T)(void);
  160. CURLcode curl_easy_setopt(CURL *curl, CURLoption tag, ...)
  161. {
  162. va_list arg;
  163. func_T param_func = (func_T)0;
  164. long param_long = 0;
  165. void *param_obj = NULL;
  166. struct SessionHandle *data = curl;
  167. va_start(arg, tag);
  168. /* PORTING NOTE:
  169. Object pointers can't necessarily be casted to function pointers and
  170. therefore we need to know what type it is and read the correct type
  171. at once. This should also correct problems with different sizes of
  172. the types.
  173. */
  174. if(tag < CURLOPTTYPE_OBJECTPOINT) {
  175. /* This is a LONG type */
  176. param_long = va_arg(arg, long);
  177. Curl_setopt(data, tag, param_long);
  178. }
  179. else if(tag < CURLOPTTYPE_FUNCTIONPOINT) {
  180. /* This is a object pointer type */
  181. param_obj = va_arg(arg, void *);
  182. Curl_setopt(data, tag, param_obj);
  183. }
  184. else {
  185. param_func = va_arg(arg, func_T );
  186. Curl_setopt(data, tag, param_func);
  187. }
  188. va_end(arg);
  189. return CURLE_OK;
  190. }
  191. CURLcode curl_easy_perform(CURL *curl)
  192. {
  193. struct SessionHandle *data = (struct SessionHandle *)curl;
  194. if (!data->hostcache) {
  195. if (Curl_global_host_cache_use(data)) {
  196. data->hostcache = Curl_global_host_cache_get();
  197. }
  198. else {
  199. data->hostcache = curl_hash_alloc(7, Curl_freeaddrinfo);
  200. }
  201. }
  202. return Curl_perform(data);
  203. }
  204. void curl_easy_cleanup(CURL *curl)
  205. {
  206. struct SessionHandle *data = (struct SessionHandle *)curl;
  207. if (!Curl_global_host_cache_use(data)) {
  208. curl_hash_destroy(data->hostcache);
  209. }
  210. Curl_close(data);
  211. }
  212. CURLcode curl_easy_getinfo(CURL *curl, CURLINFO info, ...)
  213. {
  214. va_list arg;
  215. void *paramp;
  216. struct SessionHandle *data = (struct SessionHandle *)curl;
  217. va_start(arg, info);
  218. paramp = va_arg(arg, void *);
  219. return Curl_getinfo(data, info, paramp);
  220. }
  221. CURL *curl_easy_duphandle(CURL *incurl)
  222. {
  223. struct SessionHandle *data=(struct SessionHandle *)incurl;
  224. struct SessionHandle *outcurl = (struct SessionHandle *)
  225. malloc(sizeof(struct SessionHandle));
  226. if(NULL == outcurl)
  227. return NULL; /* failure */
  228. /* start with clearing the entire new struct */
  229. memset(outcurl, 0, sizeof(struct SessionHandle));
  230. /*
  231. * We setup a few buffers we need. We should probably make them
  232. * get setup on-demand in the code, as that would probably decrease
  233. * the likeliness of us forgetting to init a buffer here in the future.
  234. */
  235. outcurl->state.headerbuff=(char*)malloc(HEADERSIZE);
  236. if(!outcurl->state.headerbuff) {
  237. free(outcurl); /* free the memory again */
  238. return NULL;
  239. }
  240. outcurl->state.headersize=HEADERSIZE;
  241. /* copy all userdefined values */
  242. outcurl->set = data->set;
  243. outcurl->state.numconnects = data->state.numconnects;
  244. outcurl->state.connects = (struct connectdata **)
  245. malloc(sizeof(struct connectdata *) * outcurl->state.numconnects);
  246. if(!outcurl->state.connects) {
  247. free(outcurl->state.headerbuff);
  248. free(outcurl);
  249. return NULL;
  250. }
  251. memset(outcurl->state.connects, 0,
  252. sizeof(struct connectdata *)*outcurl->state.numconnects);
  253. outcurl->progress.flags = data->progress.flags;
  254. outcurl->progress.callback = data->progress.callback;
  255. if(data->cookies)
  256. /* If cookies are enabled in the parent handle, we enable them
  257. in the clone as well! */
  258. outcurl->cookies = Curl_cookie_init(data->cookies->filename,
  259. outcurl->cookies);
  260. /* duplicate all values in 'change' */
  261. if(data->change.url) {
  262. outcurl->change.url = strdup(data->change.url);
  263. outcurl->change.url_alloc = TRUE;
  264. }
  265. if(data->change.proxy) {
  266. outcurl->change.proxy = strdup(data->change.proxy);
  267. outcurl->change.proxy_alloc = TRUE;
  268. }
  269. if(data->change.referer) {
  270. outcurl->change.referer = strdup(data->change.referer);
  271. outcurl->change.referer_alloc = TRUE;
  272. }
  273. return outcurl;
  274. }
  275. /*
  276. * local variables:
  277. * eval: (load-file "../curl-mode.el")
  278. * end:
  279. * vim600: fdm=marker
  280. * vim: et sw=2 ts=2 sts=2 tw=78
  281. */