Skip to content

Commit

Permalink
http/util: Add workaround for broken coroutine.wrap in openresty
Browse files Browse the repository at this point in the history
Closes #98
  • Loading branch information
daurnimator committed Jun 16, 2018
1 parent 2f70b4a commit a74462e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ UNRELEASED

- Fix incorrect Sec-WebSocket-Protocol negotiation
- Fix incorrect timeout handling in `websocket:receive()`
- Add workaround to allow being required in openresty (#98)


0.2 - 2017-05-28
Expand Down
25 changes: 22 additions & 3 deletions http/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,29 @@ local maybe_quote do
end
end

-- A pcall relative that can be yielded over in PUC 5.1
-- A pcall-alike function that can be yielded over even in PUC 5.1
local yieldable_pcall
-- See if pcall can be yielded over
if coroutine.wrap(function() return pcall(coroutine.yield, true) end)() then
--[[ If pcall can already yield, then we want to use that.
However, we can't do the feature check straight away, Openresty breaks
coroutine.wrap in some contexts. See #98
Openresty nominally only supports LuaJIT, which always supports a yieldable
pcall, so we short-circuit the feature check by checking if the 'ngx' library
is loaded, plus that jit.version_num indicates LuaJIT 2.0.
This combination ensures that we don't take the wrong branch if:
- lua-http is being used to mock the openresty environment
- openresty is compiled with something other than LuaJIT
]]
if (
package.loaded.ngx
and type(package.loaded.jit) == "table"
and type(package.loaded.jit.version_num) == "number"
and package.loaded.jit.version_num >= 20000
)
-- See if pcall can be yielded over
or coroutine.wrap(function()
return pcall(coroutine.yield, true) end
)() then
yieldable_pcall = pcall
else
local function handle_resume(co, ok, ...)
Expand Down

0 comments on commit a74462e

Please # to comment.