sq_libcurl.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #ifdef __cplusplus
  2. extern "C" {
  3. #endif
  4. #ifdef USE_EASYCURL
  5. #include "squirrel.h"
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include <stdlib.h> /* for malloc */
  9. #include <assert.h> /* for a few sanity tests */
  10. #include "EasyCurl.h"
  11. static const SQChar SQ_LIBNAME[] = _SC("EasyCurl");
  12. SQ_OPT_STRING_STRLEN();
  13. static const SQChar SSL_CTX_Tag[] = _SC("sq_EasyCurl_ctx");
  14. #define GET_EasyCurl_INSTANCE() SQ_GET_INSTANCE(v, 1, EASYCURL, EASYCURL_Tag) \
  15. if(self == NULL) return sq_throwerror(v, _SC("EasyCurl object already closed"));
  16. static SQRESULT EasyCurl_release_hook(SQUserPointer p, SQInteger size, HSQUIRRELVM v)
  17. {
  18. EASYCURL *self = (EASYCURL*)p;
  19. if(self) EasyCurl_free(self);
  20. return 0;
  21. }
  22. static SQRESULT EasyCurl_free(HSQUIRRELVM v)
  23. {
  24. SQ_FUNC_VARS_NO_TOP(v);
  25. GET_ssl_INSTANCE();
  26. EasyCurl_release_hook(self, 0, v);
  27. sq_setinstanceup(v, 1, 0);
  28. return 0;
  29. }
  30. static SQRESULT EasyCurl_constructor(HSQUIRRELVM v, EASYCURL *EasyCurl, int free_on_gc)
  31. {
  32. if(!EasyCurl)
  33. return sq_throwerror(v, _SC("Could'nt create an EasyCurl object."));
  34. sq_pushstring(v, SQ_LIBNAME, -1);
  35. if(sq_getonroottable(v) == SQ_OK){
  36. sq_pushstring(v, EasyCurl_NAME, -1);
  37. if(sq_get(v, -2) == SQ_OK){
  38. if(sq_createinstance(v, -1) == SQ_OK){
  39. sq_setinstanceup(v, -1, ssl);
  40. if(free_on_gc) sq_setreleasehook(v,-1, EasyCurl_release_hook);
  41. return 1;
  42. }
  43. }
  44. }
  45. return SQ_ERROR;
  46. }
  47. static SQRESULT sq_EasyCurl_cleanup(HSQUIRRELVM v){
  48. SQ_FUNC_VARS_NO_TOP(v);
  49. GET_EasyCurl_INSTANCE();
  50. // void curl_easy_cleanup(CURL *curl)
  51. return 0;
  52. }
  53. static SQRESULT sq_EasyCurl_constructor(HSQUIRRELVM v){
  54. SQ_FUNC_VARS_NO_TOP(v);
  55. GET_EasyCurl_INSTANCE();
  56. return 0;
  57. }
  58. static SQRESULT sq_EasyCurl_destructor(HSQUIRRELVM v){
  59. SQ_FUNC_VARS_NO_TOP(v);
  60. GET_EasyCurl_INSTANCE();
  61. return 0;
  62. }
  63. static SQRESULT sq_EasyCurl_easy_duphandle(HSQUIRRELVM v){
  64. SQ_FUNC_VARS_NO_TOP(v);
  65. GET_EasyCurl_INSTANCE();
  66. // CURL* curl_easy_duphandle(CURL *curl)
  67. return 0;
  68. }
  69. static SQRESULT sq_EasyCurl_easy_getinfo(HSQUIRRELVM v){
  70. SQ_FUNC_VARS_NO_TOP(v);
  71. GET_EasyCurl_INSTANCE();
  72. SQ_GET_STRING(v, 2, info);
  73. SQ_GET_STRING(v, 3, vargv);
  74. SQ_GET_STRING(v, 4, ...);
  75. // CURLcode curl_easy_getinfo(CURL *curl, CURLINFO info, ...)
  76. return 0;
  77. }
  78. static SQRESULT sq_EasyCurl_easy_init(HSQUIRRELVM v){
  79. SQ_FUNC_VARS_NO_TOP(v);
  80. GET_EasyCurl_INSTANCE();
  81. // CURL *curl_easy_init(void)
  82. return 0;
  83. }
  84. static SQRESULT sq_EasyCurl_easy_recv(HSQUIRRELVM v){
  85. SQ_FUNC_VARS_NO_TOP(v);
  86. GET_EasyCurl_INSTANCE();
  87. // CURLcode curl_easy_recv(CURL *curl, void *buffer, size_t buflen, size_t *n)
  88. return 0;
  89. }
  90. static SQRESULT sq_EasyCurl_easy_reset(HSQUIRRELVM v){
  91. SQ_FUNC_VARS_NO_TOP(v);
  92. GET_EasyCurl_INSTANCE();
  93. // void curl_easy_reset(CURL *curl)
  94. return 0;
  95. }
  96. static SQRESULT sq_EasyCurl_easy_send(HSQUIRRELVM v){
  97. SQ_FUNC_VARS_NO_TOP(v);
  98. GET_EasyCurl_INSTANCE();
  99. SQ_GET_STRING(v, 2, buffer);
  100. // CURLcode curl_easy_send(CURL *curl, const void *buffer, size_t buflen, size_t *n)
  101. return 0;
  102. }
  103. static SQRESULT sq_EasyCurl_easy_setopt(HSQUIRRELVM v){
  104. SQ_FUNC_VARS_NO_TOP(v);
  105. GET_EasyCurl_INSTANCE();
  106. SQ_GET_STRING(v, 2, option);
  107. SQ_GET_STRING(v, 3, vargv);
  108. SQ_GET_STRING(v, 4, ...);
  109. // CURLcode curl_easy_setopt(CURL *curl, CURLoption option, ...)
  110. return 0;
  111. }
  112. static SQRESULT sq_EasyCurl_perform(HSQUIRRELVM v){
  113. SQ_FUNC_VARS_NO_TOP(v);
  114. GET_EasyCurl_INSTANCE();
  115. // CURLcode curl_easy_perform(CURL *curl)
  116. return 0;
  117. }
  118. #define _DECL_EASYCURL_FUNC(name,nparams,pmask) {_SC(#name),sq_EasyCurl_##name,nparams,pmask}
  119. static SQRegFunction EasyCurl_obj_funcs[]={
  120. _DECL_EASYCURL_FUNC(cleanup, 1, _SC(".")),
  121. _DECL_EASYCURL_FUNC(constructor, 1, _SC(".")),
  122. _DECL_EASYCURL_FUNC(destructor, 1, _SC(".")),
  123. _DECL_EASYCURL_FUNC(easy_duphandle, 1, _SC(".")),
  124. _DECL_EASYCURL_FUNC(easy_getinfo, 4, _SC("....")),
  125. _DECL_EASYCURL_FUNC(easy_init, 1, _SC(".")),
  126. _DECL_EASYCURL_FUNC(easy_recv, 1, _SC(".")),
  127. _DECL_EASYCURL_FUNC(easy_reset, 1, _SC(".")),
  128. _DECL_EASYCURL_FUNC(easy_send, 2, _SC("..")),
  129. _DECL_EASYCURL_FUNC(easy_setopt, 4, _SC("....")),
  130. _DECL_EASYCURL_FUNC(perform, 1, _SC(".")),
  131. {0,0}
  132. };
  133. #undef _DECL_EASYCURL_FUNC
  134. typedef struct {
  135. const SQChar *Str;
  136. SQInteger Val;
  137. } KeyIntType, * KeyIntPtrType;
  138. static KeyIntType EasyCurl_constants[] = {
  139. #define MK_CONST(c) {_SC(#c), c}
  140. //MK_CONST(SSL_SESSION_ID_SIZE),
  141. MK_CONST(v, CURLPROTO_DICT);
  142. MK_CONST(v, CURLSSH_AUTH_ANY);
  143. MK_CONST(v, CURLAUTH_NTLM_WB);
  144. MK_CONST(v, CURLSSH_AUTH_KEYBOARD);
  145. MK_CONST(v, CURLPROTO_SMTP);
  146. MK_CONST(v, CURLPROTO_RTMP);
  147. MK_CONST(v, CURLSSH_AUTH_PUBLICKEY);
  148. MK_CONST(v, CURLPROTO_POP3);
  149. MK_CONST(v, CURLAUTH_NEGOTIATE);
  150. MK_CONST(v, CURLPROTO_POP3S);
  151. MK_CONST(v, CURLHEADER_SEPARATE);
  152. MK_CONST(v, CURLPROTO_RTMPTE);
  153. MK_CONST(v, CURLAUTH_DIGEST_IE);
  154. MK_CONST(v, CURLPROTO_ALL);
  155. MK_CONST(v, CURLSSH_AUTH_HOST);
  156. MK_CONST(v, CURLPROTO_HTTP);
  157. MK_CONST(v, CURLAUTH_BASIC);
  158. MK_CONST(v, CURLPROTO_IMAPS);
  159. MK_CONST(v, CURLPROTO_RTMPTS);
  160. MK_CONST(v, CURLSSH_AUTH_NONE);
  161. MK_CONST(v, CURLGSSAPI_DELEGATION_NONE);
  162. MK_CONST(v, CURLAUTH_DIGEST);
  163. MK_CONST(v, CURLGSSAPI_DELEGATION_FLAG);
  164. MK_CONST(v, CURLPROTO_LDAP);
  165. MK_CONST(v, CURLAUTH_NONE);
  166. MK_CONST(v, CURLPROTO_SMTPS);
  167. MK_CONST(v, CURLPROTO_GOPHER);
  168. MK_CONST(v, CURLSSH_AUTH_DEFAULT);
  169. MK_CONST(v, CURLPROTO_SMB);
  170. MK_CONST(v, CURLPROTO_RTSP);
  171. MK_CONST(v, CURLSSH_AUTH_PASSWORD);
  172. MK_CONST(v, CURLPROTO_SMBS);
  173. MK_CONST(v, CURLAUTH_ANYSAFE);
  174. MK_CONST(v, CURLPROTO_LDAPS);
  175. MK_CONST(v, CURLGSSAPI_DELEGATION_POLICY_FLAG);
  176. MK_CONST(v, CURLPROTO_TFTP);
  177. MK_CONST(v, CURLPROTO_SFTP);
  178. MK_CONST(v, CURLHEADER_UNIFIED);
  179. MK_CONST(v, CURLAUTH_GSSNEGOTIATE);
  180. MK_CONST(v, CURLPROTO_FILE);
  181. MK_CONST(v, CURLAUTH_NTLM);
  182. MK_CONST(v, CURLSSH_AUTH_AGENT);
  183. MK_CONST(v, CURLPROTO_HTTPS);
  184. MK_CONST(v, CURLPROTO_RTMPS);
  185. MK_CONST(v, CURLAUTH_ONLY);
  186. MK_CONST(v, CURLPROTO_TELNET);
  187. MK_CONST(v, CURLAUTH_ANY);
  188. MK_CONST(v, CURLPROTO_FTPS);
  189. MK_CONST(v, CURLPROTO_IMAP);
  190. MK_CONST(v, CURLPROTO_RTMPE);
  191. MK_CONST(v, CURLPROTO_RTMPT);
  192. MK_CONST(v, CURLPROTO_FTP);
  193. MK_CONST(v, CURLPROTO_SCP);
  194. {0,0}
  195. };
  196. /* This defines a function that opens up your library. */
  197. SQRESULT sqext_register_EasyCurl (HSQUIRRELVM v) {
  198. //add a namespace EasyCurl
  199. sq_pushstring(v, SQ_LIBNAME, -1);
  200. sq_newtable(v);
  201. sq_insert_reg_funcs(v, EasyCurl_obj_funcs);
  202. //add constants
  203. KeyIntPtrType KeyIntPtr;
  204. for (KeyIntPtr = EasyCurl_constants; KeyIntPtr->Str; KeyIntPtr++) {
  205. sq_pushstring(v, KeyIntPtr->Str, -1); //first the key
  206. sq_pushinteger(v, KeyIntPtr->Val); //then the value
  207. sq_newslot(v, -3, SQFalse); //store then
  208. }
  209. sq_newslot(v,-3,SQFalse); //add EasyCurl table to the root table
  210. return SQ_OK;
  211. }
  212. #ifdef __cplusplus
  213. }
  214. #endif //USE_EASYCURL
  215. #endif