Skip to content

Commit a2b9419

Browse files
authored
bugfix: watch body has '\n', may cause json decode err bug (#76)
1 parent c479906 commit a2b9419

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

lib/resty/etcd/utils.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
local http = require("resty.http")
33
local clear_tab = require("table.clear")
44
local split = require("ngx.re").split
5+
local find = ngx.re.find
56
local concat_tab = table.concat
67
local tostring = tostring
78
local select = select
@@ -100,5 +101,9 @@ local function verify_key(key)
100101
end
101102
_M.verify_key = verify_key
102103

104+
local function is_empty_str(input_str)
105+
return (find(input_str or '', [=[^\s*$]=], "jo"))
106+
end
107+
_M.is_empty_str = is_empty_str
103108

104109
return _M

lib/resty/etcd/v3.lua

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -513,11 +513,18 @@ local function request_chunk(self, method, host, port, path, opts, timeout)
513513
return nil, "failed to watch data, response code: " .. res.status
514514
end
515515

516+
516517
local function read_watch()
517-
local body, err = res.body_reader()
518518

519-
if not body then
520-
return nil, err
519+
while(1) do
520+
body, err = res.body_reader()
521+
if not body then
522+
return nil, err
523+
end
524+
if not utils.is_empty_str(body) then
525+
break
526+
end
527+
521528
end
522529

523530
body, err = decode_json(body)

t/normalize.t

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ __DATA__
4343
ifNotEqual(normalize('/path/../to/dir/../../../file'), '/file')
4444
ifNotEqual(normalize('path', '..', 'to', 'dir', '..', '..', '..', 'file'), '/file')
4545
46+
local is_empty_str = require("resty.etcd.utils").is_empty_str
47+
ifNotEqual(not not is_empty_str("\n"), true)
48+
ifNotEqual(not not is_empty_str("\n\n"), true)
49+
ifNotEqual(not not is_empty_str("\r\n"), true)
50+
ifNotEqual(not not is_empty_str("\t\n"), true)
51+
ifNotEqual(not not is_empty_str("\t"), true)
52+
ifNotEqual(not not is_empty_str("\t\t"), true)
53+
ifNotEqual(not not is_empty_str("\r\t\n"), true)
54+
4655
ngx.say("all done")
4756
}
4857
}

t/v2/dir.t

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ checked val as expect: b
210210
check_res(res, err, nil, nil, true)
211211
212212
local cur_time = ngx.now()
213+
ngx.update_time()
213214
local res2, err = etcd:wait("/dir", res.body.node.modifiedIndex + 1, 1)
214215
ngx.say("err: ", err, ", more than 1sec: ", ngx.now() - cur_time > 1)
215216

0 commit comments

Comments
 (0)