如何在 Lua 中对 request_uri 字符串进行 urldecode [英] how to urldecode a request_uri string in Lua
本文介绍了如何在 Lua 中对 request_uri 字符串进行 urldecode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我使用 ngx.var.request_uri
时,我得到一个包含 %20 代替空格的字符串.是否有 urldecode() 函数或类似函数来解码我的字符串?
When I use ngx.var.request_uri
I'm getting back a string that contains %20 in place of spaces. Is there a urldecode() function or similar to decode my string?
推荐答案
解码后的 URI 可以在 ngx.var.uri
中找到.它不包含查询字符串,如果您需要它,请参阅 ngx.var.query_string
.
The decoded URI can be found in ngx.var.uri
. It does not contain the query string, if you need it see ngx.var.query_string
.
如果你不能使用它,这里有一个在 Lua 中取消转义 URL 的简单方法.
if you cannot use this, here is a simple way to unescape a URL in Lua.
local hex_to_char = function(x)
return string.char(tonumber(x, 16))
end
local unescape = function(url)
return url:gsub("%%(%x%x)", hex_to_char)
end
示例用法:
local url = "/test/some%20string?foo=bar"
print(unescape(url)) -- /test/some string?foo=bar
但是您应该在使用它之前分割查询字符串.
But you should probably split the query string before using it.
这篇关于如何在 Lua 中对 request_uri 字符串进行 urldecode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文