Skip to content

Commit f28b904

Browse files
committed
Merge branch 'release/0.10'
2 parents 6f80973 + f3b4bc6 commit f28b904

File tree

6 files changed

+180
-22
lines changed

6 files changed

+180
-22
lines changed

README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ The `params` table accepts the following fields:
215215
* `version` The HTTP version number, currently supporting 1.0 or 1.1.
216216
* `method` The HTTP method string.
217217
* `path` The path string.
218+
* `query` The query string.
218219
* `headers` A table of request headers.
219220
* `body` The request body as a string, or an iterator function (see [get_client_body_reader](#get_client_body_reader)).
220221
* `ssl_verify` Verify SSL cert matches hostname
@@ -357,9 +358,11 @@ Sets the current response based on the given `res`. Ensures that hop-by-hop head
357358

358359
## parse_uri
359360

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

362-
This is a convenience function allowing one to more easily use the generic interface, when the input data is a URI.
363+
This is a convenience function allowing one to more easily use the generic interface, when the input data is a URI.
364+
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.
363366

364367

365368
## get_client_body_reader

lib/resty/http.lua

+28-9
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ end
7777

7878

7979
local _M = {
80-
_VERSION = '0.09',
80+
_VERSION = '0.10',
8181
}
8282
_M._USER_AGENT = "lua-resty-http/" .. _M._VERSION .. " (Lua) ngx_lua/" .. ngx.config.ngx_lua_version
8383

@@ -115,6 +115,16 @@ function _M.set_timeout(self, timeout)
115115
end
116116

117117

118+
function _M.set_timeouts(self, connect_timeout, send_timeout, read_timeout)
119+
local sock = self.sock
120+
if not sock then
121+
return nil, "not initialized"
122+
end
123+
124+
return sock:settimeouts(connect_timeout, send_timeout, read_timeout)
125+
end
126+
127+
118128
function _M.ssl_handshake(self, ...)
119129
local sock = self.sock
120130
if not sock then
@@ -197,8 +207,10 @@ local function _should_receive_body(method, code)
197207
end
198208

199209

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

203215
if not m then
204216
if err then
@@ -228,6 +240,12 @@ function _M.parse_uri(self, uri)
228240
end
229241
end
230242
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+
231249
return m, nil
232250
end
233251
end
@@ -238,10 +256,10 @@ local function _format_request(params)
238256
local headers = params.headers or {}
239257

240258
local query = params.query or ""
241-
if query then
242-
if type(query) == "table" then
243-
query = "?" .. ngx_encode_args(query)
244-
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
245263
end
246264

247265
-- Initialize request
@@ -749,13 +767,14 @@ end
749767
function _M.request_uri(self, uri, params)
750768
if not params then params = {} end
751769

752-
local parsed_uri, err = self:parse_uri(uri)
770+
local parsed_uri, err = self:parse_uri(uri, false)
753771
if not parsed_uri then
754772
return nil, err
755773
end
756774

757-
local scheme, host, port, path = unpack(parsed_uri)
775+
local scheme, host, port, path, query = unpack(parsed_uri)
758776
if not params.path then params.path = path end
777+
if not params.query then params.query = query end
759778

760779
local c, err = self:connect(host, port)
761780
if not c then

lib/resty/http_headers.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ local str_find, str_lower, str_sub =
66

77

88
local _M = {
9-
_VERSION = '0.01',
9+
_VERSION = '0.10',
1010
}
1111

1212

lua-resty-http-0.09-0.rockspec renamed to lua-resty-http-0.10-0.rockspec

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package = "lua-resty-http"
2-
version = "0.09-0"
2+
version = "0.10-0"
33
source = {
44
url = "git://github.com/pintsized/lua-resty-http",
5-
tag = "v0.09"
5+
tag = "v0.10"
66
}
77
description = {
88
summary = "Lua HTTP client cosocket driver for OpenResty / ngx_lua.",

t/01-basic.t

+60-7
Original file line numberDiff line numberDiff line change
@@ -297,24 +297,77 @@ bad uri: http:///example.com
297297
[warn]
298298

299299

300-
=== TEST 10: Parse URI will use current request schema if omitted (and available)
300+
=== TEST 10: Parse URI fills in defaults correctly
301301
--- http_config eval: $::HttpConfig
302302
--- config
303303
location = /a {
304304
content_by_lua '
305305
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])
306+
307+
function test_uri(uri)
308+
local scheme, host, port, path, query = unpack(http:parse_uri(uri, false))
309+
ngx.say("scheme: ", scheme, ", host: ", host, ", port: ", port, ", path: ", path, ", query: ", query)
310+
end
311+
312+
test_uri("http://example.com")
313+
test_uri("http://example.com/")
314+
test_uri("https://example.com/foo/bar")
315+
test_uri("https://example.com/foo/bar?a=1&b=2")
316+
test_uri("http://example.com?a=1&b=2")
317+
test_uri("//example.com")
318+
test_uri("//example.com?a=1&b=2")
319+
test_uri("//example.com/foo/bar?a=1&b=2")
320+
';
321+
}
322+
--- request
323+
GET /a
324+
--- response_body
325+
scheme: http, host: example.com, port: 80, path: /, query:
326+
scheme: http, host: example.com, port: 80, path: /, query:
327+
scheme: https, host: example.com, port: 443, path: /foo/bar, query:
328+
scheme: https, host: example.com, port: 443, path: /foo/bar, query: a=1&b=2
329+
scheme: http, host: example.com, port: 80, path: /, query: a=1&b=2
330+
scheme: http, host: example.com, port: 80, path: /, query:
331+
scheme: http, host: example.com, port: 80, path: /, query: a=1&b=2
332+
scheme: http, host: example.com, port: 80, path: /foo/bar, query: a=1&b=2
333+
--- no_error_log
334+
[error]
335+
[warn]
336+
337+
338+
=== TEST 11: Parse URI fills in defaults correctly, using backwards compatible mode
339+
--- http_config eval: $::HttpConfig
340+
--- config
341+
location = /a {
342+
content_by_lua '
343+
local http = require("resty.http").new()
344+
345+
function test_uri(uri)
346+
local scheme, host, port, path, query = unpack(http:parse_uri(uri))
347+
ngx.say("scheme: ", scheme, ", host: ", host, ", port: ", port, ", path: ", path)
311348
end
349+
350+
test_uri("http://example.com")
351+
test_uri("http://example.com/")
352+
test_uri("https://example.com/foo/bar")
353+
test_uri("https://example.com/foo/bar?a=1&b=2")
354+
test_uri("http://example.com?a=1&b=2")
355+
test_uri("//example.com")
356+
test_uri("//example.com?a=1&b=2")
357+
test_uri("//example.com/foo/bar?a=1&b=2")
312358
';
313359
}
314360
--- request
315361
GET /a
316362
--- response_body
317-
http
363+
scheme: http, host: example.com, port: 80, path: /
364+
scheme: http, host: example.com, port: 80, path: /
365+
scheme: https, host: example.com, port: 443, path: /foo/bar
366+
scheme: https, host: example.com, port: 443, path: /foo/bar?a=1&b=2
367+
scheme: http, host: example.com, port: 80, path: /?a=1&b=2
368+
scheme: http, host: example.com, port: 80, path: /
369+
scheme: http, host: example.com, port: 80, path: /?a=1&b=2
370+
scheme: http, host: example.com, port: 80, path: /foo/bar?a=1&b=2
318371
--- no_error_log
319372
[error]
320373
[warn]

t/06-simpleinterface.t

+84-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use Test::Nginx::Socket;
22
use Cwd qw(cwd);
33

4-
plan tests => repeat_each() * (blocks() * 4) + 6;
4+
plan tests => repeat_each() * (blocks() * 6);
55

66
my $pwd = cwd();
77

@@ -149,3 +149,86 @@ OK
149149
--- no_error_log
150150
[error]
151151
[warn]
152+
153+
154+
=== TEST 4 Simple URI interface, params override, query as string
155+
--- http_config eval: $::HttpConfig
156+
--- config
157+
location = /a {
158+
content_by_lua '
159+
local http = require "resty.http"
160+
local httpc = http.new()
161+
local res, err = httpc:request_uri(
162+
"http://127.0.0.1:"..ngx.var.server_port.."/b?a=1&b=2", {
163+
path = "/c",
164+
query = "a=2&b=3",
165+
}
166+
)
167+
168+
ngx.status = res.status
169+
170+
ngx.header["X-Header-A"] = res.headers["X-Header-A"]
171+
ngx.header["X-Header-B"] = res.headers["X-Header-B"]
172+
173+
ngx.print(res.body)
174+
';
175+
}
176+
location = /c {
177+
content_by_lua '
178+
for k,v in pairs(ngx.req.get_uri_args()) do
179+
ngx.header["X-Header-" .. string.upper(k)] = v
180+
end
181+
ngx.say("OK")
182+
';
183+
}
184+
--- request
185+
GET /a
186+
--- response_headers
187+
X-Header-A: 2
188+
X-Header-B: 3
189+
--- response_body
190+
OK
191+
--- no_error_log
192+
[error]
193+
[warn]
194+
195+
196+
=== TEST 5 Simple URI interface, params override, query as string, as leading ?
197+
--- http_config eval: $::HttpConfig
198+
--- config
199+
location = /a {
200+
content_by_lua '
201+
local http = require "resty.http"
202+
local httpc = http.new()
203+
local res, err = httpc:request_uri(
204+
"http://127.0.0.1:"..ngx.var.server_port.."/b?a=1&b=2", {
205+
query = "?a=2&b=3",
206+
}
207+
)
208+
209+
ngx.status = res.status
210+
211+
ngx.header["X-Header-A"] = res.headers["X-Header-A"]
212+
ngx.header["X-Header-B"] = res.headers["X-Header-B"]
213+
214+
ngx.print(res.body)
215+
';
216+
}
217+
location = /b {
218+
content_by_lua '
219+
for k,v in pairs(ngx.req.get_uri_args()) do
220+
ngx.header["X-Header-" .. string.upper(k)] = v
221+
end
222+
ngx.say("OK")
223+
';
224+
}
225+
--- request
226+
GET /a
227+
--- response_headers
228+
X-Header-A: 2
229+
X-Header-B: 3
230+
--- response_body
231+
OK
232+
--- no_error_log
233+
[error]
234+
[warn]

0 commit comments

Comments
 (0)