From be9a16613b17dff631dac47d84255bf6eb18d1c9 Mon Sep 17 00:00:00 2001 From: tom2nonames Date: Fri, 21 Oct 2022 09:38:55 +0800 Subject: [PATCH 1/4] new ljpack encode --- lib/lor/lib/request.lua | 4 ++++ lib/lor/lib/response.lua | 5 +++++ lib/lor/lib/utils/utils.lua | 16 +++++++++++++++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/lor/lib/request.lua b/lib/lor/lib/request.lua index a561cc3..782c756 100755 --- a/lib/lor/lib/request.lua +++ b/lib/lor/lib/request.lua @@ -22,6 +22,10 @@ function Request:new() body[k] = v end end + elseif sfind(header, "application/ljpack", 1, true) then + ngx.req.read_body() + local sb_str = ngx.req.get_body_data() + body = utils.ljpack_decode(sb_str) elseif sfind(header, "application/json", 1, true) then ngx.req.read_body() local json_str = ngx.req.get_body_data() diff --git a/lib/lor/lib/response.lua b/lib/lor/lib/response.lua index c69fac7..a87cbe6 100755 --- a/lib/lor/lib/response.lua +++ b/lib/lor/lib/response.lua @@ -47,6 +47,11 @@ function Response:json(data, empty_table_as_object) self:_send(utils.json_encode(data, empty_table_as_object)) end +function Response:ljpack(data) + self:set_header('Content-Type', 'application/ljpack') + self:_send(utils.ljpack_encode(data)) +end + function Response:redirect(url, code, query) if url and not code and not query then -- only one param ngx.redirect(url) diff --git a/lib/lor/lib/utils/utils.lua b/lib/lor/lib/utils/utils.lua index 52ca1d5..a807d82 100755 --- a/lib/lor/lib/utils/utils.lua +++ b/lib/lor/lib/utils/utils.lua @@ -8,6 +8,7 @@ local sgsub = string.gsub local smatch = string.match local table_insert = table.insert local json = require("cjson") +local st = require("string.buffer") local _M = {} @@ -69,7 +70,7 @@ function _M.json_encode(data, empty_table_as_object) local json_value if json.encode_empty_table_as_object then -- empty table encoded as array default - json.encode_empty_table_as_object(empty_table_as_object or false) + json.encode_empty_table_as_object(empty_table_as_object or false) end if require("ffi").os ~= "Windows" then json.encode_sparse_array(true) @@ -85,6 +86,19 @@ function _M.json_decode(str) end end +function _M.ljpack_encode(data) + local st_value + pcall(function(d) st_value = st.encode(d) end, data) + return st_value +end + +function _M.ljpack_decode(str) + local ok, data = pcall(st.decode, str) + if ok then + return data + end +end + function _M.start_with(str, substr) if str == nil or substr == nil then return false From 5f933709f02bc0cdb32f837cbf7d9709ac9f141e Mon Sep 17 00:00:00 2001 From: tom2nonames Date: Fri, 21 Oct 2022 16:20:43 +0800 Subject: [PATCH 2/4] fixed send ljpack data & decode err: left-over data in buffer. --- lib/lor/lib/response.lua | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/lor/lib/response.lua b/lib/lor/lib/response.lua index a87cbe6..dfa1213 100755 --- a/lib/lor/lib/response.lua +++ b/lib/lor/lib/response.lua @@ -49,7 +49,7 @@ end function Response:ljpack(data) self:set_header('Content-Type', 'application/ljpack') - self:_send(utils.ljpack_encode(data)) + self:__send(utils.ljpack_encode(data)) end function Response:redirect(url, code, query) @@ -116,6 +116,11 @@ function Response:_send(content) ngx.say(content) end +function REsponse:__send(content) + ngx.status = self.http_status or 200 + ngx.print(content) +end + function Response:get_body() return self.body end From d4e8fc2db7249dfe7cd12a609b85309c5ba9e82c Mon Sep 17 00:00:00 2001 From: tom2nonames Date: Fri, 21 Oct 2022 16:44:57 +0800 Subject: [PATCH 3/4] fixed response __send error. --- lib/lor/lib/response.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/lor/lib/response.lua b/lib/lor/lib/response.lua index dfa1213..ba8a615 100755 --- a/lib/lor/lib/response.lua +++ b/lib/lor/lib/response.lua @@ -116,7 +116,7 @@ function Response:_send(content) ngx.say(content) end -function REsponse:__send(content) +function Response:__send(content) ngx.status = self.http_status or 200 ngx.print(content) end From da2b340d2bf704934f5e5b864757ee71d28d49a4 Mon Sep 17 00:00:00 2001 From: tom2nonames Date: Wed, 20 Dec 2023 18:21:39 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E6=96=B0=E5=A2=9Ejson=5Fstably=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/lor/lib/response.lua | 8 ++++++++ lib/lor/lib/utils/utils.lua | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/lib/lor/lib/response.lua b/lib/lor/lib/response.lua index ba8a615..dd3f5b5 100755 --- a/lib/lor/lib/response.lua +++ b/lib/lor/lib/response.lua @@ -47,6 +47,14 @@ function Response:json(data, empty_table_as_object) self:_send(utils.json_encode(data, empty_table_as_object)) end + +function Response:json_stably(data) + self:set_header('Content-Type', 'application/json; charset=utf-8') + self:_send(utils.json_stably_encode(data)) +end + + + function Response:ljpack(data) self:set_header('Content-Type', 'application/ljpack') self:__send(utils.ljpack_encode(data)) diff --git a/lib/lor/lib/utils/utils.lua b/lib/lor/lib/utils/utils.lua index a807d82..d0d3f23 100755 --- a/lib/lor/lib/utils/utils.lua +++ b/lib/lor/lib/utils/utils.lua @@ -8,6 +8,7 @@ local sgsub = string.gsub local smatch = string.match local table_insert = table.insert local json = require("cjson") +local dkjson = require("dkjson") local st = require("string.buffer") local _M = {} @@ -79,6 +80,12 @@ function _M.json_encode(data, empty_table_as_object) return json_value end +function _M.json_stably_encode(data) + local json_value + pcall(function(d) json_value = dkjson.encode(d) end, data) + return json_value +end + function _M.json_decode(str) local ok, data = pcall(json.decode, str) if ok then