how to urldecode a request_uri string in Lua
Asked Answered
P

4

11

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?

Pasto answered 29/11, 2013 at 9:16 Comment(0)
D
15

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.

EDIT: 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

Example usage:

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.

Derogatory answered 29/11, 2013 at 9:59 Comment(3)
When I visit /abc and my config bounces me through a 404 using error_page 404 /test, when I access ngx.var.uri it's equal to /test rather than /abc. However, ngx.var.request_uri evaluates to /abcPasto
OK, I have edited my answer to give you another option that actually unescapes the URL in Lua.Derogatory
I wish there was a way to do this inline.Oe
R
20

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

newstr = ngx.unescape_uri(str)

You can also take a look of ngxescape_uri

Resinous answered 6/12, 2013 at 13:55 Comment(0)
D
15

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.

EDIT: 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

Example usage:

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.

Derogatory answered 29/11, 2013 at 9:59 Comment(3)
When I visit /abc and my config bounces me through a 404 using error_page 404 /test, when I access ngx.var.uri it's equal to /test rather than /abc. However, ngx.var.request_uri evaluates to /abcPasto
OK, I have edited my answer to give you another option that actually unescapes the URL in Lua.Derogatory
I wish there was a way to do this inline.Oe
C
2

Try ngx.req api from nginx-lua-module

  • ngx.req.set_uri : rewrite uri, rewrite path only, usengx.req.set_uri_args if u also want replace params
  • ngx.escape_uri: for encode string
  • ngx.unescape_uri: for decode string

For Example: decode path & args

location / {
  .....

  rewrite_by_lua_block {
       # Get nginx var $uri , ucant change to $request_uri ,$args ...
       local uri = ngx.var.uri

       # use api below to decode args
       ngx.req.set_uri_args(ngx.unescape_uri(ngx.var.args));

       # use set_uri to decode path
       ngx.req.set_uri(ngx.unescape_uri(uri));
  }
}
  proxy_pass ....;

Ref. Document : https://github.com/openresty/lua-nginx-module#ngxreqset_uri

Cwmbran answered 10/3, 2020 at 10:29 Comment(0)
K
0

LuaSocket has a url.unescape utility. Quoting:

url.unescape(content)

Removes the URL escaping content coding from a string.

Content is the string to be decoded.

The function returns the decoded string.

Kory answered 14/2, 2019 at 15:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.