Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[http] fix creating error responses #350

Merged
merged 1 commit into from
Apr 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Remove unnecessary code and comments [PR #344](https://github.com/3scale/apicast/pull/344)
- JWT expiry not taken into account in authorization response cache [PR #283](https://github.com/3scale/apicast/pull/283) / [Issue #309](https://github.com/3scale/apicast/issues/309) / Fixed by [PR #341](https://github.com/3scale/apicast/pull/341)
- Memory leak in round robin balancer [PR #345](https://github.com/3scale/apicast/pull/345)
- Error when trying to determine status of failed request when downloading configuration [PR #350](https://github.com/3scale/apicast/pull/350)

## [3.0.0-beta3] - 2017-03-20

Expand Down
32 changes: 23 additions & 9 deletions apicast/src/resty/http_ng/backend/async_resty.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ local rawset = rawset
local pairs = pairs
local unpack = unpack
local assert = assert
local print = print
local type = type
local rawlen = rawlen
local next = next
Expand All @@ -18,18 +17,33 @@ local http = require 'resty.resolver.http'
_M.async = function(request)
local httpc = http.new()

local parsed_uri = assert(httpc:parse_uri(request.url))
local parsed_uri = assert(httpc:parse_uri(assert(request.url, 'missing url')))

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

if #request.path == 0 then request.path = '/' end

local timeout = request.timeout or (request.options and request.options.timeout)

if type(timeout) == 'number' then
httpc:set_timeout(timeout)
elseif type(timeout) == 'table' then
local connect_timeout = timeout.connect
local send_timeout = timeout.send
local read_timeout = timeout.read

if httpc.set_timeouts then -- lua-resty-http >= 0.10
httpc:set_timeouts(connect_timeout, send_timeout, read_timeout)
else
httpc.sock:settimeouts(connect_timeout, send_timeout, read_timeout)
end
end

local ok, err = httpc:connect(host, port)

if not ok then
print('error connecting ', host, ':', port, ' : ', err)
return response.error(err, request)
return response.error(request, err)
end

if scheme == 'https' then
Expand All @@ -40,7 +54,7 @@ _M.async = function(request)
session, err = httpc:ssl_handshake(false, host, verify)

if not session then
return response.error(err, request)
return response.error(request, err)
end
end

Expand All @@ -50,11 +64,11 @@ _M.async = function(request)
if res then
return response.new(request, res.status, res.headers, function() return (res:read_body()) end)
else
return response.error(err, request)
return response.error(request, err)
end
end

local function future(thread)
local function future(thread, request)
local ok, res

local function load(table)
Expand All @@ -63,7 +77,7 @@ local function future(thread)

rawset(table, 'ok', ok)

if not ok then res = response.error(res) end
if not ok then res = response.error(request, res or 'failed to create async request') end

for k,v in pairs(res) do
if k == 'headers' then
Expand Down Expand Up @@ -94,7 +108,7 @@ end

_M.send = function(request)
local thread = spawn(_M.async, request)
return future(thread)
return future(thread, request)
end

return _M
2 changes: 1 addition & 1 deletion apicast/src/resty/http_ng/backend/resty.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ backend.send = function(request)
if res then
return response.new(request, res.status, res.headers, res.body)
else
return response.error(err)
return response.error(request, err)
end
end

Expand Down
4 changes: 3 additions & 1 deletion apicast/src/resty/http_ng/response.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ function response.new(request, status, headers, body)
return setmetatable(res, mt)
end

function response.error(message, request)
function response.error(request, message)
assert(request, 'missing request')
assert(message, 'missing message')
return { ok = false, error = message, request = request, status = 0, headers = {} }
end

Expand Down
44 changes: 44 additions & 0 deletions spec/resty/http_ng/backend/async_resty_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,50 @@ describe('resty backend', function()
assert.equal('string', type(response.body))
assert.truthy(response.request)
end)

it('returns proper error on connect timeout', function()
local req = { method = method, url = 'http://example.com:81/', timeout = { connect = 1 } }

local response = backend.send(req)

assert.truthy(response)
assert.equal('timeout', response.error)
assert.equal(req, response.request)
assert.falsy(response.ok)
end)

it('returns proper error on read timeout', function()
local req = { method = method, url = 'http://example.com/', timeout = { read = 1 } }

local response = backend.send(req)

assert.truthy(response)
assert.equal('timeout', response.error)
assert.equal(req, response.request)
assert.falsy(response.ok)
end)

it('returns proper error on invalid ssl', function()
local req = { method = method, url = 'https://untrusted-root.badssl.com/', options = { ssl = { verify = true } } }

local response = backend.send(req)

assert.truthy(response)
assert.match('unable to get local issuer certificate', response.error)
assert.equal(req, response.request)
assert.falsy(response.ok)
end)

it('returns proper error on invalid request', function()
local req = { method = method }

local response = backend.send(req)

assert.truthy(response)
assert.match('failed to create async request', response.error)
assert.equal(req, response.request)
assert.falsy(response.ok)
end)
end)

describe('when there is no error', function()
Expand Down
10 changes: 10 additions & 0 deletions spec/resty/http_ng/backend/resty_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,15 @@ describe('resty backend', function()
assert.truthy(response.request)
assert(response.headers.location:match('^https://'))
end)

it('returns error', function()
local req = { method = method, url = 'http://0.0.0.0:0/' }
local response, err = backend.send(req)

assert.falsy(err)
assert.truthy(response.error)
assert.falsy(response.ok)
assert.same(req, response.request)
end)
end)
end)
23 changes: 23 additions & 0 deletions spec/resty/http_ng/response_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
local _M = require 'resty.http_ng.response'
local request = require 'resty.http_ng.request'

describe('http_ng response', function()

describe('error', function()

local req = request.new{ method = 'GET', url = 'http://example.com' }

it('has the request', function()
local error = _M.error(req, 'failed')

assert.equal(req, error.request)
end)


it('has the message', function()
local error = _M.error(req, 'error message')

assert.equal('error message', error.error)
end)
end)
end)