diff options
author | Steven Barth <steven@midlink.org> | 2009-06-14 08:51:43 +0000 |
---|---|---|
committer | Steven Barth <steven@midlink.org> | 2009-06-14 08:51:43 +0000 |
commit | c47be2e727d1bb3e2e3aa415ec96be2a5f8a45b7 (patch) | |
tree | 40d9202363c5e81f9fe38b4d52a1cfa889792cda /libs/lucid-http/luasrc/lucid | |
parent | f9263e00c1371eff6ace0252143236d6bf6f2ce2 (diff) |
GSoC: Documentation #2
Diffstat (limited to 'libs/lucid-http/luasrc/lucid')
-rw-r--r-- | libs/lucid-http/luasrc/lucid/http.lua | 3 | ||||
-rw-r--r-- | libs/lucid-http/luasrc/lucid/http/DirectoryPublisher.lua | 3 | ||||
-rw-r--r-- | libs/lucid-http/luasrc/lucid/http/LuciWebPublisher.lua | 4 | ||||
-rw-r--r-- | libs/lucid-http/luasrc/lucid/http/Redirector.lua | 4 | ||||
-rw-r--r-- | libs/lucid-http/luasrc/lucid/http/handler/catchall.lua | 18 | ||||
-rw-r--r-- | libs/lucid-http/luasrc/lucid/http/handler/file.lua | 23 | ||||
-rw-r--r-- | libs/lucid-http/luasrc/lucid/http/handler/luci.lua | 16 | ||||
-rw-r--r-- | libs/lucid-http/luasrc/lucid/http/server.lua | 66 |
8 files changed, 127 insertions, 10 deletions
diff --git a/libs/lucid-http/luasrc/lucid/http.lua b/libs/lucid-http/luasrc/lucid/http.lua index 32ba5791de..931967c563 100644 --- a/libs/lucid-http/luasrc/lucid/http.lua +++ b/libs/lucid-http/luasrc/lucid/http.lua @@ -17,6 +17,9 @@ local srv = require "luci.lucid.http.server" module "luci.lucid.http" +--- Prepare the HTTP-daemon and its associated publishers. +-- @param publisher Table of publishers +-- @return factory callback or nil, error message function factory(publisher) local server = srv.Server() for _, r in ipairs(publisher) do diff --git a/libs/lucid-http/luasrc/lucid/http/DirectoryPublisher.lua b/libs/lucid-http/luasrc/lucid/http/DirectoryPublisher.lua index f471781a93..b57b5586a2 100644 --- a/libs/lucid-http/luasrc/lucid/http/DirectoryPublisher.lua +++ b/libs/lucid-http/luasrc/lucid/http/DirectoryPublisher.lua @@ -18,6 +18,9 @@ local srv = require "luci.lucid.http.server" module "luci.lucid.http.DirectoryPublisher" +--- Prepare a directory publisher and assign it to a given Virtual Host. +-- @param server HTTP daemon object +-- @param config publisher configuration function factory(server, config) config.domain = config.domain or "" local vhost = server:get_vhosts()[config.domain] diff --git a/libs/lucid-http/luasrc/lucid/http/LuciWebPublisher.lua b/libs/lucid-http/luasrc/lucid/http/LuciWebPublisher.lua index 0d06489678..f72b94f41a 100644 --- a/libs/lucid-http/luasrc/lucid/http/LuciWebPublisher.lua +++ b/libs/lucid-http/luasrc/lucid/http/LuciWebPublisher.lua @@ -18,6 +18,10 @@ local srv = require "luci.lucid.http.server" module "luci.lucid.http.LuciWebPublisher" + +--- Prepare a LuCI web publisher and assign it to a given Virtual Host. +-- @param server HTTP daemon object +-- @param config publisher configuration function factory(server, config) pcall(function() require "luci.dispatcher" diff --git a/libs/lucid-http/luasrc/lucid/http/Redirector.lua b/libs/lucid-http/luasrc/lucid/http/Redirector.lua index c0af90b00c..73561af2da 100644 --- a/libs/lucid-http/luasrc/lucid/http/Redirector.lua +++ b/libs/lucid-http/luasrc/lucid/http/Redirector.lua @@ -17,7 +17,9 @@ local srv = require "luci.lucid.http.server" module "luci.lucid.http.Redirector" - +--- Prepare a redirector publisher and assign it to a given Virtual Host. +-- @param server HTTP daemon object +-- @param config publisher configuration function factory(server, config) config.domain = config.domain or "" local vhost = server:get_vhosts()[config.domain] diff --git a/libs/lucid-http/luasrc/lucid/http/handler/catchall.lua b/libs/lucid-http/luasrc/lucid/http/handler/catchall.lua index 3b2c2b0a0c..30af84ba2a 100644 --- a/libs/lucid-http/luasrc/lucid/http/handler/catchall.lua +++ b/libs/lucid-http/luasrc/lucid/http/handler/catchall.lua @@ -15,8 +15,15 @@ local srv = require "luci.lucid.http.server" local proto = require "luci.http.protocol" local util = require "luci.util" +--- Catchall Handler +-- @cstyle instance module "luci.lucid.http.handler.catchall" +--- Create a Redirect handler. +-- @param name Name +-- @param target Redirect Target +-- @class function +-- @return Redirect handler object Redirect = util.class(srv.Handler) function Redirect.__init__(self, name, target) @@ -24,6 +31,9 @@ function Redirect.__init__(self, name, target) self.target = target end +--- Handle a GET request. +-- @param request Request object +-- @return status code, header table, response source function Redirect.handle_GET(self, request) local target = self.target local protocol = request.env.HTTPS and "https://" or "http://" @@ -46,8 +56,16 @@ function Redirect.handle_GET(self, request) return 302, { Location = target } end +--- Handle a POST request. +-- @class function +-- @param request Request object +-- @return status code, header table, response source Redirect.handle_POST = Redirect.handle_GET +--- Handle a HEAD request. +-- @class function +-- @param request Request object +-- @return status code, header table, response source function Redirect.handle_HEAD(self, request) local stat, head = self:handle_GET(request) return stat, head diff --git a/libs/lucid-http/luasrc/lucid/http/handler/file.lua b/libs/lucid-http/luasrc/lucid/http/handler/file.lua index d08e47025d..8f7bf8b8a7 100644 --- a/libs/lucid-http/luasrc/lucid/http/handler/file.lua +++ b/libs/lucid-http/luasrc/lucid/http/handler/file.lua @@ -28,8 +28,16 @@ local date = require "luci.http.protocol.date" local mime = require "luci.http.protocol.mime" local cond = require "luci.http.protocol.conditionals" +--- File system handler +-- @cstyle instance module "luci.lucid.http.handler.file" +--- Create a simple file system handler. +-- @class function +-- @param name Name +-- @param docroot Physical Document Root +-- @param options Options +-- @return Simple file system handler object Simple = util.class(srv.Handler) function Simple.__init__(self, name, docroot, options) @@ -42,6 +50,10 @@ function Simple.__init__(self, name, docroot, options) self.error404 = options.error404 end +--- Parse a range request. +-- @param request Request object +-- @param size File size +-- @return offset, length, range header or boolean status function Simple.parse_range(self, request, size) if not request.headers.Range then return true @@ -75,6 +87,9 @@ function Simple.parse_range(self, request, size) return from, (1 + to - from), range end +--- Translate path and return file information. +-- @param uri Request URI +-- @return physical file path, file information function Simple.getfile(self, uri) if not self.realdocroot then self.realdocroot = fs.realpath(self.docroot) @@ -86,6 +101,9 @@ function Simple.getfile(self, uri) return file, fs.stat(file) end +--- Handle a GET request. +-- @param request Request object +-- @return status code, header table, response source function Simple.handle_GET(self, request) local file, stat = self:getfile(prot.urldecode(request.env.PATH_INFO, true)) @@ -190,7 +208,7 @@ function Simple.handle_GET(self, request) 'p { margin:0 }' .. '\n</style></head><body><h1>Index of %s/</h1><hr /><ul>'.. '<li><p><a href="%s/../">../</a> ' .. - '<small>(parent directory)</small><br />' .. + '<small>(parent directory)</small><br />' .. '<small></small></li>', duri, duri, ruri ) @@ -244,6 +262,9 @@ function Simple.handle_GET(self, request) end end +--- Handle a HEAD request. +-- @param request Request object +-- @return status code, header table, response source function Simple.handle_HEAD(self, ...) local stat, head = self:handle_GET(...) return stat, head diff --git a/libs/lucid-http/luasrc/lucid/http/handler/luci.lua b/libs/lucid-http/luasrc/lucid/http/handler/luci.lua index c54e39366a..d816aae13b 100644 --- a/libs/lucid-http/luasrc/lucid/http/handler/luci.lua +++ b/libs/lucid-http/luasrc/lucid/http/handler/luci.lua @@ -19,8 +19,15 @@ local srv = require "luci.lucid.http.server" local coroutine = require "coroutine" local type = type +--- LuCI web handler +-- @cstyle instance module "luci.lucid.http.handler.luci" +--- Create a LuCI web handler. +-- @class function +-- @param name Name +-- @param prefix Dispatching prefix +-- @return LuCI web handler object Luci = util.class(srv.Handler) function Luci.__init__(self, name, prefix) @@ -28,15 +35,24 @@ function Luci.__init__(self, name, prefix) self.prefix = prefix end +--- Handle a HEAD request. +-- @param request Request object +-- @return status code, header table, response source function Luci.handle_HEAD(self, ...) local stat, head = self:handle_GET(...) return stat, head end +--- Handle a POST request. +-- @param request Request object +-- @return status code, header table, response source function Luci.handle_POST(self, ...) return self:handle_GET(...) end +--- Handle a GET request. +-- @param request Request object +-- @return status code, header table, response source function Luci.handle_GET(self, request, sourcein) local r = http.Request( request.env, diff --git a/libs/lucid-http/luasrc/lucid/http/server.lua b/libs/lucid-http/luasrc/lucid/http/server.lua index 95484a02f0..25f2535d99 100644 --- a/libs/lucid-http/luasrc/lucid/http/server.lua +++ b/libs/lucid-http/luasrc/lucid/http/server.lua @@ -23,6 +23,8 @@ local proto = require "luci.http.protocol" local table = require "table" local date = require "luci.http.protocol.date" +--- HTTP Daemon +-- @cstyle instance module "luci.lucid.http.server" VERSION = "1.0" @@ -46,7 +48,11 @@ statusmsg = { [503] = "Server Unavailable", } --- File Resource +--- Create a new IO resource response. +-- @class function +-- @param fd File descriptor +-- @param len Length of data +-- @return IO resource IOResource = util.class() function IOResource.__init__(self, fd, len) @@ -54,19 +60,26 @@ function IOResource.__init__(self, fd, len) end --- Server handler implementation +--- Create a server handler. +-- @class function +-- @param name Name +-- @return Handler Handler = util.class() function Handler.__init__(self, name) self.name = name or tostring(self) end --- Creates a failure reply +--- Create a failure reply. +-- @param code HTTP status code +-- @param msg Status message +-- @return status code, header table, response source function Handler.failure(self, code, msg) return code, { ["Content-Type"] = "text/plain" }, ltn12.source.string(msg) end --- Access Restrictions +--- Add an access restriction. +-- @param restriction Restriction specification function Handler.restrict(self, restriction) if not self.restrictions then self.restrictions = {restriction} @@ -75,7 +88,9 @@ function Handler.restrict(self, restriction) end end --- Check restrictions +--- Enforce access restrictions. +-- @param request Request object +-- @return nil or HTTP statuscode, table of headers, response source function Handler.checkrestricted(self, request) if not self.restrictions then return @@ -126,7 +141,10 @@ function Handler.checkrestricted(self, request) }, ltn12.source.string("Unauthorized") end --- Processes a request +--- Process a request. +-- @param request Request object +-- @param sourcein Request data source +-- @return HTTP statuscode, table of headers, response source function Handler.process(self, request, sourcein) local stat, code, hdr, sourceout @@ -153,12 +171,19 @@ function Handler.process(self, request, sourcein) end +--- Create a Virtual Host. +-- @class function +-- @return Virtual Host VHost = util.class() function VHost.__init__(self) self.handlers = {} end +--- Process a request and invoke the appropriate handler. +-- @param request Request object +-- @param ... Additional parameters passed to the handler +-- @return HTTP statuscode, table of headers, response source function VHost.process(self, request, ...) local handler local hlen = -1 @@ -189,15 +214,20 @@ function VHost.process(self, request, ...) end end +--- Get a list of registered handlers. +-- @return Table of handlers function VHost.get_handlers(self) return self.handlers end +--- Register handler with a given URI prefix. +-- @oaram match URI prefix +-- @param handler Handler object function VHost.set_handler(self, match, handler) self.handlers[match] = handler end - +-- Remap IPv6-IPv4-compatibility addresses back to IPv4 addresses. local function remapipv6(adr) local map = "::ffff:" if adr:sub(1, #map) == map then @@ -207,6 +237,7 @@ local function remapipv6(adr) end end +-- Create a source that decodes chunked-encoded data from a socket. local function chunksource(sock, buffer) buffer = buffer or "" return function() @@ -250,6 +281,7 @@ local function chunksource(sock, buffer) end end +-- Create a sink that chunk-encodes data and writes it on a given socket. local function chunksink(sock) return function(chunk, err) if not chunk then @@ -260,20 +292,33 @@ local function chunksink(sock) end end + +--- Create a server object. +-- @class function +-- @return Server object Server = util.class() function Server.__init__(self) self.vhosts = {} end +--- Get a list of registered virtual hosts. +-- @return Table of virtual hosts function Server.get_vhosts(self) return self.vhosts end +--- Register a virtual host with a given name. +-- @param name Hostname +-- @param vhost Virtual host object function Server.set_vhost(self, name, vhost) self.vhosts[name] = vhost end +--- Send a fatal error message to given client and close the connection. +-- @param client Client socket +-- @param code HTTP status code +-- @param msg status message function Server.error(self, client, code, msg) hcode = tostring(code) @@ -304,6 +349,9 @@ local hdr2env = { ["User-Agent"] = "HTTP_USER_AGENT" } +--- Parse the request headers and prepare the environment. +-- @param source line-based input source +-- @return Request object function Server.parse_headers(self, source) local env = {} local req = {env = env, headers = {}} @@ -348,7 +396,9 @@ function Server.parse_headers(self, source) return req end - +--- Handle a new client connection. +-- @param client client socket +-- @param env superserver environment function Server.process(self, client, env) local sourcein = function() end local sourcehdr = client:linesource() |