sqmongoose.cpp 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271
  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. //Digest authentication functions
  222. #define MAX_USER_LEN 20
  223. #define MAX_NONCE_LEN 36 //extra bytes for zero terminator
  224. #define MAX_SESSIONS 5
  225. #define SESSION_TTL 3600
  226. #define SESSION_REQUEST_TTL 120
  227. enum e_session_state {e_session_invalid, e_session_sent_request, e_session_authorized};
  228. struct st_session_t
  229. {
  230. int state;
  231. char nonce[MAX_NONCE_LEN];
  232. char user[MAX_USER_LEN]; // Authenticated user
  233. long last_nc;
  234. time_t last_access; // last_access time
  235. long remote_ip; // Client's IP address
  236. };
  237. static struct st_session_t sessions[MAX_SESSIONS]; // Current sessions
  238. // Protects messages, sessions, last_message_id
  239. static mg_thread_mutex_t session_rwlock;
  240. //should be called only after locking session_rwlock
  241. static struct st_session_t *my_get_session_only_after_lock(const char *nonce){
  242. int i;
  243. for(i=0; i < MAX_SESSIONS; ++i){
  244. //printf("%s : %s\n", sessions[i].nonce, ah->nonce);
  245. if(strcmp(sessions[i].nonce, nonce) == 0){
  246. return &sessions[i];
  247. }
  248. }
  249. return 0;
  250. }
  251. // Check the user's password, return 1 if OK
  252. static SQBool my_check_password(const struct mg_request_info *request_info, const char *my_tmp_password)
  253. {
  254. md5_buf_t ha2, expected_response;
  255. struct mg_auth_header *ah = request_info->ah;
  256. // Some of the parameters may be NULL
  257. if (request_info->request_method == NULL ||
  258. //strcmp(ri->request_method, "PUT") == 0 ||
  259. //strcmp(ri->request_method, "DELETE") == 0 ||
  260. ah == NULL ||
  261. ah->nonce == NULL || ah->nc == NULL ||
  262. ah->cnonce == NULL || ah->qop == NULL || ah->uri == NULL ||
  263. ah->response == NULL)
  264. {
  265. return SQFalse;
  266. }
  267. // NOTE(lsm): due to a bug in MSIE, we do not compare the URI
  268. // TODO(lsm): check for authentication timeout
  269. if (// strcmp(ah->uri, conn->request_info.uri) != 0 ||
  270. strlen(ah->response) != 32
  271. )
  272. {
  273. return SQFalse;
  274. }
  275. mg_md5(ha2, request_info->request_method, ":", ah->uri, NULL);
  276. mg_md5(expected_response, my_tmp_password /*ah->ha1*/, ":", ah->nonce, ":", ah->nc,
  277. ":", ah->cnonce, ":", ah->qop, ":", ha2, NULL);
  278. SQBool result = mg_strcasecmp(ah->response, expected_response) == 0;
  279. //printf("%s : %s : %s\n", my_tmp_password, ah->response, expected_response);
  280. if(result) //lets check timeout and other issues
  281. {
  282. struct st_session_t *session_found = 0;
  283. result = SQFalse;
  284. mg_thread_mutex_lock(&session_rwlock);
  285. session_found = my_get_session_only_after_lock(ah->nonce);
  286. if(session_found)
  287. {
  288. do
  289. {
  290. //mobile ip is a pain for security
  291. if( /*(ses.remote_ip != request_info->remote_ip) ||*/
  292. ((time(NULL) - session_found->last_access) > SESSION_TTL)
  293. ) break;
  294. if(session_found->state == e_session_sent_request)
  295. {
  296. session_found->state = e_session_authorized;
  297. snprintf(session_found->user, sizeof(session_found->user), "%s", ah->user);
  298. }
  299. else if(strcmp(session_found->user, ah->user) != 0) break;
  300. long recived_nc = strtol(ah->nc, 0, 16);
  301. time_t received_time = time(NULL);
  302. /*
  303. //printf("%d : %lu : %lu : %lu : %lu\n", request_info->remote_port, ses.last_nc, recived_nc, ses.last_access, received_time);
  304. if((ses.last_nc+1) != recived_nc){
  305. //lets see if we can manage out of order sent by some browsers
  306. if((received_time - ses.last_access) > 2) break;
  307. //inside a window of 2 seconds we tolerate nc out of order
  308. if(ses.last_nc > recived_nc) recived_nc = ses.last_nc;
  309. }
  310. */
  311. session_found->last_access = received_time;
  312. session_found->last_nc = recived_nc;
  313. result = SQTrue;
  314. } while(0);
  315. if(!result){
  316. session_found->state = e_session_invalid;
  317. session_found->nonce[0] = '\0';
  318. }
  319. }
  320. else {
  321. //dbg_msg("Session not found ! : %s : %s\n", ah->nonce, request_info->uri);
  322. }
  323. mg_thread_mutex_unlock(&session_rwlock);
  324. }
  325. return result;
  326. }
  327. static SQRESULT
  328. sq_http_request_check_password(HSQUIRRELVM v)
  329. {
  330. SQ_FUNC_VARS_NO_TOP(v);
  331. GET_http_request_INSTANCE();
  332. SQ_GET_STRING(v, 2, my_tmp_password);
  333. const struct mg_request_info *request_info = mg_get_request_info(conn);
  334. sq_pushbool(v, my_check_password(request_info, my_tmp_password));
  335. return 1;
  336. }
  337. // Close user session return 1 if closed
  338. static SQBool my_close_session(const struct mg_request_info *request_info)
  339. {
  340. SQBool result = SQFalse;
  341. struct st_session_t *session_found = 0;
  342. struct mg_auth_header *ah = request_info->ah;
  343. // Some of the parameters may be NULL
  344. if (request_info->request_method == NULL ||
  345. //strcmp(ri->request_method, "PUT") == 0 ||
  346. //strcmp(ri->request_method, "DELETE") == 0 ||
  347. ah == NULL ||
  348. ah->nonce == NULL || ah->nc == NULL ||
  349. ah->cnonce == NULL || ah->qop == NULL || ah->uri == NULL ||
  350. ah->response == NULL)
  351. {
  352. return 0;
  353. }
  354. mg_thread_mutex_lock(&session_rwlock);
  355. session_found = my_get_session_only_after_lock(ah->nonce);
  356. if(session_found) {
  357. session_found->state = e_session_invalid;
  358. session_found->nonce[0] = '\0';
  359. result = SQTrue;
  360. }
  361. mg_thread_mutex_unlock(&session_rwlock);
  362. return result;
  363. }
  364. static SQRESULT
  365. sq_http_request_close_session(HSQUIRRELVM v)
  366. {
  367. SQ_FUNC_VARS_NO_TOP(v);
  368. GET_http_request_INSTANCE();
  369. const struct mg_request_info *request_info = mg_get_request_info(conn);
  370. sq_pushbool(v, my_close_session(request_info));
  371. return 1;
  372. }
  373. typedef char buf_1024_t[1024];
  374. static const char *my_encode_nonce(buf_1024_t buf, md5_buf_t md5_buf, unsigned long ip)
  375. {
  376. snprintf(buf, sizeof(buf), "%lu:%d:%d:%lu", (unsigned long) time(NULL), rand(), rand(), ip);
  377. mg_md5(md5_buf, buf, NULL);
  378. return md5_buf;
  379. }
  380. static void my_send_authorization_request(struct mg_connection *conn,
  381. const struct mg_request_info *request_info,
  382. const char *authentication_domain,
  383. const char *nonce)
  384. {
  385. md5_buf_t md5_buf;
  386. buf_1024_t buf;
  387. int i;
  388. if (nonce == NULL)
  389. {
  390. nonce = my_encode_nonce(buf, md5_buf, (unsigned long) request_info->remote_ip);
  391. }
  392. struct st_session_t *available_session = NULL;
  393. mg_thread_mutex_lock(&session_rwlock);
  394. for(i=0; i<3; ++i)
  395. {
  396. for(i=0; i < MAX_SESSIONS; ++i){
  397. if(sessions[i].state == e_session_invalid){
  398. available_session = &sessions[i];
  399. }
  400. if(sessions[i].state == e_session_sent_request){
  401. if((time(NULL) - available_session->last_access) > SESSION_REQUEST_TTL){
  402. //if session request bigger than 2 minutes reuse it
  403. available_session = &sessions[i];
  404. }
  405. }
  406. }
  407. if(available_session){
  408. available_session->state = e_session_sent_request;
  409. available_session->last_access = time(NULL);
  410. available_session->last_nc = 0;
  411. available_session->remote_ip = request_info->remote_ip;
  412. snprintf(available_session->nonce, sizeof(available_session->nonce),
  413. "%s", nonce);
  414. break;
  415. }
  416. else
  417. {
  418. nonce = my_encode_nonce(buf, md5_buf, (unsigned long) request_info->remote_ip);
  419. }
  420. }
  421. mg_thread_mutex_unlock(&session_rwlock);
  422. if(available_session) {
  423. i = snprintf(buf, sizeof(buf),
  424. "HTTP/1.1 401 Unauthorized\r\n"
  425. "Content-Length: 0\r\n"
  426. "WWW-Authenticate: Digest qop=\"auth\", "
  427. "realm=\"%s\", nonce=\"%s\"\r\n\r\n",
  428. authentication_domain, nonce);
  429. } else {
  430. i = snprintf(buf, sizeof(buf),
  431. "HTTP/1.1 503 Service Temporary Unavailable\r\n"
  432. "Content-Length: 0\r\n\r\n");
  433. }
  434. mg_write(conn, buf, i);
  435. }
  436. static SQRESULT
  437. sq_http_request_send_authorization_request(HSQUIRRELVM v)
  438. {
  439. SQ_FUNC_VARS_NO_TOP(v);
  440. GET_http_request_INSTANCE();
  441. SQ_GET_STRING(v, 2, authentication_domain);
  442. const struct mg_request_info *request_info = mg_get_request_info(conn);
  443. my_send_authorization_request(conn, request_info, authentication_domain, NULL);
  444. return 0;
  445. }
  446. #define _DECL_FUNC(name,nparams,tycheck) {_SC(#name), sq_http_request_##name,nparams,tycheck}
  447. static SQRegFunction mg_http_request_methods[] =
  448. {
  449. _DECL_FUNC(constructor, 1, _SC("x")),
  450. _DECL_FUNC(print, -2, _SC("x n|s")),
  451. _DECL_FUNC(read, 2, _SC("xi")),
  452. _DECL_FUNC(write, 3, _SC("xsi")),
  453. _DECL_FUNC(write_blob, -2, _SC("xxi")),
  454. _DECL_FUNC(get_var, 3, _SC("xs")),
  455. _DECL_FUNC(get_cookie, 2, _SC("xs")),
  456. _DECL_FUNC(get_header, 2, _SC("xs")),
  457. _DECL_FUNC(send_file, 2, _SC("xs")),
  458. _DECL_FUNC(handle_cgi_request, 2, _SC("xs")),
  459. _DECL_FUNC(get_option, 2, _SC("xs")),
  460. _DECL_FUNC(check_password, 2, _SC("xs")),
  461. _DECL_FUNC(close_session, 1, _SC("x")),
  462. _DECL_FUNC(send_authorization_request, 2, _SC("xs")),
  463. {0,0}
  464. };
  465. #undef _DECL_FUNC
  466. /***********/
  467. // helper function to extract a single mg_config value from a Lua table
  468. static void
  469. fetchfield(HSQUIRRELVM v, int idx, const char *key, char **value, const char *d)
  470. {
  471. const char *s;
  472. sq_pushstring(v, key, -1);
  473. if(sq_rawget(v, idx) == SQ_OK){
  474. if(sq_gettype(v, -1) != OT_NULL){
  475. sq_tostring(v, -1);
  476. sq_getstring(v, -1, &s);
  477. *value = mg_strdup(s);
  478. sq_pop(v, 2); //retrieved value and converted value
  479. return;
  480. }
  481. }
  482. *value = NULL;
  483. }
  484. // initializes an options string array from a Lua table
  485. static SQRESULT
  486. fetchoptions(HSQUIRRELVM v, int idx, const char **options)
  487. {
  488. struct {
  489. const char *key;
  490. const char *value;
  491. } OPTIONS[] = {
  492. { "cgi_extensions", ".cgi,.pl,.php" },
  493. { "cgi_environment", NULL },
  494. { "put_delete_passwords_file", NULL },
  495. { "cgi_interpreter", NULL },
  496. { "protect_uri", NULL },
  497. { "authentication_domain", "mydomain.com" },
  498. { "ssi_extensions", ".shtml,.shtm" },
  499. { "access_log_file", NULL },
  500. { "ssl_chain_file", NULL },
  501. { "enable_directory_listing", "yes" },
  502. { "error_log_file", NULL },
  503. { "global_passwords_file", NULL },
  504. { "index_files", "index.html,index.htm,index.cgi" },
  505. { "enable_keep_alive", "no" },
  506. { "access_control_list", NULL },
  507. { "max_request_size", "16384" },
  508. { "extra_mime_types", NULL },
  509. { "listening_ports", "8080" },
  510. { "document_root", "." },
  511. { "ssl_certificate", NULL },
  512. { "num_threads", "10" },
  513. { "run_as_user", NULL },
  514. { NULL, NULL }
  515. };
  516. char *value;
  517. int i, j;
  518. if(sq_gettype(v, idx) != OT_TABLE) return sq_throwerror(v, "a table is needed to retrieve options");
  519. for (i = 0, j = 0; OPTIONS[i].key; ++i) {
  520. fetchfield(v, idx, OPTIONS[i].key, &value, OPTIONS[i].value);
  521. if (NULL != value) {
  522. options[j++] = mg_strdup(OPTIONS[i].key);
  523. options[j++] = value;
  524. }
  525. }
  526. options[j] = NULL;
  527. return 0;
  528. }
  529. /***********/
  530. static const char sq_mongoose_TAG[] = "Mongoose";
  531. static SQBool show_errors_on_stdout = SQFalse;
  532. static void sq_mongoose_release_references(SQ_Mg_Context *self){
  533. SQ_MG_Callback_free(self->master_plugin);
  534. SQ_MG_Callback_free(self->master_plugin_exit);
  535. SQ_MG_Callback_free(self->user_callback);
  536. SQ_MG_Callback_free(self->user_callback_setup);
  537. SQ_MG_Callback_free(self->user_callback_exit);
  538. }
  539. static SQRESULT sq_mongoose_releasehook(SQUserPointer p, SQInteger size, HSQUIRRELVM v)
  540. {
  541. SQ_Mg_Context *self = (SQ_Mg_Context *)p;
  542. if(self){
  543. if(self->ctx){
  544. mg_stop(self->ctx);
  545. sq_mongoose_release_references(self);
  546. self->ctx = 0;
  547. }
  548. sq_free(self, sizeof(SQ_Mg_Context));
  549. }
  550. return 0;
  551. }
  552. static SQRESULT sq_mongoose_constructor(HSQUIRRELVM v)
  553. {
  554. //SQ_FUNC_VARS_NO_TOP(v);
  555. SQ_Mg_Context *sq_mg_ctx = (SQ_Mg_Context *)sq_malloc(sizeof(SQ_Mg_Context));
  556. memset(sq_mg_ctx, 0, sizeof(SQ_Mg_Context));
  557. sq_setinstanceup(v, 1, sq_mg_ctx);
  558. sq_setreleasehook(v,1, sq_mongoose_releasehook);
  559. return 1;
  560. }
  561. #define GET_mongoose_INSTANCE() \
  562. SQ_Mg_Context *self; \
  563. if((_rc_ = sq_getinstanceup(v,1,(SQUserPointer*)&self,(void*)sq_mongoose_TAG)) < 0) return _rc_;
  564. static SQRESULT
  565. sq_mongoose_show_errors_on_stdout(HSQUIRRELVM v)
  566. {
  567. SQ_FUNC_VARS_NO_TOP(v);
  568. SQ_GET_BOOL(v, 2, onOff);
  569. show_errors_on_stdout = onOff;
  570. return 0;
  571. }
  572. static SQRESULT
  573. sq_mongoose_modify_passwords_file(HSQUIRRELVM v)
  574. {
  575. SQ_FUNC_VARS_NO_TOP(v);
  576. SQ_GET_STRING(v, 2, passwords_file_name);
  577. SQ_GET_STRING(v, 3, domain);
  578. SQ_GET_STRING(v, 4, user);
  579. SQ_GET_STRING(v, 5, password);
  580. sq_pushinteger(v, mg_modify_passwords_file(passwords_file_name,
  581. domain, user, password));
  582. return 1;
  583. }
  584. static SQRESULT
  585. sq_mongoose_version(HSQUIRRELVM v)
  586. {
  587. sq_pushstring(v, mg_version(), -1);
  588. return 1;
  589. }
  590. SQInteger blob_write(SQUserPointer file,SQUserPointer p,SQInteger size);
  591. // creates a reference dispatching callbacks to squirrel functions
  592. static SQRESULT
  593. fetchcallback(HSQUIRRELVM v, const char *key, SQ_MG_Callback **sq_cb)
  594. {
  595. if(!sq_cb) return sq_throwerror(v, "inavlid SQ_MG_Callback parameter value (NULL)");
  596. if(sq_gettype(v, -1) != OT_TABLE) return sq_throwerror(v, "table expected to fetch callbacks");
  597. sq_pushstring(v, key, -1);
  598. sq_rawget(v, -2);
  599. if (sq_gettype(v, -1) == OT_CLOSURE){
  600. SQBlob b(0, 8192);
  601. if(SQ_SUCCEEDED(sq_writeclosure(v,blob_write,&b))) {
  602. *sq_cb = SQ_MG_Callback_malloc(b.Len());
  603. if(*sq_cb) memcpy((*sq_cb)->buf, b.GetBuf(), b.Len());
  604. sq_poptop(v);
  605. }
  606. }
  607. else
  608. {
  609. sq_pop(v, 1);
  610. return sq_throwerror(v, "closure expected for callbacks");
  611. }
  612. return SQ_OK;
  613. }
  614. static void *
  615. user_callback_proxy(enum mg_event event,
  616. struct mg_connection *conn,
  617. const struct mg_request_info *ri);
  618. // creates a new server using a configuration table
  619. static SQRESULT
  620. sq_mongoose_start(HSQUIRRELVM v)
  621. {
  622. SQ_FUNC_VARS_NO_TOP(v);
  623. GET_mongoose_INSTANCE();
  624. _rc_ = 1; //assumes succeed
  625. const char *options[64];
  626. SQInteger i;
  627. if(self->ctx) return sq_throwerror(v, _SC("mongoose already running or stoped incorrectly"));
  628. // store the Squirrel vm for use in callback proxies
  629. self->v = v;
  630. // prepare the mg_config structure from the squirrel table argument
  631. memset(options, 0, sizeof (options));
  632. fetchoptions(v, 2, options);
  633. fetchcallback(v, "master_plugin", &self->master_plugin);
  634. fetchcallback(v, "master_plugin_exit", &self->master_plugin_exit);
  635. fetchcallback(v, "user_callback", &self->user_callback);
  636. fetchcallback(v, "user_callback_setup", &self->user_callback_setup);
  637. fetchcallback(v, "user_callback_exit", &self->user_callback_exit);
  638. self->ctx = mg_start(user_callback_proxy, self, options);
  639. // free the options string list memory
  640. for (i = 0; options[i]; ++i)
  641. free((void *) options[i]);
  642. // throw an error if the server did not start
  643. if (self->ctx == NULL) {
  644. sq_mongoose_release_references(self);
  645. _rc_ = sq_throwerror(v, "could not start mongoose");
  646. }
  647. // return the context so it can be stopped later
  648. return _rc_;
  649. }
  650. static SQRESULT
  651. sq_mongoose_stop(HSQUIRRELVM v)
  652. {
  653. SQ_FUNC_VARS_NO_TOP(v);
  654. GET_mongoose_INSTANCE();
  655. if(self->ctx){
  656. mg_stop(self->ctx);
  657. sq_mongoose_release_references(self);
  658. self->ctx = 0;
  659. }
  660. return 0;
  661. }
  662. #define _DECL_FUNC(name,nparams,tycheck) {_SC(#name), sq_mongoose_##name,nparams,tycheck}
  663. static SQRegFunction sq_mongoose_methods[] =
  664. {
  665. _DECL_FUNC(constructor, 1, _SC("x")),
  666. _DECL_FUNC(start, -1, _SC("x")),
  667. _DECL_FUNC(stop, 1, _SC("x")),
  668. _DECL_FUNC(modify_passwords_file, 1, _SC("x")),
  669. _DECL_FUNC(version, 1, _SC("x")),
  670. _DECL_FUNC(show_errors_on_stdout, 2, _SC("xb")),
  671. {0,0}
  672. };
  673. #undef _DECL_FUNC
  674. static SQRESULT
  675. sq_mg_url_decode_base(HSQUIRRELVM v, SQInteger is_form_url_encoded)
  676. {
  677. SQ_FUNC_VARS_NO_TOP(v);
  678. SQ_GET_STRING(v, 2, src);
  679. int dst_len = src_size +1;
  680. char *dst = sq_getscratchpad(v,dst_len);
  681. dst_len = mg_url_decode(src, src_size, dst, dst_len, is_form_url_encoded);
  682. sq_pushstring(v, dst, dst_len);
  683. return 1;
  684. }
  685. static SQRESULT
  686. sq_mg_url_decode(HSQUIRRELVM v)
  687. {
  688. return sq_mg_url_decode_base(v, 1);
  689. }
  690. static SQRESULT
  691. sq_mg_uri_decode(HSQUIRRELVM v)
  692. {
  693. return sq_mg_url_decode_base(v, 0);
  694. }
  695. static SQRESULT
  696. sq_mg_url_encode(HSQUIRRELVM v)
  697. {
  698. SQ_FUNC_VARS_NO_TOP(v);
  699. SQ_GET_STRING(v, 2, src);
  700. char *dst = mg_url_encode(src);
  701. sq_pushstring(v, dst, -1);
  702. free(dst);
  703. return 1;
  704. }
  705. static SQRESULT
  706. sq_mg_md5(HSQUIRRELVM v)
  707. {
  708. SQ_FUNC_VARS(v);
  709. char buf[32 + 1];
  710. unsigned char hash[16];
  711. MD5_CTX ctx;
  712. MD5Init(&ctx);
  713. for (int i = 2; i <= _top_; ++i) {
  714. SQ_GET_STRING(v, i, p);
  715. MD5Update(&ctx, (const unsigned char *) p, p_size);
  716. }
  717. MD5Final(hash, &ctx);
  718. mg_bin2str(buf, hash, sizeof(hash));
  719. sq_pushstring(v, buf, -1);
  720. return 1;
  721. }
  722. static SQRESULT
  723. sq_mg_debug_print(HSQUIRRELVM v)
  724. {
  725. SQ_FUNC_VARS(v);
  726. SQInteger i, write_count = 0;
  727. for (i = 2; i <= _top_; ++i) {
  728. sq_tostring(v, i);
  729. SQ_GET_STRING(v, -1, value);
  730. write_count += fwrite(value, 1, value_size, stderr);
  731. sq_poptop(v);
  732. }
  733. sq_pushinteger(v, write_count);
  734. return 1;
  735. }
  736. #ifdef JNI_ENABLE_LOG
  737. #include <android/log.h>
  738. /*
  739. ** A callback for the android_log() SQIntegererface.
  740. */
  741. static void jniLog(int iErrCode, const char *zMsg){
  742. __android_log_print(ANDROID_LOG_ERROR,"jniLuaServerLog","(%d) %s\n", iErrCode, zMsg);
  743. }
  744. static SQRESULT sq_mg_jniLog(HSQUIRRELVM v)
  745. {
  746. SQ_FUNC_VARS_NO_TOP(v);
  747. SQ_GET_INTEGER(v, 2, code);
  748. SQ_GET_STRING(v, 3, error_message);
  749. jniLog(code, error_message);
  750. return 0;
  751. }
  752. #endif
  753. #define _DECL_FUNC(name,nparams,tycheck) {_SC(#name), sq_mg_##name,nparams,tycheck, SQTrue}
  754. static SQRegFunction sq_mg_methods[] =
  755. {
  756. _DECL_FUNC(url_decode, 2, _SC(".s")),
  757. _DECL_FUNC(uri_decode, 2, _SC(".s")),
  758. _DECL_FUNC(url_encode, 2, _SC(".s")),
  759. _DECL_FUNC(md5, -2, _SC(".s")),
  760. #ifdef JNI_ENABLE_LOG
  761. _DECL_FUNC(jniLog, -2, _SC(".s")),
  762. #endif
  763. _DECL_FUNC(debug_print, -2, _SC(".s")),
  764. {0,0}
  765. };
  766. #undef _DECL_FUNC
  767. static void reg_string(HSQUIRRELVM v, const char *name, const char *val) {
  768. sq_pushstring(v, name, -1);
  769. if(val) sq_pushstring(v, val, -1);
  770. else sq_pushnull(v);
  771. sq_rawset(v, -3);
  772. }
  773. static void reg_integer(HSQUIRRELVM v, const char *name, int val) {
  774. sq_pushstring(v, name, -1);
  775. sq_pushinteger(v, val);
  776. sq_rawset(v, -3);
  777. }
  778. // pushes request info on the Lua stack as a table
  779. static void
  780. push_request(HSQUIRRELVM v, const struct mg_request_info *ri)
  781. {
  782. int i;
  783. sq_pushliteral(v, _SC("info"));
  784. sq_get(v, -2);
  785. #define NEWSLOT_STR(ks) reg_string(v, #ks, ri->ks);
  786. NEWSLOT_STR(request_method);
  787. NEWSLOT_STR(uri);
  788. NEWSLOT_STR(http_version);
  789. NEWSLOT_STR(query_string);
  790. NEWSLOT_STR(remote_user);
  791. NEWSLOT_STR(log_message);
  792. #define NEWSLOT_INT(ks) reg_integer(v, #ks, ri->ks);
  793. NEWSLOT_INT(remote_ip);
  794. NEWSLOT_INT(remote_port);
  795. NEWSLOT_INT(status_code);
  796. NEWSLOT_INT(is_ssl);
  797. #undef NEWSLOT_STR
  798. #undef NEWSLOT_INT
  799. sq_pushliteral(v, "http_headers");
  800. sq_newtable(v);
  801. for (i = 0; i < ri->num_headers; ++i) {
  802. reg_string(v, ri->http_headers[i].name, ri->http_headers[i].value);
  803. }
  804. sq_newslot(v, -3, SQFalse);
  805. sq_poptop(v); //remove data table
  806. }
  807. static SQInteger
  808. sq_mg_pcall_master_plugin(HSQUIRRELVM v)
  809. {
  810. #if 0
  811. SQ_FUNC_VARS_NO_TOP(v);
  812. GET_MG_CONNECION();
  813. SQ_GET_STRING(v, 2, func_name);
  814. const SQChar *error_message = _SC("No master plugin installed !");
  815. HSQUIRRELVM master_plugin = (HSQUIRRELVM) mg_lock_master_plugin(conn);
  816. if(master_plugin){
  817. int master_plugin_saved_top = sq_gettop(master_plugin);
  818. //sq_pushcfunction(master_plugin, traceback); /* push traceback function */
  819. //int error_func = sq_gettop(master_plugin);
  820. sq_pushstring(master_plugin, func_name, func_name_size);
  821. sq_getonroottable(master_plugin);
  822. if(sq_gettype(master_plugin, -1) == OT_CLOSURE){
  823. sq_pushroottable(master_plugin);
  824. int arg_top = sq_gettop (v);
  825. /* Push arguments to dst stack */
  826. int idx = 4;
  827. copy_values_between_vms (master_plugin, v, arg_top-idx, idx);
  828. if (sq_pcall (master_plugin, arg_top-idx+1, SQTrue, SQTrue) == SQ_OK) {
  829. /* run OK? */
  830. int ret_top = sq_gettop (master_plugin);
  831. /* Push status = OK */
  832. sq_pushbool (v, SQTrue);
  833. /* Return values to src */
  834. //copy_values_between_vms (v, master_plugin, master_plugin_saved_top+2, ret_top);
  835. /* pops debug.traceback and result from dst state */
  836. //sq_settop(master_plugin, master_plugin_saved_top);
  837. /*unlock master plugin*/
  838. mg_unlock_master_plugin(conn);
  839. /* Return true (success) plus return values */
  840. return 1;
  841. }
  842. error_message = sq_tostring (master_plugin, -1);
  843. } else error_message = "Attempt to call an invalid function on master plugin !";
  844. /* pops debug.traceback and result from dst state */
  845. sq_settop(master_plugin, master_plugin_saved_top);
  846. /*unlock master plugin*/
  847. mg_unlock_master_plugin(conn);
  848. }
  849. return sq_throwerror(v, error_message);
  850. #endif
  851. }
  852. static void write_error_message(struct mg_connection *conn,
  853. const char * error_msg, SQInteger error_len){
  854. #define PRE_TAG_OPEN "<pre>"
  855. #define PRE_TAG_CLOSE "</pre>"
  856. mg_write(conn, PRE_TAG_OPEN, sizeof(PRE_TAG_OPEN));
  857. if(error_len < 0) error_len = strlen(error_msg);
  858. mg_write(conn, error_msg, error_len);
  859. mg_write(conn, PRE_TAG_CLOSE, sizeof(PRE_TAG_CLOSE));
  860. }
  861. #ifdef __cplusplus
  862. extern "C" {
  863. #endif
  864. SQUIRREL_API SQRESULT sqstd_register_SQLite3(HSQUIRRELVM v);
  865. SQUIRREL_API SQRESULT sqstd_register_base64(HSQUIRRELVM v);
  866. SQUIRREL_API SQRESULT sq_register_mix(HSQUIRRELVM v);
  867. SQUIRREL_API SQRESULT sqstd_register_bloblib(HSQUIRRELVM v);
  868. SQUIRREL_API SQRESULT sqstd_register_iolib(HSQUIRRELVM v);
  869. SQUIRREL_API SQInteger sqstd_register_systemlib(HSQUIRRELVM v);
  870. SQUIRREL_API SQRESULT sqstd_register_mathlib(HSQUIRRELVM v);
  871. SQUIRREL_API SQRESULT sqstd_register_stringlib(HSQUIRRELVM v);
  872. SQUIRREL_API SQRESULT sqstd_register_Sq_Fpdf(HSQUIRRELVM v);
  873. SQUIRREL_API SQRESULT sqstd_register_sqfs(HSQUIRRELVM v);
  874. SQUIRREL_API void sqstd_seterrorhandlers(HSQUIRRELVM v);
  875. SQUIRREL_API void sqstd_printcallstack(HSQUIRRELVM v);
  876. #ifdef __cplusplus
  877. } /*extern "C"*/
  878. #endif
  879. void sq_printfunc(HSQUIRRELVM v,const SQChar *s,...)
  880. {
  881. va_list vl;
  882. va_start(vl, s);
  883. vfprintf(stdout, s, vl);
  884. va_end(vl);
  885. }
  886. void sq_errorfunc(HSQUIRRELVM v,const SQChar *s,...)
  887. {
  888. va_list vl;
  889. va_start(vl, s);
  890. vfprintf(stderr, s, vl);
  891. va_end(vl);
  892. }
  893. #define INT_CONST(v,num) sq_pushstring(v,_SC(#num),-1);sq_pushinteger(v,num);sq_newslot(v,-3,SQTrue);
  894. static HSQUIRRELVM my_new_squirrel(struct mg_context *ctx) {
  895. HSQUIRRELVM v = sq_open(1024);
  896. if(!v) return 0;
  897. sqstd_seterrorhandlers(v); //registers the default error handlers
  898. sq_setprintfunc(v, sq_printfunc, sq_errorfunc); //sets the print function
  899. sq_pushroottable(v);
  900. sqstd_register_bloblib(v);
  901. sqstd_register_iolib(v);
  902. sqstd_register_systemlib(v);
  903. sqstd_register_mathlib(v);
  904. sqstd_register_stringlib(v);
  905. sqstd_register_base64(v);
  906. sqstd_register_Sq_Fpdf(v);
  907. sqstd_register_SQLite3(v);
  908. sqstd_register_sqfs(v);
  909. sq_register_mix(v);
  910. sq_pushstring(v,sq_http_request_TAG, -1);
  911. sq_newclass(v,SQFalse);
  912. sq_settypetag(v,-1,(void*)sq_http_request_TAG);
  913. sq_insert_reg_funcs(v, mg_http_request_methods);
  914. sq_pushstring(v, _SC("info"), -1);
  915. sq_newtable(v);
  916. sq_newslot(v,-3,SQFalse);
  917. sq_newslot(v,-3,SQFalse);
  918. sq_insert_reg_funcs(v, sq_mg_methods);
  919. sq_pushliteral(v, "APP_ROOT_FOLDER");
  920. sq_pushstring(v, mg_get_option(ctx, "document_root"), -1);
  921. sq_newslot(v, -3, SQFalse);
  922. sq_poptop(v); //remove roottable
  923. return v;
  924. }
  925. SQInteger blob_read(SQUserPointer file,SQUserPointer p,SQInteger size);
  926. SQInteger loadstring(HSQUIRRELVM v, const char * bcode, SQInteger bcode_size)
  927. {
  928. SQBlob b(0, bcode_size);
  929. b.Write(bcode, bcode_size);
  930. b.Seek(0, SQ_SEEK_SET);
  931. SQInteger rc = sq_readclosure(v, blob_read, &b);
  932. return rc < 0 ? rc : 1;
  933. }
  934. // dispatches a callback to a Lua function if one is registered
  935. static void *
  936. user_callback_proxy(enum mg_event event,
  937. struct mg_connection *conn,
  938. const struct mg_request_info *ri)
  939. {
  940. HSQUIRRELVM v;
  941. int e;
  942. switch(event){
  943. case MG_NEW_MASTER_PLUGIN: {
  944. SQ_Mg_Context *sq_mg_ctx = (SQ_Mg_Context *) conn;
  945. SQ_MG_Callback *cb = sq_mg_ctx->master_plugin;
  946. v = my_new_squirrel((struct mg_context *)ri);
  947. if(!v) return 0;
  948. if(cb){
  949. if (loadstring(v, cb->buf, cb->size) == 1) {
  950. sq_pushroottable(v);
  951. if(sq_call(v, 1, SQFalse, SQTrue) != SQ_OK){
  952. sq_errorfunc(v, "sq_call failed %d\n%s", __LINE__, sq_getlasterror_str(v));
  953. }
  954. sq_poptop(v); //remove function from stack
  955. }
  956. }
  957. return v;
  958. }
  959. break;
  960. case MG_FREE_MASTER_PLUGIN:
  961. v = (HSQUIRRELVM)conn;
  962. if(v) {
  963. SQ_Mg_Context *sq_mg_ctx = (SQ_Mg_Context *) ri;
  964. SQ_MG_Callback *cb = sq_mg_ctx->master_plugin_exit;
  965. if(cb){
  966. if (loadstring(v, cb->buf, cb->size) == 1) {
  967. sq_pushroottable(v);
  968. if(sq_call(v, 1, SQFalse, SQFalse) != SQ_OK){
  969. sq_errorfunc(v, "sq_call failed %d\n%s", __LINE__, sq_getlasterror_str(v));
  970. }
  971. sq_poptop(v); //remove function from stack
  972. }
  973. else sq_errorfunc(v, "sq_call failed %d\n%s", __LINE__, sq_getlasterror_str(v));
  974. }
  975. sq_close(v);
  976. }
  977. return 0;
  978. break;
  979. case MG_NEW_PLUGIN:{
  980. SQ_Mg_Context *sq_mg_ctx = (SQ_Mg_Context *) mg_get_user_data(conn);
  981. if(!sq_mg_ctx) return 0;
  982. SQ_MG_Callback *cb = sq_mg_ctx->user_callback;
  983. if (cb) {
  984. v = my_new_squirrel((struct mg_context *)ri);
  985. if(!v) return 0;
  986. SQInteger top = sq_gettop(v);
  987. SQ_MG_Callback *cb_setup = sq_mg_ctx->user_callback_setup;
  988. if(cb_setup){
  989. if (loadstring(v, cb_setup->buf, cb_setup->size) == 1) {
  990. sq_pushroottable(v);
  991. if(sq_call(v, 1, SQFalse, SQFalse) != SQ_OK){
  992. sq_errorfunc(v, "sq_call failed %d\n%s", __LINE__, sq_getlasterror_str(v));
  993. }
  994. sq_poptop(v); //remove function from stack
  995. }
  996. else sq_errorfunc(v, "sq_call failed %d\n%s", __LINE__, sq_getlasterror_str(v));
  997. }
  998. sq_pushroottable(v);
  999. sq_pushstring(v, sq_mg_user_callback, -1);
  1000. if (loadstring(v, cb->buf, cb->size) == 1) {
  1001. sq_newslot(v, -3, SQFalse);
  1002. }
  1003. else sq_errorfunc(v, "sq_call failed %d\n%s", __LINE__, sq_getlasterror_str(v));
  1004. sq_settop(v, top); //remove everithing left on stack while working here
  1005. return v;
  1006. }
  1007. }
  1008. return 0;
  1009. case MG_FREE_PLUGIN:
  1010. v = (HSQUIRRELVM)mg_get_plugin(conn);
  1011. if(v){
  1012. SQ_Mg_Context *sq_mg_ctx = (SQ_Mg_Context *) ri;
  1013. if(sq_mg_ctx){
  1014. SQ_MG_Callback *cb = sq_mg_ctx->user_callback_exit;
  1015. if(cb){
  1016. if (loadstring(v, cb->buf, cb->size) == 1) {
  1017. sq_pushroottable(v);
  1018. if(sq_call(v, 1, SQFalse, SQFalse) != SQ_OK){
  1019. sq_errorfunc(v, "sq_call failed %d\n%s", __LINE__, sq_getlasterror_str(v));
  1020. }
  1021. sq_poptop(v); //remove function from stack
  1022. }
  1023. else sq_errorfunc(v, "sq_call failed %d\n%s", __LINE__, sq_getlasterror_str(v));
  1024. }
  1025. }
  1026. sq_close(v);
  1027. }
  1028. return NULL;
  1029. case MG_INIT_SSL:
  1030. #ifdef USE_AXTLS
  1031. //lua_pushstring(L, "MG_INIT_SSL");
  1032. *((void**)conn) = SSL_CTX_new(0);
  1033. return 0;
  1034. #endif
  1035. case MG_NEW_REQUEST:
  1036. case MG_HTTP_ERROR:
  1037. case MG_EVENT_LOG:{
  1038. v = (HSQUIRRELVM) mg_get_plugin(conn);
  1039. if (v) {
  1040. SQInteger saved_top = sq_gettop(v);
  1041. //SQ_Mg_Context *sq_mg_ctx = (SQ_Mg_Context *) mg_get_user_data(conn);
  1042. sq_pushstring(v, sq_mg_user_callback, -1);
  1043. if(sq_getonroottable(v) != SQ_OK) {
  1044. write_error_message(conn, "failed to find mg_user_callback", -1);
  1045. return NULL;
  1046. }
  1047. sq_pushroottable(v);
  1048. #define CASE(n) case n: sq_pushstring(v, #n, -1);break
  1049. switch (event) {
  1050. CASE(MG_INIT_SSL);
  1051. CASE(MG_HTTP_ERROR);
  1052. CASE(MG_EVENT_LOG);
  1053. CASE(MG_NEW_REQUEST);
  1054. default:
  1055. sq_pushnull(v);
  1056. }
  1057. sq_pushstring(v, sq_http_request_TAG, -1);
  1058. if(sq_getonroottable(v) == SQ_OK){
  1059. sq_pushroottable(v);
  1060. if(sq_call(v, 1, SQTrue, SQFalse) == SQ_OK){
  1061. sq_remove(v, -2); //remove class
  1062. sq_setinstanceup(v, -1, conn);
  1063. }
  1064. }
  1065. push_request(v, ri);
  1066. if(sq_call(v, 3, SQTrue, SQTrue) != SQ_OK) {
  1067. write_error_message(conn, sq_getlasterror_str(v), -1);
  1068. e = 0;
  1069. } else {
  1070. SQBool bval;
  1071. if(sq_getbool(v, -1, &bval) == SQ_OK) e = bval == SQTrue ? 1 : 0;
  1072. else e = 0;
  1073. }
  1074. sq_settop(v, saved_top);
  1075. return (void *) e;
  1076. }
  1077. }
  1078. default:
  1079. return NULL;
  1080. }
  1081. return NULL;
  1082. }
  1083. #ifdef __cplusplus
  1084. extern "C" {
  1085. #endif
  1086. SQRESULT sqstd_register_mongoose(HSQUIRRELVM v)
  1087. {
  1088. sq_insert_reg_funcs(v, sq_mg_methods);
  1089. sq_pushstring(v,sq_mongoose_TAG, -1);
  1090. sq_newclass(v,SQFalse);
  1091. sq_settypetag(v,-1,(void*)sq_mongoose_TAG);
  1092. sq_insert_reg_funcs(v, sq_mongoose_methods);
  1093. sq_newslot(v,-3,SQTrue);
  1094. return 1;
  1095. }
  1096. #ifdef __cplusplus
  1097. }
  1098. #endif