Like urldecode string request_uri in Lua

When I use ngx.var.request_uri , I return a string containing% 20 instead of spaces. Is there a urldecode () function or similar to decrypting my string?

+4
source share
2 answers

The decoded URI can be found at ngx.var.uri . It does not contain a query string, if you need it, see ngx.var.query_string .

EDIT: if you cannot use this, here is a simple way to unescape URLs 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 

Usage example:

 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.

+8
source

If you are using nginx-lua-module , you can use the api below for this.

  newstr = ngx.unescape_uri (str)

You can also take a look at ngxescape_uri

+14
source

Source: https://habr.com/ru/post/959407/


All Articles