Skip to content

Commit

Permalink
Merge branch 'release/0.10'
Browse files Browse the repository at this point in the history
  • Loading branch information
pintsized committed Nov 22, 2016
2 parents 6f80973 + f3b4bc6 commit f28b904
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 22 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ The `params` table accepts the following fields:
* `version` The HTTP version number, currently supporting 1.0 or 1.1.
* `method` The HTTP method string.
* `path` The path string.
* `query` The query string.
* `headers` A table of request headers.
* `body` The request body as a string, or an iterator function (see [get_client_body_reader](#get_client_body_reader)).
* `ssl_verify` Verify SSL cert matches hostname
Expand Down Expand Up @@ -357,9 +358,11 @@ Sets the current response based on the given `res`. Ensures that hop-by-hop head

## parse_uri

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

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

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.


## get_client_body_reader
Expand Down
37 changes: 28 additions & 9 deletions lib/resty/http.lua
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ end


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

Expand Down Expand Up @@ -115,6 +115,16 @@ function _M.set_timeout(self, timeout)
end


function _M.set_timeouts(self, connect_timeout, send_timeout, read_timeout)
local sock = self.sock
if not sock then
return nil, "not initialized"
end

return sock:settimeouts(connect_timeout, send_timeout, read_timeout)
end


function _M.ssl_handshake(self, ...)
local sock = self.sock
if not sock then
Expand Down Expand Up @@ -197,8 +207,10 @@ local function _should_receive_body(method, code)
end


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

local m, err = ngx_re_match(uri, [[^(?:(http[s]?):)?//([^:/\?]+)(?::(\d+))?([^\?]*)\??(.*)]], "jo")

if not m then
if err then
Expand Down Expand Up @@ -228,6 +240,12 @@ function _M.parse_uri(self, uri)
end
end
if not m[4] or "" == m[4] then m[4] = "/" end

if query_in_path and m[5] and m[5] ~= "" then
m[4] = m[4] .. "?" .. m[5]
m[5] = nil
end

return m, nil
end
end
Expand All @@ -238,10 +256,10 @@ local function _format_request(params)
local headers = params.headers or {}

local query = params.query or ""
if query then
if type(query) == "table" then
query = "?" .. ngx_encode_args(query)
end
if type(query) == "table" then
query = "?" .. ngx_encode_args(query)
elseif query ~= "" and str_sub(query, 1, 1) ~= "?" then
query = "?" .. query
end

-- Initialize request
Expand Down Expand Up @@ -749,13 +767,14 @@ end
function _M.request_uri(self, uri, params)
if not params then params = {} end

local parsed_uri, err = self:parse_uri(uri)
local parsed_uri, err = self:parse_uri(uri, false)
if not parsed_uri then
return nil, err
end

local scheme, host, port, path = unpack(parsed_uri)
local scheme, host, port, path, query = unpack(parsed_uri)
if not params.path then params.path = path end
if not params.query then params.query = query end

local c, err = self:connect(host, port)
if not c then
Expand Down
2 changes: 1 addition & 1 deletion lib/resty/http_headers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ local str_find, str_lower, str_sub =


local _M = {
_VERSION = '0.01',
_VERSION = '0.10',
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package = "lua-resty-http"
version = "0.09-0"
version = "0.10-0"
source = {
url = "git://github.com/pintsized/lua-resty-http",
tag = "v0.09"
tag = "v0.10"
}
description = {
summary = "Lua HTTP client cosocket driver for OpenResty / ngx_lua.",
Expand Down
67 changes: 60 additions & 7 deletions t/01-basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -297,24 +297,77 @@ bad uri: http:///example.com
[warn]


=== TEST 10: Parse URI will use current request schema if omitted (and available)
=== TEST 10: Parse URI fills in defaults correctly
--- http_config eval: $::HttpConfig
--- config
location = /a {
content_by_lua '
local http = require("resty.http").new()
local parts, err = http:parse_uri("//example.com")
if not parts then
ngx.say(err)
else
ngx.say(parts[1])
function test_uri(uri)
local scheme, host, port, path, query = unpack(http:parse_uri(uri, false))
ngx.say("scheme: ", scheme, ", host: ", host, ", port: ", port, ", path: ", path, ", query: ", query)
end
test_uri("http://example.com")
test_uri("http://example.com/")
test_uri("https://example.com/foo/bar")
test_uri("https://example.com/foo/bar?a=1&b=2")
test_uri("http://example.com?a=1&b=2")
test_uri("//example.com")
test_uri("//example.com?a=1&b=2")
test_uri("//example.com/foo/bar?a=1&b=2")
';
}
--- request
GET /a
--- response_body
scheme: http, host: example.com, port: 80, path: /, query:
scheme: http, host: example.com, port: 80, path: /, query:
scheme: https, host: example.com, port: 443, path: /foo/bar, query:
scheme: https, host: example.com, port: 443, path: /foo/bar, query: a=1&b=2
scheme: http, host: example.com, port: 80, path: /, query: a=1&b=2
scheme: http, host: example.com, port: 80, path: /, query:
scheme: http, host: example.com, port: 80, path: /, query: a=1&b=2
scheme: http, host: example.com, port: 80, path: /foo/bar, query: a=1&b=2
--- no_error_log
[error]
[warn]


=== TEST 11: Parse URI fills in defaults correctly, using backwards compatible mode
--- http_config eval: $::HttpConfig
--- config
location = /a {
content_by_lua '
local http = require("resty.http").new()
function test_uri(uri)
local scheme, host, port, path, query = unpack(http:parse_uri(uri))
ngx.say("scheme: ", scheme, ", host: ", host, ", port: ", port, ", path: ", path)
end
test_uri("http://example.com")
test_uri("http://example.com/")
test_uri("https://example.com/foo/bar")
test_uri("https://example.com/foo/bar?a=1&b=2")
test_uri("http://example.com?a=1&b=2")
test_uri("//example.com")
test_uri("//example.com?a=1&b=2")
test_uri("//example.com/foo/bar?a=1&b=2")
';
}
--- request
GET /a
--- response_body
http
scheme: http, host: example.com, port: 80, path: /
scheme: http, host: example.com, port: 80, path: /
scheme: https, host: example.com, port: 443, path: /foo/bar
scheme: https, host: example.com, port: 443, path: /foo/bar?a=1&b=2
scheme: http, host: example.com, port: 80, path: /?a=1&b=2
scheme: http, host: example.com, port: 80, path: /
scheme: http, host: example.com, port: 80, path: /?a=1&b=2
scheme: http, host: example.com, port: 80, path: /foo/bar?a=1&b=2
--- no_error_log
[error]
[warn]
85 changes: 84 additions & 1 deletion t/06-simpleinterface.t
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use Test::Nginx::Socket;
use Cwd qw(cwd);

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

my $pwd = cwd();

Expand Down Expand Up @@ -149,3 +149,86 @@ OK
--- no_error_log
[error]
[warn]


=== TEST 4 Simple URI interface, params override, query as string
--- http_config eval: $::HttpConfig
--- config
location = /a {
content_by_lua '
local http = require "resty.http"
local httpc = http.new()
local res, err = httpc:request_uri(
"http://127.0.0.1:"..ngx.var.server_port.."/b?a=1&b=2", {
path = "/c",
query = "a=2&b=3",
}
)
ngx.status = res.status
ngx.header["X-Header-A"] = res.headers["X-Header-A"]
ngx.header["X-Header-B"] = res.headers["X-Header-B"]
ngx.print(res.body)
';
}
location = /c {
content_by_lua '
for k,v in pairs(ngx.req.get_uri_args()) do
ngx.header["X-Header-" .. string.upper(k)] = v
end
ngx.say("OK")
';
}
--- request
GET /a
--- response_headers
X-Header-A: 2
X-Header-B: 3
--- response_body
OK
--- no_error_log
[error]
[warn]


=== TEST 5 Simple URI interface, params override, query as string, as leading ?
--- http_config eval: $::HttpConfig
--- config
location = /a {
content_by_lua '
local http = require "resty.http"
local httpc = http.new()
local res, err = httpc:request_uri(
"http://127.0.0.1:"..ngx.var.server_port.."/b?a=1&b=2", {
query = "?a=2&b=3",
}
)
ngx.status = res.status
ngx.header["X-Header-A"] = res.headers["X-Header-A"]
ngx.header["X-Header-B"] = res.headers["X-Header-B"]
ngx.print(res.body)
';
}
location = /b {
content_by_lua '
for k,v in pairs(ngx.req.get_uri_args()) do
ngx.header["X-Header-" .. string.upper(k)] = v
end
ngx.say("OK")
';
}
--- request
GET /a
--- response_headers
X-Header-A: 2
X-Header-B: 3
--- response_body
OK
--- no_error_log
[error]
[warn]

0 comments on commit f28b904

Please sign in to comment.