Skip to content

Commit a730f90

Browse files
committed
Encode spaces in proxied URIs
1 parent e937324 commit a730f90

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

lib/resty/http.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ local tbl_concat = table.concat
1515
local tbl_insert = table.insert
1616
local ngx_encode_args = ngx.encode_args
1717
local ngx_re_match = ngx.re.match
18+
local ngx_re_gsub = ngx.re.gsub
1819
local ngx_log = ngx.log
1920
local ngx_DEBUG = ngx.DEBUG
2021
local ngx_ERR = ngx.ERR
@@ -773,7 +774,7 @@ end
773774
function _M.proxy_request(self, chunksize)
774775
return self:request{
775776
method = ngx_req_get_method(),
776-
path = ngx_var.uri .. ngx_var.is_args .. (ngx_var.query_string or ""),
777+
path = ngx_re_gsub(ngx_var.uri, "\\s", "%20", "jo") .. ngx_var.is_args .. (ngx_var.query_string or ""),
777778
body = self:get_client_body_reader(chunksize),
778779
headers = ngx_req_get_headers(),
779780
}

t/11-proxy.t

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ X-Test: foo
8989
[warn]
9090

9191

92-
=== TEST 3: Proxy multiple headers
92+
=== TEST 3: Proxy multiple headers
9393
--- http_config eval: $::HttpConfig
9494
--- config
9595
location = /a_prx {
@@ -118,3 +118,35 @@ OK
118118
--- no_error_log
119119
[error]
120120
[warn]
121+
122+
123+
=== TEST 4: Proxy still works with spaces in URI
124+
--- http_config eval: $::HttpConfig
125+
--- config
126+
location = "/a_ b_prx" {
127+
rewrite ^(.*)_prx$ $1 break;
128+
content_by_lua '
129+
local http = require "resty.http"
130+
local httpc = http.new()
131+
httpc:connect("127.0.0.1", ngx.var.server_port)
132+
httpc:proxy_response(httpc:proxy_request())
133+
httpc:set_keepalive()
134+
';
135+
}
136+
location = "/a_ b" {
137+
content_by_lua '
138+
ngx.status = 200
139+
ngx.header["X-Test"] = "foo"
140+
ngx.say("OK")
141+
';
142+
}
143+
--- request
144+
GET /a_%20b_prx
145+
--- response_body
146+
OK
147+
--- response_headers
148+
X-Test: foo
149+
--- error_code: 200
150+
--- no_error_log
151+
[error]
152+
[warn]

0 commit comments

Comments
 (0)