Quellcode durchsuchen

Tell Lua which operating system is running, so scripts may handle differences

bel vor 11 Jahren
Ursprung
Commit
87a3b74ee5
4 geänderte Dateien mit 50 neuen und 6 gelöschten Zeilen
  1. 37 0
      src/civetweb.c
  2. 4 0
      src/mod_lua.inl
  3. 4 1
      test/ajax/echo.lp
  4. 5 5
      test/ajax/echo.lua

+ 37 - 0
src/civetweb.c

@@ -256,6 +256,7 @@ struct pollfd {
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <sys/time.h>
+#include <sys/utsname.h>
 #include <stdint.h>
 #include <inttypes.h>
 #include <netdb.h>
@@ -630,6 +631,8 @@ struct mg_context {
     unsigned long start_time;  /* Server start time, used for authentication */
     unsigned long nonce_count; /* Used nonces, used for authentication */
 
+    char *systemName;          /* What operating system is running */
+
     /* linked list of uri handlers */
     struct mg_request_handler_info *request_handlers;
 };
@@ -6411,6 +6414,9 @@ static void free_context(struct mg_context *ctx)
         pthread_key_delete(sTlsKey);
     }
 
+    /* deallocate system name string */
+    free(ctx->systemName);
+
     /* Deallocate context itself */
     free(ctx);
 }
@@ -6431,6 +6437,35 @@ void mg_stop(struct mg_context *ctx)
 #endif /* _WIN32 && !__SYMBIAN32__ */
 }
 
+void get_system_name(char **sysName)
+{
+#if defined(_WIN32)
+#if !defined(__SYMBIAN32__)
+    char name[128];
+    DWORD dwVersion = 0;
+    DWORD dwMajorVersion = 0;
+    DWORD dwMinorVersion = 0;
+    DWORD dwBuild = 0;
+
+    dwVersion = GetVersion();
+
+    dwMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
+    dwMinorVersion = (DWORD)(HIBYTE(LOWORD(dwVersion)));
+    dwBuild = ((dwVersion < 0x80000000) ? (DWORD)(HIWORD(dwVersion)) : 0);
+
+    sprintf(name, "Windows %d.%d", dwMajorVersion, dwMinorVersion);
+    *sysName = mg_strdup(name);
+#else
+    *sysName = mg_strdup("Symbian");
+#endif
+#else
+    struct utsname name;
+    memset(&name, 0, sizeof(name));
+    uname(&name);
+    *sysName = mg_strdup(name.sysname);
+#endif
+}
+
 struct mg_context *mg_start(const struct mg_callbacks *callbacks,
                             void *user_data,
                             const char **options)
@@ -6496,6 +6531,8 @@ struct mg_context *mg_start(const struct mg_callbacks *callbacks,
         }
     }
 
+    get_system_name(&ctx->systemName);
+
     /* NOTE(lsm): order is important here. SSL certificates must
        be initialized before listening ports. UID must be set last. */
     if (!set_gpass_option(ctx) ||

+ 4 - 0
src/mod_lua.inl

@@ -751,6 +751,10 @@ static void prepare_lua_environment(struct mg_connection *conn, lua_State *L, co
     reg_string(L, "websocket_root", conn->ctx->config[WEBSOCKET_ROOT]);
 #endif
 
+    if (conn->ctx->systemName) {
+        reg_string(L, "system", conn->ctx->systemName);
+    }
+
     /* Export request_info */
     lua_pushstring(L, "request_info");
     lua_newtable(L);

+ 4 - 1
test/ajax/echo.lp

@@ -1,6 +1,9 @@
 <?
+-- This *.lp file simply runs the *.lua file in the same directory.
 n = string.match(mg.request_info.uri, "^(.*)%.lp$")
-n = string.gsub(n, [[/]], [[\]])
+if mg.system:find("Windows") then
+    n = string.gsub(n, [[/]], [[\]])
+end
 n = mg.document_root .. n .. ".lua"
 dofile(n)
 ?>

+ 5 - 5
test/ajax/echo.lua

@@ -24,12 +24,12 @@ resp = resp .. "}";
 
 
 
-mg.write("HTTP/1.1 200 OK\n")
-mg.write("Connection: close\n")
-mg.write("Content-Type: text/html\n")
-mg.write("Cache-Control: no-cache\n")
+mg.write("HTTP/1.1 200 OK\r\n")
+mg.write("Connection: close\r\n")
+mg.write("Content-Type: text/html\r\n")
+mg.write("Cache-Control: no-cache\r\n")
 --mg.write("Content-Length: " .. resp:len() .. "\n")
-mg.write("\n")
+mg.write("\r\n")
 
 mg.write(resp)