Ver Fonte

Add url_encode to Lua

bel há 11 anos atrás
pai
commit
4d5d6dfee0
1 ficheiros alterados com 23 adições e 0 exclusões
  1. 23 0
      src/mod_lua.inl

+ 23 - 0
src/mod_lua.inl

@@ -491,6 +491,28 @@ static int lsp_md5(lua_State *L)
     return 1;
     return 1;
 }
 }
 
 
+/* mg.url_encode */
+static int lsp_url_encode(lua_State *L)
+{
+    int num_args = lua_gettop(L);
+    const char *text;
+    size_t text_len;
+    char dst[512];
+
+    if (num_args==1) {
+        text = lua_tolstring(L, 1, &text_len);
+        if (text) {
+            mg_url_encode(text, dst, sizeof(dst));
+            lua_pushstring(L, dst);
+        } 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:
 /* TODO: Other functions in civetweb.h that should be available to Lua too:
 mg_url_decode
 mg_url_decode
@@ -611,6 +633,7 @@ static void prepare_lua_environment(struct mg_connection *conn, lua_State *L, co
     reg_function(L, "get_mime_type", lsp_get_mime_type, conn);
     reg_function(L, "get_mime_type", lsp_get_mime_type, conn);
     reg_function(L, "get_cookie", lsp_get_cookie, conn);
     reg_function(L, "get_cookie", lsp_get_cookie, conn);
     reg_function(L, "md5", lsp_md5, conn);
     reg_function(L, "md5", lsp_md5, conn);
+    reg_function(L, "url_encode", lsp_url_encode, conn);
 
 
     reg_string(L, "version", CIVETWEB_VERSION);
     reg_string(L, "version", CIVETWEB_VERSION);
     reg_string(L, "document_root", conn->ctx->config[DOCUMENT_ROOT]);
     reg_string(L, "document_root", conn->ctx->config[DOCUMENT_ROOT]);