libs/web: Cleanup and inline documentation

This commit is contained in:
Steven Barth 2008-07-23 19:27:33 +00:00
parent 22451345d8
commit f94c7b2c10

View file

@ -31,7 +31,6 @@ require("luci.util")
context = luci.util.threadlocal() context = luci.util.threadlocal()
Request = luci.util.class() Request = luci.util.class()
function Request.__init__(self, env, sourcein, sinkerr) function Request.__init__(self, env, sourcein, sinkerr)
self.input = sourcein self.input = sourcein
@ -109,7 +108,7 @@ function Request._parse_input(self)
self.parsed_input = true self.parsed_input = true
end end
--- Close the HTTP-Connection.
function close() function close()
if not context.eoh then if not context.eoh then
context.eoh = true context.eoh = true
@ -122,34 +121,45 @@ function close()
end end
end end
function formvalue(...) --- Get a certain HTTP input value or a table of all input values.
return context.request:formvalue(...) -- @param name Name of the GET or POST variable to fetch
-- @param noparse Don't parse POST data before getting the value
-- @return HTTP input value or table of all input value
function formvalue(name, noparse)
return context.request:formvalue(name, noparse)
end end
function formvaluetable(...) --- Get a table of all HTTP input values with a certain prefix.
return context.request:formvaluetable(...) -- @param prefix Prefix
-- @return Table of all HTTP input values with given prefix
function formvaluetable(prefix)
return context.request:formvaluetable(prefix)
end end
function getcookie(...) --- Get the value of a certain HTTP-Cookie.
return context.request:getcookie(...) -- @param name Cookie Name
-- @return String containing cookie data
function getcookie(name)
return context.request:getcookie(name)
end end
function getvalue(...) --- Get the value of a certain HTTP environment variable
return context.request:getvalue(...) -- or the environment table itself.
-- @param name Environment variable
-- @return HTTP environment value or environment table
function getenv(name)
return context.request:getenv(name)
end end
function postvalue(...) --- Set a handler function for incoming user file uploads.
return context.request:postvalue(...) -- @param callback Handler function
end function setfilehandler(callback)
return context.request:setfilehandler(callback)
function getenv(...)
return context.request:getenv(...)
end
function setfilehandler(...)
return context.request:setfilehandler(...)
end end
--- Send a HTTP-Header.
-- @param key Header key
-- @param value Header value
function header(key, value) function header(key, value)
if not context.headers then if not context.headers then
context.headers = {} context.headers = {}
@ -158,10 +168,15 @@ function header(key, value)
coroutine.yield(2, key, value) coroutine.yield(2, key, value)
end end
--- Set the mime type of following content data.
-- @param mime Mimetype of following content
function prepare_content(mime) function prepare_content(mime)
header("Content-Type", mime) header("Content-Type", mime)
end end
--- Set the HTTP status code and status message.
-- @param code Status code
-- @param message Status message
function status(code, message) function status(code, message)
code = code or 200 code = code or 200
message = message or "OK" message = message or "OK"
@ -169,6 +184,12 @@ function status(code, message)
coroutine.yield(1, code, message) coroutine.yield(1, code, message)
end end
--- Send a chunk of content data to the client.
-- This function is as a valid LTN12 sink.
-- If the content chunk is nil this function will automatically invoke close.
-- @param content Content chunk
-- @param src_err Error object from source (optional)
-- @see close
function write(content, src_err) function write(content, src_err)
if not content then if not content then
if src_err then if src_err then
@ -200,12 +221,17 @@ function write(content, src_err)
end end
end end
--- Redirects the client to a new URL and closes the connection.
-- @param url Target URL
function redirect(url) function redirect(url)
status(302, "Found") status(302, "Found")
header("Location", url) header("Location", url)
close() close()
end end
--- Create a querystring out of a table of key - value pairs.
-- @param table Query string source table
-- @return Encoded HTTP query string
function build_querystring(table) function build_querystring(table)
local s="?" local s="?"
@ -216,5 +242,15 @@ function build_querystring(table)
return s return s
end end
--- Return the URL-decoded equivalent of a string.
-- @param str URL-encoded string
-- @param no_plus Don't decode + to " "
-- @return URL-decoded string
-- @see urlencode
urldecode = luci.http.protocol.urldecode urldecode = luci.http.protocol.urldecode
--- Return the URL-encoded equivalent of a string.
-- @param str Source string
-- @return URL-encoded string
-- @see urldecode
urlencode = luci.http.protocol.urlencode urlencode = luci.http.protocol.urlencode