sq_mongoose.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdarg.h>
  5. #include <time.h>
  6. #include "squirrel.h"
  7. #include "sqstdblobimpl.h"
  8. SQ_OPT_STRING_STRLEN();
  9. #ifdef USE_SQ_SQLITE3
  10. #include "lsqlite3.h"
  11. #endif
  12. #if defined(USE_AXTLS) || defined(USE_OPENSSL)
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. void * SSL_CTX_new(void *);
  17. #ifdef __cplusplus
  18. }
  19. #endif
  20. #endif
  21. #define USE_MG_MD5 1
  22. #include "mongoose.h"
  23. //#include "civetweb.h"
  24. #define PRINT_FILE_LINE printf("%s %d\n", __FILE__,__LINE__);
  25. #ifdef UNUSED
  26. #elif defined(__GNUC__)
  27. # define UNUSED(x) UNUSED_ ## x __attribute__((unused))
  28. #elif defined(__LCLINT__)
  29. # define UNUSED(x) /*@unused@*/ x
  30. #else
  31. # define UNUSED(x) x
  32. #endif
  33. #define MARK() do { \
  34. printf("%s:%d (%s)\n", __FILE__, __LINE__, __FUNCTION__); \
  35. } while (0)
  36. typedef struct SQ_Mg_Context SQ_Mg_Context;
  37. typedef struct SQ_MG_Callback SQ_MG_Callback;
  38. struct SQ_MG_Callback {
  39. size_t size;
  40. char buf[1];
  41. };
  42. static SQ_MG_Callback *SQ_MG_Callback_malloc(size_t x){
  43. SQ_MG_Callback *p = (SQ_MG_Callback *)sq_malloc(sizeof(SQ_MG_Callback)+x);
  44. if(p) p->size = x;
  45. return p;
  46. }
  47. static void SQ_MG_Callback_free(SQ_MG_Callback *p){
  48. if(p) sq_free(p, sizeof(SQ_MG_Callback)+p->size);
  49. }
  50. struct SQ_Mg_Context {
  51. HSQUIRRELVM v;
  52. struct mg_context *ctx;
  53. SQ_MG_Callback *master_plugin;
  54. SQ_MG_Callback *master_plugin_exit;
  55. SQ_MG_Callback *user_callback;
  56. SQ_MG_Callback *user_callback_setup;
  57. SQ_MG_Callback *user_callback_exit;
  58. };
  59. static const SQChar sq_mg_context_TAG[] = "sq_mg_conn_class";
  60. #define GET_mg_context_INSTANCE() \
  61. SQ_Mg_Context *self; \
  62. if((_rc_ = sq_getinstanceup(v,1,(SQUserPointer*)&self,(void*)sq_mg_context_TAG)) < 0) return _rc_;
  63. static const SQChar sq_mg_user_callback[] = "mg_user_callback";
  64. static const SQChar sq_http_request_TAG[] = "HttpRequest";
  65. #define GET_http_request_INSTANCE() \
  66. struct mg_connection *conn; \
  67. if((_rc_ = sq_getinstanceup(v,1,(SQUserPointer*)&conn,(void*)sq_http_request_TAG)) < 0) return _rc_;
  68. static SQRESULT sq_http_request_releasehook(SQUserPointer p, SQInteger size, HSQUIRRELVM v)
  69. {
  70. return 1;
  71. }
  72. static SQRESULT sq_http_request_constructor(HSQUIRRELVM v)
  73. {
  74. //SQ_FUNC_VARS_NO_TOP(v);
  75. struct mg_connection *conn = 0;
  76. sq_setinstanceup(v, 1, conn);
  77. sq_setreleasehook(v,1, sq_http_request_releasehook);
  78. return 1;
  79. }
  80. static SQRESULT
  81. sq_http_request_print(HSQUIRRELVM v)
  82. {
  83. SQ_FUNC_VARS(v);
  84. GET_http_request_INSTANCE();
  85. SQInteger i, write_count = 0;
  86. for (i = 2; i <= _top_; ++i) {
  87. sq_tostring(v, i);
  88. SQ_GET_STRING(v, -1, value);
  89. write_count += mg_write(conn, value, value_size);
  90. sq_poptop(v);
  91. }
  92. sq_pushinteger(v, write_count);
  93. return 1;
  94. }
  95. #ifdef USE_SQ_LSQLITE3
  96. static SQRESULT
  97. sq_http_request_vm_print(HSQUIRRELVM v)
  98. {
  99. GET_http_request_INSTANCE();
  100. lsqlite3_sdb_vm *svm = (lsqlite3_sdb_vm *)luaL_checkudata(v, 1,
  101. lsqlite3_sqlite_vm_meta);
  102. SQInteger idx = luaL_checkint(v,2);
  103. SQInteger len = sqlite3_column_bytes(svm->vm, idx);
  104. if(len) sq_pushinteger(v, mg_write(conn, sqlite3_column_text(svm->vm, idx), len));
  105. else sq_pushinteger(v, 0);
  106. return 1;
  107. }
  108. #endif
  109. static SQRESULT
  110. sq_http_request_write(HSQUIRRELVM v)
  111. {
  112. SQ_FUNC_VARS(v);
  113. GET_http_request_INSTANCE();
  114. SQ_GET_STRING(v, 2, buf);
  115. SQ_OPT_INTEGER(v, 3, write_size, buf_size);
  116. sq_pushinteger(v, mg_write(conn, buf, write_size));
  117. return 1;
  118. }
  119. static SQRESULT
  120. sq_http_request_read(HSQUIRRELVM v)
  121. {
  122. SQ_FUNC_VARS(v);
  123. GET_http_request_INSTANCE();
  124. SQ_OPT_INTEGER(v, 2, n, 1024*2000);
  125. if(n <= 0) return sq_throwerror(v, _SC("invalid size to read (%d)"), n);
  126. size_t rlen; /* how much to read */
  127. size_t nr; /* number of chars actually read */
  128. rlen = 8192; /* try to read that much each time */
  129. SQBlob *blob = NULL;
  130. if(_top_ > 2) {
  131. if(SQ_FAILED(sq_getinstanceup(v,3,(SQUserPointer*)&blob,(SQUserPointer)SQBlob::SQBlob_TAG)))
  132. return sq_throwerror(v,_SC("expect a blob as second parameter"));
  133. if(!blob || !blob->IsValid())
  134. return sq_throwerror(v,_SC("the blob is invalid"));
  135. } else {
  136. blob = new SQBlob(0, rlen);
  137. }
  138. if (rlen > n) rlen = n; /* cannot read more than asked */
  139. char *p = sq_getscratchpad(v,rlen);
  140. do {
  141. //there is a bug in axtls that can return a number bigger than the actual bytes transfered
  142. nr = mg_read(conn, p, rlen);
  143. blob->Write(p, nr);
  144. n -= nr; /* still have to read `n' chars */
  145. } while (n > 0 && nr == rlen); /* until end of count or eof */
  146. if(_top_ <= 2) {
  147. sq_pushstring(v, (const SQChar *)blob->GetBuf(), blob->Len()); /* close buffer */
  148. delete blob;
  149. return 1;
  150. }
  151. return 0;
  152. }
  153. static SQRESULT
  154. sq_http_request_send_file(HSQUIRRELVM v)
  155. {
  156. SQ_FUNC_VARS_NO_TOP(v);
  157. GET_http_request_INSTANCE();
  158. SQ_GET_STRING(v, 2, file_path);
  159. mg_send_file(conn, file_path);
  160. return 0;
  161. }
  162. static SQRESULT
  163. sq_http_request_write_blob(HSQUIRRELVM v)
  164. {
  165. SQ_FUNC_VARS_NO_TOP(v);
  166. GET_http_request_INSTANCE();
  167. SQBlob *blob = NULL;
  168. { if(SQ_FAILED(sq_getinstanceup(v,2,(SQUserPointer*)&blob,(SQUserPointer)SQBlob::SQBlob_TAG)))
  169. return sq_throwerror(v,_SC("invalid type tag")); }
  170. if(!blob || !blob->IsValid())
  171. return sq_throwerror(v,_SC("the blob is invalid"));
  172. sq_pushinteger(v, mg_write(conn, (const SQChar*)blob->GetBuf(), blob->Len()));
  173. return 1;
  174. }
  175. static SQRESULT
  176. sq_http_request_get_header(HSQUIRRELVM v)
  177. {
  178. SQ_FUNC_VARS_NO_TOP(v);
  179. GET_http_request_INSTANCE();
  180. SQ_GET_STRING(v, 2, name);
  181. sq_pushstring(v, mg_get_header(conn, name), -1);
  182. return 1;
  183. }
  184. static SQRESULT
  185. sq_http_request_get_cookie(HSQUIRRELVM v)
  186. {
  187. SQ_FUNC_VARS_NO_TOP(v);
  188. GET_http_request_INSTANCE();
  189. SQ_GET_STRING(v, 2, cookie_name);
  190. const char *start;
  191. int len = mg_find_cookie(conn, cookie_name, &start);
  192. if(len > 0) sq_pushstring(v, start, len-1);
  193. else sq_pushnull(v);
  194. return 1;
  195. }
  196. static SQRESULT
  197. sq_http_request_get_var(HSQUIRRELVM v)
  198. {
  199. SQ_FUNC_VARS_NO_TOP(v);
  200. GET_http_request_INSTANCE();
  201. SQ_GET_STRING(v, 2, data);
  202. SQ_GET_STRING(v, 3, name);
  203. const char *start;
  204. size_t buffer_len;
  205. int var_len = mg_find_var(data, data_size, name, &start);
  206. if(var_len > 0){
  207. buffer_len = var_len+1;
  208. char *buffer = sq_getscratchpad(v,buffer_len);
  209. if(buffer){
  210. var_len = mg_url_decode(start, var_len, buffer, buffer_len, 1);
  211. sq_pushstring(v, buffer, var_len);
  212. return 1;
  213. }
  214. }
  215. sq_pushnull(v);
  216. return 1;
  217. }
  218. static SQRESULT
  219. sq_http_request_get_conn_buf(HSQUIRRELVM v)
  220. {
  221. SQ_FUNC_VARS_NO_TOP(v);
  222. GET_http_request_INSTANCE();
  223. int buf_size;
  224. const char *buf = mg_get_conn_buf(conn, &buf_size);
  225. sq_pushstring(v, buf, buf_size);
  226. return 1;
  227. }
  228. #ifndef _WIN32_WCE
  229. static SQRESULT
  230. sq_http_request_handle_cgi_request(HSQUIRRELVM v)
  231. {
  232. SQ_FUNC_VARS_NO_TOP(v);
  233. GET_http_request_INSTANCE();
  234. SQ_GET_STRING(v, 2, prog);
  235. mg_handle_cgi_request(conn, prog);
  236. return 0;
  237. }
  238. #endif
  239. static SQRESULT
  240. sq_http_request_get_option(HSQUIRRELVM v)
  241. {
  242. SQ_FUNC_VARS_NO_TOP(v);
  243. GET_http_request_INSTANCE();
  244. SQ_GET_STRING(v, 2, name);
  245. SQ_Mg_Context *sq_mg_ctx = (SQ_Mg_Context *) mg_get_user_data(conn);
  246. sq_pushstring(v, mg_get_option(sq_mg_ctx->ctx, name), -1);
  247. return 1;
  248. }
  249. //Digest authentication functions
  250. #define MAX_USER_LEN 20
  251. #define MAX_NONCE_LEN 36 //extra bytes for zero terminator
  252. #define MAX_SESSIONS 25
  253. #define SESSION_TTL 3600
  254. #define SESSION_REQUEST_TTL 120
  255. enum e_session_state {e_session_invalid, e_session_sent_request, e_session_authorized};
  256. struct st_session_t
  257. {
  258. int state;
  259. char nonce[MAX_NONCE_LEN];
  260. char user[MAX_USER_LEN]; // Authenticated user
  261. long last_nc;
  262. time_t last_access; // last_access time
  263. long remote_ip; // Client's IP address
  264. };
  265. static struct st_session_t sessions[MAX_SESSIONS]; // Current sessions
  266. // Protects messages, sessions, last_message_id
  267. static mg_thread_mutex_t session_rwlock;
  268. //should be called only after locking session_rwlock
  269. static struct st_session_t *my_get_session_only_after_lock(const char *nonce){
  270. int i;
  271. for(i=0; i < MAX_SESSIONS; ++i){
  272. //printf("%s : %s\n", sessions[i].nonce, ah->nonce);
  273. if(strcmp(sessions[i].nonce, nonce) == 0){
  274. return &sessions[i];
  275. }
  276. }
  277. return 0;
  278. }
  279. // Check the user's password, return 1 if OK
  280. static SQBool my_check_password(const struct mg_request_info *request_info, const char *my_tmp_password)
  281. {
  282. md5_buf_t ha2, expected_response;
  283. struct mg_auth_header *ah = request_info->ah;
  284. // Some of the parameters may be NULL
  285. if (request_info->request_method == NULL ||
  286. //strcmp(ri->request_method, "PUT") == 0 ||
  287. //strcmp(ri->request_method, "DELETE") == 0 ||
  288. ah == NULL ||
  289. ah->nonce == NULL || ah->nc == NULL ||
  290. ah->cnonce == NULL || ah->qop == NULL || ah->uri == NULL ||
  291. ah->response == NULL)
  292. {
  293. return SQFalse;
  294. }
  295. // NOTE(lsm): due to a bug in MSIE, we do not compare the URI
  296. // TODO(lsm): check for authentication timeout
  297. if (// strcmp(ah->uri, conn->request_info.uri) != 0 ||
  298. strlen(ah->response) != 32
  299. )
  300. {
  301. return SQFalse;
  302. }
  303. mg_md5(ha2, request_info->request_method, ":", ah->uri, NULL);
  304. mg_md5(expected_response, my_tmp_password /*ah->ha1*/, ":", ah->nonce, ":", ah->nc,
  305. ":", ah->cnonce, ":", ah->qop, ":", ha2, NULL);
  306. SQBool result = mg_strcasecmp(ah->response, expected_response) == 0;
  307. //printf("%s : %s : %s\n", my_tmp_password, ah->response, expected_response);
  308. if(result) //lets check timeout and other issues
  309. {
  310. struct st_session_t *session_found = 0;
  311. result = SQFalse;
  312. mg_thread_mutex_lock(&session_rwlock);
  313. session_found = my_get_session_only_after_lock(ah->nonce);
  314. if(session_found)
  315. {
  316. do
  317. {
  318. //mobile ip is a pain for security
  319. if( /*(ses.remote_ip != request_info->remote_ip) ||*/
  320. ((time(NULL) - session_found->last_access) > SESSION_TTL)
  321. ) break;
  322. if(session_found->state == e_session_sent_request)
  323. {
  324. session_found->state = e_session_authorized;
  325. snprintf(session_found->user, sizeof(session_found->user), "%s", ah->user);
  326. }
  327. else if(strcmp(session_found->user, ah->user) != 0) break;
  328. long recived_nc = strtol(ah->nc, 0, 16);
  329. time_t received_time = time(NULL);
  330. /*
  331. //printf("%d : %lu : %lu : %lu : %lu\n", request_info->remote_port, ses.last_nc, recived_nc, ses.last_access, received_time);
  332. if((ses.last_nc+1) != recived_nc){
  333. //lets see if we can manage out of order sent by some browsers
  334. if((received_time - ses.last_access) > 2) break;
  335. //inside a window of 2 seconds we tolerate nc out of order
  336. if(ses.last_nc > recived_nc) recived_nc = ses.last_nc;
  337. }
  338. */
  339. session_found->last_access = received_time;
  340. session_found->last_nc = recived_nc;
  341. result = SQTrue;
  342. } while(0);
  343. if(!result){
  344. session_found->state = e_session_invalid;
  345. session_found->nonce[0] = '\0';
  346. }
  347. }
  348. else {
  349. //dbg_msg("Session not found ! : %s : %s\n", ah->nonce, request_info->uri);
  350. }
  351. mg_thread_mutex_unlock(&session_rwlock);
  352. }
  353. return result;
  354. }
  355. static SQRESULT
  356. sq_http_request_check_password(HSQUIRRELVM v)
  357. {
  358. SQ_FUNC_VARS_NO_TOP(v);
  359. GET_http_request_INSTANCE();
  360. SQ_GET_STRING(v, 2, my_tmp_password);
  361. const struct mg_request_info *request_info = mg_get_request_info(conn);
  362. sq_pushbool(v, my_check_password(request_info, my_tmp_password));
  363. return 1;
  364. }
  365. // Close user session return 1 if closed
  366. static SQBool my_close_session(const struct mg_request_info *request_info)
  367. {
  368. SQBool result = SQFalse;
  369. struct st_session_t *session_found = 0;
  370. struct mg_auth_header *ah = request_info->ah;
  371. // Some of the parameters may be NULL
  372. if (request_info->request_method == NULL ||
  373. //strcmp(ri->request_method, "PUT") == 0 ||
  374. //strcmp(ri->request_method, "DELETE") == 0 ||
  375. ah == NULL ||
  376. ah->nonce == NULL || ah->nc == NULL ||
  377. ah->cnonce == NULL || ah->qop == NULL || ah->uri == NULL ||
  378. ah->response == NULL)
  379. {
  380. return 0;
  381. }
  382. mg_thread_mutex_lock(&session_rwlock);
  383. session_found = my_get_session_only_after_lock(ah->nonce);
  384. if(session_found) {
  385. session_found->state = e_session_invalid;
  386. session_found->nonce[0] = '\0';
  387. result = SQTrue;
  388. }
  389. mg_thread_mutex_unlock(&session_rwlock);
  390. return result;
  391. }
  392. static SQRESULT
  393. sq_http_request_close_session(HSQUIRRELVM v)
  394. {
  395. SQ_FUNC_VARS_NO_TOP(v);
  396. GET_http_request_INSTANCE();
  397. const struct mg_request_info *request_info = mg_get_request_info(conn);
  398. sq_pushbool(v, my_close_session(request_info));
  399. return 1;
  400. }
  401. typedef char buf_1024_t[1024];
  402. static const char *my_encode_nonce(buf_1024_t buf, md5_buf_t md5_buf, unsigned long ip)
  403. {
  404. snprintf(buf, sizeof(buf_1024_t), "%lu:%d:%d:%lu", (unsigned long) time(NULL), rand(), rand(), ip);
  405. mg_md5(md5_buf, buf, NULL);
  406. return md5_buf;
  407. }
  408. static void my_send_authorization_request(struct mg_connection *conn,
  409. const struct mg_request_info *request_info,
  410. const char *authentication_domain,
  411. const char *nonce)
  412. {
  413. md5_buf_t md5_buf;
  414. buf_1024_t buf;
  415. int i;
  416. if (nonce == NULL)
  417. {
  418. nonce = my_encode_nonce(buf, md5_buf, (unsigned long) request_info->remote_ip);
  419. }
  420. struct st_session_t *available_session = NULL;
  421. mg_thread_mutex_lock(&session_rwlock);
  422. time_t now = time(NULL);
  423. for(int j=0; j<3; ++j)
  424. {
  425. for(i=0; i < MAX_SESSIONS; ++i){
  426. if(sessions[i].state == e_session_invalid){
  427. break;
  428. }
  429. if(sessions[i].state == e_session_sent_request){
  430. if((now - sessions[i].last_access) > SESSION_REQUEST_TTL){
  431. //if session request bigger than 2 minutes reuse it
  432. break;
  433. }
  434. }
  435. //on the second intent we will reuse idle authorized sessions
  436. if( (j > 0) && sessions[i].state == e_session_authorized){
  437. if((now - sessions[i].last_access) > (SESSION_REQUEST_TTL*5)){
  438. //if session request bigger than 10 minutes reuse it
  439. break;
  440. }
  441. }
  442. }
  443. if(i < MAX_SESSIONS){
  444. available_session = &sessions[i];
  445. available_session->state = e_session_sent_request;
  446. available_session->last_access = now;
  447. available_session->last_nc = 0;
  448. available_session->remote_ip = request_info->remote_ip;
  449. snprintf(available_session->nonce, sizeof(available_session->nonce),
  450. "%s", nonce);
  451. break;
  452. }
  453. else
  454. {
  455. nonce = my_encode_nonce(buf, md5_buf, (unsigned long) request_info->remote_ip);
  456. }
  457. }
  458. mg_thread_mutex_unlock(&session_rwlock);
  459. if(available_session) {
  460. //warning android ics/jelly bean expect realm to follow digest
  461. //otherwise it will not connect
  462. i = snprintf(buf, sizeof(buf),
  463. "HTTP/1.1 401 Unauthorized\r\n"
  464. "WWW-Authenticate: Digest realm=\"%s\", qop=\"auth\", nonce=\"%s\"\r\n"
  465. "Content-Length: 0\r\n\r\n",
  466. authentication_domain, nonce);
  467. } else {
  468. i = snprintf(buf, sizeof(buf),
  469. "HTTP/1.1 503 Service Temporary Unavailable\r\n"
  470. "Content-Length: 0\r\n\r\n");
  471. }
  472. //printf("%s", buf);
  473. mg_write(conn, buf, i);
  474. }
  475. static SQRESULT
  476. sq_http_request_send_authorization_request(HSQUIRRELVM v)
  477. {
  478. SQ_FUNC_VARS_NO_TOP(v);
  479. GET_http_request_INSTANCE();
  480. SQ_GET_STRING(v, 2, authentication_domain);
  481. const struct mg_request_info *request_info = mg_get_request_info(conn);
  482. my_send_authorization_request(conn, request_info, authentication_domain, NULL);
  483. return 0;
  484. }
  485. #define _DECL_FUNC(name,nparams,tycheck) {_SC(#name), sq_http_request_##name,nparams,tycheck}
  486. static SQRegFunction mg_http_request_methods[] =
  487. {
  488. _DECL_FUNC(constructor, 1, _SC("x")),
  489. _DECL_FUNC(print, -2, _SC("x n|s")),
  490. _DECL_FUNC(read, -2, _SC("xix")),
  491. _DECL_FUNC(write, 3, _SC("xsi")),
  492. _DECL_FUNC(write_blob, -2, _SC("xxi")),
  493. _DECL_FUNC(get_var, 3, _SC("xs")),
  494. _DECL_FUNC(get_cookie, 2, _SC("xs")),
  495. _DECL_FUNC(get_header, 2, _SC("xs")),
  496. _DECL_FUNC(send_file, 2, _SC("xs")),
  497. #ifndef _WIN32_WCE
  498. _DECL_FUNC(handle_cgi_request, 2, _SC("xs")),
  499. #endif
  500. _DECL_FUNC(get_option, 2, _SC("xs")),
  501. _DECL_FUNC(check_password, 2, _SC("xs")),
  502. _DECL_FUNC(close_session, 1, _SC("x")),
  503. _DECL_FUNC(send_authorization_request, 2, _SC("xs")),
  504. _DECL_FUNC(get_conn_buf, 1, _SC("x")),
  505. {0,0}
  506. };
  507. #undef _DECL_FUNC
  508. /***********/
  509. // helper function to extract a single mg_config value from a Lua table
  510. static void
  511. fetchfield(HSQUIRRELVM v, int idx, const char *key, char **value, const char *d)
  512. {
  513. const char *s;
  514. sq_pushstring(v, key, -1);
  515. if(sq_rawget(v, idx) == SQ_OK){
  516. if(sq_gettype(v, -1) != OT_NULL){
  517. sq_tostring(v, -1);
  518. sq_getstring(v, -1, &s);
  519. *value = mg_strdup(s);
  520. sq_pop(v, 2); //retrieved value and converted value
  521. return;
  522. }
  523. }
  524. *value = NULL;
  525. }
  526. // initializes an options string array from a Lua table
  527. static SQRESULT
  528. fetchoptions(HSQUIRRELVM v, int idx, const char **options)
  529. {
  530. struct {
  531. const char *key;
  532. const char *value;
  533. } OPTIONS[] = {
  534. { "cgi_extensions", ".cgi,.pl,.php" },
  535. { "cgi_environment", NULL },
  536. { "put_delete_passwords_file", NULL },
  537. { "cgi_interpreter", NULL },
  538. { "protect_uri", NULL },
  539. { "authentication_domain", "mydomain.com" },
  540. { "ssi_extensions", ".shtml,.shtm" },
  541. { "access_log_file", NULL },
  542. { "ssl_chain_file", NULL },
  543. { "enable_directory_listing", "yes" },
  544. { "error_log_file", NULL },
  545. { "global_passwords_file", NULL },
  546. { "index_files", "index.html,index.htm,index.cgi" },
  547. { "enable_keep_alive", "no" },
  548. { "enable_tcp_nodelay", "no" },
  549. { "access_control_list", NULL },
  550. { "max_request_size", "16384" },
  551. { "extra_mime_types", NULL },
  552. { "listening_ports", "8080" },
  553. { "document_root", "." },
  554. { "ssl_certificate", NULL },
  555. { "num_threads", "10" },
  556. { "request_timeout_ms", "30000" },
  557. { "run_as_user", NULL },
  558. { NULL, NULL }
  559. };
  560. char *value;
  561. int i, j;
  562. if(sq_gettype(v, idx) != OT_TABLE) return sq_throwerror(v, "a table is needed to retrieve options");
  563. for (i = 0, j = 0; OPTIONS[i].key; ++i) {
  564. fetchfield(v, idx, OPTIONS[i].key, &value, OPTIONS[i].value);
  565. if (NULL != value) {
  566. options[j++] = mg_strdup(OPTIONS[i].key);
  567. options[j++] = value;
  568. }
  569. }
  570. options[j] = NULL;
  571. return 0;
  572. }
  573. /***********/
  574. static const char sq_mongoose_TAG[] = "Mongoose";
  575. static SQBool show_errors_on_stdout = SQFalse;
  576. static void sq_mongoose_release_references(SQ_Mg_Context *self){
  577. SQ_MG_Callback_free(self->master_plugin);
  578. SQ_MG_Callback_free(self->master_plugin_exit);
  579. SQ_MG_Callback_free(self->user_callback);
  580. SQ_MG_Callback_free(self->user_callback_setup);
  581. SQ_MG_Callback_free(self->user_callback_exit);
  582. }
  583. static SQRESULT sq_mongoose_releasehook(SQUserPointer p, SQInteger size, HSQUIRRELVM v)
  584. {
  585. SQ_Mg_Context *self = (SQ_Mg_Context *)p;
  586. if(self){
  587. if(self->ctx){
  588. mg_stop(self->ctx);
  589. sq_mongoose_release_references(self);
  590. self->ctx = 0;
  591. }
  592. sq_free(self, sizeof(SQ_Mg_Context));
  593. }
  594. return 0;
  595. }
  596. static SQRESULT sq_mongoose_constructor(HSQUIRRELVM v)
  597. {
  598. //SQ_FUNC_VARS_NO_TOP(v);
  599. SQ_Mg_Context *sq_mg_ctx = (SQ_Mg_Context *)sq_malloc(sizeof(SQ_Mg_Context));
  600. memset(sq_mg_ctx, 0, sizeof(SQ_Mg_Context));
  601. sq_setinstanceup(v, 1, sq_mg_ctx);
  602. sq_setreleasehook(v,1, sq_mongoose_releasehook);
  603. return 1;
  604. }
  605. #define GET_mongoose_INSTANCE() \
  606. SQ_Mg_Context *self; \
  607. if((_rc_ = sq_getinstanceup(v,1,(SQUserPointer*)&self,(void*)sq_mongoose_TAG)) < 0) return _rc_;
  608. static SQRESULT
  609. sq_mongoose_show_errors_on_stdout(HSQUIRRELVM v)
  610. {
  611. SQ_FUNC_VARS_NO_TOP(v);
  612. SQ_GET_BOOL(v, 2, onOff);
  613. show_errors_on_stdout = onOff;
  614. return 0;
  615. }
  616. static SQRESULT
  617. sq_mongoose_modify_passwords_file(HSQUIRRELVM v)
  618. {
  619. SQ_FUNC_VARS_NO_TOP(v);
  620. SQ_GET_STRING(v, 2, passwords_file_name);
  621. SQ_GET_STRING(v, 3, domain);
  622. SQ_GET_STRING(v, 4, user);
  623. SQ_GET_STRING(v, 5, password);
  624. sq_pushinteger(v, mg_modify_passwords_file(passwords_file_name,
  625. domain, user, password));
  626. return 1;
  627. }
  628. static SQRESULT
  629. sq_mongoose_version(HSQUIRRELVM v)
  630. {
  631. sq_pushstring(v, mg_version(), -1);
  632. return 1;
  633. }
  634. SQInteger blob_write(SQUserPointer file,SQUserPointer p,SQInteger size);
  635. // creates a reference dispatching callbacks to squirrel functions
  636. static SQRESULT
  637. fetchcallback(HSQUIRRELVM v, const char *key, SQ_MG_Callback **sq_cb)
  638. {
  639. if(!sq_cb) return sq_throwerror(v, "inavlid SQ_MG_Callback parameter value (NULL)");
  640. if(sq_gettype(v, -1) != OT_TABLE) return sq_throwerror(v, "table expected to fetch callbacks");
  641. sq_pushstring(v, key, -1);
  642. sq_rawget(v, -2);
  643. if (sq_gettype(v, -1) == OT_CLOSURE){
  644. SQBlob b(0,8192);
  645. if(SQ_SUCCEEDED(sq_writeclosure(v,blob_write,&b))) {
  646. *sq_cb = SQ_MG_Callback_malloc(b.Len());
  647. if(*sq_cb) memcpy((*sq_cb)->buf, b.GetBuf(), b.Len());
  648. sq_poptop(v);
  649. }
  650. }
  651. else
  652. {
  653. sq_pop(v, 1);
  654. return sq_throwerror(v, "closure expected for callbacks");
  655. }
  656. return SQ_OK;
  657. }
  658. static void *
  659. user_callback_proxy(enum mg_event event,
  660. struct mg_connection *conn,
  661. const struct mg_request_info *ri);
  662. // creates a new server using a configuration table
  663. static SQRESULT
  664. sq_mongoose_start(HSQUIRRELVM v)
  665. {
  666. SQ_FUNC_VARS_NO_TOP(v);
  667. GET_mongoose_INSTANCE();
  668. _rc_ = 1; //assumes succeed
  669. const char *options[64];
  670. SQInteger i;
  671. if(self->ctx) return sq_throwerror(v, _SC("mongoose already running or stoped incorrectly"));
  672. // store the Squirrel vm for use in callback proxies
  673. self->v = v;
  674. // prepare the mg_config structure from the squirrel table argument
  675. memset(options, 0, sizeof (options));
  676. fetchoptions(v, 2, options);
  677. fetchcallback(v, "master_plugin", &self->master_plugin);
  678. fetchcallback(v, "master_plugin_exit", &self->master_plugin_exit);
  679. fetchcallback(v, "user_callback", &self->user_callback);
  680. fetchcallback(v, "user_callback_setup", &self->user_callback_setup);
  681. fetchcallback(v, "user_callback_exit", &self->user_callback_exit);
  682. self->ctx = mg_start(user_callback_proxy, self, options);
  683. // free the options string list memory
  684. for (i = 0; options[i]; ++i)
  685. free((void *) options[i]);
  686. // throw an error if the server did not start
  687. if (self->ctx == NULL) {
  688. sq_mongoose_release_references(self);
  689. _rc_ = sq_throwerror(v, "could not start mongoose");
  690. }
  691. // return the context so it can be stopped later
  692. return _rc_;
  693. }
  694. static SQRESULT
  695. sq_mongoose_stop(HSQUIRRELVM v)
  696. {
  697. SQ_FUNC_VARS_NO_TOP(v);
  698. GET_mongoose_INSTANCE();
  699. if(self->ctx){
  700. mg_stop(self->ctx);
  701. sq_mongoose_release_references(self);
  702. self->ctx = 0;
  703. }
  704. return 0;
  705. }
  706. #define _DECL_FUNC(name,nparams,tycheck) {_SC(#name), sq_mongoose_##name,nparams,tycheck}
  707. static SQRegFunction sq_mongoose_methods[] =
  708. {
  709. _DECL_FUNC(constructor, 1, _SC("x")),
  710. _DECL_FUNC(start, -1, _SC("x")),
  711. _DECL_FUNC(stop, 1, _SC("x")),
  712. _DECL_FUNC(modify_passwords_file, 1, _SC("x")),
  713. _DECL_FUNC(version, 1, _SC("x")),
  714. _DECL_FUNC(show_errors_on_stdout, 2, _SC("xb")),
  715. {0,0}
  716. };
  717. #undef _DECL_FUNC
  718. static SQRESULT
  719. sq_mg_url_decode_base(HSQUIRRELVM v, SQInteger is_form_url_encoded)
  720. {
  721. SQ_FUNC_VARS_NO_TOP(v);
  722. SQ_GET_STRING(v, 2, src);
  723. int dst_len = src_size +1;
  724. char *dst = sq_getscratchpad(v,dst_len);
  725. dst_len = mg_url_decode(src, src_size, dst, dst_len, is_form_url_encoded);
  726. sq_pushstring(v, dst, dst_len);
  727. return 1;
  728. }
  729. static SQRESULT
  730. sq_mg_url_decode(HSQUIRRELVM v)
  731. {
  732. return sq_mg_url_decode_base(v, 1);
  733. }
  734. static SQRESULT
  735. sq_mg_uri_decode(HSQUIRRELVM v)
  736. {
  737. return sq_mg_url_decode_base(v, 0);
  738. }
  739. static SQRESULT
  740. sq_mg_url_encode(HSQUIRRELVM v)
  741. {
  742. SQ_FUNC_VARS_NO_TOP(v);
  743. SQ_GET_STRING(v, 2, src);
  744. char *dst = mg_url_encode(src);
  745. sq_pushstring(v, dst, -1);
  746. free(dst);
  747. return 1;
  748. }
  749. static SQRESULT
  750. sq_mg_md5(HSQUIRRELVM v)
  751. {
  752. SQ_FUNC_VARS(v);
  753. char buf[32 + 1];
  754. unsigned char hash[16];
  755. MD5_CTX ctx;
  756. MD5Init(&ctx);
  757. for (int i = 2; i <= _top_; ++i) {
  758. SQ_GET_STRING(v, i, p);
  759. MD5Update(&ctx, (const unsigned char *) p, p_size);
  760. }
  761. MD5Final(hash, &ctx);
  762. mg_bin2str(buf, hash, sizeof(hash));
  763. sq_pushstring(v, buf, -1);
  764. return 1;
  765. }
  766. static SQRESULT
  767. sq_mg_debug_print(HSQUIRRELVM v)
  768. {
  769. SQ_FUNC_VARS(v);
  770. SQInteger i, write_count = 0;
  771. for (i = 2; i <= _top_; ++i) {
  772. sq_tostring(v, i);
  773. SQ_GET_STRING(v, -1, value);
  774. write_count += fwrite(value, 1, value_size, stderr);
  775. sq_poptop(v);
  776. }
  777. sq_pushinteger(v, write_count);
  778. return 1;
  779. }
  780. #ifdef JNI_ENABLE_LOG
  781. #include <android/log.h>
  782. /*
  783. ** A callback for the android_log() SQIntegererface.
  784. */
  785. static void jniLog(int iErrCode, const char *zMsg){
  786. __android_log_print(ANDROID_LOG_ERROR,"jniLuaServerLog","(%d) %s\n", iErrCode, zMsg);
  787. }
  788. static SQRESULT sq_mg_jniLog(HSQUIRRELVM v)
  789. {
  790. SQ_FUNC_VARS_NO_TOP(v);
  791. SQ_GET_INTEGER(v, 2, code);
  792. SQ_GET_STRING(v, 3, error_message);
  793. jniLog(code, error_message);
  794. return 0;
  795. }
  796. #endif
  797. #define _DECL_FUNC(name,nparams,tycheck) {_SC(#name), sq_mg_##name,nparams,tycheck, SQTrue}
  798. static SQRegFunction sq_mg_methods[] =
  799. {
  800. _DECL_FUNC(url_decode, 2, _SC(".s")),
  801. _DECL_FUNC(uri_decode, 2, _SC(".s")),
  802. _DECL_FUNC(url_encode, 2, _SC(".s")),
  803. _DECL_FUNC(md5, -2, _SC(".s")),
  804. #ifdef JNI_ENABLE_LOG
  805. _DECL_FUNC(jniLog, -2, _SC(".s")),
  806. #endif
  807. _DECL_FUNC(debug_print, -2, _SC(".s")),
  808. {0,0}
  809. };
  810. #undef _DECL_FUNC
  811. static void reg_string(HSQUIRRELVM v, const char *name, const char *val) {
  812. sq_pushstring(v, name, -1);
  813. if(val) sq_pushstring(v, val, -1);
  814. else sq_pushnull(v);
  815. sq_rawset(v, -3);
  816. }
  817. static void reg_integer(HSQUIRRELVM v, const char *name, int val) {
  818. sq_pushstring(v, name, -1);
  819. sq_pushinteger(v, val);
  820. sq_rawset(v, -3);
  821. }
  822. // pushes request info on the Lua stack as a table
  823. static void
  824. push_request(HSQUIRRELVM v, const struct mg_request_info *ri)
  825. {
  826. int i;
  827. sq_pushliteral(v, _SC("info"));
  828. sq_get(v, -2);
  829. #define NEWSLOT_STR(ks) reg_string(v, #ks, ri->ks);
  830. NEWSLOT_STR(request_method);
  831. NEWSLOT_STR(uri);
  832. NEWSLOT_STR(http_version);
  833. NEWSLOT_STR(query_string);
  834. NEWSLOT_STR(remote_user);
  835. NEWSLOT_STR(log_message);
  836. #define NEWSLOT_INT(ks) reg_integer(v, #ks, ri->ks);
  837. NEWSLOT_INT(remote_ip);
  838. NEWSLOT_INT(remote_port);
  839. NEWSLOT_INT(status_code);
  840. NEWSLOT_INT(is_ssl);
  841. #undef NEWSLOT_STR
  842. #undef NEWSLOT_INT
  843. sq_pushliteral(v, "http_headers");
  844. sq_newtable(v);
  845. for (i = 0; i < ri->num_headers; ++i) {
  846. reg_string(v, ri->http_headers[i].name, ri->http_headers[i].value);
  847. }
  848. sq_newslot(v, -3, SQFalse);
  849. sq_poptop(v); //remove data table
  850. }
  851. #if 0
  852. static SQRESULT
  853. sq_mg_pcall_master_plugin(HSQUIRRELVM v)
  854. {
  855. SQ_FUNC_VARS_NO_TOP(v);
  856. GET_MG_CONNECION();
  857. SQ_GET_STRING(v, 2, func_name);
  858. const SQChar *error_message = _SC("No master plugin installed !");
  859. HSQUIRRELVM master_plugin = (HSQUIRRELVM) mg_lock_master_plugin(conn);
  860. if(master_plugin){
  861. int master_plugin_saved_top = sq_gettop(master_plugin);
  862. //sq_pushcfunction(master_plugin, traceback); /* push traceback function */
  863. //int error_func = sq_gettop(master_plugin);
  864. sq_pushstring(master_plugin, func_name, func_name_size);
  865. sq_getonroottable(master_plugin);
  866. if(sq_gettype(master_plugin, -1) == OT_CLOSURE){
  867. sq_pushroottable(master_plugin);
  868. int arg_top = sq_gettop (v);
  869. /* Push arguments to dst stack */
  870. int idx = 4;
  871. copy_values_between_vms (master_plugin, v, arg_top-idx, idx);
  872. if (sq_pcall (master_plugin, arg_top-idx+1, SQTrue, SQTrue) == SQ_OK) {
  873. /* run OK? */
  874. int ret_top = sq_gettop (master_plugin);
  875. /* Push status = OK */
  876. sq_pushbool (v, SQTrue);
  877. /* Return values to src */
  878. //copy_values_between_vms (v, master_plugin, master_plugin_saved_top+2, ret_top);
  879. /* pops debug.traceback and result from dst state */
  880. //sq_settop(master_plugin, master_plugin_saved_top);
  881. /*unlock master plugin*/
  882. mg_unlock_master_plugin(conn);
  883. /* Return true (success) plus return values */
  884. return 1;
  885. }
  886. error_message = sq_tostring (master_plugin, -1);
  887. } else error_message = "Attempt to call an invalid function on master plugin !";
  888. /* pops debug.traceback and result from dst state */
  889. sq_settop(master_plugin, master_plugin_saved_top);
  890. /*unlock master plugin*/
  891. mg_unlock_master_plugin(conn);
  892. }
  893. return sq_throwerror(v, error_message);
  894. }
  895. #endif
  896. static void write_error_message(struct mg_connection *conn,
  897. const char * error_msg, SQInteger error_len){
  898. #define PRE_TAG_OPEN "<pre>"
  899. #define PRE_TAG_CLOSE "</pre>"
  900. mg_write(conn, PRE_TAG_OPEN, sizeof(PRE_TAG_OPEN));
  901. if(error_len < 0) error_len = strlen(error_msg);
  902. mg_write(conn, error_msg, error_len);
  903. mg_write(conn, PRE_TAG_CLOSE, sizeof(PRE_TAG_CLOSE));
  904. }
  905. #ifdef __cplusplus
  906. extern "C" {
  907. #endif
  908. SQUIRREL_API SQRESULT sqext_register_SQLite3(HSQUIRRELVM v);
  909. SQRESULT sqext_register_MySQL(HSQUIRRELVM v);
  910. SQRESULT sqext_register_PostgreSQL(HSQUIRRELVM v);
  911. SQUIRREL_API SQRESULT sqext_register_base64(HSQUIRRELVM v);
  912. SQUIRREL_API SQRESULT sqext_register_mix(HSQUIRRELVM v);
  913. SQUIRREL_API SQRESULT sqstd_register_bloblib(HSQUIRRELVM v);
  914. SQUIRREL_API SQRESULT sqstd_register_iolib(HSQUIRRELVM v);
  915. SQUIRREL_API SQInteger sqstd_register_systemlib(HSQUIRRELVM v);
  916. SQUIRREL_API SQRESULT sqstd_register_mathlib(HSQUIRRELVM v);
  917. SQUIRREL_API SQRESULT sqstd_register_stringlib(HSQUIRRELVM v);
  918. SQUIRREL_API SQRESULT sqext_register_Sq_Fpdf(HSQUIRRELVM v);
  919. SQUIRREL_API SQRESULT sqext_register_sqfs(HSQUIRRELVM v);
  920. SQUIRREL_API void sqstd_seterrorhandlers(HSQUIRRELVM v);
  921. SQUIRREL_API void sqstd_printcallstack(HSQUIRRELVM v);
  922. SQUIRREL_API SQRESULT sqext_register_sq_socket(HSQUIRRELVM v);
  923. SQUIRREL_API SQRESULT sqext_register_sq_slave_vm(HSQUIRRELVM v);
  924. SQUIRREL_API SQRESULT sqext_register_axtls (HSQUIRRELVM v);
  925. SQUIRREL_API SQRESULT sqext_register_openssl (HSQUIRRELVM v);
  926. SQRESULT sqext_register_decimal(HSQUIRRELVM v);
  927. #ifdef __cplusplus
  928. } /*extern "C"*/
  929. #endif
  930. void sq_printfunc(HSQUIRRELVM v,const SQChar *s,...)
  931. {
  932. va_list vl;
  933. va_start(vl, s);
  934. #ifdef JNI_ENABLE_LOG
  935. char buf[2048];
  936. vsnprintf(buf, sizeof(buf), s, vl);
  937. jniLog(-1, buf);
  938. #else
  939. vfprintf(stdout, s, vl);
  940. #endif
  941. va_end(vl);
  942. }
  943. void sq_errorfunc(HSQUIRRELVM v,const SQChar *s,...)
  944. {
  945. va_list vl;
  946. va_start(vl, s);
  947. #ifdef JNI_ENABLE_LOG
  948. char buf[2048];
  949. vsnprintf(buf, sizeof(buf), s, vl);
  950. jniLog(-2, buf);
  951. #else
  952. vfprintf(stderr, s, vl);
  953. #endif
  954. va_end(vl);
  955. }
  956. #define INT_CONST(v,num) sq_pushstring(v,_SC(#num),-1);sq_pushinteger(v,num);sq_newslot(v,-3,SQTrue);
  957. static HSQUIRRELVM my_new_squirrel(struct mg_context *ctx) {
  958. HSQUIRRELVM v = sq_open(1024);
  959. if(!v) return 0;
  960. sqstd_seterrorhandlers(v); //registers the default error handlers
  961. sq_setprintfunc(v, sq_printfunc, sq_errorfunc); //sets the print function
  962. sq_pushroottable(v);
  963. sqstd_register_bloblib(v);
  964. sqstd_register_iolib(v);
  965. sqstd_register_systemlib(v);
  966. sqstd_register_mathlib(v);
  967. sqstd_register_stringlib(v);
  968. sqstd_register_stringlib(v);
  969. sqext_register_base64(v);
  970. sqext_register_Sq_Fpdf(v);
  971. sqext_register_SQLite3(v);
  972. #ifdef WITH_MYSQL
  973. sqext_register_MySQL(v);
  974. #endif
  975. #ifdef WITH_POSTGRESQL
  976. sqext_register_PostgreSQL(v);
  977. #endif
  978. sqext_register_sqfs(v);
  979. sqext_register_mix(v);
  980. sqext_register_sq_socket(v);
  981. sqext_register_sq_slave_vm(v);
  982. #ifdef USE_AXTLS
  983. sqext_register_axtls(v);
  984. #endif
  985. #ifdef USE_OPENSSL
  986. sqext_register_openssl(v);
  987. #endif
  988. sq_pushstring(v,sq_http_request_TAG, -1);
  989. sq_newclass(v,SQFalse);
  990. sq_settypetag(v,-1,(void*)sq_http_request_TAG);
  991. sq_insert_reg_funcs(v, mg_http_request_methods);
  992. sq_pushstring(v, _SC("info"), -1);
  993. sq_newtable(v);
  994. sq_newslot(v,-3,SQFalse);
  995. sq_newslot(v,-3,SQFalse);
  996. sq_insert_reg_funcs(v, sq_mg_methods);
  997. sq_pushliteral(v, "APP_ROOT_FOLDER");
  998. sq_pushstring(v, mg_get_option(ctx, "document_root"), -1);
  999. sq_newslot(v, -3, SQFalse);
  1000. sq_poptop(v); //remove roottable
  1001. return v;
  1002. }
  1003. SQInteger blob_read(SQUserPointer file,SQUserPointer p,SQInteger size);
  1004. SQInteger loadstring(HSQUIRRELVM v, const char * bcode, SQInteger bcode_size)
  1005. {
  1006. SQBlob b(0, bcode_size);
  1007. b.Write(bcode, bcode_size);
  1008. b.Seek(0, SQ_SEEK_SET);
  1009. SQInteger rc = sq_readclosure(v, blob_read, &b);
  1010. return rc < 0 ? rc : 1;
  1011. }
  1012. // dispatches a callback to a Lua function if one is registered
  1013. static void *
  1014. user_callback_proxy(enum mg_event event,
  1015. struct mg_connection *conn,
  1016. const struct mg_request_info *ri)
  1017. {
  1018. HSQUIRRELVM v;
  1019. int e;
  1020. switch(event){
  1021. case MG_NEW_MASTER_PLUGIN: {
  1022. SQ_Mg_Context *sq_mg_ctx = (SQ_Mg_Context *) conn;
  1023. SQ_MG_Callback *cb = sq_mg_ctx->master_plugin;
  1024. v = my_new_squirrel((struct mg_context *)ri);
  1025. if(!v) return 0;
  1026. if(cb){
  1027. if (loadstring(v, cb->buf, cb->size) == 1) {
  1028. sq_pushroottable(v);
  1029. if(sq_call(v, 1, SQFalse, SQTrue) != SQ_OK){
  1030. sq_errorfunc(v, "sq_call failed %d\n%s", __LINE__, sq_getlasterror_str(v));
  1031. }
  1032. sq_poptop(v); //remove function from stack
  1033. }
  1034. }
  1035. return v;
  1036. }
  1037. break;
  1038. case MG_FREE_MASTER_PLUGIN:
  1039. v = (HSQUIRRELVM)conn;
  1040. if(v) {
  1041. SQ_Mg_Context *sq_mg_ctx = (SQ_Mg_Context *) ri;
  1042. SQ_MG_Callback *cb = sq_mg_ctx->master_plugin_exit;
  1043. if(cb){
  1044. if (loadstring(v, cb->buf, cb->size) == 1) {
  1045. sq_pushroottable(v);
  1046. if(sq_call(v, 1, SQFalse, SQFalse) != SQ_OK){
  1047. sq_errorfunc(v, "sq_call failed %d\n%s", __LINE__, sq_getlasterror_str(v));
  1048. }
  1049. sq_poptop(v); //remove function from stack
  1050. }
  1051. else sq_errorfunc(v, "sq_call failed %d\n%s", __LINE__, sq_getlasterror_str(v));
  1052. }
  1053. sq_close(v);
  1054. }
  1055. return 0;
  1056. break;
  1057. case MG_NEW_PLUGIN:{
  1058. SQ_Mg_Context *sq_mg_ctx = (SQ_Mg_Context *) mg_get_user_data(conn);
  1059. if(!sq_mg_ctx) return 0;
  1060. SQ_MG_Callback *cb = sq_mg_ctx->user_callback;
  1061. if (cb) {
  1062. v = my_new_squirrel((struct mg_context *)ri);
  1063. if(!v) return 0;
  1064. SQInteger top = sq_gettop(v);
  1065. SQ_MG_Callback *cb_setup = sq_mg_ctx->user_callback_setup;
  1066. if(cb_setup){
  1067. if (loadstring(v, cb_setup->buf, cb_setup->size) == 1) {
  1068. sq_pushroottable(v);
  1069. if(sq_call(v, 1, SQFalse, SQFalse) != SQ_OK){
  1070. sq_errorfunc(v, "sq_call failed %d\n%s", __LINE__, sq_getlasterror_str(v));
  1071. }
  1072. sq_poptop(v); //remove function from stack
  1073. }
  1074. else sq_errorfunc(v, "sq_call failed %d\n%s", __LINE__, sq_getlasterror_str(v));
  1075. }
  1076. sq_pushroottable(v);
  1077. sq_pushstring(v, sq_mg_user_callback, -1);
  1078. if (loadstring(v, cb->buf, cb->size) == 1) {
  1079. sq_newslot(v, -3, SQFalse);
  1080. }
  1081. else sq_errorfunc(v, "sq_call failed %d\n%s", __LINE__, sq_getlasterror_str(v));
  1082. sq_settop(v, top); //remove everithing left on stack while working here
  1083. return v;
  1084. }
  1085. }
  1086. return 0;
  1087. case MG_FREE_PLUGIN:
  1088. v = (HSQUIRRELVM)mg_get_plugin(conn);
  1089. if(v){
  1090. SQ_Mg_Context *sq_mg_ctx = (SQ_Mg_Context *) ri;
  1091. if(sq_mg_ctx){
  1092. SQ_MG_Callback *cb = sq_mg_ctx->user_callback_exit;
  1093. if(cb){
  1094. if (loadstring(v, cb->buf, cb->size) == 1) {
  1095. sq_pushroottable(v);
  1096. if(sq_call(v, 1, SQFalse, SQFalse) != SQ_OK){
  1097. sq_errorfunc(v, "sq_call failed %d\n%s", __LINE__, sq_getlasterror_str(v));
  1098. }
  1099. sq_poptop(v); //remove function from stack
  1100. }
  1101. else sq_errorfunc(v, "sq_call failed %d\n%s", __LINE__, sq_getlasterror_str(v));
  1102. }
  1103. }
  1104. sq_close(v);
  1105. }
  1106. return NULL;
  1107. case MG_INIT_SSL:
  1108. #if defined(USE_AXTLS) || defined(USE_OPENSSL)
  1109. //lua_pushstring(L, "MG_INIT_SSL");
  1110. *((void**)conn) = SSL_CTX_new(0);
  1111. #endif
  1112. return 0;
  1113. case MG_NEW_REQUEST:
  1114. case MG_HTTP_ERROR:
  1115. case MG_EVENT_LOG:{
  1116. v = (HSQUIRRELVM) mg_get_plugin(conn);
  1117. if (v) {
  1118. SQInteger saved_top = sq_gettop(v);
  1119. //SQ_Mg_Context *sq_mg_ctx = (SQ_Mg_Context *) mg_get_user_data(conn);
  1120. sq_pushstring(v, sq_mg_user_callback, -1);
  1121. if(sq_getonroottable(v) != SQ_OK) {
  1122. write_error_message(conn, "failed to find mg_user_callback", -1);
  1123. return NULL;
  1124. }
  1125. sq_pushroottable(v);
  1126. #define CASE(n) case n: sq_pushstring(v, #n, -1);break
  1127. switch(event){
  1128. CASE(MG_INIT_SSL);
  1129. CASE(MG_HTTP_ERROR);
  1130. CASE(MG_EVENT_LOG);
  1131. CASE(MG_NEW_REQUEST);
  1132. default:
  1133. sq_pushnull(v);
  1134. }
  1135. sq_pushstring(v, sq_http_request_TAG, -1);
  1136. if(sq_getonroottable(v) == SQ_OK){
  1137. sq_pushroottable(v);
  1138. if(sq_call(v, 1, SQTrue, SQFalse) == SQ_OK){
  1139. sq_remove(v, -2); //remove class
  1140. sq_setinstanceup(v, -1, conn);
  1141. }
  1142. }
  1143. push_request(v, ri);
  1144. if(sq_call(v, 3, SQTrue, SQTrue) != SQ_OK) {
  1145. write_error_message(conn, sq_getlasterror_str(v), -1);
  1146. e = 0;
  1147. } else {
  1148. SQBool bval;
  1149. if(sq_getbool(v, -1, &bval) == SQ_OK) e = bval == SQTrue ? 1 : 0;
  1150. else e = 0;
  1151. }
  1152. sq_settop(v, saved_top);
  1153. return (void *) e;
  1154. }
  1155. }
  1156. default:
  1157. return NULL;
  1158. }
  1159. return NULL;
  1160. }
  1161. #ifdef __cplusplus
  1162. extern "C" {
  1163. #endif
  1164. SQRESULT sqext_register_mongoose(HSQUIRRELVM v)
  1165. {
  1166. sq_insert_reg_funcs(v, sq_mg_methods);
  1167. sq_pushstring(v,sq_mongoose_TAG, -1);
  1168. sq_newclass(v,SQFalse);
  1169. sq_settypetag(v,-1,(void*)sq_mongoose_TAG);
  1170. sq_insert_reg_funcs(v, sq_mongoose_methods);
  1171. sq_newslot(v,-3,SQTrue);
  1172. return 1;
  1173. }
  1174. #ifdef __cplusplus
  1175. }
  1176. #endif