From 1a2468dc20dde14362c43dbc05af3835ce07aba4 Mon Sep 17 00:00:00 2001 From: Bart Van Der Meerssche Date: Fri, 1 Oct 2010 19:28:08 +0200 Subject: [PATCH] openwrt: add jsonp support to local RESTful API --- .../admin-mini/luasrc/controller/restful.lua | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/mote/v1/openwrt/package/luci/modules/admin-mini/luasrc/controller/restful.lua b/mote/v1/openwrt/package/luci/modules/admin-mini/luasrc/controller/restful.lua index 3c878bd..9219e5f 100644 --- a/mote/v1/openwrt/package/luci/modules/admin-mini/luasrc/controller/restful.lua +++ b/mote/v1/openwrt/package/luci/modules/admin-mini/luasrc/controller/restful.lua @@ -66,15 +66,17 @@ function rest_sensor(id) local version = uci:get("flukso", "main", "localVersion") local param = decode(http.getenv("QUERY_STRING")) + http.prepare_content("application/json") + 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:") - 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