mod_duktape.inl 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /* This file is part of the CivetWeb web server.
  2. * See https://github.com/civetweb/civetweb/
  3. * (C) 2015-2018 by the CivetWeb authors, MIT license.
  4. */
  5. #include "duktape.h"
  6. /* TODO: the mg context should be added to duktape as well */
  7. /* Alternative: redefine a new, clean API from scratch (instead of using mg),
  8. * or at least do not add problematic functions. */
  9. /* For evaluation purposes, currently only "send" is supported.
  10. * All other ~50 functions will be added later. */
  11. /* Note: This is only experimental support, so the API may still change. */
  12. static const char *const civetweb_conn_id = "\xFF"
  13. "civetweb_conn";
  14. static const char *const civetweb_ctx_id = "\xFF"
  15. "civetweb_ctx";
  16. static void *
  17. mg_duk_mem_alloc(void *udata, duk_size_t size)
  18. {
  19. return mg_malloc_ctx(size, (struct mg_context *)udata);
  20. }
  21. static void *
  22. mg_duk_mem_realloc(void *udata, void *ptr, duk_size_t newsize)
  23. {
  24. return mg_realloc_ctx(ptr, newsize, (struct mg_context *)udata);
  25. }
  26. static void
  27. mg_duk_mem_free(void *udata, void *ptr)
  28. {
  29. (void)udata;
  30. mg_free(ptr);
  31. }
  32. static void
  33. mg_duk_fatal_handler(duk_context *duk_ctx, duk_errcode_t code, const char *msg)
  34. {
  35. /* Script is called "protected" (duk_peval_file), so script errors should
  36. * never yield in a call to this function. Maybe calls prior to executing
  37. * the script could raise a fatal error. */
  38. struct mg_connection *conn;
  39. duk_push_global_stash(duk_ctx);
  40. duk_get_prop_string(duk_ctx, -1, civetweb_conn_id);
  41. conn = (struct mg_connection *)duk_to_pointer(duk_ctx, -1);
  42. mg_cry_internal(conn, "JavaScript fatal (%u): %s", (unsigned)code, msg);
  43. }
  44. #if DUK_VERSION >= 20000L
  45. /* Dropped from interface */
  46. duk_int_t duk_peval_file(duk_context *duk_ctx, const char *script);
  47. static void
  48. mg_duk_v2_fatal(void *udata, const char *msg)
  49. {
  50. ; /* TODO: How to get "conn" without duk_ctx */
  51. }
  52. static void
  53. push_file_as_string(duk_context *ctx, const char *filename)
  54. {
  55. FILE *f;
  56. struct stat fst;
  57. void *buf;
  58. size_t len;
  59. if (0 != stat(filename, &fst)) {
  60. duk_push_undefined(ctx);
  61. return;
  62. }
  63. f = fopen(filename, "rb");
  64. if (!f) {
  65. duk_push_undefined(ctx);
  66. return;
  67. }
  68. buf = mg_malloc(fst.st_size);
  69. if (!f) {
  70. fclose(f);
  71. duk_push_undefined(ctx);
  72. return;
  73. }
  74. len = fread(buf, 1, fst.st_size, f);
  75. fclose(f);
  76. duk_push_lstring(ctx, (const char *)buf, (duk_size_t)len);
  77. mg_free(buf);
  78. }
  79. duk_int_t
  80. duk_peval_file(duk_context *duk_ctx, const char *script)
  81. {
  82. push_file_as_string(duk_ctx, script);
  83. return duk_peval(duk_ctx);
  84. }
  85. #endif
  86. static duk_ret_t
  87. duk_itf_write(duk_context *duk_ctx)
  88. {
  89. struct mg_connection *conn;
  90. duk_double_t ret;
  91. duk_size_t len = 0;
  92. const char *val = duk_require_lstring(duk_ctx, -1, &len);
  93. /*
  94. duk_push_global_stash(duk_ctx);
  95. duk_get_prop_string(duk_ctx, -1, civetweb_conn_id);
  96. conn = (struct mg_connection *)duk_to_pointer(duk_ctx, -1);
  97. */
  98. duk_push_current_function(duk_ctx);
  99. duk_get_prop_string(duk_ctx, -1, civetweb_conn_id);
  100. conn = (struct mg_connection *)duk_to_pointer(duk_ctx, -1);
  101. if (!conn) {
  102. duk_error(duk_ctx,
  103. DUK_ERR_ERROR,
  104. "function not available without connection object");
  105. return DUK_RET_ERROR;
  106. }
  107. ret = mg_write(conn, val, len);
  108. duk_push_number(duk_ctx, ret);
  109. return 1;
  110. }
  111. static duk_ret_t
  112. duk_itf_read(duk_context *duk_ctx)
  113. {
  114. struct mg_connection *conn;
  115. char buf[1024];
  116. int len;
  117. duk_push_current_function(duk_ctx);
  118. duk_get_prop_string(duk_ctx, -1, civetweb_conn_id);
  119. conn = (struct mg_connection *)duk_to_pointer(duk_ctx, -1);
  120. if (!conn) {
  121. duk_error(duk_ctx,
  122. DUK_ERR_ERROR,
  123. "function not available without connection object");
  124. return DUK_RET_ERROR;
  125. }
  126. len = mg_read(conn, buf, sizeof(buf));
  127. duk_push_lstring(duk_ctx, buf, len);
  128. return 1;
  129. }
  130. static duk_ret_t
  131. duk_itf_getoption(duk_context *duk_ctx)
  132. {
  133. struct mg_connection *conn;
  134. const char *ret;
  135. int optidx;
  136. duk_size_t len = 0;
  137. const char *val = duk_require_lstring(duk_ctx, -1, &len);
  138. duk_push_current_function(duk_ctx);
  139. duk_get_prop_string(duk_ctx, -1, civetweb_conn_id);
  140. conn = (struct mg_connection *)duk_to_pointer(duk_ctx, -1);
  141. if (!conn) {
  142. duk_error(duk_ctx,
  143. DUK_ERR_ERROR,
  144. "function not available without context object");
  145. return DUK_RET_ERROR;
  146. }
  147. optidx = get_option_index(val);
  148. if (optidx >= 0) {
  149. ret = conn->dom_ctx->config[optidx];
  150. } else {
  151. ret = NULL;
  152. }
  153. if (ret) {
  154. duk_push_string(duk_ctx, ret);
  155. } else {
  156. duk_push_null(duk_ctx);
  157. }
  158. return 1;
  159. }
  160. static void
  161. mg_exec_duktape_script(struct mg_connection *conn, const char *script_name)
  162. {
  163. int i;
  164. duk_context *duk_ctx = NULL;
  165. conn->must_close = 1;
  166. /* Create Duktape interpreter state */
  167. duk_ctx = duk_create_heap(mg_duk_mem_alloc,
  168. mg_duk_mem_realloc,
  169. mg_duk_mem_free,
  170. (void *)conn->phys_ctx,
  171. #if DUK_VERSION >= 20000L
  172. mg_duk_v2_fatal
  173. #else
  174. mg_duk_fatal_handler
  175. #endif
  176. );
  177. if (!duk_ctx) {
  178. mg_cry_internal(conn, "%s", "Failed to create a Duktape heap.");
  179. goto exec_duktape_finished;
  180. }
  181. /* Add "conn" object */
  182. duk_push_global_object(duk_ctx);
  183. duk_push_object(duk_ctx); /* create a new table/object ("conn") */
  184. /* add function conn.write */
  185. duk_push_c_function(duk_ctx, duk_itf_write, 1 /* 1 = nargs */);
  186. duk_push_pointer(duk_ctx, (void *)conn);
  187. duk_put_prop_string(duk_ctx, -2, civetweb_conn_id);
  188. duk_put_prop_string(duk_ctx, -2, "write");
  189. /* add function conn.read */
  190. duk_push_c_function(duk_ctx, duk_itf_read, 0 /* 0 = nargs */);
  191. duk_push_pointer(duk_ctx, (void *)conn);
  192. duk_put_prop_string(duk_ctx, -2, civetweb_conn_id);
  193. duk_put_prop_string(duk_ctx, -2, "read");
  194. /* add request_method object */
  195. duk_push_string(duk_ctx, conn->request_info.request_method);
  196. duk_put_prop_string(duk_ctx,
  197. -2,
  198. "request_method"); /* add string conn.r... */
  199. duk_push_string(duk_ctx, conn->request_info.request_uri);
  200. duk_put_prop_string(duk_ctx, -2, "request_uri");
  201. duk_push_string(duk_ctx, conn->request_info.local_uri);
  202. duk_put_prop_string(duk_ctx, -2, "uri");
  203. duk_push_string(duk_ctx, conn->request_info.http_version);
  204. duk_put_prop_string(duk_ctx, -2, "http_version");
  205. duk_push_string(duk_ctx, conn->request_info.query_string);
  206. duk_put_prop_string(duk_ctx, -2, "query_string");
  207. duk_push_string(duk_ctx, conn->request_info.remote_addr);
  208. duk_put_prop_string(duk_ctx, -2, "remote_addr");
  209. duk_push_int(duk_ctx, conn->request_info.remote_port);
  210. duk_put_prop_string(duk_ctx, -2, "remote_port");
  211. duk_push_int(duk_ctx, ntohs(conn->client.lsa.sin.sin_port));
  212. duk_put_prop_string(duk_ctx, -2, "server_port");
  213. duk_push_object(duk_ctx); /* subfolder "conn.http_headers" */
  214. for (i = 0; i < conn->request_info.num_headers; i++) {
  215. duk_push_string(duk_ctx, conn->request_info.http_headers[i].value);
  216. duk_put_prop_string(duk_ctx,
  217. -2,
  218. conn->request_info.http_headers[i].name);
  219. }
  220. duk_put_prop_string(duk_ctx, -2, "http_headers");
  221. duk_put_prop_string(duk_ctx, -2, "conn"); /* call the table "conn" */
  222. /* Add "civetweb" object */
  223. duk_push_global_object(duk_ctx);
  224. duk_push_object(duk_ctx); /* create a new table/object ("conn") */
  225. duk_push_string(duk_ctx, CIVETWEB_VERSION);
  226. duk_put_prop_string(duk_ctx, -2, "version");
  227. duk_push_string(duk_ctx, script_name);
  228. duk_put_prop_string(duk_ctx, -2, "script_name");
  229. /* add function civetweb.getoption */
  230. duk_push_c_function(duk_ctx, duk_itf_getoption, 1 /* 1 = nargs */);
  231. duk_push_pointer(duk_ctx, (void *)conn);
  232. duk_put_prop_string(duk_ctx, -2, civetweb_conn_id);
  233. duk_put_prop_string(duk_ctx, -2, "getoption");
  234. if (conn->phys_ctx != NULL) {
  235. /* add system name */
  236. if (conn->phys_ctx->systemName != NULL) {
  237. duk_push_string(duk_ctx, conn->phys_ctx->systemName);
  238. duk_put_prop_string(duk_ctx, -2, "system");
  239. }
  240. }
  241. duk_put_prop_string(duk_ctx,
  242. -2,
  243. "civetweb"); /* call the table "civetweb" */
  244. duk_push_global_stash(duk_ctx);
  245. duk_push_pointer(duk_ctx, (void *)conn);
  246. duk_put_prop_string(duk_ctx, -2, civetweb_conn_id);
  247. if (duk_peval_file(duk_ctx, script_name) != 0) {
  248. mg_cry_internal(conn, "%s", duk_safe_to_string(duk_ctx, -1));
  249. goto exec_duktape_finished;
  250. }
  251. duk_pop(duk_ctx); /* ignore result */
  252. exec_duktape_finished:
  253. duk_destroy_heap(duk_ctx);
  254. }
  255. /* End of mod_duktape.inl */