diff --git a/VS2012/civetweb_lua/civetweb_lua.vcxproj b/VS2012/civetweb_lua/civetweb_lua.vcxproj
index e5e57eb9..bc23eaa6 100644
--- a/VS2012/civetweb_lua/civetweb_lua.vcxproj
+++ b/VS2012/civetweb_lua/civetweb_lua.vcxproj
@@ -233,6 +233,7 @@
+
diff --git a/VS2012/civetweb_lua/civetweb_lua.vcxproj.filters b/VS2012/civetweb_lua/civetweb_lua.vcxproj.filters
index db2f8d3d..84bc20eb 100644
--- a/VS2012/civetweb_lua/civetweb_lua.vcxproj.filters
+++ b/VS2012/civetweb_lua/civetweb_lua.vcxproj.filters
@@ -62,5 +62,8 @@
inl files
+
+ inl files
+
\ No newline at end of file
diff --git a/format.bat b/format.bat
index 293684c5..2a31f142 100644
--- a/format.bat
+++ b/format.bat
@@ -4,6 +4,7 @@ clang-format -i src/CivetServer.cpp
clang-format -i src/civetweb_private_lua.h
clang-format -i src/md5.inl
clang-format -i src/mod_lua.inl
+clang-format -i src/mod_duktape.inl
clang-format -i src/timer.inl
clang-format -i src/third_party/civetweb_lua.h
diff --git a/src/civetweb.c b/src/civetweb.c
index 651cdc4d..0bca5eef 100755
--- a/src/civetweb.c
+++ b/src/civetweb.c
@@ -962,6 +962,10 @@ enum {
LUA_SCRIPT_EXTENSIONS,
LUA_SERVER_PAGE_EXTENSIONS,
#endif
+#if defined(USE_DUKTAPE)
+ DUKTAPE_SCRIPT_EXTENSIONS,
+#endif
+
#if defined(USE_WEBSOCKET)
WEBSOCKET_ROOT,
#endif
@@ -1017,6 +1021,12 @@ static struct mg_option config_options[] = {
{"lua_script_pattern", CONFIG_TYPE_EXT_PATTERN, "**.lua$"},
{"lua_server_page_pattern", CONFIG_TYPE_EXT_PATTERN, "**.lp$|**.lsp$"},
#endif
+#if defined(USE_DUKTAPE)
+ {"_experimental_duktape_script_pattern",
+ CONFIG_TYPE_EXT_PATTERN,
+ "**.ssjs$"}, /* TODO: redefine parameter */
+#endif
+
#if defined(USE_WEBSOCKET)
{"websocket_root", CONFIG_TYPE_DIRECTORY, NULL},
#endif
@@ -3508,7 +3518,7 @@ static int mg_read_inner(struct mg_connection *conn, void *buf, size_t len)
/* Adjust number of bytes to read. */
int64_t left_to_read = conn->content_len - conn->consumed_content;
if (left_to_read < len64) {
- /* Do not reade more than the total content length of the request.
+ /* Do not read more than the total content length of the request.
*/
len64 = left_to_read;
}
@@ -4126,7 +4136,7 @@ interpret_uri(struct mg_connection *conn, /* in: request */
/* Local file path and name, corresponding to requested URI
* is now stored in "filename" variable. */
if (mg_stat(conn, filename, filep)) {
-#if !defined(NO_CGI) || defined(USE_LUA)
+#if !defined(NO_CGI) || defined(USE_LUA) || defined(USE_DUKTAPE)
/* File exists. Check if it is a script type. */
if (0
#if !defined(NO_CGI)
@@ -4140,6 +4150,13 @@ interpret_uri(struct mg_connection *conn, /* in: request */
match_prefix(conn->ctx->config[LUA_SCRIPT_EXTENSIONS],
strlen(conn->ctx->config[LUA_SCRIPT_EXTENSIONS]),
filename) > 0
+#endif
+#if defined(USE_DUKTAPE)
+ ||
+ match_prefix(
+ conn->ctx->config[DUKTAPE_SCRIPT_EXTENSIONS],
+ strlen(conn->ctx->config[DUKTAPE_SCRIPT_EXTENSIONS]),
+ filename) > 0
#endif
) {
/* The request addresses a CGI script or a Lua script. The URI
@@ -4154,7 +4171,7 @@ interpret_uri(struct mg_connection *conn, /* in: request */
* generated response. */
*is_script_ressource = !*is_put_or_delete_request;
}
-#endif /* !defined(NO_CGI) || defined(USE_LUA) */
+#endif /* !defined(NO_CGI) || defined(USE_LUA) || defined(USE_DUKTAPE) */
*is_found = 1;
return;
}
@@ -4190,7 +4207,7 @@ interpret_uri(struct mg_connection *conn, /* in: request */
}
}
-#if !defined(NO_CGI) || defined(USE_LUA)
+#if !defined(NO_CGI) || defined(USE_LUA) || defined(USE_DUKTAPE)
/* Support PATH_INFO for CGI scripts. */
for (p = filename + strlen(filename); p > filename + 1; p--) {
if (*p == '/') {
@@ -4208,6 +4225,13 @@ interpret_uri(struct mg_connection *conn, /* in: request */
conn->ctx->config[LUA_SCRIPT_EXTENSIONS],
strlen(conn->ctx->config[LUA_SCRIPT_EXTENSIONS]),
filename) > 0
+#endif
+#if defined(USE_DUKTAPE)
+ ||
+ match_prefix(
+ conn->ctx->config[DUKTAPE_SCRIPT_EXTENSIONS],
+ strlen(conn->ctx->config[DUKTAPE_SCRIPT_EXTENSIONS]),
+ filename) > 0
#endif
) &&
mg_stat(conn, filename, filep)) {
@@ -4227,7 +4251,7 @@ interpret_uri(struct mg_connection *conn, /* in: request */
}
}
}
-#endif /* !defined(NO_CGI) || defined(USE_LUA) */
+#endif /* !defined(NO_CGI) || defined(USE_LUA) || defined(USE_DUKTAPE) */
#endif /* !defined(NO_FILES) */
}
return;
@@ -7532,6 +7556,10 @@ void mg_unlock_context(struct mg_context *ctx)
#include "mod_lua.inl"
#endif /* USE_LUA */
+#ifdef USE_DUKTAPE
+#include "mod_duktape.inl"
+#endif /* USE_DUKTAPE */
+
#if defined(USE_WEBSOCKET)
@@ -9132,6 +9160,14 @@ static void handle_file_based_request(struct mg_connection *conn,
* entire reply. */
mg_exec_lua_script(conn, path, NULL);
#endif
+#if defined(USE_DUKTAPE)
+ } else if (match_prefix(
+ conn->ctx->config[DUKTAPE_SCRIPT_EXTENSIONS],
+ strlen(conn->ctx->config[DUKTAPE_SCRIPT_EXTENSIONS]),
+ path) > 0) {
+ /* Call duktape to generate the page */
+ mg_exec_duktape_script(conn, path);
+#endif
#if !defined(NO_CGI)
} else if (match_prefix(conn->ctx->config[CGI_EXTENSIONS],
strlen(conn->ctx->config[CGI_EXTENSIONS]),
diff --git a/src/main.c b/src/main.c
index 747f0777..9257b2c4 100644
--- a/src/main.c
+++ b/src/main.c
@@ -740,9 +740,6 @@ static int run_lua(const char *file_name)
static int run_duktape(const char *file_name)
{
duk_context *ctx = NULL;
- char line[4096];
- char idx;
- int ch;
#ifdef WIN32
(void)MakeConsole();
diff --git a/src/mod_duktape.inl b/src/mod_duktape.inl
new file mode 100644
index 00000000..8665e682
--- /dev/null
+++ b/src/mod_duktape.inl
@@ -0,0 +1,32 @@
+/* This file is part of the CivetWeb web server.
+ * See https://github.com/civetweb/civetweb/
+ * (C) 2015 by the CivetWeb authors, MIT license.
+ */
+
+#include "duktape.h"
+
+/* TODO: This stub is currently not useful, since there is no way to communicate
+ * with the client. */
+static void mg_exec_lua_script(struct mg_connection *conn, const char *path)
+{
+ duk_context *ctx = NULL;
+
+#ifdef WIN32
+ (void)MakeConsole();
+#endif
+
+ ctx = duk_create_heap_default();
+ if (!ctx) {
+ fprintf(stderr, "Failed to create a Duktape heap.\n");
+ goto finished;
+ }
+
+ if (duk_peval_file(ctx, path) != 0) {
+ fprintf(stderr, "%s\n", duk_safe_to_string(ctx, -1));
+ goto finished;
+ }
+ duk_pop(ctx); /* ignore result */
+
+finished:
+ duk_destroy_heap(ctx);
+}