diff options
author | Taco Hoekwater <taco@elvenkind.com> | 2011-06-01 08:54:21 +0000 |
---|---|---|
committer | Taco Hoekwater <taco@elvenkind.com> | 2011-06-01 08:54:21 +0000 |
commit | d7ccb42582f85acf30568913610ccf4d602023fb (patch) | |
tree | 7292e3545a420676878e7451b68892d360c62cb6 /Master/texmf-dist/scripts/context/lua/mtx-server.lua | |
parent | 2d62a6fe9b80def59c392268022f1f9a2d6e358f (diff) |
commit context 2011.05.18
git-svn-id: svn://tug.org/texlive/trunk@22719 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/scripts/context/lua/mtx-server.lua')
-rw-r--r-- | Master/texmf-dist/scripts/context/lua/mtx-server.lua | 83 |
1 files changed, 45 insertions, 38 deletions
diff --git a/Master/texmf-dist/scripts/context/lua/mtx-server.lua b/Master/texmf-dist/scripts/context/lua/mtx-server.lua index dc0befcaa2f..4547877b5ef 100644 --- a/Master/texmf-dist/scripts/context/lua/mtx-server.lua +++ b/Master/texmf-dist/scripts/context/lua/mtx-server.lua @@ -6,11 +6,28 @@ if not modules then modules = { } end modules ['mtx-server'] = { license = "see context related readme files" } +local helpinfo = [[ +--start start server +--port port to listen to +--root server root +--scripts scripts sub path +--index index file +--auto start on own path +]] + +local application = logs.application { + name = "mtx-server", + banner = "Simple Webserver For Helpers 0.10", + helpinfo = helpinfo, +} + +local report = application.report + scripts = scripts or { } scripts.webserver = scripts.webserver or { } -dofile(resolvers.find_file("l-url.lua","tex")) -dofile(resolvers.find_file("luat-soc.lua","tex")) +dofile(resolvers.findfile("l-url.lua","tex")) +dofile(resolvers.findfile("luat-soc.lua","tex")) local socket = socket or require("socket") -- redundant in future version local format = string.format @@ -127,7 +144,7 @@ local handlers = { } local function errormessage(client,configuration,n) local data = format("<head><title>%s %s</title></head><html><h2>%s %s</h2></html>",n,messages[n],n,messages[n]) - logs.simple("handling error %s: %s",n,messages[n]) + report("handling error %s: %s",n,messages[n]) handlers.generic(client,configuration,data,nil,true) end @@ -136,7 +153,7 @@ local validpaths, registered = { }, { } function scripts.webserver.registerpath(name) if not registered[name] then local cleanname = string.gsub(name,"%.%.","deleted-parent") - logs.simple("registering path '%s'",cleanname) + report("registering path '%s'",cleanname) validpaths[#validpaths+1] = cleanname registered[name] = true end @@ -145,7 +162,7 @@ end function handlers.generic(client,configuration,data,suffix,iscontent) if not iscontent then local name = data - logs.simple("requested file '%s'",name) + report("requested file '%s'",name) local fullname = file.join(configuration.root,name) data = io.loaddata(fullname) or "" if data == "" then @@ -153,12 +170,12 @@ function handlers.generic(client,configuration,data,suffix,iscontent) local fullname = file.join(validpaths[n],name) data = io.loaddata(fullname) or "" if data ~= "" then - logs.simple("sending generic file '%s'",fullname) + report("sending generic file '%s'",fullname) break end end else - logs.simple("sending generic file '%s'",fullname) + report("sending generic file '%s'",fullname) end end if data and data ~= "" then @@ -192,18 +209,18 @@ function handlers.lua(client,configuration,filename,suffix,iscontent,hashed) -- -- todo: split url in components, see l-url; rather trivial local result, keep = loaded[filename], false if result then - logs.simple("reusing script: %s",filename) + report("reusing script: %s",filename) else - logs.simple("locating script: %s",filename) + report("locating script: %s",filename) if lfs.isfile(filename) then - logs.simple("loading script: %s",filename) + report("loading script: %s",filename) result = loadfile(filename) - logs.simple("return type: %s",type(result)) + report("return type: %s",type(result)) if result and type(result) == "function" then -- result() should return a table { [type=,] [length=,] content= }, function or string result, keep = result() if keep then - logs.simple("saving script: %s",type(result)) + report("saving script: %s",type(result)) loaded[filename] = result end end @@ -262,7 +279,7 @@ function scripts.webserver.run(configuration) end end end - configuration.root = dir.expand_name(configuration.root) + configuration.root = dir.expandname(configuration.root) if not configuration.index then for i=1,#indices do local name = indices[i] @@ -274,14 +291,14 @@ function scripts.webserver.run(configuration) configuration.index = configuration.index or "unknown" end if not configuration.scripts or configuration.scripts == "" then - configuration.scripts = dir.expand_name(file.join(configuration.root or ".",configuration.scripts or ".")) + configuration.scripts = dir.expandname(file.join(configuration.root or ".",configuration.scripts or ".")) end -- so far for checks - logs.simple("running at port: %s",configuration.port) - logs.simple("document root: %s",configuration.root or resolvers.ownpath) - logs.simple("main index file: %s",configuration.index) - logs.simple("scripts subpath: %s",configuration.scripts) - logs.simple("context services: http://localhost:%s/mtx-server-ctx-startup.lua",configuration.port) + report("running at port: %s",configuration.port) + report("document root: %s",configuration.root or resolvers.ownpath) + report("main index file: %s",configuration.index) + report("scripts subpath: %s",configuration.scripts) + report("context services: http://localhost:%s/mtx-server-ctx-startup.lua",configuration.port) local server = assert(socket.bind("*", configuration.port)) --~ local reading = { server } while true do -- no multiple clients @@ -295,26 +312,27 @@ function scripts.webserver.run(configuration) errormessage(client,configuration,404) else local from = client:getpeername() - logs.simple("request from: %s",tostring(from)) + report("request from: %s",tostring(from)) local fullurl = request:match("GET (.+) HTTP/.*$") -- todo: more clever fullurl = socket.url.unescape(fullurl) local hashed = url.hashed(fullurl) local query = url.query(hashed.query) local filename = hashed.path +--~ table.print(hashed) if filename then filename = socket.url.unescape(filename) - logs.simple("requested action: %s",filename) + report("requested action: %s",filename) if filename:find("%.%.") then filename = nil -- invalid path end if filename == nil or filename == "" or filename == "/" then filename = configuration.index - logs.simple("invalid filename, forcing: %s",filename) + report("invalid filename, forcing: %s",filename) end local suffix = file.extname(filename) local action = handlers[suffix] or handlers.generic if action then - logs.simple("performing action: %s",filename) + report("performing action: %s",filename) action(client,configuration,filename,suffix,false,hashed) -- filename and no content else errormessage(client,configuration,404) @@ -324,23 +342,12 @@ function scripts.webserver.run(configuration) end end client:close() - logs.simple("time spent with client: %0.03f seconds",os.clock()-start) + report("time spent with client: %0.03f seconds",os.clock()-start) end end -logs.extendbanner("Simple Webserver For Helpers 0.10") - -messages.help = [[ ---start start server ---port port to listen to ---root server root ---scripts scripts sub path ---index index file ---auto start on own path -]] - if environment.argument("auto") then - local path = resolvers.find_file("mtx-server.lua") or "." + local path = resolvers.findfile("mtx-server.lua") or "." scripts.webserver.run { port = environment.argument("port"), root = environment.argument("root") or file.dirname(path) or ".", @@ -354,8 +361,8 @@ elseif environment.argument("start") then scripts = environment.argument("scripts"), } else - logs.help(messages.help) + application.help() end --- mtxrun --script server --start => http://localhost:8080/mtx-server-ctx-help.lua +-- mtxrun --script server --start => http://localhost:31415/mtx-server-ctx-startup.lua |