1 /* This file is part of the CivetWeb web server.
2 * See https://github.com/civetweb/civetweb/
3 * (C) 2015 by the CivetWeb authors, MIT license.
8 /* TODO: the mg context should be added to duktape as well */
9 /* Alternative: redefine a new, clean API from scratch (instead of using mg),
10 * or at least do not add problematic functions. */
11 /* For evaluation purposes, currently only "send" is supported.
12 * All other ~50 functions will be added later. */
14 /* Note: This is only experimental support, so the API may still change. */
16 static const char *civetweb_conn_id = "\xFF"
18 static const char *civetweb_ctx_id = "\xFF"
23 mg_duk_mem_alloc(void *udata, duk_size_t size)
25 return mg_malloc(size);
30 mg_duk_mem_realloc(void *udata, void *ptr, duk_size_t newsize)
32 return mg_realloc(ptr, newsize);
37 mg_duk_mem_free(void *udata, void *ptr)
44 mg_duk_fatal_handler(duk_context *ctx, duk_errcode_t code, const char *msg)
46 /* Script is called "protected" (duk_peval_file), so script errors should
47 * never yield in a call to this function. Maybe calls prior to executing
48 * the script could raise a fatal error. */
49 struct mg_connection *conn;
51 duk_push_global_stash(ctx);
52 duk_get_prop_string(ctx, -1, civetweb_conn_id);
53 conn = (struct mg_connection *)duk_to_pointer(ctx, -1);
55 mg_cry(conn, "%s", msg);
60 duk_itf_write(duk_context *ctx)
62 struct mg_connection *conn;
65 const char *val = duk_require_lstring(ctx, -1, &len);
68 duk_push_global_stash(ctx);
69 duk_get_prop_string(ctx, -1, civetweb_conn_id);
70 conn = (struct mg_connection *)duk_to_pointer(ctx, -1);
72 duk_push_current_function(ctx);
73 duk_get_prop_string(ctx, -1, civetweb_conn_id);
74 conn = (struct mg_connection *)duk_to_pointer(ctx, -1);
78 DUK_ERR_INTERNAL_ERROR,
79 "function not available without connection object");
80 /* probably never reached, but satisfies static code analysis */
81 return DUK_RET_INTERNAL_ERROR;
84 ret = mg_write(conn, val, len);
86 duk_push_number(ctx, ret);
92 duk_itf_read(duk_context *ctx)
94 struct mg_connection *conn;
98 duk_push_global_stash(ctx);
99 duk_get_prop_string(ctx, -1, civetweb_conn_id);
100 conn = (struct mg_connection *)duk_to_pointer(ctx, -1);
104 DUK_ERR_INTERNAL_ERROR,
105 "function not available without connection object");
106 /* probably never reached, but satisfies static code analysis */
107 return DUK_RET_INTERNAL_ERROR;
110 len = mg_read(conn, buf, sizeof(buf));
112 duk_push_lstring(ctx, buf, len);
118 duk_itf_getoption(duk_context *ctx)
120 struct mg_context *cv_ctx;
123 const char *val = duk_require_lstring(ctx, -1, &len);
125 duk_push_current_function(ctx);
126 duk_get_prop_string(ctx, -1, civetweb_ctx_id);
127 cv_ctx = (struct mg_context *)duk_to_pointer(ctx, -1);
131 DUK_ERR_INTERNAL_ERROR,
132 "function not available without connection object");
133 /* probably never reached, but satisfies static code analysis */
134 return DUK_RET_INTERNAL_ERROR;
137 ret = mg_get_option(cv_ctx, val);
139 duk_push_string(ctx, ret);
149 mg_exec_duktape_script(struct mg_connection *conn, const char *script_name)
152 duk_context *ctx = NULL;
154 conn->must_close = 1;
156 /* Create Duktape interpreter state */
157 ctx = duk_create_heap(mg_duk_mem_alloc,
161 mg_duk_fatal_handler);
163 mg_cry(conn, "Failed to create a Duktape heap.");
164 goto exec_duktape_finished;
167 /* Add "conn" object */
168 duk_push_global_object(ctx);
169 duk_push_object(ctx); /* create a new table/object ("conn") */
171 duk_push_c_function(ctx, duk_itf_write, 1 /* 1 = nargs */);
172 duk_push_pointer(ctx, (void *)conn);
173 duk_put_prop_string(ctx, -2, civetweb_conn_id);
174 duk_put_prop_string(ctx, -2, "write"); /* add function conn.write */
176 duk_push_c_function(ctx, duk_itf_read, 0 /* 0 = nargs */);
177 duk_push_pointer(ctx, (void *)conn);
178 duk_put_prop_string(ctx, -2, civetweb_conn_id);
179 duk_put_prop_string(ctx, -2, "read"); /* add function conn.read */
181 duk_push_string(ctx, conn->request_info.request_method);
182 duk_put_prop_string(ctx, -2, "request_method"); /* add string conn.r... */
184 duk_push_string(ctx, conn->request_info.request_uri);
185 duk_put_prop_string(ctx, -2, "request_uri");
187 duk_push_string(ctx, conn->request_info.local_uri);
188 duk_put_prop_string(ctx, -2, "uri");
190 duk_push_string(ctx, conn->request_info.http_version);
191 duk_put_prop_string(ctx, -2, "http_version");
193 duk_push_string(ctx, conn->request_info.query_string);
194 duk_put_prop_string(ctx, -2, "query_string");
196 duk_push_string(ctx, conn->request_info.remote_addr);
197 duk_put_prop_string(ctx, -2, "remote_addr");
199 duk_push_int(ctx, conn->request_info.remote_port);
200 duk_put_prop_string(ctx, -2, "remote_port");
202 duk_push_int(ctx, ntohs(conn->client.lsa.sin.sin_port));
203 duk_put_prop_string(ctx, -2, "server_port");
205 duk_push_object(ctx); /* subfolder "conn.http_headers" */
206 for (i = 0; i < conn->request_info.num_headers; i++) {
207 duk_push_string(ctx, conn->request_info.http_headers[i].value);
208 duk_put_prop_string(ctx, -2, conn->request_info.http_headers[i].name);
210 duk_put_prop_string(ctx, -2, "http_headers");
212 duk_put_prop_string(ctx, -2, "conn"); /* call the table "conn" */
214 /* Add "civetweb" object */
215 duk_push_global_object(ctx);
216 duk_push_object(ctx); /* create a new table/object ("conn") */
218 duk_push_string(ctx, CIVETWEB_VERSION);
219 duk_put_prop_string(ctx, -2, "version");
221 duk_push_string(ctx, script_name);
222 duk_put_prop_string(ctx, -2, "script_name");
224 if (conn->ctx != NULL) {
225 duk_push_c_function(ctx, duk_itf_getoption, 1 /* 1 = nargs */);
226 duk_push_pointer(ctx, (void *)(conn->ctx));
227 duk_put_prop_string(ctx, -2, civetweb_ctx_id);
228 duk_put_prop_string(ctx, -2, "getoption"); /* add function conn.write */
230 if (conn->ctx->systemName != NULL) {
231 duk_push_string(ctx, conn->ctx->systemName);
232 duk_put_prop_string(ctx, -2, "system");
236 duk_put_prop_string(ctx, -2, "civetweb"); /* call the table "civetweb" */
238 duk_push_global_stash(ctx);
239 duk_push_pointer(ctx, (void *)conn);
240 duk_put_prop_string(ctx, -2, civetweb_conn_id);
242 if (duk_peval_file(ctx, script_name) != 0) {
243 mg_cry(conn, "%s", duk_safe_to_string(ctx, -1));
244 goto exec_duktape_finished;
246 duk_pop(ctx); /* ignore result */
248 exec_duktape_finished:
249 duk_destroy_heap(ctx);