openwrt: add jsonp support to local RESTful API

This commit is contained in:
Bart Van Der Meerssche 2010-10-01 19:28:08 +02:00
parent 47e2268abc
commit 1a2468dc20
1 changed files with 8 additions and 6 deletions

View File

@ -66,15 +66,17 @@ function rest_sensor(id)
local version = uci:get("flukso", "main", "localVersion")
local param = decode(http.getenv("QUERY_STRING"))
if param.interval == "minute" and param.unit == "watt" and param.version == version then
http.prepare_content("application/json")
http.write("sensor:" .. (id[1] or 'nok') .. "\n" ..
"query string:" .. http.getenv("QUERY_STRING") .. "\n" ..
"contents:")
if param.interval == "minute" and param.unit == "watt" and param.version == version then
local source = ltn12.source.file(io.open(path .. "/" .. id[1], "r"))
if param.jsonp_callback then
source = ltn12.source.cat(ltn12.source.string(param.jsonp_callback .. "("), source, ltn12.source.string(")"))
end
ltn12.pump.all(source, http.write)
else
error400("Malformed query string: interval, unit and version query parameters are obligatory.")
error400("Malformed query string: interval, unit and version query parameters are required.")
end
end