Skip to content

Commit 6615e0a

Browse files
committed
Made the parse_uri query changes backwards compatible
1 parent 875fe30 commit 6615e0a

File tree

4 files changed

+19
-14
lines changed

4 files changed

+19
-14
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,11 +358,11 @@ Sets the current response based on the given `res`. Ensures that hop-by-hop head
358358

359359
## parse_uri
360360

361-
`syntax: local scheme, host, port, path, query = unpack(httpc:parse_uri(uri))`
361+
`syntax: local scheme, host, port, path, query? = unpack(httpc:parse_uri(uri, query_in_path?))`
362362

363363
This is a convenience function allowing one to more easily use the generic interface, when the input data is a URI.
364364

365-
Note: Prior to v0.10 the `query` was returned as part of the `path`, but from v0.10 onwards they are returned separately, and `query` does not contain the `?` marker.
365+
As of version `0.10`, the optional `query_in_path` parameter was added, which specifies whether the querystring is to be included in the `path` return value, or separately as its own return value. This defaults to `true` in order to maintain backwards compatability. When set to `false`, `path` will only include the path, and `query` will contain the URI args, not inluding the `?` delimeter.
366366

367367

368368
## get_client_body_reader

lib/resty/http.lua

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,9 @@ local function _should_receive_body(method, code)
207207
end
208208

209209

210-
function _M.parse_uri(self, uri)
210+
function _M.parse_uri(self, uri, query_in_path)
211+
if query_in_path == nil then query_in_path = true end
212+
211213
local m, err = ngx_re_match(uri, [[^(?:(http[s]?):)?//([^:/\?]+)(?::(\d+))?([^\?]*)\??(.*)]], "jo")
212214

213215
if not m then
@@ -238,6 +240,12 @@ function _M.parse_uri(self, uri)
238240
end
239241
end
240242
if not m[4] or "" == m[4] then m[4] = "/" end
243+
244+
if query_in_path and m[5] and m[5] ~= "" then
245+
m[4] = m[4] .. "?" .. m[5]
246+
m[5] = nil
247+
end
248+
241249
return m, nil
242250
end
243251
end
@@ -248,12 +256,10 @@ local function _format_request(params)
248256
local headers = params.headers or {}
249257

250258
local query = params.query or ""
251-
if query then
252-
if type(query) == "table" then
253-
query = "?" .. ngx_encode_args(query)
254-
elseif query ~= "" and str_sub(query, 1, 1) ~= "?" then
255-
query = "?" .. query
256-
end
259+
if type(query) == "table" then
260+
query = "?" .. ngx_encode_args(query)
261+
elseif query ~= "" and str_sub(query, 1, 1) ~= "?" then
262+
query = "?" .. query
257263
end
258264

259265
-- Initialize request
@@ -761,7 +767,7 @@ end
761767
function _M.request_uri(self, uri, params)
762768
if not params then params = {} end
763769

764-
local parsed_uri, err = self:parse_uri(uri)
770+
local parsed_uri, err = self:parse_uri(uri, false)
765771
if not parsed_uri then
766772
return nil, err
767773
end

t/01-basic.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ bad uri: http:///example.com
305305
local http = require("resty.http").new()
306306
307307
function test_uri(uri)
308-
local scheme, host, port, path, query = unpack(http:parse_uri(uri))
308+
local scheme, host, port, path, query = unpack(http:parse_uri(uri, false))
309309
ngx.say("scheme: ", scheme, ", host: ", host, ", port: ", port, ", path: ", path, ", query: ", query)
310310
end
311311

t/06-simpleinterface.t

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ OK
193193
[warn]
194194

195195

196-
=== TEST 4 Simple URI interface, params override, query as string, as leading ?
196+
=== TEST 5 Simple URI interface, params override, query as string, as leading ?
197197
--- http_config eval: $::HttpConfig
198198
--- config
199199
location = /a {
@@ -202,7 +202,6 @@ OK
202202
local httpc = http.new()
203203
local res, err = httpc:request_uri(
204204
"http://127.0.0.1:"..ngx.var.server_port.."/b?a=1&b=2", {
205-
path = "/c",
206205
query = "?a=2&b=3",
207206
}
208207
)
@@ -215,7 +214,7 @@ OK
215214
ngx.print(res.body)
216215
';
217216
}
218-
location = /c {
217+
location = /b {
219218
content_by_lua '
220219
for k,v in pairs(ngx.req.get_uri_args()) do
221220
ngx.header["X-Header-" .. string.upper(k)] = v

0 commit comments

Comments
 (0)