Changeset 6391

Show
Ignore:
Timestamp:
11/07/10 20:27:15 (3 years ago)
Author:
jow
Message:

libs/web: add luci.http.write_json()

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • luci/trunk/libs/web/luasrc/http.lua

    r5140 r6391  
    1313Licensed under the Apache License, Version 2.0 (the "License"); 
    1414you may not use this file except in compliance with the License. 
    15 You may obtain a copy of the License at  
    16  
    17     http://www.apache.org/licenses/LICENSE-2.0  
     15You may obtain a copy of the License at 
     16 
     17    http://www.apache.org/licenses/LICENSE-2.0 
    1818 
    1919Unless required by applicable law or agreed to in writing, software 
     
    3030local string = require "string" 
    3131local coroutine = require "coroutine" 
    32  
    33 local pairs, tostring, error = pairs, tostring, error 
     32local table = require "table" 
     33 
     34local ipairs, pairs, next, type, tostring, error = 
     35    ipairs, pairs, next, type, tostring, error 
    3436 
    3537--- LuCI Web Framework high-level HTTP functions. 
     
    4648    -- File handler 
    4749    self.filehandler = function() end 
    48      
     50 
    4951    -- HTTP-Message table 
    5052    self.message = { 
     
    5355        params = protocol.urldecode_params(env.QUERY_STRING or ""), 
    5456    } 
    55      
     57 
    5658    self.parsed_input = false 
    5759end 
     
    6163        self:_parse_input() 
    6264    end 
    63      
     65 
    6466    if name then 
    6567        return self.message.params[name] 
     
    7274    local vals = {} 
    7375    prefix = prefix and prefix .. "." or "." 
    74      
     76 
    7577    if not self.parsed_input then 
    7678        self:_parse_input() 
    7779    end 
    78      
     80 
    7981    local void = self.message.params[nil] 
    8082    for k, v in pairs(self.message.params) do 
     
    8385        end 
    8486    end 
    85      
     87 
    8688    return vals 
    8789end 
     
    9193        self:_parse_input() 
    9294    end 
    93      
     95 
    9496    return self.message.content, self.message.content_length 
    9597end 
     
    129131        coroutine.yield(3) 
    130132    end 
    131      
     133 
    132134    if not context.closed then 
    133135        context.closed = true 
     
    165167end 
    166168 
    167 --- Get the value of a certain HTTP environment variable  
     169--- Get the value of a certain HTTP environment variable 
    168170-- or the environment table itself. 
    169171-- @param name      Environment variable 
     
    249251                header("Expires", "0") 
    250252            end 
    251              
    252              
     253 
     254 
    253255            context.eoh = true 
    254256            coroutine.yield(3) 
     
    277279-- @param table     Query string source table 
    278280-- @return          Encoded HTTP query string 
    279 function build_querystring(table) 
    280     local s="?" 
    281      
    282     for k, v in pairs(table) do 
    283         s = s .. urlencode(k) .. "=" .. urlencode(v) .. "&" 
    284     end 
    285      
    286     return s 
     281function build_querystring(q) 
     282    local s = { "?" } 
     283 
     284    for k, v in pairs(q) do 
     285        if #s > 1 then s[#s+1] = "&" end 
     286 
     287        s[#s+1] = urldecode(k) 
     288        s[#s+1] = "=" 
     289        s[#s+1] = urldecode(v) 
     290    end 
     291 
     292    return table.concat(s, "") 
    287293end 
    288294 
     
    299305-- @see urldecode 
    300306urlencode = protocol.urlencode 
     307 
     308--- Send the given data as JSON encoded string. 
     309-- @param data      Data to send 
     310function write_json(x) 
     311    if x == nil then 
     312        write("null") 
     313    elseif type(x) == "table" then 
     314        local k, v 
     315        if type(next(x)) == "number" then 
     316            write("[ ") 
     317            for k, v in ipairs(x) do 
     318                write_json(v) 
     319                if next(x, k) then 
     320                    write(", ") 
     321                end 
     322            end 
     323            write(" ]") 
     324        else 
     325            write("{ ") 
     326            for k, v in pairs(x) do 
     327            write("%q: " % k) 
     328                write_json(v) 
     329                if next(x, k) then 
     330                    write(", ") 
     331                end 
     332            end 
     333            write(" }") 
     334        end 
     335    elseif type(x) == "number" or type(x) == "boolean" then 
     336        write(tostring(x)) 
     337    elseif type(x) == "string" then 
     338        write("%q" % tostring(x)) 
     339    end 
     340end