Changeset 6391 for luci/trunk/libs/web/luasrc/http.lua
- Timestamp:
- 11/07/10 20:27:15 (3 years ago)
- Files:
-
- 1 modified
-
luci/trunk/libs/web/luasrc/http.lua (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
-
luci/trunk/libs/web/luasrc/http.lua
r5140 r6391 13 13 Licensed under the Apache License, Version 2.0 (the "License"); 14 14 you 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 15 You may obtain a copy of the License at 16 17 http://www.apache.org/licenses/LICENSE-2.0 18 18 19 19 Unless required by applicable law or agreed to in writing, software … … 30 30 local string = require "string" 31 31 local coroutine = require "coroutine" 32 33 local pairs, tostring, error = pairs, tostring, error 32 local table = require "table" 33 34 local ipairs, pairs, next, type, tostring, error = 35 ipairs, pairs, next, type, tostring, error 34 36 35 37 --- LuCI Web Framework high-level HTTP functions. … … 46 48 -- File handler 47 49 self.filehandler = function() end 48 50 49 51 -- HTTP-Message table 50 52 self.message = { … … 53 55 params = protocol.urldecode_params(env.QUERY_STRING or ""), 54 56 } 55 57 56 58 self.parsed_input = false 57 59 end … … 61 63 self:_parse_input() 62 64 end 63 65 64 66 if name then 65 67 return self.message.params[name] … … 72 74 local vals = {} 73 75 prefix = prefix and prefix .. "." or "." 74 76 75 77 if not self.parsed_input then 76 78 self:_parse_input() 77 79 end 78 80 79 81 local void = self.message.params[nil] 80 82 for k, v in pairs(self.message.params) do … … 83 85 end 84 86 end 85 87 86 88 return vals 87 89 end … … 91 93 self:_parse_input() 92 94 end 93 95 94 96 return self.message.content, self.message.content_length 95 97 end … … 129 131 coroutine.yield(3) 130 132 end 131 133 132 134 if not context.closed then 133 135 context.closed = true … … 165 167 end 166 168 167 --- Get the value of a certain HTTP environment variable 169 --- Get the value of a certain HTTP environment variable 168 170 -- or the environment table itself. 169 171 -- @param name Environment variable … … 249 251 header("Expires", "0") 250 252 end 251 252 253 254 253 255 context.eoh = true 254 256 coroutine.yield(3) … … 277 279 -- @param table Query string source table 278 280 -- @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 281 function 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, "") 287 293 end 288 294 … … 299 305 -- @see urldecode 300 306 urlencode = protocol.urlencode 307 308 --- Send the given data as JSON encoded string. 309 -- @param data Data to send 310 function 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 340 end
