Ver código fonte

Experimental support for duktape (Step 3/?)

bel 9 anos atrás
pai
commit
5fbb1db308

+ 1 - 0
VS2012/civetweb_lua/civetweb_lua.vcxproj

@@ -233,6 +233,7 @@
   </ItemGroup>
   <ItemGroup>
     <None Include="..\..\src\md5.inl" />
+    <None Include="..\..\src\mod_duktape.inl" />
     <None Include="..\..\src\mod_lua.inl" />
     <None Include="..\..\src\timer.inl" />
   </ItemGroup>

+ 3 - 0
VS2012/civetweb_lua/civetweb_lua.vcxproj.filters

@@ -62,5 +62,8 @@
     <None Include="..\..\src\timer.inl">
       <Filter>inl files</Filter>
     </None>
+    <None Include="..\..\src\mod_duktape.inl">
+      <Filter>inl files</Filter>
+    </None>
   </ItemGroup>
 </Project>

+ 1 - 0
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

+ 41 - 5
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)
@@ -4141,6 +4151,13 @@ interpret_uri(struct mg_connection *conn,   /* in: request */
 			                 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
 				 * corresponds to the script itself (like /path/script.cgi),
@@ -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 == '/') {
@@ -4209,6 +4226,13 @@ interpret_uri(struct mg_connection *conn,   /* in: request */
 				         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)) {
 					/* Shift PATH_INFO block one character right, e.g.
@@ -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]),

+ 0 - 3
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();

+ 32 - 0
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);
+}