From 7873cdad956791517f1cfbcab0e5ffb44627a36f Mon Sep 17 00:00:00 2001 From: suika-kong Date: Tue, 14 Mar 2023 15:04:39 +0800 Subject: [PATCH 1/3] fix: no trailing "?" for empty query table --- lib/resty/http.lua | 6 ++++-- t/01-basic.t | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/lib/resty/http.lua b/lib/resty/http.lua index e97c456..9eeeeb5 100644 --- a/lib/resty/http.lua +++ b/lib/resty/http.lua @@ -313,8 +313,10 @@ local function _format_request(self, params) local query = params.query or "" if type(query) == "table" then - query = "?" .. ngx_encode_args(query) - elseif query ~= "" and str_sub(query, 1, 1) ~= "?" then + query = ngx_encode_args(query) + end + + if query ~= "" and str_sub(query, 1, 1) ~= "?" then query = "?" .. query end diff --git a/t/01-basic.t b/t/01-basic.t index f3c413d..472bed1 100644 --- a/t/01-basic.t +++ b/t/01-basic.t @@ -389,3 +389,45 @@ GET /a --- no_error_log [error] [warn] + + + +=== TEST 14: Empty query +--- http_config eval: $::HttpConfig +--- config + location = /a { + content_by_lua ' + local http = require "resty.http" + local httpc = http.new() + httpc:connect{ + scheme = "http", + host = "127.0.0.1", + port = ngx.var.server_port + } + + local res, err = httpc:request{ + query = {}, + path = "/b" + } + + ngx.status = res.status + + ngx.print(ngx.header.test) + + httpc:close() + '; + } + location = /b { + content_by_lua ' + ngx.header.test = ngx.var.request_uri + '; + } +--- request +GET /a +--- response_headers +/b +--- no_error_log +[error] +[warn] + + From ced0ed4e856d2de941545a9b089d76638e52a26f Mon Sep 17 00:00:00 2001 From: Xumin <100666470+StarlightIbuki@users.noreply.github.com> Date: Tue, 14 Mar 2023 18:06:47 +0800 Subject: [PATCH 2/3] Update lib/resty/http.lua --- lib/resty/http.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/resty/http.lua b/lib/resty/http.lua index 9eeeeb5..0cd5603 100644 --- a/lib/resty/http.lua +++ b/lib/resty/http.lua @@ -315,7 +315,6 @@ local function _format_request(self, params) if type(query) == "table" then query = ngx_encode_args(query) end - if query ~= "" and str_sub(query, 1, 1) ~= "?" then query = "?" .. query end From 5a45c08a1b9e452300898afc6f0e3e1e6b200835 Mon Sep 17 00:00:00 2001 From: Xumin <100666470+StarlightIbuki@users.noreply.github.com> Date: Tue, 14 Mar 2023 18:21:27 +0800 Subject: [PATCH 3/3] Update lib/resty/http.lua --- lib/resty/http.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/resty/http.lua b/lib/resty/http.lua index 0cd5603..ceccbcf 100644 --- a/lib/resty/http.lua +++ b/lib/resty/http.lua @@ -315,6 +315,7 @@ local function _format_request(self, params) if type(query) == "table" then query = ngx_encode_args(query) end + if query ~= "" and str_sub(query, 1, 1) ~= "?" then query = "?" .. query end