Skip to content

Commit 30a7266

Browse files
committed
bugfix: ensured resty.core can be loaded in binaries without PCRE support.
1 parent f45c32e commit 30a7266

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

lib/resty/core/regex.lua

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,35 @@ local ngx_log = ngx.log
3535
local ngx_NOTICE = ngx.NOTICE
3636

3737

38+
local _M = {
39+
version = base.version
40+
}
41+
42+
3843
if not ngx.re then
3944
ngx.re = {}
4045
end
4146

4247

48+
ffi.cdef[[
49+
const char *pcre_version(void);
50+
]]
51+
52+
53+
local pcre_ver
54+
55+
if not pcall(function() pcre_ver = ffi_string(C.pcre_version()) end) then
56+
setmetatable(ngx.re, {
57+
__index = function(_, key)
58+
error("no support for 'ngx.re." .. key .. "': OpenResty was " ..
59+
"compiled without PCRE support", 2)
60+
end
61+
})
62+
63+
return _M
64+
end
65+
66+
4367
local MAX_ERR_MSG_LEN = 128
4468

4569

@@ -95,12 +119,6 @@ local ngx_lua_ffi_script_eval_data
95119
local no_jit_in_init
96120

97121
if jit.os == "OSX" then
98-
ffi.cdef[[
99-
const char *pcre_version(void);
100-
]]
101-
102-
local pcre_ver = ffi_string(C.pcre_version())
103-
104122
local maj, min = string.match(pcre_ver, "^(%d+)%.(%d+)")
105123
if maj and min then
106124
local pcre_ver_num = tonumber(maj .. min)
@@ -278,11 +296,6 @@ local c_str_type = ffi.typeof("const char *")
278296

279297
local cached_re_opts = new_tab(0, 4)
280298

281-
local _M = {
282-
version = base.version
283-
}
284-
285-
286299
local buf_grow_ratio = 2
287300

288301

lib/resty/core/request.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ local ffi_str = ffi.string
1616
local get_string_buf = base.get_string_buf
1717
local get_size_ptr = base.get_size_ptr
1818
local setmetatable = setmetatable
19-
local gsub = ngx.re.gsub
19+
local re = ngx.re
2020
local lower = string.lower
2121
local rawget = rawget
2222
local ngx = ngx
@@ -68,7 +68,7 @@ local truncated = ffi.new("int[1]")
6868

6969
local req_headers_mt = {
7070
__index = function (tb, key)
71-
return rawget(tb, (gsub(lower(key), '_', '-', "jo")))
71+
return rawget(tb, (re.gsub(lower(key), '_', '-', "jo")))
7272
end
7373
}
7474

0 commit comments

Comments
 (0)