From dede4d8f53660a0fc41f3143b5d0727f45cc7e57 Mon Sep 17 00:00:00 2001 From: Kaarle Ritvanen Date: Fri, 26 Apr 2013 13:26:44 +0300 Subject: use JSON encoding also for primitive values in response --- server.lua | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) (limited to 'server.lua') diff --git a/server.lua b/server.lua index 8ad6d88..9fe4f5e 100644 --- a/server.lua +++ b/server.lua @@ -22,18 +22,17 @@ return function(env) local method = env.REQUEST_METHOD local path = env.REQUEST_URI - local function wrap(code, headers, res) + local function wrap(code, headers, res, encode) if not headers then headers = {} end - local ctype - if type(res) == 'table' then - ctype = 'application/json' - res = json.encode(res) - else - ctype = 'text/plain' - if not res then res = '' end - end - headers['Content-Type'] = ctype + if res then + local ctype + if encode then + ctype = 'application/json' + res = json.encode(res) + else ctype = 'text/plain' end + headers['Content-Type'] = ctype + else res = '' end return code, headers, coroutine.wrap( function() coroutine.yield(res) end @@ -225,9 +224,9 @@ return function(env) end ) - if success then return wrap(code, hdr, res) end + if success then return wrap(code, hdr, res, true) end - if code.conflict then return wrap(409, nil, code.conflict) end + if code.conflict then return wrap(409, nil, code.conflict, true) end - return wrap(422, nil, code) + return wrap(422, nil, code, true) end -- cgit v1.2.3