Skip to content

Commit 82aa296

Browse files
committed
Added support for schemaless URIs
1 parent 7f948a6 commit 82aa296

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

lib/resty/http.lua

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,7 @@ end
198198

199199

200200
function _M.parse_uri(self, uri)
201-
local m, err = ngx_re_match(uri, [[^(http[s]?)://([^:/]+)(?::(\d+))?(.*)]],
202-
"jo")
201+
local m, err = ngx_re_match(uri, [[^(?:(http[s]?):)?//([^:/]+)(?::(\d+))?(.*)]], "jo")
203202

204203
if not m then
205204
if err then
@@ -208,6 +207,17 @@ function _M.parse_uri(self, uri)
208207

209208
return nil, "bad uri: " .. uri
210209
else
210+
-- If the URI is schemaless (i.e. //example.com) try to use our current
211+
-- request scheme.
212+
if not m[1] then
213+
local scheme = ngx.var.scheme
214+
if scheme then
215+
m[1] = scheme
216+
else
217+
return nil, "schemaless URIs require a request context: " .. uri
218+
end
219+
end
220+
211221
if m[3] then
212222
m[3] = tonumber(m[3])
213223
else

t/01-basic.t

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,3 +295,26 @@ bad uri: http:///example.com
295295
--- no_error_log
296296
[error]
297297
[warn]
298+
299+
300+
=== TEST 10: Parse URI will use current request schema if omitted (and available)
301+
--- http_config eval: $::HttpConfig
302+
--- config
303+
location = /a {
304+
content_by_lua '
305+
local http = require("resty.http").new()
306+
local parts, err = http:parse_uri("//example.com")
307+
if not parts then
308+
ngx.say(err)
309+
else
310+
ngx.say(parts[1])
311+
end
312+
';
313+
}
314+
--- request
315+
GET /a
316+
--- response_body
317+
http
318+
--- no_error_log
319+
[error]
320+
[warn]

0 commit comments

Comments
 (0)