sqmongoose.cpp 28 KB

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