Closed ignacio closed 11 years ago
While this commit makes qs_encode behave properly according to the spec, it makes it cumbersome when using that to build urls. So, an alternative way is proposed here. It trades correctness for ease of use:
function methods:qs_encode(query, url)
local parts = {}
for k, v in pairs(query or {}) do
parts[#parts+1] = k .. "=" .. util.url_encode(v)
end
if #parts > 0 then
return (url and (url .. "?") or "") .. table.concat(parts, "&")
else
return (url and url or "")
end
end
function methods:route_link(route, query, ...)
local builder = self.mk_app["link_" .. route]
if builder then
local uri = builder(self.mk_app, self.env, ...)
return self:qs_encode(query, uri)
else
error("there is no route named " .. route)
end
end
function methods:link(url, query)
local prefix = (self.mk_app and self.mk_app.prefix) or self.script_name
local uri = prefix .. url
return self:qs_encode(query, uri)
end
function methods:absolute_link(url, query)
return self:qs_encode(query, url)
end
Maybe there is some middle ground, like _qsencode having a flag to control whether '?' is prepended or not.
According to RFC 3875, the question mark is not part of the querystring.
This code fails because the first key-value pair is missing:
I could fix that in wsapi.mock, just stripping the question mark, but it seems wrong to me. If _wsapi.request.qsencode does not include it, methods like _request.routelink and request.link should be fixed. That seems like the proper thing to do. What do you think?