sqmongoose.cpp 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdarg.h>
  5. #include "squirrel.h"
  6. #include "sqstdblobimpl.h"
  7. SQ_OPT_STRING_STRLEN();
  8. #ifdef USE_SQ_SQLITE3
  9. #include "lsqlite3.h"
  10. #endif
  11. #ifdef USE_AXTLS
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. void * SSL_CTX_new(void *);
  16. #ifdef __cplusplus
  17. }
  18. #endif
  19. #endif
  20. #define USE_MG_MD5 1
  21. #include "mongoose.h"
  22. #define PRINT_FILE_LINE printf("%s %d\n", __FILE__,__LINE__);
  23. #ifdef UNUSED
  24. #elif defined(__GNUC__)
  25. # define UNUSED(x) UNUSED_ ## x __attribute__((unused))
  26. #elif defined(__LCLINT__)
  27. # define UNUSED(x) /*@unused@*/ x
  28. #else
  29. # define UNUSED(x) x
  30. #endif
  31. #define MARK() do { \
  32. printf("%s:%d (%s)\n", __FILE__, __LINE__, __FUNCTION__); \
  33. } while (0)
  34. typedef struct SQ_Mg_Context SQ_Mg_Context;
  35. typedef struct SQ_MG_Callback SQ_MG_Callback;
  36. struct SQ_MG_Callback {
  37. size_t size;
  38. char buf[1];
  39. };
  40. static SQ_MG_Callback *SQ_MG_Callback_malloc(size_t x){
  41. SQ_MG_Callback *p = (SQ_MG_Callback *)sq_malloc(sizeof(SQ_MG_Callback)+x);
  42. if(p) p->size = x;
  43. return p;
  44. }
  45. static void SQ_MG_Callback_free(SQ_MG_Callback *p){
  46. if(p) sq_free(p, sizeof(SQ_MG_Callback)+p->size);
  47. }
  48. struct SQ_Mg_Context {
  49. HSQUIRRELVM v;
  50. struct mg_context *ctx;
  51. SQ_MG_Callback *master_plugin;
  52. SQ_MG_Callback *master_plugin_exit;
  53. SQ_MG_Callback *user_callback;
  54. SQ_MG_Callback *user_callback_setup;
  55. SQ_MG_Callback *user_callback_exit;
  56. };
  57. static const SQChar sq_mg_context_TAG[] = "sq_mg_conn_class";
  58. #define GET_mg_context_INSTANCE() \
  59. SQ_Mg_Context *self; \
  60. if((_rc_ = sq_getinstanceup(v,1,(SQUserPointer*)&self,(void*)sq_mg_context_TAG)) < 0) return _rc_;
  61. static const SQChar sq_mg_user_callback[] = "mg_user_callback";
  62. static const SQChar sq_http_request_TAG[] = "HttpRequest";
  63. #define GET_http_request_INSTANCE() \
  64. struct mg_connection *conn; \
  65. if((_rc_ = sq_getinstanceup(v,1,(SQUserPointer*)&conn,(void*)sq_http_request_TAG)) < 0) return _rc_;
  66. static SQRESULT sq_http_request_releasehook(SQUserPointer p, SQInteger size, HSQUIRRELVM v)
  67. {
  68. return 1;
  69. }
  70. static SQRESULT sq_http_request_constructor(HSQUIRRELVM v)
  71. {
  72. //SQ_FUNC_VARS_NO_TOP(v);
  73. struct mg_connection *conn = 0;
  74. sq_setinstanceup(v, 1, conn);
  75. sq_setreleasehook(v,1, sq_http_request_releasehook);
  76. return 1;
  77. }
  78. static SQRESULT
  79. sq_http_request_print(HSQUIRRELVM v)
  80. {
  81. SQ_FUNC_VARS(v);
  82. GET_http_request_INSTANCE();
  83. SQInteger i, write_count = 0;
  84. for (i = 2; i <= _top_; ++i) {
  85. sq_tostring(v, i);
  86. SQ_GET_STRING(v, -1, value);
  87. write_count += mg_write(conn, value, value_size);
  88. sq_poptop(v);
  89. }
  90. sq_pushinteger(v, write_count);
  91. return 1;
  92. }
  93. #ifdef USE_SQ_LSQLITE3
  94. static SQRESULT
  95. sq_http_request_vm_print(HSQUIRRELVM v)
  96. {
  97. GET_http_request_INSTANCE();
  98. lsqlite3_sdb_vm *svm = (lsqlite3_sdb_vm *)luaL_checkudata(v, 1,
  99. lsqlite3_sqlite_vm_meta);
  100. SQInteger idx = luaL_checkint(v,2);
  101. SQInteger len = sqlite3_column_bytes(svm->vm, idx);
  102. if(len) sq_pushinteger(v, mg_write(conn, sqlite3_column_text(svm->vm, idx), len));
  103. else sq_pushinteger(v, 0);
  104. return 1;
  105. }
  106. #endif
  107. static SQRESULT
  108. sq_http_request_write(HSQUIRRELVM v)
  109. {
  110. SQ_FUNC_VARS(v);
  111. GET_http_request_INSTANCE();
  112. SQ_GET_STRING(v, 2, buf);
  113. SQ_OPT_INTEGER(v, 3, write_size, buf_size);
  114. sq_pushinteger(v, mg_write(conn, buf, write_size));
  115. return 1;
  116. }
  117. static SQRESULT
  118. sq_http_request_read(HSQUIRRELVM v)
  119. {
  120. SQ_FUNC_VARS(v);
  121. GET_http_request_INSTANCE();
  122. SQ_OPT_INTEGER(v, 2, n, 1024*2000);
  123. size_t rlen; /* how much to read */
  124. size_t nr; /* number of chars actually read */
  125. rlen = 8192; /* try to read that much each time */
  126. SQBlob blob(0, rlen);
  127. if (rlen > n) rlen = n; /* cannot read more than asked */
  128. char *p = sq_getscratchpad(v,rlen);
  129. do {
  130. nr = mg_read(conn, p, rlen);
  131. blob.Write(p, nr);
  132. n -= nr; /* still have to read `n' chars */
  133. } while (n > 0 && nr == rlen); /* until end of count or eof */
  134. sq_pushstring(v, (const SQChar *)blob.GetBuf(), blob.Len()); /* close buffer */
  135. return 1;
  136. }
  137. static SQRESULT
  138. sq_http_request_send_file(HSQUIRRELVM v)
  139. {
  140. SQ_FUNC_VARS_NO_TOP(v);
  141. GET_http_request_INSTANCE();
  142. SQ_GET_STRING(v, 2, file_path);
  143. mg_send_file(conn, file_path);
  144. return 0;
  145. }
  146. static SQRESULT
  147. sq_http_request_write_blob(HSQUIRRELVM v)
  148. {
  149. SQ_FUNC_VARS_NO_TOP(v);
  150. GET_http_request_INSTANCE();
  151. SQBlob *blob = NULL;
  152. { if(SQ_FAILED(sq_getinstanceup(v,2,(SQUserPointer*)&blob,(SQUserPointer)SQBlob::SQBlob_TAG)))
  153. return sq_throwerror(v,_SC("invalid type tag")); }
  154. if(!blob || !blob->IsValid())
  155. return sq_throwerror(v,_SC("the blob is invalid"));
  156. sq_pushinteger(v, mg_write(conn, (const SQChar*)blob->GetBuf(), blob->Len()));
  157. return 1;
  158. }
  159. static SQRESULT
  160. sq_http_request_get_header(HSQUIRRELVM v)
  161. {
  162. SQ_FUNC_VARS_NO_TOP(v);
  163. GET_http_request_INSTANCE();
  164. SQ_GET_STRING(v, 2, name);
  165. sq_pushstring(v, mg_get_header(conn, name), -1);
  166. return 1;
  167. }
  168. static SQRESULT
  169. sq_http_request_get_cookie(HSQUIRRELVM v)
  170. {
  171. SQ_FUNC_VARS_NO_TOP(v);
  172. GET_http_request_INSTANCE();
  173. SQ_GET_STRING(v, 2, cookie_name);
  174. const char *start;
  175. int len = mg_find_cookie(conn, cookie_name, &start);
  176. if(len > 0) sq_pushstring(v, start, len);
  177. else sq_pushnull(v);
  178. return 1;
  179. }
  180. static SQRESULT
  181. sq_http_request_get_var(HSQUIRRELVM v)
  182. {
  183. SQ_FUNC_VARS_NO_TOP(v);
  184. GET_http_request_INSTANCE();
  185. SQ_GET_STRING(v, 2, data);
  186. SQ_GET_STRING(v, 3, name);
  187. const char *start;
  188. size_t buffer_len;
  189. int var_len = mg_find_var(data, data_size, name, &start);
  190. if(var_len > 0){
  191. buffer_len = var_len+1;
  192. char *buffer = sq_getscratchpad(v,buffer_len);
  193. if(buffer){
  194. var_len = mg_url_decode(start, var_len, buffer, buffer_len, 1);
  195. sq_pushstring(v, buffer, var_len);
  196. return 1;
  197. }
  198. }
  199. sq_pushnull(v);
  200. return 1;
  201. }
  202. static SQRESULT
  203. sq_http_request_handle_cgi_request(HSQUIRRELVM v)
  204. {
  205. SQ_FUNC_VARS_NO_TOP(v);
  206. GET_http_request_INSTANCE();
  207. SQ_GET_STRING(v, 2, prog);
  208. mg_handle_cgi_request(conn, prog);
  209. return 0;
  210. }
  211. static SQRESULT
  212. sq_http_request_get_option(HSQUIRRELVM v)
  213. {
  214. SQ_FUNC_VARS_NO_TOP(v);
  215. GET_http_request_INSTANCE();
  216. SQ_GET_STRING(v, 2, name);
  217. SQ_Mg_Context *sq_mg_ctx = (SQ_Mg_Context *) mg_get_user_data(conn);
  218. sq_pushstring(v, mg_get_option(sq_mg_ctx->ctx, name), -1);
  219. return 1;
  220. }
  221. #define _DECL_FUNC(name,nparams,tycheck) {_SC(#name), sq_http_request_##name,nparams,tycheck}
  222. static SQRegFunction mg_http_request_methods[] =
  223. {
  224. _DECL_FUNC(constructor, 1, _SC("x")),
  225. _DECL_FUNC(print, -2, _SC("x n|s")),
  226. _DECL_FUNC(read, 2, _SC("xi")),
  227. _DECL_FUNC(write, 3, _SC("xsi")),
  228. _DECL_FUNC(write_blob, -2, _SC("xxi")),
  229. _DECL_FUNC(get_var, 2, _SC("xs")),
  230. _DECL_FUNC(get_cookie, 2, _SC("xs")),
  231. _DECL_FUNC(get_header, 2, _SC("xs")),
  232. _DECL_FUNC(send_file, 2, _SC("xs")),
  233. _DECL_FUNC(handle_cgi_request, 2, _SC("xs")),
  234. _DECL_FUNC(get_option, 2, _SC("xs")),
  235. {0,0}
  236. };
  237. #undef _DECL_FUNC
  238. /***********/
  239. // helper function to extract a single mg_config value from a Lua table
  240. static void
  241. fetchfield(HSQUIRRELVM v, int idx, const char *key, char **value, const char *d)
  242. {
  243. const char *s;
  244. sq_pushstring(v, key, -1);
  245. if(sq_rawget(v, idx) == SQ_OK){
  246. if(sq_gettype(v, -1) != OT_NULL){
  247. sq_tostring(v, -1);
  248. sq_getstring(v, -1, &s);
  249. *value = mg_strdup(s);
  250. sq_pop(v, 2); //retrieved value and converted value
  251. return;
  252. }
  253. }
  254. *value = NULL;
  255. }
  256. // initializes an options string array from a Lua table
  257. static SQRESULT
  258. fetchoptions(HSQUIRRELVM v, int idx, const char **options)
  259. {
  260. struct {
  261. const char *key;
  262. const char *value;
  263. } OPTIONS[] = {
  264. { "cgi_extensions", ".cgi,.pl,.php" },
  265. { "cgi_environment", NULL },
  266. { "put_delete_passwords_file", NULL },
  267. { "cgi_interpreter", NULL },
  268. { "protect_uri", NULL },
  269. { "authentication_domain", "mydomain.com" },
  270. { "ssi_extensions", ".shtml,.shtm" },
  271. { "access_log_file", NULL },
  272. { "ssl_chain_file", NULL },
  273. { "enable_directory_listing", "yes" },
  274. { "error_log_file", NULL },
  275. { "global_passwords_file", NULL },
  276. { "index_files", "index.html,index.htm,index.cgi" },
  277. { "enable_keep_alive", "no" },
  278. { "access_control_list", NULL },
  279. { "max_request_size", "16384" },
  280. { "extra_mime_types", NULL },
  281. { "listening_ports", "8080" },
  282. { "document_root", "." },
  283. { "ssl_certificate", NULL },
  284. { "num_threads", "10" },
  285. { "run_as_user", NULL },
  286. { NULL, NULL }
  287. };
  288. char *value;
  289. int i, j;
  290. if(sq_gettype(v, idx) != OT_TABLE) return sq_throwerror(v, "a table is needed to retrieve options");
  291. for (i = 0, j = 0; OPTIONS[i].key; ++i) {
  292. fetchfield(v, idx, OPTIONS[i].key, &value, OPTIONS[i].value);
  293. if (NULL != value) {
  294. options[j++] = mg_strdup(OPTIONS[i].key);
  295. options[j++] = value;
  296. }
  297. }
  298. options[j] = NULL;
  299. return 0;
  300. }
  301. /***********/
  302. static const char sq_mongoose_TAG[] = "Mongoose";
  303. static SQBool show_errors_on_stdout = SQFalse;
  304. static void sq_mongoose_release_references(SQ_Mg_Context *self){
  305. SQ_MG_Callback_free(self->master_plugin);
  306. SQ_MG_Callback_free(self->master_plugin_exit);
  307. SQ_MG_Callback_free(self->user_callback);
  308. SQ_MG_Callback_free(self->user_callback_setup);
  309. SQ_MG_Callback_free(self->user_callback_exit);
  310. }
  311. static SQRESULT sq_mongoose_releasehook(SQUserPointer p, SQInteger size, HSQUIRRELVM v)
  312. {
  313. SQ_Mg_Context *self = (SQ_Mg_Context *)p;
  314. if(self){
  315. if(self->ctx){
  316. mg_stop(self->ctx);
  317. sq_mongoose_release_references(self);
  318. self->ctx = 0;
  319. }
  320. sq_free(self, sizeof(SQ_Mg_Context));
  321. }
  322. return 0;
  323. }
  324. static SQRESULT sq_mongoose_constructor(HSQUIRRELVM v)
  325. {
  326. //SQ_FUNC_VARS_NO_TOP(v);
  327. SQ_Mg_Context *sq_mg_ctx = (SQ_Mg_Context *)sq_malloc(sizeof(SQ_Mg_Context));
  328. memset(sq_mg_ctx, 0, sizeof(SQ_Mg_Context));
  329. sq_setinstanceup(v, 1, sq_mg_ctx);
  330. sq_setreleasehook(v,1, sq_mongoose_releasehook);
  331. return 1;
  332. }
  333. #define GET_mongoose_INSTANCE() \
  334. SQ_Mg_Context *self; \
  335. if((_rc_ = sq_getinstanceup(v,1,(SQUserPointer*)&self,(void*)sq_mongoose_TAG)) < 0) return _rc_;
  336. static SQRESULT
  337. sq_mongoose_show_errors_on_stdout(HSQUIRRELVM v)
  338. {
  339. SQ_FUNC_VARS_NO_TOP(v);
  340. SQ_GET_BOOL(v, 2, onOff);
  341. show_errors_on_stdout = onOff;
  342. return 0;
  343. }
  344. static SQRESULT
  345. sq_mongoose_modify_passwords_file(HSQUIRRELVM v)
  346. {
  347. SQ_FUNC_VARS_NO_TOP(v);
  348. SQ_GET_STRING(v, 2, passwords_file_name);
  349. SQ_GET_STRING(v, 3, domain);
  350. SQ_GET_STRING(v, 4, user);
  351. SQ_GET_STRING(v, 5, password);
  352. sq_pushinteger(v, mg_modify_passwords_file(passwords_file_name,
  353. domain, user, password));
  354. return 1;
  355. }
  356. static SQRESULT
  357. sq_mongoose_version(HSQUIRRELVM v)
  358. {
  359. sq_pushstring(v, mg_version(), -1);
  360. return 1;
  361. }
  362. SQInteger blob_write(SQUserPointer file,SQUserPointer p,SQInteger size);
  363. // creates a reference dispatching callbacks to squirrel functions
  364. static SQRESULT
  365. fetchcallback(HSQUIRRELVM v, const char *key, SQ_MG_Callback **sq_cb)
  366. {
  367. if(!sq_cb) return sq_throwerror(v, "inavlid SQ_MG_Callback parameter value (NULL)");
  368. if(sq_gettype(v, -1) != OT_TABLE) return sq_throwerror(v, "table expected to fetch callbacks");
  369. sq_pushstring(v, key, -1);
  370. sq_rawget(v, -2);
  371. if (sq_gettype(v, -1) == OT_CLOSURE){
  372. SQBlob b(0, 8192);
  373. if(SQ_SUCCEEDED(sq_writeclosure(v,blob_write,&b))) {
  374. *sq_cb = SQ_MG_Callback_malloc(b.Len());
  375. if(*sq_cb) memcpy((*sq_cb)->buf, b.GetBuf(), b.Len());
  376. sq_poptop(v);
  377. }
  378. }
  379. else
  380. {
  381. sq_pop(v, 1);
  382. return sq_throwerror(v, "closure expected for callbacks");
  383. }
  384. return SQ_OK;
  385. }
  386. static void *
  387. user_callback_proxy(enum mg_event event,
  388. struct mg_connection *conn,
  389. const struct mg_request_info *ri);
  390. // creates a new server using a configuration table
  391. static SQRESULT
  392. sq_mongoose_start(HSQUIRRELVM v)
  393. {
  394. SQ_FUNC_VARS_NO_TOP(v);
  395. GET_mongoose_INSTANCE();
  396. _rc_ = 1; //assumes succeed
  397. const char *options[64];
  398. SQInteger i;
  399. if(self->ctx) return sq_throwerror(v, _SC("mongoose already running or stoped incorrectly"));
  400. // store the Squirrel vm for use in callback proxies
  401. self->v = v;
  402. // prepare the mg_config structure from the squirrel table argument
  403. memset(options, 0, sizeof (options));
  404. fetchoptions(v, 2, options);
  405. fetchcallback(v, "master_plugin", &self->master_plugin);
  406. fetchcallback(v, "master_plugin_exit", &self->master_plugin_exit);
  407. fetchcallback(v, "user_callback", &self->user_callback);
  408. fetchcallback(v, "user_callback_setup", &self->user_callback_setup);
  409. fetchcallback(v, "user_callback_exit", &self->user_callback_exit);
  410. self->ctx = mg_start(user_callback_proxy, self, options);
  411. // free the options string list memory
  412. for (i = 0; options[i]; ++i)
  413. free((void *) options[i]);
  414. // throw an error if the server did not start
  415. if (self->ctx == NULL) {
  416. sq_mongoose_release_references(self);
  417. _rc_ = sq_throwerror(v, "could not start mongoose");
  418. }
  419. // return the context so it can be stopped later
  420. return _rc_;
  421. }
  422. static SQRESULT
  423. sq_mongoose_stop(HSQUIRRELVM v)
  424. {
  425. SQ_FUNC_VARS_NO_TOP(v);
  426. GET_mongoose_INSTANCE();
  427. if(self->ctx){
  428. mg_stop(self->ctx);
  429. sq_mongoose_release_references(self);
  430. self->ctx = 0;
  431. }
  432. return 0;
  433. }
  434. #define _DECL_FUNC(name,nparams,tycheck) {_SC(#name), sq_mongoose_##name,nparams,tycheck}
  435. static SQRegFunction sq_mongoose_methods[] =
  436. {
  437. _DECL_FUNC(constructor, 1, _SC("x")),
  438. _DECL_FUNC(start, -1, _SC("x")),
  439. _DECL_FUNC(stop, 1, _SC("x")),
  440. _DECL_FUNC(modify_passwords_file, 1, _SC("x")),
  441. _DECL_FUNC(version, 1, _SC("x")),
  442. _DECL_FUNC(show_errors_on_stdout, 2, _SC("xb")),
  443. {0,0}
  444. };
  445. #undef _DECL_FUNC
  446. static SQRESULT
  447. sq_mg_url_decode_base(HSQUIRRELVM v, SQInteger is_form_url_encoded)
  448. {
  449. SQ_FUNC_VARS_NO_TOP(v);
  450. GET_mg_context_INSTANCE();
  451. SQ_GET_STRING(v, 2, src);
  452. int dst_len = src_size +1;
  453. char *dst = sq_getscratchpad(v,dst_len);
  454. dst_len = mg_url_decode(src, src_size, dst, dst_len, is_form_url_encoded);
  455. sq_pushstring(v, dst, dst_len);
  456. return 1;
  457. }
  458. static SQRESULT
  459. sq_mg_url_decode(HSQUIRRELVM v)
  460. {
  461. return sq_mg_url_decode_base(v, 1);
  462. }
  463. static SQRESULT
  464. sq_mg_uri_decode(HSQUIRRELVM v)
  465. {
  466. return sq_mg_url_decode_base(v, 0);
  467. }
  468. static SQRESULT
  469. sq_mg_url_encode(HSQUIRRELVM v)
  470. {
  471. SQ_FUNC_VARS_NO_TOP(v);
  472. GET_mg_context_INSTANCE();
  473. SQ_GET_STRING(v, 2, src);
  474. char *dst = mg_url_encode(src);
  475. sq_pushstring(v, dst, -1);
  476. free(dst);
  477. return 1;
  478. }
  479. static SQRESULT
  480. sq_mg_md5(HSQUIRRELVM v)
  481. {
  482. SQ_FUNC_VARS(v);
  483. char buf[32 + 1];
  484. unsigned char hash[16];
  485. MD5_CTX ctx;
  486. MD5Init(&ctx);
  487. for (int i = 2; i <= _top_; ++i) {
  488. SQ_GET_STRING(v, i, p);
  489. MD5Update(&ctx, (const unsigned char *) p, p_size);
  490. }
  491. MD5Final(hash, &ctx);
  492. mg_bin2str(buf, hash, sizeof(hash));
  493. sq_pushstring(v, buf, -1);
  494. return 1;
  495. }
  496. static SQRESULT
  497. sq_mg_debug_print(HSQUIRRELVM v)
  498. {
  499. SQ_FUNC_VARS(v);
  500. SQInteger i, write_count = 0;
  501. for (i = 2; i <= _top_; ++i) {
  502. sq_tostring(v, i);
  503. SQ_GET_STRING(v, -1, value);
  504. write_count += fwrite(value, 1, value_size, stderr);
  505. sq_poptop(v);
  506. }
  507. sq_pushinteger(v, write_count);
  508. return 1;
  509. }
  510. #ifdef JNI_ENABLE_LOG
  511. #include <android/log.h>
  512. /*
  513. ** A callback for the android_log() SQIntegererface.
  514. */
  515. static void jniLog(int iErrCode, const char *zMsg){
  516. __android_log_print(ANDROID_LOG_ERROR,"jniLuaServerLog","(%d) %s\n", iErrCode, zMsg);
  517. }
  518. static SQRESULT sq_mg_jniLog(HSQUIRRELVM v)
  519. {
  520. SQ_FUNC_VARS_NO_TOP(v);
  521. SQ_GET_INTEGER(v, 2, code);
  522. SQ_GET_STRING(v, 3, error_message);
  523. jniLog(code, error_message);
  524. return 0;
  525. }
  526. #endif
  527. #define _DECL_FUNC(name,nparams,tycheck) {_SC(#name), sq_mg_##name,nparams,tycheck}
  528. static SQRegFunction sq_mg_methods[] =
  529. {
  530. _DECL_FUNC(url_decode, 2, _SC(".s")),
  531. _DECL_FUNC(uri_decode, 2, _SC(".s")),
  532. _DECL_FUNC(url_encode, 2, _SC(".s")),
  533. _DECL_FUNC(md5, -2, _SC("xs")),
  534. #ifdef JNI_ENABLE_LOG
  535. _DECL_FUNC(jniLog, -2, _SC("xs")),
  536. #endif
  537. _DECL_FUNC(debug_print, -2, _SC(".s")),
  538. {0,0}
  539. };
  540. #undef _DECL_FUNC
  541. static void reg_string(HSQUIRRELVM v, const char *name, const char *val) {
  542. sq_pushstring(v, name, -1);
  543. if(val) sq_pushstring(v, val, -1);
  544. else sq_pushnull(v);
  545. sq_rawset(v, -3);
  546. }
  547. static void reg_integer(HSQUIRRELVM v, const char *name, int val) {
  548. sq_pushstring(v, name, -1);
  549. sq_pushinteger(v, val);
  550. sq_rawset(v, -3);
  551. }
  552. // pushes request info on the Lua stack as a table
  553. static void
  554. push_request(HSQUIRRELVM v, const struct mg_request_info *ri)
  555. {
  556. int i;
  557. sq_pushliteral(v, _SC("data"));
  558. sq_get(v, -2);
  559. #define NEWSLOT_STR(ks) reg_string(v, #ks, ri->ks);
  560. NEWSLOT_STR(request_method);
  561. NEWSLOT_STR(uri);
  562. NEWSLOT_STR(http_version);
  563. NEWSLOT_STR(query_string);
  564. NEWSLOT_STR(remote_user);
  565. NEWSLOT_STR(log_message);
  566. #define NEWSLOT_INT(ks) reg_integer(v, #ks, ri->ks);
  567. NEWSLOT_INT(remote_ip);
  568. NEWSLOT_INT(remote_port);
  569. NEWSLOT_INT(status_code);
  570. NEWSLOT_INT(is_ssl);
  571. #undef NEWSLOT_STR
  572. #undef NEWSLOT_INT
  573. sq_pushliteral(v, "http_headers");
  574. sq_newtable(v);
  575. for (i = 0; i < ri->num_headers; ++i) {
  576. reg_string(v, ri->http_headers[i].name, ri->http_headers[i].value);
  577. }
  578. sq_newslot(v, -3, SQFalse);
  579. sq_poptop(v); //remove data table
  580. }
  581. static SQInteger
  582. sq_mg_pcall_master_plugin(HSQUIRRELVM v)
  583. {
  584. #if 0
  585. SQ_FUNC_VARS_NO_TOP(v);
  586. GET_MG_CONNECION();
  587. SQ_GET_STRING(v, 2, func_name);
  588. const SQChar *error_message = _SC("No master plugin installed !");
  589. HSQUIRRELVM master_plugin = (HSQUIRRELVM) mg_lock_master_plugin(conn);
  590. if(master_plugin){
  591. int master_plugin_saved_top = sq_gettop(master_plugin);
  592. //sq_pushcfunction(master_plugin, traceback); /* push traceback function */
  593. //int error_func = sq_gettop(master_plugin);
  594. sq_pushstring(master_plugin, func_name, func_name_size);
  595. sq_getonroottable(master_plugin);
  596. if(sq_gettype(master_plugin, -1) == OT_CLOSURE){
  597. sq_pushroottable(master_plugin);
  598. int arg_top = sq_gettop (v);
  599. /* Push arguments to dst stack */
  600. int idx = 4;
  601. copy_values_between_vms (master_plugin, v, arg_top-idx, idx);
  602. if (sq_pcall (master_plugin, arg_top-idx+1, SQTrue, SQTrue) == SQ_OK) {
  603. /* run OK? */
  604. int ret_top = sq_gettop (master_plugin);
  605. /* Push status = OK */
  606. sq_pushbool (v, SQTrue);
  607. /* Return values to src */
  608. //copy_values_between_vms (v, master_plugin, master_plugin_saved_top+2, ret_top);
  609. /* pops debug.traceback and result from dst state */
  610. //sq_settop(master_plugin, master_plugin_saved_top);
  611. /*unlock master plugin*/
  612. mg_unlock_master_plugin(conn);
  613. /* Return true (success) plus return values */
  614. return 1;
  615. }
  616. error_message = sq_tostring (master_plugin, -1);
  617. } else error_message = "Attempt to call an invalid function on master plugin !";
  618. /* pops debug.traceback and result from dst state */
  619. sq_settop(master_plugin, master_plugin_saved_top);
  620. /*unlock master plugin*/
  621. mg_unlock_master_plugin(conn);
  622. }
  623. return sq_throwerror(v, error_message);
  624. #endif
  625. }
  626. static void write_error_message(struct mg_connection *conn,
  627. const char * error_msg, SQInteger error_len){
  628. #define PRE_TAG_OPEN "<pre>"
  629. #define PRE_TAG_CLOSE "</pre>"
  630. mg_write(conn, PRE_TAG_OPEN, sizeof(PRE_TAG_OPEN));
  631. if(error_len < 0) error_len = strlen(error_msg);
  632. mg_write(conn, error_msg, error_len);
  633. mg_write(conn, PRE_TAG_CLOSE, sizeof(PRE_TAG_CLOSE));
  634. }
  635. #ifdef __cplusplus
  636. extern "C" {
  637. #endif
  638. SQUIRREL_API SQRESULT sqstd_register_SQLite3(HSQUIRRELVM v);
  639. SQUIRREL_API SQRESULT sqstd_register_base64(HSQUIRRELVM v);
  640. SQUIRREL_API SQRESULT sq_register_mix(HSQUIRRELVM v);
  641. SQUIRREL_API SQRESULT sqstd_register_bloblib(HSQUIRRELVM v);
  642. SQUIRREL_API SQRESULT sqstd_register_iolib(HSQUIRRELVM v);
  643. SQUIRREL_API SQInteger sqstd_register_systemlib(HSQUIRRELVM v);
  644. SQUIRREL_API SQRESULT sqstd_register_mathlib(HSQUIRRELVM v);
  645. SQUIRREL_API SQRESULT sqstd_register_stringlib(HSQUIRRELVM v);
  646. SQUIRREL_API SQRESULT sqstd_register_Sq_Fpdf(HSQUIRRELVM v);
  647. SQUIRREL_API SQRESULT sqstd_register_sqfs(HSQUIRRELVM v);
  648. SQUIRREL_API void sqstd_seterrorhandlers(HSQUIRRELVM v);
  649. SQUIRREL_API void sqstd_printcallstack(HSQUIRRELVM v);
  650. #ifdef __cplusplus
  651. } /*extern "C"*/
  652. #endif
  653. void sq_printfunc(HSQUIRRELVM v,const SQChar *s,...)
  654. {
  655. va_list vl;
  656. va_start(vl, s);
  657. vfprintf(stdout, s, vl);
  658. va_end(vl);
  659. }
  660. void sq_errorfunc(HSQUIRRELVM v,const SQChar *s,...)
  661. {
  662. va_list vl;
  663. va_start(vl, s);
  664. vfprintf(stderr, s, vl);
  665. va_end(vl);
  666. }
  667. static HSQUIRRELVM my_new_squirrel(struct mg_context *ctx) {
  668. HSQUIRRELVM v = sq_open(1024);
  669. if(!v) return 0;
  670. sqstd_seterrorhandlers(v); //registers the default error handlers
  671. sq_setprintfunc(v, sq_printfunc, sq_errorfunc); //sets the print function
  672. sq_pushroottable(v);
  673. sqstd_register_bloblib(v);
  674. sqstd_register_iolib(v);
  675. sqstd_register_systemlib(v);
  676. sqstd_register_mathlib(v);
  677. sqstd_register_stringlib(v);
  678. sqstd_register_base64(v);
  679. sqstd_register_Sq_Fpdf(v);
  680. sqstd_register_SQLite3(v);
  681. sqstd_register_sqfs(v);
  682. sq_register_mix(v);
  683. sq_pushstring(v,sq_http_request_TAG, -1);
  684. sq_newclass(v,SQFalse);
  685. sq_settypetag(v,-1,(void*)sq_http_request_TAG);
  686. sq_insert_reg_funcs(v, mg_http_request_methods);
  687. sq_pushstring(v, _SC("data"), -1);
  688. sq_newtable(v);
  689. sq_newslot(v,-3,SQFalse);
  690. sq_newslot(v,-3,SQFalse);
  691. sq_insert_reg_funcs(v, sq_mg_methods);
  692. sq_pushliteral(v, "APP_ROOT_FOLDER");
  693. sq_pushstring(v, mg_get_option(ctx, "document_root"), -1);
  694. sq_newslot(v, -3, SQFalse);
  695. sq_poptop(v); //remove roottable
  696. return v;
  697. }
  698. SQInteger blob_read(SQUserPointer file,SQUserPointer p,SQInteger size);
  699. SQInteger loadstring(HSQUIRRELVM v, const char * bcode, SQInteger bcode_size)
  700. {
  701. SQBlob b(0, bcode_size);
  702. b.Write(bcode, bcode_size);
  703. b.Seek(0, SQ_SEEK_SET);
  704. SQInteger rc = sq_readclosure(v, blob_read, &b);
  705. return rc < 0 ? rc : 1;
  706. }
  707. // dispatches a callback to a Lua function if one is registered
  708. static void *
  709. user_callback_proxy(enum mg_event event,
  710. struct mg_connection *conn,
  711. const struct mg_request_info *ri)
  712. {
  713. HSQUIRRELVM v;
  714. int e;
  715. switch(event){
  716. case MG_NEW_MASTER_PLUGIN: {
  717. SQ_Mg_Context *sq_mg_ctx = (SQ_Mg_Context *) conn;
  718. SQ_MG_Callback *cb = sq_mg_ctx->master_plugin;
  719. v = my_new_squirrel((struct mg_context *)ri);
  720. if(!v) return 0;
  721. if(cb){
  722. if (loadstring(v, cb->buf, cb->size) == 1) {
  723. sq_pushroottable(v);
  724. if(sq_call(v, 1, SQFalse, SQTrue) != SQ_OK){
  725. sq_errorfunc(v, "sq_call failed %d\n%s", __LINE__, sq_getlasterror_str(v));
  726. }
  727. sq_poptop(v); //remove function from stack
  728. }
  729. }
  730. return v;
  731. }
  732. break;
  733. case MG_FREE_MASTER_PLUGIN:
  734. v = (HSQUIRRELVM)conn;
  735. if(v) {
  736. SQ_Mg_Context *sq_mg_ctx = (SQ_Mg_Context *) ri;
  737. SQ_MG_Callback *cb = sq_mg_ctx->master_plugin_exit;
  738. if(cb){
  739. if (loadstring(v, cb->buf, cb->size) == 1) {
  740. sq_pushroottable(v);
  741. if(sq_call(v, 1, SQFalse, SQFalse) != SQ_OK){
  742. sq_errorfunc(v, "sq_call failed %d\n%s", __LINE__, sq_getlasterror_str(v));
  743. }
  744. sq_poptop(v); //remove function from stack
  745. }
  746. else sq_errorfunc(v, "sq_call failed %d\n%s", __LINE__, sq_getlasterror_str(v));
  747. }
  748. sq_close(v);
  749. }
  750. return 0;
  751. break;
  752. case MG_NEW_PLUGIN:{
  753. SQ_Mg_Context *sq_mg_ctx = (SQ_Mg_Context *) mg_get_user_data(conn);
  754. if(!sq_mg_ctx) return 0;
  755. SQ_MG_Callback *cb = sq_mg_ctx->user_callback;
  756. if (cb) {
  757. v = my_new_squirrel((struct mg_context *)ri);
  758. if(!v) return 0;
  759. SQInteger top = sq_gettop(v);
  760. SQ_MG_Callback *cb_setup = sq_mg_ctx->user_callback_setup;
  761. if(cb_setup){
  762. if (loadstring(v, cb_setup->buf, cb_setup->size) == 1) {
  763. sq_pushroottable(v);
  764. if(sq_call(v, 1, SQFalse, SQFalse) != SQ_OK){
  765. sq_errorfunc(v, "sq_call failed %d\n%s", __LINE__, sq_getlasterror_str(v));
  766. }
  767. sq_poptop(v); //remove function from stack
  768. }
  769. else sq_errorfunc(v, "sq_call failed %d\n%s", __LINE__, sq_getlasterror_str(v));
  770. }
  771. sq_pushroottable(v);
  772. sq_pushstring(v, sq_mg_user_callback, -1);
  773. if (loadstring(v, cb->buf, cb->size) == 1) {
  774. sq_newslot(v, -3, SQFalse);
  775. }
  776. else sq_errorfunc(v, "sq_call failed %d\n%s", __LINE__, sq_getlasterror_str(v));
  777. sq_settop(v, top); //remove everithing left on stack while working here
  778. return v;
  779. }
  780. }
  781. return 0;
  782. case MG_FREE_PLUGIN:
  783. v = (HSQUIRRELVM)mg_get_plugin(conn);
  784. if(v){
  785. SQ_Mg_Context *sq_mg_ctx = (SQ_Mg_Context *) ri;
  786. if(sq_mg_ctx){
  787. SQ_MG_Callback *cb = sq_mg_ctx->user_callback_exit;
  788. if(cb){
  789. if (loadstring(v, cb->buf, cb->size) == 1) {
  790. sq_pushroottable(v);
  791. if(sq_call(v, 1, SQFalse, SQFalse) != SQ_OK){
  792. sq_errorfunc(v, "sq_call failed %d\n%s", __LINE__, sq_getlasterror_str(v));
  793. }
  794. sq_poptop(v); //remove function from stack
  795. }
  796. else sq_errorfunc(v, "sq_call failed %d\n%s", __LINE__, sq_getlasterror_str(v));
  797. }
  798. }
  799. sq_close(v);
  800. }
  801. return NULL;
  802. case MG_INIT_SSL:
  803. #ifdef USE_AXTLS
  804. //lua_pushstring(L, "MG_INIT_SSL");
  805. *((void**)conn) = SSL_CTX_new(0);
  806. return 0;
  807. #endif
  808. case MG_NEW_REQUEST:
  809. case MG_HTTP_ERROR:
  810. case MG_EVENT_LOG:{
  811. v = (HSQUIRRELVM) mg_get_plugin(conn);
  812. if (v) {
  813. SQInteger saved_top = sq_gettop(v);
  814. //SQ_Mg_Context *sq_mg_ctx = (SQ_Mg_Context *) mg_get_user_data(conn);
  815. sq_pushstring(v, sq_mg_user_callback, -1);
  816. if(sq_getonroottable(v) != SQ_OK) {
  817. write_error_message(conn, "failed to find mg_user_callback", -1);
  818. return NULL;
  819. }
  820. sq_pushroottable(v);
  821. switch (event) {
  822. case MG_NEW_REQUEST: sq_pushliteral(v, "MG_NEW_REQUEST"); break;
  823. case MG_HTTP_ERROR: sq_pushliteral(v, "MG_HTTP_ERROR"); break;
  824. case MG_EVENT_LOG: sq_pushliteral(v, "MG_EVENT_LOG"); break;
  825. case MG_INIT_SSL: sq_pushliteral(v, "MG_INIT_SSL"); break;
  826. default: sq_pushnull(v); break;
  827. }
  828. sq_pushstring(v, sq_http_request_TAG, -1);
  829. if(sq_getonroottable(v) == SQ_OK){
  830. sq_pushroottable(v);
  831. if(sq_call(v, 1, SQTrue, SQFalse) == SQ_OK){
  832. sq_remove(v, -2); //remove class
  833. sq_setinstanceup(v, -1, conn);
  834. }
  835. }
  836. push_request(v, ri);
  837. if(sq_call(v, 3, SQTrue, SQFalse) != SQ_OK) {
  838. write_error_message(conn, sq_getlasterror_str(v), -1);
  839. e = 0;
  840. } else {
  841. SQBool bval;
  842. if(sq_getbool(v, -1, &bval) == SQ_OK) e = bval == SQTrue ? 1 : 0;
  843. else e = 0;
  844. }
  845. sq_settop(v, saved_top);
  846. return (void *) e;
  847. }
  848. }
  849. default:
  850. return NULL;
  851. }
  852. return NULL;
  853. }
  854. #ifdef __cplusplus
  855. extern "C" {
  856. #endif
  857. SQRESULT sqstd_register_mongoose(HSQUIRRELVM v)
  858. {
  859. sq_insert_reg_funcs(v, sq_mg_methods);
  860. sq_pushstring(v,sq_mongoose_TAG, -1);
  861. sq_newclass(v,SQFalse);
  862. sq_settypetag(v,-1,(void*)sq_mongoose_TAG);
  863. sq_insert_reg_funcs(v, sq_mongoose_methods);
  864. sq_newslot(v,-3,SQTrue);
  865. return 1;
  866. }
  867. #ifdef __cplusplus
  868. }
  869. #endif