diff options
Diffstat (limited to 'Master/texmf-dist/scripts/context/lua/mtx-server.lua')
-rw-r--r-- | Master/texmf-dist/scripts/context/lua/mtx-server.lua | 82 |
1 files changed, 55 insertions, 27 deletions
diff --git a/Master/texmf-dist/scripts/context/lua/mtx-server.lua b/Master/texmf-dist/scripts/context/lua/mtx-server.lua index 068d5111130..5ec15de708f 100644 --- a/Master/texmf-dist/scripts/context/lua/mtx-server.lua +++ b/Master/texmf-dist/scripts/context/lua/mtx-server.lua @@ -7,12 +7,26 @@ if not modules then modules = { } end modules ['mtx-server'] = { } 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 +<?xml version="1.0"?> +<application> + <metadata> + <entry name="name">mtx-server</entry> + <entry name="detail">Simple Webserver For Helpers</entry> + <entry name="version">0.10</entry> + </metadata> + <flags> + <category name="basic"> + <subcategory> + <flag name="start"><short>start server</short></flag> + <flag name="port"><short>port to listen to</short></flag> + <flag name="root"><short>server root</short></flag> + <flag name="scripts"><short>scripts sub path</short></flag> + <flag name="index"><short>index file</short></flag> + <flag name="auto"><short>start on own path</short></flag> + </subcategory> + </category> + </flags> +</application> ]] local application = logs.application { @@ -26,11 +40,10 @@ local report = application.report scripts = scripts or { } scripts.webserver = scripts.webserver or { } -dofile(resolvers.findfile("l-url.lua","tex")) dofile(resolvers.findfile("luat-soc.lua","tex")) local socket = socket or require("socket") -local http = socket or require("socket.http") +local http = http or require("socket.http") -- not needed local format = string.format -- The following two lists are taken from webrick (ruby) and @@ -192,13 +205,13 @@ function handlers.generic(client,configuration,data,suffix,iscontent) end end ---~ return os.date() +-- return os.date() ---~ return { content = "crap" } +-- return { content = "crap" } ---~ return function(configuration,filename) ---~ return { content = filename } ---~ end +-- return function(configuration,filename) +-- return { content = filename } +-- end local loaded = { } @@ -226,11 +239,13 @@ function handlers.lua(client,configuration,filename,suffix,iscontent,hashed) -- end end else + report("problematic script: %s",filename) errormessage(client,configuration,404) end end if result then if type(result) == "function" then + report("running script: %s",filename) result = result(configuration,filename,hashed) -- second argument will become query end if result and type(result) == "string" then @@ -242,7 +257,7 @@ function handlers.lua(client,configuration,filename,suffix,iscontent,hashed) -- local action = handlers[suffix] or handlers.generic action(client,configuration,result.content,suffix,true) -- content elseif result.filename then - local suffix = file.extname(result.filename) or "text/html" + local suffix = file.suffix(result.filename) or "text/html" local action = handlers[suffix] or handlers.generic action(client,configuration,result.filename,suffix,false) -- filename else @@ -301,40 +316,50 @@ function scripts.webserver.run(configuration) 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 + local script = configuration.script + while true do -- blocking local start = os.clock() --- local input = socket.select(reading) --- local client = input:accept() local client = server:accept() client:settimeout(configuration.timeout or 60) local request, e = client:receive() --- local request, e = client:receive("*a") -- doesn't work well (so no post) if e then errormessage(client,configuration,404) else local from = client:getpeername() report("request from: %s",tostring(from)) - local fullurl = request:match("GET (.+) HTTP/.*$") or "" -- todo: more clever / post + report("request data: %s",tostring(request)) + local fullurl = string.match(request,"GET (.+) HTTP/.*$") or "" -- todo: more clever / post if fullurl == "" then + report("no url") errormessage(client,configuration,404) else - fullurl = socket.url.unescape(fullurl) + report("requested url: %s",fullurl) + fullurl = socket.url.unescape(fullurl) -- still needed? local hashed = url.hashed(fullurl) local query = url.query(hashed.query) - local filename = hashed.path --- table.print(hashed) - if filename then + local filename = hashed.path -- hm, not query? + if script then + filename = script + report("forced script: %s",filename) + local suffix = file.suffix(filename) + local action = handlers[suffix] or handlers.generic + if action then + report("performing action: %s",filename) + action(client,configuration,filename,suffix,false,hashed) -- filename and no content + else + errormessage(client,configuration,404) + end + elseif filename then filename = socket.url.unescape(filename) report("requested action: %s",filename) - if filename:find("%.%.") then + if string.find(filename,"%.%.") then filename = nil -- invalid path end if filename == nil or filename == "" or filename == "/" then filename = configuration.index report("invalid filename, forcing: %s",filename) end - local suffix = file.extname(filename) + local suffix = file.suffix(filename) local action = handlers[suffix] or handlers.generic if action then report("performing action: %s",filename) @@ -358,6 +383,7 @@ if environment.argument("auto") then port = environment.argument("port"), root = environment.argument("root") or file.dirname(path) or ".", scripts = environment.argument("scripts") or file.dirname(path) or ".", + script = environment.argument("script"), } elseif environment.argument("start") then scripts.webserver.run { @@ -365,10 +391,12 @@ elseif environment.argument("start") then root = environment.argument("root") or ".", -- "e:/websites/www.pragma-ade.com", index = environment.argument("index"), scripts = environment.argument("scripts"), + script = environment.argument("script"), } +elseif environment.argument("exporthelp") then + application.export(environment.argument("exporthelp"),environment.files[1]) else application.help() end - -- mtxrun --script server --start => http://localhost:31415/mtx-server-ctx-startup.lua |