如何在Lua中urldecode一个request_uri字符串 [英] how to urldecode a request_uri string in Lua

查看:848
本文介绍了如何在Lua中urldecode一个request_uri字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用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中urldecode一个request_uri字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆