Mercurial Hosting > luan
changeset 789:e2eb55d86bb2
add http/tools
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 04 Sep 2016 20:34:55 -0600 |
parents | c9409e28daa2 |
children | 4cddfc06a34f |
files | examples/blog/src/private/tools/backup.luan examples/blog/src/private/tools/run.luan examples/blog/src/private/tools/shell.luan examples/blog/src/private/tools/test.luan src/luan/modules/http/Dump_mod.luan src/luan/modules/http/Shell_mod.luan src/luan/modules/http/dump.luan src/luan/modules/http/run.luan src/luan/modules/http/shell.luan src/luan/modules/http/tools/Dump_mod.luan src/luan/modules/http/tools/Shell_mod.luan src/luan/modules/http/tools/dump.luan src/luan/modules/http/tools/run.luan src/luan/modules/http/tools/shell.luan |
diffstat | 14 files changed, 270 insertions(+), 270 deletions(-) [+] |
line wrap: on
line diff
--- a/examples/blog/src/private/tools/backup.luan Sun Sep 04 20:11:11 2016 -0600 +++ b/examples/blog/src/private/tools/backup.luan Sun Sep 04 20:34:55 2016 -0600 @@ -6,7 +6,7 @@ return function() local backup = Io.uri "site:/private/local/backup.zip" backup.delete() - Db.backup(backup) + Db.zip(backup) Io.stdout = Http.response.text_writer() %>
--- a/examples/blog/src/private/tools/run.luan Sun Sep 04 20:11:11 2016 -0600 +++ b/examples/blog/src/private/tools/run.luan Sun Sep 04 20:34:55 2016 -0600 @@ -1,1 +1,1 @@ -return require "luan:http/run.luan" +return require "luan:http/tools/run.luan"
--- a/examples/blog/src/private/tools/shell.luan Sun Sep 04 20:11:11 2016 -0600 +++ b/examples/blog/src/private/tools/shell.luan Sun Sep 04 20:34:55 2016 -0600 @@ -1,1 +1,1 @@ -return require "luan:http/shell.luan" +return require "luan:http/tools/shell.luan"
--- a/examples/blog/src/private/tools/test.luan Sun Sep 04 20:11:11 2016 -0600 +++ b/examples/blog/src/private/tools/test.luan Sun Sep 04 20:34:55 2016 -0600 @@ -18,6 +18,6 @@ print("error:",e) end; } - local Db = require "site:/lib/Db" + local Db = require "site:/lib/Db.luan" Db.close() end
--- a/src/luan/modules/http/Dump_mod.luan Sun Sep 04 20:11:11 2016 -0600 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,50 +0,0 @@ -local Luan = require "luan:Luan.luan" -local pairs = Luan.pairs -local ipairs = Luan.ipairs -local Io = require "luan:Io.luan" -local Http = require "luan:http/Http.luan" -java() -local HttpServicer = require "java:luan.modules.http.HttpServicer" - -local M = {} - -local to_http_header_name = HttpServicer.toHttpHeaderName -M.to_http_header_name = to_http_header_name - -function M.respond() - Http.response.header.content_type = "text/plain" - Io.stdout = Http.response.text_writer() - - local method = Http.request.method - local path = Http.request.path - local query = Http.request.query_string() - if method ~= "POST" and query ~= nil then - path = path.."?"..query - end -%> -<%=method%> <%=path%> <%=Http.request.protocol%> -<% - M.dump_headers(Http.request.headers) -%> - -<% - if method == "POST" and query ~= nil then -%> -<%=query%> -<% - end -end - - -function M.dump_headers(headers) - for name, values in pairs(headers) do - local header_name = to_http_header_name(name) - for _, value in ipairs(values) do -%> -<%=header_name%>: <%=value%> -<% - end - end -end - -return M
--- a/src/luan/modules/http/Shell_mod.luan Sun Sep 04 20:11:11 2016 -0600 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,108 +0,0 @@ -local Luan = require "luan:Luan.luan" -local error = Luan.error -local ipairs = Luan.ipairs or error() -local load = Luan.load or error() -local try = Luan.try or error() -local Io = require "luan:Io.luan" -local print = Io.print or error() -local Http = require "luan:http/Http.luan" - - -local M = {} - -local history = {} -M.env = {} - -function M.respond() - if Http.request.parameter.clear ~= nil then - Http.clear_session() - Http.response.send_redirect(Http.request.path) -- reload page - return - else - local cmd = Http.request.parameter.cmd - if cmd ~= nil then - Io.stdout = {} - function Io.stdout.write(...) - for v in Luan.values(...) do - history[#history+1] = v - end - end - print( "% "..cmd ) - try { - function() - local line - try { - function() - line = load("return "..cmd,"<web_shell>",M.env) - end - catch = function(e) - line = load(cmd,"<web_shell>",M.env) - end - } - print( line() ) - end - catch = function(e) - Io.print_to(Io.stderr,e) - print(e) - end - } - end - end - - Io.stdout = Http.response.text_writer() -%> -<html> - <head> - <title>Luan Shell</title> - <style> - body { - font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; - margin: 2em 5% 0 5%; - } - pre { - font: inherit; - } - input[type="text"] { - font: inherit; - padding: .5em .8em; - border-radius: 8px; - border-style: groove; - } - input[type="text"]:focus { - border-color: #66afe9; - outline: none; - } - input[type="submit"] { - color: white; - background: #337ab7; - border-color: #337ab7; - font: inherit; - padding: .5em; - border-radius: 4px; - } - input[type="submit"]:hover { - background: #236aa7 !important; - } - </style> - </head> - <body> - <h2>Luan Shell</h2> - <p>This is a command shell. Enter commands below.</p> - <pre><% - for _,v in ipairs(history) do - Io.stdout.write(v) - end - %></pre> - <form name='form0' method='post'> - % <input type="text" name='cmd' size="80" autofocus> - <input type="submit" value="run"> - <input type="submit" name="clear" value="clear"> - </form> - </body> -</html> -<% -end - -Http.per_session(M.respond) - -return M
--- a/src/luan/modules/http/dump.luan Sun Sep 04 20:11:11 2016 -0600 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -return require("luan:http/Dump_mod.luan").respond
--- a/src/luan/modules/http/run.luan Sun Sep 04 20:11:11 2016 -0600 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,106 +0,0 @@ -local Luan = require "luan:Luan.luan" -local error = Luan.error -local load = Luan.load or error() -local try = Luan.try or error() -local Io = require "luan:Io.luan" -local print = Io.print or error() -local String = require "luan:String.luan" -local gmatch = String.gmatch or error() -local Http = require "luan:http/Http.luan" - - -local function lines(s) - local matcher = gmatch(s,"([^\n]*)\n|([^\n])+$") - return function() - local m1, m2 = matcher() - return m1 or m2 - end -end - -local function print_with_line_numbers(s) - local i = 1 - for line in lines(s) do - print(i,line) - i = i + 1 - end -end - -local function form() %> -<html> - <head> - <title>Run Luan Code</title> - <style> - body { - font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; - text-align: center; - margin-top: 1em; - } - h2 { - margin-bottom: .3em; - font-weight: normal; - } - textarea { - font: inherit; - border-radius: 4px; - padding: .5em .8em; - } - input[type="submit"] { - margin-top: .3em; - color: white; - background: #337ab7; - border-color: #337ab7; - font: inherit; - padding: .5em; - border-radius: 4px; - } - input[type="submit"]:hover { - background: #236aa7 !important; - } - </style> - </head> - <body> - <h2>Run Luan Code</h2> - <form name="form0" method="post"> - <input type="hidden" name="content_type" value="text/plain" /> - <div> - <textarea name="code" rows="20" cols="90" wrap="off" autofocus></textarea> - </div> - <div> - <input type="submit" value="Execute Luan Code"/> - </div> - </form> - </body> -</html> -<% end - -return function() - local content_type = Http.request.parameter.content_type - if content_type ~= nil then - Http.response.header.content_type = content_type - end - Io.stdout = Http.response.text_writer() - local code = Http.request.parameter.code - if code == nil then - form() - return - end - local env = { - request = Http.request; - response = Http.response; - } - try { - function() - local run = load(code,"<web_run>",env) - run() - end; - catch = function(e) - Http.response.reset() - Http.response.header.content_type = "text/plain" - Io.stdout = Http.response.text_writer() - print(e) - print"" - print"" - print_with_line_numbers(code) - end; - } -end
--- a/src/luan/modules/http/shell.luan Sun Sep 04 20:11:11 2016 -0600 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -return require("luan:http/Shell_mod.luan").respond
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/modules/http/tools/Dump_mod.luan Sun Sep 04 20:34:55 2016 -0600 @@ -0,0 +1,50 @@ +local Luan = require "luan:Luan.luan" +local pairs = Luan.pairs +local ipairs = Luan.ipairs +local Io = require "luan:Io.luan" +local Http = require "luan:http/Http.luan" +java() +local HttpServicer = require "java:luan.modules.http.HttpServicer" + +local M = {} + +local to_http_header_name = HttpServicer.toHttpHeaderName +M.to_http_header_name = to_http_header_name + +function M.respond() + Http.response.header.content_type = "text/plain" + Io.stdout = Http.response.text_writer() + + local method = Http.request.method + local path = Http.request.path + local query = Http.request.query_string() + if method ~= "POST" and query ~= nil then + path = path.."?"..query + end +%> +<%=method%> <%=path%> <%=Http.request.protocol%> +<% + M.dump_headers(Http.request.headers) +%> + +<% + if method == "POST" and query ~= nil then +%> +<%=query%> +<% + end +end + + +function M.dump_headers(headers) + for name, values in pairs(headers) do + local header_name = to_http_header_name(name) + for _, value in ipairs(values) do +%> +<%=header_name%>: <%=value%> +<% + end + end +end + +return M
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/modules/http/tools/Shell_mod.luan Sun Sep 04 20:34:55 2016 -0600 @@ -0,0 +1,108 @@ +local Luan = require "luan:Luan.luan" +local error = Luan.error +local ipairs = Luan.ipairs or error() +local load = Luan.load or error() +local try = Luan.try or error() +local Io = require "luan:Io.luan" +local print = Io.print or error() +local Http = require "luan:http/Http.luan" + + +local M = {} + +local history = {} +M.env = {} + +function M.respond() + if Http.request.parameter.clear ~= nil then + Http.clear_session() + Http.response.send_redirect(Http.request.path) -- reload page + return + else + local cmd = Http.request.parameter.cmd + if cmd ~= nil then + Io.stdout = {} + function Io.stdout.write(...) + for v in Luan.values(...) do + history[#history+1] = v + end + end + print( "% "..cmd ) + try { + function() + local line + try { + function() + line = load("return "..cmd,"<web_shell>",M.env) + end + catch = function(e) + line = load(cmd,"<web_shell>",M.env) + end + } + print( line() ) + end + catch = function(e) + Io.print_to(Io.stderr,e) + print(e) + end + } + end + end + + Io.stdout = Http.response.text_writer() +%> +<html> + <head> + <title>Luan Shell</title> + <style> + body { + font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; + margin: 2em 5% 0 5%; + } + pre { + font: inherit; + } + input[type="text"] { + font: inherit; + padding: .5em .8em; + border-radius: 8px; + border-style: groove; + } + input[type="text"]:focus { + border-color: #66afe9; + outline: none; + } + input[type="submit"] { + color: white; + background: #337ab7; + border-color: #337ab7; + font: inherit; + padding: .5em; + border-radius: 4px; + } + input[type="submit"]:hover { + background: #236aa7 !important; + } + </style> + </head> + <body> + <h2>Luan Shell</h2> + <p>This is a command shell. Enter commands below.</p> + <pre><% + for _,v in ipairs(history) do + Io.stdout.write(v) + end + %></pre> + <form name='form0' method='post'> + % <input type="text" name='cmd' size="80" autofocus> + <input type="submit" value="run"> + <input type="submit" name="clear" value="clear"> + </form> + </body> +</html> +<% +end + +Http.per_session(M.respond) + +return M
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/modules/http/tools/dump.luan Sun Sep 04 20:34:55 2016 -0600 @@ -0,0 +1,1 @@ +return require("luan:http/tools/Dump_mod.luan").respond
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/modules/http/tools/run.luan Sun Sep 04 20:34:55 2016 -0600 @@ -0,0 +1,106 @@ +local Luan = require "luan:Luan.luan" +local error = Luan.error +local load = Luan.load or error() +local try = Luan.try or error() +local Io = require "luan:Io.luan" +local print = Io.print or error() +local String = require "luan:String.luan" +local gmatch = String.gmatch or error() +local Http = require "luan:http/Http.luan" + + +local function lines(s) + local matcher = gmatch(s,"([^\n]*)\n|([^\n])+$") + return function() + local m1, m2 = matcher() + return m1 or m2 + end +end + +local function print_with_line_numbers(s) + local i = 1 + for line in lines(s) do + print(i,line) + i = i + 1 + end +end + +local function form() %> +<html> + <head> + <title>Run Luan Code</title> + <style> + body { + font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; + text-align: center; + margin-top: 1em; + } + h2 { + margin-bottom: .3em; + font-weight: normal; + } + textarea { + font: inherit; + border-radius: 4px; + padding: .5em .8em; + } + input[type="submit"] { + margin-top: .3em; + color: white; + background: #337ab7; + border-color: #337ab7; + font: inherit; + padding: .5em; + border-radius: 4px; + } + input[type="submit"]:hover { + background: #236aa7 !important; + } + </style> + </head> + <body> + <h2>Run Luan Code</h2> + <form name="form0" method="post"> + <input type="hidden" name="content_type" value="text/plain" /> + <div> + <textarea name="code" rows="20" cols="90" wrap="off" autofocus></textarea> + </div> + <div> + <input type="submit" value="Execute Luan Code"/> + </div> + </form> + </body> +</html> +<% end + +return function() + local content_type = Http.request.parameter.content_type + if content_type ~= nil then + Http.response.header.content_type = content_type + end + Io.stdout = Http.response.text_writer() + local code = Http.request.parameter.code + if code == nil then + form() + return + end + local env = { + request = Http.request; + response = Http.response; + } + try { + function() + local run = load(code,"<web_run>",env) + run() + end; + catch = function(e) + Http.response.reset() + Http.response.header.content_type = "text/plain" + Io.stdout = Http.response.text_writer() + print(e) + print"" + print"" + print_with_line_numbers(code) + end; + } +end