瀏覽代碼

Duktape: Add http_headers

bel 9 年之前
父節點
當前提交
9a9a424511
共有 2 個文件被更改,包括 14 次插入5 次删除
  1. 11 3
      src/mod_duktape.inl
  2. 3 2
      test/page3.ssjs

+ 11 - 3
src/mod_duktape.inl

@@ -43,9 +43,9 @@ mg_duk_mem_free(void *udata, void *ptr)
 static void
 mg_duk_fatal_handler(duk_context *ctx, duk_errcode_t code, const char *msg)
 {
-	/* TODO: check if this handler is required - duk_peval_file probably
-	 * already avoids that this function is called. */
-	/* TODO: test this handler (if it is called) */
+	/* Script is called "protected" (duk_peval_file), so script errors should
+	 * never yield in a call to this function. Maybe calls prior to executing
+	 * the script could raise a fatal error. */
 	struct mg_connection *conn;
 
 	duk_push_global_stash(ctx);
@@ -148,6 +148,7 @@ duk_itf_getoption(duk_context *ctx)
 static void
 mg_exec_duktape_script(struct mg_connection *conn, const char *script_name)
 {
+	int i;
 	duk_context *ctx = NULL;
 
 	conn->must_close = 1;
@@ -201,6 +202,13 @@ mg_exec_duktape_script(struct mg_connection *conn, const char *script_name)
 	duk_push_int(ctx, ntohs(conn->client.lsa.sin.sin_port));
 	duk_put_prop_string(ctx, -2, "server_port");
 
+	duk_push_object(ctx); /* subfolder "conn.http_headers" */
+	for (i = 0; i < conn->request_info.num_headers; i++) {
+		duk_push_string(ctx, conn->request_info.http_headers[i].value);
+		duk_put_prop_string(ctx, -2, conn->request_info.http_headers[i].name);
+	}
+	duk_put_prop_string(ctx, -2, "http_headers");
+
 	duk_put_prop_string(ctx, -2, "conn"); /* call the table "conn" */
 
 	/* Add "civetweb" object */

+ 3 - 2
test/page3.ssjs

@@ -34,7 +34,8 @@ opts = [
 "websocket_root",
 "lua_websocket_pattern",
 "access_control_allow_origin",
-"error_pages"
+"error_pages",
+"_unknown__option"
 ]
 
 // send a header
@@ -53,7 +54,7 @@ for (var i=0; i < opts.length; i++) {
   if (typeof(n) == "string") {
     print("<p>Option " + o + " = " + n + "</p>\n");
   } else {
-    print("<p>Option " + o + " not set</p>\n");
+    print("<p>Option " + o + " not known</p>\n");
   }
 }