diff --git a/README.md b/README.md index 9890719..4fe9827 100644 --- a/README.md +++ b/README.md @@ -1,52 +1,22 @@ -# json4lua -JSON and JSONRPC for Lua +# jsonrpc4lua +JSON RPC over HTTP Lua module. Fork of [json4lua](https://github.com/craigmj/json4lua), refactored to use [CJSON](http://www.kyne.com.au/~mark/software/lua-cjson.php). -# Installation # -``` -luarocks install --server=http://rocks.moonscript.org/manifests/amrhassan --local json4Lua -``` - -# JSON Usage # +## Dependencies +* lua >= 5.1 +* luasocket +* cgilua (patch required) +* lua-cjson -## Encoding ## +Optional requirement to run the server-side example: +* xavante -```lua -json = require('json') -print(json.encode({ 1, 2, 'fred', {first='mars',second='venus',third='earth'} })) +## Installation ``` -```json -[1,2,"fred", {"first":"mars","second":"venus","third","earth"}] +luarocks --local install jsonrpc4lua ``` -## Decoding ## - -```lua -json = require("json") -testString = [[ { "one":1 , "two":2, "primes":[2,3,5,7] } ]] -decoded = json.decode(testString) -table.foreach(decoded, print) -print ("Primes are:") -table.foreach(o.primes,print) -``` -``` -one 1 -two 2 -primes table: 0032B928 -Primes are: -1 2 -2 3 -3 5 -4 7 -``` +## Required CGILua Fix +See https://github.com/pdxmeshnet/cgilua/commit/1b35d812c7d637b91f2ac0a8d91f9698ba84d8d9 -# JSONRPC Usage # -```lua -json = require('json') -require("json.rpc") -server = json.rpc.proxy("http://jsolait.net/testj.py") -result, error = server.echo('Test echo!') -print(result) -``` -``` -Test echo! -``` +## Usage +See examples directory. diff --git a/doc/VERSION.txt b/doc/VERSION.txt index deae881..a5f7143 100755 --- a/doc/VERSION.txt +++ b/doc/VERSION.txt @@ -1,4 +1,4 @@ -JSON4Lua and JSONRPC4Lua +JSONRPC4Lua Version 1.0.0 -4 March 2015 -http://github.com/craigmj/json4lua/ \ No newline at end of file +31 July 2015 +http://github.com/pdxmeshnet/jsonrpc4lua/ \ No newline at end of file diff --git a/doc/cgilua_patch.html b/doc/cgilua_patch.html index 1b4d6b3..3a80ff4 100755 --- a/doc/cgilua_patch.html +++ b/doc/cgilua_patch.html @@ -160,19 +160,24 @@
text/plain
text/plain
.
+JSON RPC (both the JSONRPC4Lua implementation and the jsolait Javascript implementation) send the http request with a Content-Type of application/json-rpc
.
-CGILua 5.0 does not accept text/plain
content, and will generate an error of 'Unsupported Media Type: text/plain'.
+CGILua does not accept application/json-rpc
content, and will generate 'Unsupported Media Type' error.
-This is easily patched in CGILua 5.0 by making the following change to cgilua/post.lua
, line 286:
+This is easily patched in CGILua by making the following change to cgilua/post.lua
, line 289:
Change:- elseif strfind (contenttype, "text/xml") then + elseif strfind (contenttype, "application/xml", 1, true) or strfind (contenttype, "text/xml", 1, true) or strfind (contenttype, "text/plain", 1, true) then + tinsert (defs.args, read (inputsize)) + else + error("Unsupported Media Type: "..contenttype)to
- elseif strfind (contenttype, "text/xml") or strfind (contenttype, "text/plain") then + else + local input = read(inputsize) + tinsert (defs.args, input)-This makes CGILua handle
text/plain
as it does text/xml
, without parsing the incoming POST data.
+This makes CGILua handle all unsupported content types as text/plain
, without parsing the incoming POST data.
Please note: I have requested the maintainers of CGILua to make this change to CGILua, whereafter this patch will no longer be required.
diff --git a/examples/example.lua b/examples/example.lua
deleted file mode 100755
index 36497da..0000000
--- a/examples/example.lua
+++ /dev/null
@@ -1,23 +0,0 @@
---[[
-JSON4Lua example script.
-Demonstrates the simple functionality of the json module.
-]]--
-json = require('json')
-
-
--- Object to JSON encode
-test = {
- one='first',two='second',three={2,3,5}
-}
-
-jsonTest = json.encode(test)
-
-print('JSON encoded test is: ' .. jsonTest)
-
--- Now JSON decode the json string
-result = json.decode(jsonTest)
-
-print ("The decoded table result:")
-table.foreach(result,print)
-print ("The decoded table result.three")
-table.foreach(result.three, print)
diff --git a/examples/rpcclient.lua b/examples/rpcclient.lua
new file mode 100644
index 0000000..64e6089
--- /dev/null
+++ b/examples/rpcclient.lua
@@ -0,0 +1,12 @@
+
+-- this example will work only if cgilua module has this fix https://github.com/pdxmeshnet/cgilua/commit/1b35d812c7d637b91f2ac0a8d91f9698ba84d8d9
+
+local rpc = require("json.rpc")
+
+server = rpc.proxy("http://localhost:8080/jsonrpc")
+result, error = server.average(10,15,23)
+if error then
+ print(error)
+else
+ table.foreach(result, print)
+end
diff --git a/examples/rpcserver/rpcserver.lua b/examples/rpcserver/rpcserver.lua
new file mode 100644
index 0000000..2e4d9b9
--- /dev/null
+++ b/examples/rpcserver/rpcserver.lua
@@ -0,0 +1,52 @@
+
+local xavante = require "xavante"
+local filehandler = require "xavante.filehandler"
+local cgiluahandler = require "xavante.cgiluahandler"
+local redirecthandler = require "xavante.redirecthandler"
+
+
+-- Define here where Xavante HTTP documents scripts are located
+local webDir = "./www"
+
+local rules = {
+
+ -- redirect
+ {
+ match = "^[^%./]*/$",
+ with = redirecthandler,
+ params = {"index.lua"}
+ },
+ {
+ match = "^[^%./]*/jsonrpc/?$",
+ with = redirecthandler,
+ params = {"jsonrpc.lua"}
+ },
+
+ -- cgi
+ {
+ match = {"%.lp$", "%.lp/.*$", "%.lua$", "%.lua/.*$" },
+ with = cgiluahandler.makeHandler (webDir)
+ },
+
+ -- static content
+ {
+ match = ".",
+ with = filehandler,
+ params = {baseDir = webDir}
+ },
+}
+
+xavante.HTTP{
+ server = {host = "*", port = 8080},
+
+ defaultHost = {
+ rules = rules
+ },
+}
+
+print "\nStarting server...\n";
+
+xavante.start();
+
+print "exiting...\n"
+
diff --git a/examples/rpcserver/www/index.lua b/examples/rpcserver/www/index.lua
new file mode 100644
index 0000000..489dcc7
--- /dev/null
+++ b/examples/rpcserver/www/index.lua
@@ -0,0 +1 @@
+print "index"
diff --git a/examples/jsonrpc.lua b/examples/rpcserver/www/jsonrpc.lua
similarity index 82%
rename from examples/jsonrpc.lua
rename to examples/rpcserver/www/jsonrpc.lua
index f265b8a..d3b84a7 100755
--- a/examples/jsonrpc.lua
+++ b/examples/rpcserver/www/jsonrpc.lua
@@ -2,7 +2,8 @@
-- jsonrpc.lua
-- Installed in a CGILua webserver environment (with necessary CGI Lua 5.0 patch)
--
-require ('json.rpcserver')
+
+local rpcserver = require('json.rpcserver')
-- The Lua class that is to serve JSON RPC requests
local myServer = {
@@ -18,4 +19,4 @@ local myServer = {
end
}
-json.rpcserver.serve(myServer)
\ No newline at end of file
+rpcserver.serve(myServer)
diff --git a/examples/tests.lua b/examples/tests.lua
deleted file mode 100755
index 999bec3..0000000
--- a/examples/tests.lua
+++ /dev/null
@@ -1,205 +0,0 @@
---[[
-Some basic tests for JSON4Lua.
-]]--
-
---- Compares two tables for being data-identical.
-function compareData(a,b)
- if (type(a)=='string' or type(a)=='number' or type(a)=='boolean' or type(a)=='nil') then return a==b end
- -- After basic data types, we're only interested in tables
- if (type(a)~='table') then return true end
- -- Check that a has everything b has
- for k,v in pairs(b) do
- if (not compareData( a[k], v ) ) then return false end
- end
- for k,v in pairs(a) do
- if (not compareData( v, b[k] ) ) then return false end
- end
- return true
-end
-
----
--- Checks that our compareData function works properly
-function testCompareData()
- s = "name"
- r = "name"
- assert(compareData(s,r))
- assert(not compareData('fred',s))
- assert(not compareData(nil, s))
- assert(not compareData("123",123))
- assert(not compareData(false, nil))
- assert(compareData(true, true))
- assert(compareData({1,2,3},{1,2,3}))
- assert(compareData({'one',2,'three'},{'one',2,'three'}))
- assert(not compareData({'one',2,4},{4,2,'one'}))
- assert(compareData({one='ichi',two='nichi',three='san'}, {three='san',two='nichi',one='ichi'}))
- s = { one={1,2,3}, two={one='hitotsu',two='futatsu',three='mitsu'} }
- assert(compareData(s,s))
- t = { one={1,2,3}, two={one='een',two='twee',three='drie'} }
- assert(not compareData(s,t))
-end
-
-testCompareData()
-
---
---
--- Performs some perfunctory tests on JSON module
-function testJSON4Lua()
- json = require('json')
-
- if nil then
- -- Test encodeString
- s = [["\"
-]]
- r = json._encodeString(s)
- assert(r=='\\"\\\\\\"\\n')
- s = [["""\\\"]]
- r = json._encodeString(s)
- assert(r==[[\"\"\"\\\\\\\"]])
-
- end
-
- -- Test encode for basic strings (complicated strings)
- s = [[Hello, Lua!]]
- r = json.encode(s)
- assert(r=='"Hello, Lua!"')
- s = [["\"
-]]
- r = json.encode(s)
- assert(r=='\"\\"\\\\\\"\\n\"')
- s = [["""\\\"]]
- r = json.encode(s)
- assert(r==[["\"\"\"\\\\\\\""]])
-
- -- Test encode for numeric values
- s = 23
- r = json.encode(s)
- assert(r=='23')
- s=48.123
- r = json.encode(s)
- assert(r=='48.123')
-
- -- Test encode for boolean values
- assert(json.encode(true)=='true')
- assert(json.encode(false)=='false')
- assert(json.encode(nil)=='null')
-
- -- Test encode for arrays
- s = {1,2,3}
- r = json.encode(s)
- assert(r=="[1,2,3]")
- s = {9,9,9}
- r = json.encode(s)
- assert(r=="[9,9,9]")
-
- -- Complex array test
- s = { 2, 'joe', false, nil, 'hi' }
- r = json.encode(s)
- assert(r=='[2,"joe",false,null,"hi"]')
-
- -- Test encode for tables
- s = {Name='Craig',email='craig@lateral.co.za',age=35}
- r = json.encode(s)
- -- NB: This test can fail because of order: need to test further once
- -- decoding is supported.
- -- assert(r==[[{"age":35,"Name":"Craig","email":"craig@lateral.co.za"}]])
-
- -- Test decode_scanWhitespace
- if nil then
- s = " \n \r \t "
- e = json._decode_scanWhitespace(s,1)
- assert(e==string.len(s)+1)
- s = " \n\r\t4"
- assert(json._decode_scanWhitespace(s,1)==5)
-
- -- Test decode_scanString
- s = [["Test"]]
- r,e = json._decode_scanString(s,1)
- assert(r=='Test' and e==7)
- s = [["This\nis a \"test"]]
- r = json._decode_scanString(s,1)
- assert(r=="This\nis a \"test")
-
- s = [["Test\u00A7\\"]]
- r,e = json._decode_scanString(s,1)
- assert(r=="Test\xC2\xA7\\" and e==9)
- print(s,r)
-
- -- Test decode_scanNumber
- s = [[354]]
- r,e = json._decode_scanNumber(s,1)
- assert(r==354 and e==4)
- s = [[ 4565.23 AND OTHER THINGS ]]
- r,e = json._decode_scanNumber(s,2)
- assert(r==4565.23 and e==9)
- s = [[ -23.22 and ]]
- r,e = json._decode_scanNumber(s,2)
- assert(r==-23.22 and e==8)
-
- -- Test decode_scanConstant
- s = "true"
- r,e = json._decode_scanConstant(s,1)
- assert(r==true and e==5)
- s = " false "
- r,e = json._decode_scanConstant(s,3)
- assert(r==false and e==8)
- s = "1null6"
- r,e = json._decode_scanConstant(s,2)
- assert(r==nil and e==6)
-
- -- Test decode_scanArray
- s = "[1,2,3]"
- r,e = json._decode_scanArray(s,1)
- assert(compareData(r,{1,2,3}))
- s = [[[ 1 , 3 ,5 , "Fred" , true, false, null, -23 ] ]]
- r,e = json._decode_scanArray(s,1)
- assert(compareData(r, {1,3,5,'Fred',true,false,nil,-23} ) )
- s = "[3,5,null,7,9]"
- r,e = json._decode_scanArray(s,1)
- assert(compareData(r, {3,5,nil,7,9}))
- s = "[3,5,null,7,9,null,null]"
- r,e = json._decode_scanArray(s,1)
- assert(compareData(r, {3,5,nil,7,9,nil,nil}))
-
- end
-
- s = [["Test\u00A7\\\""]]
- r,e = json.decode(s)
- assert(r=="Test\xC2\xA7\\\"", r)
- print(s,r)
-
- -- Test decode_scanObject
- s = [[ {"one":1, "two":2, "three":"three", "four":true} ]]
- r,e = json.decode(s)
- for x,y in pairs(r) do
- print(x,y)
- end
- assert(compareData(r,{one=1,two=2,three='three',four=true}))
- s = [[ { "one" : { "first":1,"second":2,"third":3}, "two":2, "three":false } ]]
- r,e = json.decode(s)
- assert(compareData(r, {one={first=1,second=2,third=3},two=2,three=false}))
- s = [[ { "primes" : [2,3,5,7,9], "user":{"name":"craig","age":35,"programs_lua":true},
- "lua_is_great":true } ]]
- r,e = json.decode(s)
- assert(compareData(r, {primes={2,3,5,7,9},user={name='craig',age=35,programs_lua=true},lua_is_great=true}))
-
- -- Test json.null management
- t = { 1,2,json.null,4 }
- assert( json.encode(t)=="[1,2,null,4]" )
- t = {x=json.null }
- r = json.encode(t)
- assert( json.encode(t) == '{"x":null}' )
-
- -- Test comment decoding
- s = [[ /* A comment
- that spans
- a few lines
- */
- "test"
- ]]
- r,e = json.decode(s)
- assert(r=='test',"Comment decoding failed")
-end
-
-testJSON4Lua()
-
-print("JSON4Lua tests completed successfully")
diff --git a/examples/timetrials.lua b/examples/timetrials.lua
deleted file mode 100755
index cbda514..0000000
--- a/examples/timetrials.lua
+++ /dev/null
@@ -1,46 +0,0 @@
---[[
- Some Time Trails for the JSON4Lua package
-]]--
-
-
-require('json')
-require('os')
-require('table')
-
-local t1 = os.clock()
-local jstr
-local v
-for i=1,100 do
- local t = {}
- for j=1,500 do
- table.insert(t,j)
- end
- for j=1,500 do
- table.insert(t,"VALUE")
- end
- jstr = json.encode(t)
- v = json.decode(jstr)
- --print(json.encode(t))
-end
-
-for i = 1,100 do
- local t = {}
- for j=1,500 do
- local m= math.mod(j,3)
- if (m==0) then
- t['a'..j] = true
- elseif m==1 then
- t['a'..j] = json.null
- else
- t['a'..j] = j
- end
- end
- jstr = json.encode(t)
- v = json.decode(jstr)
-end
-
-print (jstr)
---print(type(t1))
-local t2 = os.clock()
-
-print ("Elapsed time=" .. os.difftime(t2,t1) .. "s")
\ No newline at end of file
diff --git a/json/json.lua b/json/json.lua
deleted file mode 100755
index 9ab4abd..0000000
--- a/json/json.lua
+++ /dev/null
@@ -1,417 +0,0 @@
------------------------------------------------------------------------------
--- JSON4Lua: JSON encoding / decoding support for the Lua language.
--- json Module.
--- Author: Craig Mason-Jones
--- Homepage: http://github.com/craigmj/json4lua/
--- Version: 1.0.0
--- This module is released under the MIT License (MIT).
--- Please see LICENCE.txt for details.
---
--- USAGE:
--- This module exposes two functions:
--- json.encode(o)
--- Returns the table / string / boolean / number / nil / json.null value as a JSON-encoded string.
--- json.decode(json_string)
--- Returns a Lua object populated with the data encoded in the JSON string json_string.
---
--- REQUIREMENTS:
--- compat-5.1 if using Lua 5.0
---
--- CHANGELOG
--- 0.9.20 Introduction of local Lua functions for private functions (removed _ function prefix).
--- Fixed Lua 5.1 compatibility issues.
--- Introduced json.null to have null values in associative arrays.
--- json.encode() performance improvement (more than 50%) through table.concat rather than ..
--- Introduced decode ability to ignore /**/ comments in the JSON string.
--- 0.9.10 Fix to array encoding / decoding to correctly manage nil/null values in arrays.
------------------------------------------------------------------------------
-
------------------------------------------------------------------------------
--- Imports and dependencies
------------------------------------------------------------------------------
-local math = require('math')
-local string = require("string")
-local table = require("table")
-
------------------------------------------------------------------------------
--- Module declaration
------------------------------------------------------------------------------
-local json = {} -- Public namespace
-local json_private = {} -- Private namespace
-
--- Public functions
-
--- Private functions
-local decode_scanArray
-local decode_scanComment
-local decode_scanConstant
-local decode_scanNumber
-local decode_scanObject
-local decode_scanString
-local decode_scanWhitespace
-local encodeString
-local isArray
-local isEncodable
-
------------------------------------------------------------------------------
--- PUBLIC FUNCTIONS
------------------------------------------------------------------------------
---- Encodes an arbitrary Lua object / variable.
--- @param v The Lua object / variable to be JSON encoded.
--- @return String containing the JSON encoding in internal Lua string format (i.e. not unicode)
-function json.encode (v)
- -- Handle nil values
- if v==nil then
- return "null"
- end
-
- local vtype = type(v)
-
- -- Handle strings
- if vtype=='string' then
- return '"' .. json_private.encodeString(v) .. '"' -- Need to handle encoding in string
- end
-
- -- Handle booleans
- if vtype=='number' or vtype=='boolean' then
- return tostring(v)
- end
-
- -- Handle tables
- if vtype=='table' then
- local rval = {}
- -- Consider arrays separately
- local bArray, maxCount = isArray(v)
- if bArray then
- for i = 1,maxCount do
- table.insert(rval, json.encode(v[i]))
- end
- else -- An object, not an array
- for i,j in pairs(v) do
- if isEncodable(i) and isEncodable(j) then
- table.insert(rval, '"' .. json_private.encodeString(i) .. '":' .. json.encode(j))
- end
- end
- end
- if bArray then
- return '[' .. table.concat(rval,',') ..']'
- else
- return '{' .. table.concat(rval,',') .. '}'
- end
- end
-
- -- Handle null values
- if vtype=='function' and v==null then
- return 'null'
- end
-
- assert(false,'encode attempt to encode unsupported type ' .. vtype .. ':' .. tostring(v))
-end
-
-
---- Decodes a JSON string and returns the decoded value as a Lua data structure / value.
--- @param s The string to scan.
--- @param [startPos] Optional starting position where the JSON string is located. Defaults to 1.
--- @param Lua object, number The object that was scanned, as a Lua table / string / number / boolean or nil,
--- and the position of the first character after
--- the scanned JSON object.
-function json.decode(s, startPos)
- startPos = startPos and startPos or 1
- startPos = decode_scanWhitespace(s,startPos)
- assert(startPos<=string.len(s), 'Unterminated JSON encoded object found at position in [' .. s .. ']')
- local curChar = string.sub(s,startPos,startPos)
- -- Object
- if curChar=='{' then
- return decode_scanObject(s,startPos)
- end
- -- Array
- if curChar=='[' then
- return decode_scanArray(s,startPos)
- end
- -- Number
- if string.find("+-0123456789.e", curChar, 1, true) then
- return decode_scanNumber(s,startPos)
- end
- -- String
- if curChar==[["]] or curChar==[[']] then
- return decode_scanString(s,startPos)
- end
- if string.sub(s,startPos,startPos+1)=='/*' then
- return decode(s, decode_scanComment(s,startPos))
- end
- -- Otherwise, it must be a constant
- return decode_scanConstant(s,startPos)
-end
-
---- The null function allows one to specify a null value in an associative array (which is otherwise
--- discarded if you set the value with 'nil' in Lua. Simply set t = { first=json.null }
-function null()
- return null -- so json.null() will also return null ;-)
-end
------------------------------------------------------------------------------
--- Internal, PRIVATE functions.
--- Following a Python-like convention, I have prefixed all these 'PRIVATE'
--- functions with an underscore.
------------------------------------------------------------------------------
-
---- Scans an array from JSON into a Lua object
--- startPos begins at the start of the array.
--- Returns the array and the next starting position
--- @param s The string being scanned.
--- @param startPos The starting position for the scan.
--- @return table, int The scanned array as a table, and the position of the next character to scan.
-function decode_scanArray(s,startPos)
- local array = {} -- The return value
- local stringLen = string.len(s)
- assert(string.sub(s,startPos,startPos)=='[','decode_scanArray called but array does not start at position ' .. startPos .. ' in string:\n'..s )
- startPos = startPos + 1
- -- Infinite loop for array elements
- repeat
- startPos = decode_scanWhitespace(s,startPos)
- assert(startPos<=stringLen,'JSON String ended unexpectedly scanning array.')
- local curChar = string.sub(s,startPos,startPos)
- if (curChar==']') then
- return array, startPos+1
- end
- if (curChar==',') then
- startPos = decode_scanWhitespace(s,startPos+1)
- end
- assert(startPos<=stringLen, 'JSON String ended unexpectedly scanning array.')
- object, startPos = json.decode(s,startPos)
- table.insert(array,object)
- until false
-end
-
---- Scans a comment and discards the comment.
--- Returns the position of the next character following the comment.
--- @param string s The JSON string to scan.
--- @param int startPos The starting position of the comment
-function decode_scanComment(s, startPos)
- assert( string.sub(s,startPos,startPos+1)=='/*', "decode_scanComment called but comment does not start at position " .. startPos)
- local endPos = string.find(s,'*/',startPos+2)
- assert(endPos~=nil, "Unterminated comment in string at " .. startPos)
- return endPos+2
-end
-
---- Scans for given constants: true, false or null
--- Returns the appropriate Lua type, and the position of the next character to read.
--- @param s The string being scanned.
--- @param startPos The position in the string at which to start scanning.
--- @return object, int The object (true, false or nil) and the position at which the next character should be
--- scanned.
-function decode_scanConstant(s, startPos)
- local consts = { ["true"] = true, ["false"] = false, ["null"] = nil }
- local constNames = {"true","false","null"}
-
- for i,k in pairs(constNames) do
- if string.sub(s,startPos, startPos + string.len(k) -1 )==k then
- return consts[k], startPos + string.len(k)
- end
- end
- assert(nil, 'Failed to scan constant from string ' .. s .. ' at starting position ' .. startPos)
-end
-
---- Scans a number from the JSON encoded string.
--- (in fact, also is able to scan numeric +- eqns, which is not
--- in the JSON spec.)
--- Returns the number, and the position of the next character
--- after the number.
--- @param s The string being scanned.
--- @param startPos The position at which to start scanning.
--- @return number, int The extracted number and the position of the next character to scan.
-function decode_scanNumber(s,startPos)
- local endPos = startPos+1
- local stringLen = string.len(s)
- local acceptableChars = "+-0123456789.e"
- while (string.find(acceptableChars, string.sub(s,endPos,endPos), 1, true)
- and endPos<=stringLen
- ) do
- endPos = endPos + 1
- end
- local stringValue = 'return ' .. string.sub(s,startPos, endPos-1)
- local stringEval = loadstring(stringValue)
- assert(stringEval, 'Failed to scan number [ ' .. stringValue .. '] in JSON string at position ' .. startPos .. ' : ' .. endPos)
- return stringEval(), endPos
-end
-
---- Scans a JSON object into a Lua object.
--- startPos begins at the start of the object.
--- Returns the object and the next starting position.
--- @param s The string being scanned.
--- @param startPos The starting position of the scan.
--- @return table, int The scanned object as a table and the position of the next character to scan.
-function decode_scanObject(s,startPos)
- local object = {}
- local stringLen = string.len(s)
- local key, value
- assert(string.sub(s,startPos,startPos)=='{','decode_scanObject called but object does not start at position ' .. startPos .. ' in string:\n' .. s)
- startPos = startPos + 1
- repeat
- startPos = decode_scanWhitespace(s,startPos)
- assert(startPos<=stringLen, 'JSON string ended unexpectedly while scanning object.')
- local curChar = string.sub(s,startPos,startPos)
- if (curChar=='}') then
- return object,startPos+1
- end
- if (curChar==',') then
- startPos = decode_scanWhitespace(s,startPos+1)
- end
- assert(startPos<=stringLen, 'JSON string ended unexpectedly scanning object.')
- -- Scan the key
- key, startPos = json.decode(s,startPos)
- assert(startPos<=stringLen, 'JSON string ended unexpectedly searching for value of key ' .. key)
- startPos = decode_scanWhitespace(s,startPos)
- assert(startPos<=stringLen, 'JSON string ended unexpectedly searching for value of key ' .. key)
- assert(string.sub(s,startPos,startPos)==':','JSON object key-value assignment mal-formed at ' .. startPos)
- startPos = decode_scanWhitespace(s,startPos+1)
- assert(startPos<=stringLen, 'JSON string ended unexpectedly searching for value of key ' .. key)
- value, startPos = json.decode(s,startPos)
- object[key]=value
- until false -- infinite loop while key-value pairs are found
-end
-
--- START SoniEx2
--- Initialize some things used by decode_scanString
--- You know, for efficiency
-local escapeSequences = {
- ["\\t"] = "\t",
- ["\\f"] = "\f",
- ["\\r"] = "\r",
- ["\\n"] = "\n",
- ["\\b"] = "\b"
-}
-setmetatable(escapeSequences, {__index = function(t,k)
- -- skip "\" aka strip escape
- return string.sub(k,2)
-end})
--- END SoniEx2
-
---- Scans a JSON string from the opening inverted comma or single quote to the
--- end of the string.
--- Returns the string extracted as a Lua string,
--- and the position of the next non-string character
--- (after the closing inverted comma or single quote).
--- @param s The string being scanned.
--- @param startPos The starting position of the scan.
--- @return string, int The extracted string as a Lua string, and the next character to parse.
-function decode_scanString(s,startPos)
- assert(startPos, 'decode_scanString(..) called without start position')
- local startChar = string.sub(s,startPos,startPos)
- -- START SoniEx2
- -- PS: I don't think single quotes are valid JSON
- assert(startChar == [["]] or startChar == [[']],'decode_scanString called for a non-string')
- --assert(startPos, "String decoding failed: missing closing " .. startChar .. " for string at position " .. oldStart)
- local t = {}
- local i,j = startPos,startPos
- while string.find(s, startChar, j+1) ~= j+1 do
- local oldj = j
- i,j = string.find(s, "\\.", j+1)
- local x,y = string.find(s, startChar, oldj+1)
- if not i or x < i then
- i,j = x,y-1
- end
- table.insert(t, string.sub(s, oldj+1, i-1))
- if string.sub(s, i, j) == "\\u" then
- local a = string.sub(s,j+1,j+4)
- j = j + 4
- local n = tonumber(a, 16)
- assert(n, "String decoding failed: bad Unicode escape " .. a .. " at position " .. i .. " : " .. j)
- -- math.floor(x/2^y) == lazy right shift
- -- a % 2^b == bitwise_and(a, (2^b)-1)
- -- 64 = 2^6
- -- 4096 = 2^12 (or 2^6 * 2^6)
- local x
- if n < 0x80 then
- x = string.char(n % 0x80)
- elseif n < 0x800 then
- -- [110x xxxx] [10xx xxxx]
- x = string.char(0xC0 + (math.floor(n/64) % 0x20), 0x80 + (n % 0x40))
- else
- -- [1110 xxxx] [10xx xxxx] [10xx xxxx]
- x = string.char(0xE0 + (math.floor(n/4096) % 0x10), 0x80 + (math.floor(n/64) % 0x40), 0x80 + (n % 0x40))
- end
- table.insert(t, x)
- else
- table.insert(t, escapeSequences[string.sub(s, i, j)])
- end
- end
- table.insert(t,string.sub(j, j+1))
- assert(string.find(s, startChar, j+1), "String decoding failed: missing closing " .. startChar .. " at position " .. j .. "(for string at position " .. startPos .. ")")
- return table.concat(t,""), j+2
- -- END SoniEx2
-end
-
---- Scans a JSON string skipping all whitespace from the current start position.
--- Returns the position of the first non-whitespace character, or nil if the whole end of string is reached.
--- @param s The string being scanned
--- @param startPos The starting position where we should begin removing whitespace.
--- @return int The first position where non-whitespace was encountered, or string.len(s)+1 if the end of string
--- was reached.
-function decode_scanWhitespace(s,startPos)
- local whitespace=" \n\r\t"
- local stringLen = string.len(s)
- while ( string.find(whitespace, string.sub(s,startPos,startPos), 1, true) and startPos <= stringLen) do
- startPos = startPos + 1
- end
- return startPos
-end
-
---- Encodes a string to be JSON-compatible.
--- This just involves back-quoting inverted commas, back-quotes and newlines, I think ;-)
--- @param s The string to return as a JSON encoded (i.e. backquoted string)
--- @return The string appropriately escaped.
-
-local escapeList = {
- ['"'] = '\\"',
- ['\\'] = '\\\\',
- ['/'] = '\\/',
- ['\b'] = '\\b',
- ['\f'] = '\\f',
- ['\n'] = '\\n',
- ['\r'] = '\\r',
- ['\t'] = '\\t'
-}
-
-function json_private.encodeString(s)
- local s = tostring(s)
- return s:gsub(".", function(c) return escapeList[c] end) -- SoniEx2: 5.0 compat
-end
-
--- Determines whether the given Lua type is an array or a table / dictionary.
--- We consider any table an array if it has indexes 1..n for its n items, and no
--- other data in the table.
--- I think this method is currently a little 'flaky', but can't think of a good way around it yet...
--- @param t The table to evaluate as an array
--- @return boolean, number True if the table can be represented as an array, false otherwise. If true,
--- the second returned value is the maximum
--- number of indexed elements in the array.
-function isArray(t)
- -- Next we count all the elements, ensuring that any non-indexed elements are not-encodable
- -- (with the possible exception of 'n')
- local maxIndex = 0
- for k,v in pairs(t) do
- if (type(k)=='number' and math.floor(k)==k and 1<=k) then -- k,v is an indexed pair
- if (not isEncodable(v)) then return false end -- All array elements must be encodable
- maxIndex = math.max(maxIndex,k)
- else
- if (k=='n') then
- if v ~= table.getn(t) then return false end -- False if n does not hold the number of elements
- else -- Else of (k=='n')
- if isEncodable(v) then return false end
- end -- End of (k~='n')
- end -- End of k,v not an indexed pair
- end -- End of loop across all pairs
- return true, maxIndex
-end
-
---- Determines whether the given Lua object / table / variable can be JSON encoded. The only
--- types that are JSON encodable are: string, boolean, number, nil, table and json.null.
--- In this implementation, all other types are ignored.
--- @param o The object to examine.
--- @return boolean True if the object should be JSON encoded, false if it should be ignored.
-function isEncodable(o)
- local t = type(o)
- return (t=='string' or t=='boolean' or t=='number' or t=='nil' or t=='table') or (t=='function' and o==null)
-end
-
-return json
\ No newline at end of file
diff --git a/json/rpc.lua b/json/rpc.lua
index 330b635..6d01935 100755
--- a/json/rpc.lua
+++ b/json/rpc.lua
@@ -22,15 +22,17 @@
-- compat-5.1 if using Lua 5.0.
-----------------------------------------------------------------------------
-local json = require('json')
-json.rpc = {} -- Module public namespace
+--- @module json.rpc
+local rpc = {} -- Module public namespace
-----------------------------------------------------------------------------
-- Imports and dependencies
-----------------------------------------------------------------------------
-local json = require('json')
+local cjson_safe = require("cjson.safe")
local http = require("socket.http")
+local socketTimeout = 5
+
-----------------------------------------------------------------------------
-- PUBLIC functions
-----------------------------------------------------------------------------
@@ -43,12 +45,12 @@ local http = require("socket.http")
-- print(jsolait.echo('This is a test of the echo method!'))
-- print(jsolait.args2String('first','second','third'))
-- table.foreachi( jsolait.args2Array(5,4,3,2,1), print)
-function json.rpc.proxy(url)
+function rpc.proxy(url)
local serverProxy = {}
local proxyMeta = {
__index = function(self, key)
return function(...)
- return json.rpc.call(url, key, ...)
+ return rpc.call(url, key, ...)
end
end
}
@@ -56,6 +58,12 @@ function json.rpc.proxy(url)
return serverProxy
end
+--- Sets connection timeout
+-- @param timeout The number of seconds to wait for connection
+function rpc.setTimeout(timeout)
+ socketTimeout = timeout
+end
+
--- Calls a JSON RPC method on a remote server.
-- Returns a boolean true if the call succeeded, false otherwise.
-- On success, the second returned parameter is the decoded
@@ -69,20 +77,25 @@ end
-- are nil, this means that the result of the RPC call was nil.
-- EXAMPLE Usage:
-- print(json.rpc.call('http://jsolait.net/testj.py','echo','This string will be returned'))
-function json.rpc.call(url, method, ...)
+function rpc.call(url, method, ...)
local JSONRequestArray = {
id=tostring(math.random()),
["method"]=method,
- params = ...
+ params = {...}
}
local httpResponse, result , code
- local jsonRequest = json.encode(JSONRequestArray)
+ local jsonRequest, err = cjson_safe.encode(JSONRequestArray)
+ if jsonRequest == nil then
+ return nil, err
+ end
-- We use the sophisticated http.request form (with ltn12 sources and sinks) so that
- -- we can set the content-type to text/plain. While this shouldn't strictly-speaking be true,
- -- it seems a good idea (Xavante won't work w/out a content-type header, although a patch
- -- is needed to Xavante to make it work with text/plain)
+ -- we can set the content-type to application/json-rpc. While this shouldn't strictly-speaking be true,
+ -- it seems a good idea.
+ -- cgilua does not support application/json-rpc at the moment of this writing
+ -- fix: https://github.com/pdxmeshnet/cgilua/commit/1b35d812c7d637b91f2ac0a8d91f9698ba84d8d9
local ltn12 = require('ltn12')
local resultChunks = {}
+ http.TIMEOUT = socketTimeout
httpResponse, code = http.request(
{ ['url'] = url,
sink = ltn12.sink.table(resultChunks),
@@ -97,10 +110,20 @@ function json.rpc.call(url, method, ...)
return nil, "HTTP ERROR: " .. code
end
-- And decode the httpResponse and check the JSON RPC result code
- result = json.decode( httpResponse )
- if result.result then
+ result, err = cjson_safe.decode( httpResponse )
+ if result and result.result then
return result.result, nil
else
- return nil, result.error
+ if err then
+ return nil, err
+ else
+ if result and result.error then
+ return nil, result.error
+ else
+ return nil, "Unknown error"
+ end
+ end
end
end
+
+return rpc
diff --git a/json/rpcserver.lua b/json/rpcserver.lua
index e01f1f8..b320921 100755
--- a/json/rpcserver.lua
+++ b/json/rpcserver.lua
@@ -24,7 +24,13 @@
-- for details.
----------------------------------------------------------------------------
-module ('json.rpcserver')
+
+local cgilua = require "cgilua"
+local cjson_safe = require "cjson.safe"
+
+--- @module json.rpcserver
+local rpcserver = {}
+
---
-- Implements a JSON RPC Server wrapping for luaClass, exposing each of luaClass's
@@ -35,44 +41,56 @@ module ('json.rpcserver')
-- false, when a function returns multiple values, only the first of these values will
-- be returned.
--
-function serve(luaClass, packReturn)
- cgilua.contentheader('text','plain')
- require('cgilua')
- require ('json')
- local postData = ""
+function rpcserver.serve(luaClass, packReturn)
+
+ cgilua.contentheader('application','json-rpc')
+
+ local postData = "{}"
- if not cgilua.servervariable('CONTENT_LENGTH') then
- cgilua.put("Please access JSON Request using HTTP POST Request")
- return 0
- else
- postData = cgi[1] -- SAPI.Request.getpostdata() --[[{ "id":1, "method":"echo","params":["Hi there"]}]] --
- end
- -- @TODO Catch an error condition on decoding the data
- local jsonRequest = json.decode(postData)
local jsonResponse = {}
- jsonResponse.id = jsonRequest.id
- local method = luaClass[ jsonRequest.method ]
-
- if not method then
- jsonResponse.error = 'Method ' .. jsonRequest.method .. ' does not exist at this server.'
+ jsonResponse.result = nil
+ jsonResponse.error = nil
+
+ if cgilua.servervariable('REQUEST_METHOD') ~= "POST" then
+ jsonResponse.error = "Please use HTTP POST"
else
- local callResult = { pcall( method, unpack( jsonRequest.params ) ) }
- if callResult[1] then -- Function call successfull
- table.remove(callResult,1)
- if packReturn and table.getn(callResult)>1 then
- jsonResponse.result = callResult
+ postData = cgilua.POST[1] --[[{ "id":1, "method":"echo","params":["Hi there"]}]] --
+
+ local jsonRequest, err = cjson_safe.decode(postData)
+ if err then
+ jsonResponse.error = "Failed to parse JSON POST data: " .. err
+ else
+ jsonResponse.id = jsonRequest.id
+ local method = luaClass[ jsonRequest.method ]
+
+ if not method then
+ jsonResponse.error = 'Method ' .. jsonRequest.method .. ' does not exist at this server.'
+ elseif type(jsonRequest.params) ~= "table" then
+ jsonResponse.error = 'Invalid method parameters list "' .. cjson_safe.encode(jsonRequest.params) .. '".'
else
- jsonResponse.result = unpack(callResult) -- NB: Does not support multiple argument returns
+ local callResult = { pcall( method, unpack( jsonRequest.params ) ) }
+ if callResult[1] then -- Function call successfull
+ table.remove(callResult,1)
+ if packReturn and table.getn(callResult)>1 then
+ jsonResponse.result = callResult
+ else
+ jsonResponse.result = unpack(callResult) -- NB: Does not support multiple argument returns
+ end
+ else
+ jsonResponse.error = callResult[2]
+ end
end
- else
- jsonResponse.error = callResult[2]
end
- end
+ end
-- Output the result
- -- TODO: How to be sure that the result and error tags are there even when they are nil in Lua?
- -- Can force them by hand... ?
- cgilua.contentheader('text','plain')
- cgilua.put( json.encode( jsonResponse ) )
+ local data, err = cjson_safe.encode( jsonResponse )
+ if data == nil then
+ jsonResponse.result = nil
+ jsonResponse.error = "Failed to encode JSON response: " .. err
+ data = cjson_safe.encode( jsonResponse )
+ end
+ cgilua.put( data )
end
+return rpcserver
diff --git a/json4lua-0.9.53-1.rockspec b/json4lua-0.9.53-1.rockspec
deleted file mode 100644
index b3b737a..0000000
--- a/json4lua-0.9.53-1.rockspec
+++ /dev/null
@@ -1,32 +0,0 @@
-package="JSON4Lua"
-version="1.0.0"
-source = {
- url = "git://github.com/craigmj/json4lua.git",
- tag = "1.0.0"
-}
-description = {
- summary = "JSON4Lua and JSONRPC4Lua implement JSON (JavaScript Object Notation) encoding and decoding and a JSON-RPC-over-http client for Lua.",
- detailed = [[
- JSON4Lua and JSONRPC4Lua implement JSON (JavaScript Object Notation)
- encoding and decoding and a JSON-RPC-over-http client for Lua.
- JSON is JavaScript Object Notation, a simple encoding of
- Javascript-like objects that is ideal for lightweight transmission
- of relatively weakly-typed data. A sub-package of JSON4Lua is
- JSONRPC4Lua, which provides a simple JSON-RPC-over-http client and server
- (in a CGILua environment) for Lua.
- ]],
- homepage = "http://github.com/craigmj/json4lua/",
- license = "GPL"
-}
-dependencies = {
- "lua >= 5.2",
- "luasocket",
-}
-
-build = {
- type = "builtin",
- modules = {
- ["json"] = "json/json.lua",
- ["json.rpc"] = "json/rpc.lua"
- }
-}
diff --git a/jsonrpc4lua-1.0.0-1.rockspec b/jsonrpc4lua-1.0.0-1.rockspec
new file mode 100644
index 0000000..7f1129d
--- /dev/null
+++ b/jsonrpc4lua-1.0.0-1.rockspec
@@ -0,0 +1,35 @@
+package="JSONRPC4Lua"
+version="1.0.0-1"
+source = {
+ url = "git://github.com/pdxmeshnet/jsonrpc4lua.git",
+ tag = "1.0.0"
+}
+description = {
+ summary = "JSONRPC4Lua implement JSON-RPC-over-http client and server-side for Lua.",
+ detailed = [[
+ JSONRPC4Lua implement JSON-RPC-over-http client and server-side for Lua.
+ JSON is JavaScript Object Notation, a simple encoding of
+ Javascript-like objects that is ideal for lightweight transmission
+ of relatively weakly-typed data. RPC is an inter-process communication
+ mechanism that allows a computer program to cause a subroutine to
+ execute in a program running on a remote host. JSONRPC4Lua provides a
+ simple JSON-RPC-over-http client and server-side (in a CGILua
+ environment) for Lua.
+ ]],
+ homepage = "http://github.com/pdxmeshnet/jsonrpc4lua/",
+ license = "GPL"
+}
+dependencies = {
+ "lua >= 5.1",
+ "luasocket",
+ "cgilua",
+ "lua-cjson",
+}
+
+build = {
+ type = "builtin",
+ modules = {
+ ["json.rpc"] = "json/rpc.lua",
+ ["json.rpcserver"] = "json/rpcserver.lua"
+ }
+}
diff --git a/jsonrpc4lua-1.0.1-1.rockspec b/jsonrpc4lua-1.0.1-1.rockspec
new file mode 100644
index 0000000..61b3468
--- /dev/null
+++ b/jsonrpc4lua-1.0.1-1.rockspec
@@ -0,0 +1,35 @@
+package="JSONRPC4Lua"
+version="1.0.1-1"
+source = {
+ url = "git://github.com/pdxmeshnet/jsonrpc4lua.git",
+ tag = "1.0.1"
+}
+description = {
+ summary = "JSONRPC4Lua implement JSON-RPC-over-http client and server-side for Lua.",
+ detailed = [[
+ JSONRPC4Lua implement JSON-RPC-over-http client and server-side for Lua.
+ JSON is JavaScript Object Notation, a simple encoding of
+ Javascript-like objects that is ideal for lightweight transmission
+ of relatively weakly-typed data. RPC is an inter-process communication
+ mechanism that allows a computer program to cause a subroutine to
+ execute in a program running on a remote host. JSONRPC4Lua provides a
+ simple JSON-RPC-over-http client and server-side (in a CGILua
+ environment) for Lua.
+ ]],
+ homepage = "http://github.com/pdxmeshnet/jsonrpc4lua/",
+ license = "GPL"
+}
+dependencies = {
+ "lua >= 5.1",
+ "luasocket",
+ "cgilua",
+ "lua-cjson",
+}
+
+build = {
+ type = "builtin",
+ modules = {
+ ["json.rpc"] = "json/rpc.lua",
+ ["json.rpcserver"] = "json/rpcserver.lua"
+ }
+}