瀏覽代碼

Add get_mime_type function for Lua, add test

bel 11 年之前
父節點
當前提交
a4b652d16c
共有 2 個文件被更改,包括 26 次插入2 次删除
  1. 24 1
      src/mod_lua.inl
  2. 2 1
      test/resource_script_demo.lua

+ 24 - 1
src/mod_lua.inl

@@ -372,9 +372,31 @@ static int lsp_get_var(lua_State *L)
     return 1;
 }
 
+/* mg.get_mime_type */
+static int lsp_get_mime_type(lua_State *L)
+{
+    struct mg_connection *conn = lua_touserdata(L, lua_upvalueindex(1));
+    int params = lua_gettop(L);
+    struct vec mime_type = {0};
+    const char *text;
+
+    if (params==1) {
+        text = lua_tostring(L, 1);
+        if (text) {
+            get_mime_type(conn->ctx, text, &mime_type);
+            lua_pushlstring(L, mime_type.ptr, mime_type.len);
+        } else {
+            lua_pushnil(L);
+        }
+    } else {
+        /* Syntax error */
+        return luaL_error(L, "invalid get_mime_type() call");
+    }
+    return 1;
+}
+
 /* TODO: Other functions in civetweb.h that should be available to Lua too:
 mg_get_cookie
-mg_get_builtin_mime_type
 mg_url_decode
 mg_url_encode
 mg_md5
@@ -491,6 +513,7 @@ static void prepare_lua_environment(struct mg_connection *conn, lua_State *L, co
 
     reg_function(L, "send_file", lsp_send_file, conn);
     reg_function(L, "get_var", lsp_get_var, conn);
+    reg_function(L, "get_mime_type", lsp_get_mime_type, conn);
 
     reg_string(L, "version", CIVETWEB_VERSION);
     reg_string(L, "document_root", conn->ctx->config[DOCUMENT_ROOT]);

+ 2 - 1
test/resource_script_demo.lua

@@ -49,12 +49,13 @@ if method=="GET" then
     if lfs.attributes(file) then
         mg.send_file(file)
     else
+        mime = mg.get_mime_type(file)
         mg.write("HTTP/1.0 404 Not Found\r\n")
         mg.write("Connection: close\r\n")
         mg.write("Content-Type: text/html; charset=utf-8\r\n")
         mg.write("\r\n")
         mg.write("<html><head><title>Civetweb Lua script resource handling test</title></head>\r\n")
-        mg.write("<body>Resource not found.</body></html>\r\n")
+        mg.write("<body>Resource of type \"" .. mime .. "\" not found.</body></html>\r\n")
     end
     return
 end