Mercurial Hosting > luan
changeset 1152:21d157b153fe
change http parameters interface
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 04 Feb 2018 19:25:12 -0700 |
parents | dbb3cb906482 |
children | 1f4da56abd4f |
files | examples/blog/src/edit.luan examples/blog/src/index.html.luan examples/blog/src/lib/test.luan examples/blog/src/new.luan scripts/test.luan src/luan/modules/http/Http.luan src/luan/modules/http/jetty/HttpServicer.java src/luan/modules/http/tools/Shell_mod.luan src/luan/modules/http/tools/run.luan src/luan/modules/lucene/Web_search.luan website/src/examples/hi2.luan website/src/examples/upload-and-email.luan |
diffstat | 12 files changed, 58 insertions(+), 95 deletions(-) [+] |
line wrap: on
line diff
--- a/examples/blog/src/edit.luan Sun Feb 04 18:50:25 2018 -0700 +++ b/examples/blog/src/edit.luan Sun Feb 04 19:25:12 2018 -0700 @@ -8,11 +8,11 @@ return function() - local post_id = to_number(Http.request.parameter.post) or error() + local post_id = to_number(Http.request.parameters.post) or error() local post = Post.get_by_id(post_id) or error() - if Http.request.parameter.save ~= nil then - post.subject = Http.request.parameter.subject - post.content = Http.request.parameter.content + if Http.request.parameters.save ~= nil then + post.subject = Http.request.parameters.subject + post.content = Http.request.parameters.content post.save() Http.response.send_redirect("/#p"..post.id) return
--- a/examples/blog/src/index.html.luan Sun Feb 04 18:50:25 2018 -0700 +++ b/examples/blog/src/index.html.luan Sun Feb 04 19:25:12 2018 -0700 @@ -12,7 +12,7 @@ return function() - local query = Http.request.parameter.query + local query = Http.request.parameters.query Io.stdout = Http.response.text_writer() %>
--- a/examples/blog/src/lib/test.luan Sun Feb 04 18:50:25 2018 -0700 +++ b/examples/blog/src/lib/test.luan Sun Feb 04 19:25:12 2018 -0700 @@ -32,9 +32,9 @@ get_page '/new' init() -Http.request.parameter.subject = 'test' -Http.request.parameter.content = 'this is a test' -Http.request.parameter.save = 'whatever' +Http.request.parameters.subject = 'test' +Http.request.parameters.content = 'this is a test' +Http.request.parameters.save = 'whatever' print '/new submit' get_page '/new' local posts = Post.get_all() @@ -42,15 +42,15 @@ local post_id = to_string(posts[1].id) init() -Http.request.parameter.post = post_id +Http.request.parameters.post = post_id print '/edit' get_page '/edit' init() -Http.request.parameter.post = post_id -Http.request.parameter.subject = 'test' -Http.request.parameter.content = 'this is an edit' -Http.request.parameter.save = 'whatever' +Http.request.parameters.post = post_id +Http.request.parameters.subject = 'test' +Http.request.parameters.content = 'this is an edit' +Http.request.parameters.save = 'whatever' print '/edit submit' get_page '/edit'
--- a/examples/blog/src/new.luan Sun Feb 04 18:50:25 2018 -0700 +++ b/examples/blog/src/new.luan Sun Feb 04 19:25:12 2018 -0700 @@ -6,9 +6,9 @@ return function() - local subject = Http.request.parameter.subject - local content = Http.request.parameter.content - if Http.request.parameter.save ~= nil then + local subject = Http.request.parameters.subject + local content = Http.request.parameters.content + if Http.request.parameters.save ~= nil then local post = Post.new{ subject=subject, content=content } post.save() Http.response.send_redirect("/")
--- a/scripts/test.luan Sun Feb 04 18:50:25 2018 -0700 +++ b/scripts/test.luan Sun Feb 04 19:25:12 2018 -0700 @@ -36,12 +36,12 @@ local page init() -Http.request.parameter.code = "require('luan:Io.luan').print 'hi'" +Http.request.parameters.code = "require('luan:Io.luan').print 'hi'" page = get_page "/run" trim(page) == "hi" or error "failed" init() -Http.request.parameter.cmd = "'ab'..'cd'" +Http.request.parameters.cmd = "'ab'..'cd'" page = get_page "/shell" find(page,"abcd") or error "failed" @@ -82,9 +82,9 @@ web_search() init() -Http.request.parameter.query = "" -Http.request.parameter.rows = "100" -Http.request.parameter.sort = "" +Http.request.parameters.query = "" +Http.request.parameters.rows = "100" +Http.request.parameters.sort = "" web_search() @@ -105,7 +105,7 @@ init(); get_page "/examples/shell" init() -Http.request.parameter.name = "bob" +Http.request.parameters.name = "bob" page = get_page "/examples/hi2" find(page,"bob") or error "failed"
--- a/src/luan/modules/http/Http.luan Sun Feb 04 18:50:25 2018 -0700 +++ b/src/luan/modules/http/Http.luan Sun Feb 04 19:25:12 2018 -0700 @@ -4,6 +4,7 @@ local ipairs = Luan.ipairs or error() local pairs = Luan.pairs or error() local set_metatable = Luan.set_metatable or error() +local type = Luan.type or error() local Io = require "luan:Io.luan" local Html = require "luan:Html.luan" local url_encode = Html.url_encode or error() @@ -16,27 +17,9 @@ local HttpServicer = require(Implementation.java.."HttpServicer") local IoLuan = require "java:luan.modules.IoLuan" + local Http = {} -local singular_metatable = {} - -function singular_metatable.__index(table,key) - local list = table.__plural[key] - return list and list[1] -end - -function singular_metatable.__new_index(table,key,value) - table.__plural[key] = value and {value} -end - -function singular_metatable.__pairs(table) - local iter = pairs(table.__plural) - return function() - local key, value = iter() - return key, value and value[1] - end -end - local function sent_error(_,_,_) error "headers are not accessible after you start writing content" end @@ -55,6 +38,10 @@ return this end +local function to_list(input) + return type(input) == "table" and input or {input} +end + function Http.new_request(this) this = new_common(this) @@ -64,8 +51,6 @@ this.scheme = "http" -- default this.port = 80 -- default this.parameters = {} - this.parameter = {__plural=this.parameters} - set_metatable(this.parameter,singular_metatable) this.cookie = {} function this.query_string() @@ -73,7 +58,7 @@ local out = string_uri.text_writer() local and_char = "" for name, values in pairs(this.parameters) do - for _, value in ipairs(values) do + for _, value in ipairs(to_list(values)) do out.write( and_char, url_encode(name), "=", url_encode(value) ) and_char = "&" end
--- a/src/luan/modules/http/jetty/HttpServicer.java Sun Feb 04 18:50:25 2018 -0700 +++ b/src/luan/modules/http/jetty/HttpServicer.java Sun Feb 04 19:25:12 2018 -0700 @@ -28,7 +28,6 @@ import luan.LuanFunction; import luan.LuanException; import luan.LuanTable; -//import luan.LuanPropertyMeta; import luan.LuanCloner; import luan.modules.PackageLuan; import luan.modules.IoLuan; @@ -102,7 +101,9 @@ String contentType = request.getContentType(); if( contentType==null || !contentType.startsWith("multipart/form-data") ) { for( Map.Entry<String,String[]> entry : request.getParameterMap().entrySet() ) { - parametersTbl.rawPut(entry.getKey(),new LuanTable(Arrays.asList(entry.getValue()))); + String[] a = entry.getValue(); + Object value = a.length==1 ? a[0] : new LuanTable(Arrays.asList(a)); + parametersTbl.rawPut(entry.getKey(),value); } } else { // multipart try { @@ -124,52 +125,29 @@ if( filename == null ) { value = new String(part.getBytes()); } else { -/* - LuanTable partTbl = LuanPropertyMeta.INSTANCE.newTable(); - partTbl.rawPut("filename",filename); - partTbl.rawPut("content_type",part.getContentType()); - LuanPropertyMeta.INSTANCE.getters(partTbl).rawPut( "content", new LuanFunction() { - @Override public Object call(LuanState luan,Object[] args) throws LuanException { - try { - InputStream in = part.getInputStream(); - byte[] content = Utils.readAll(in); - in.close(); - return content; - } catch(IOException e) { - throw new RuntimeException(e); - } - } - } ); -*/ LuanTable partTbl = new LuanTable(); partTbl.rawPut("filename",filename); partTbl.rawPut("content_type",part.getContentType()); - LuanTable mt = new LuanTable(); - partTbl.setMetatable(mt); - mt.rawPut( "__index", new LuanFunction() { - @Override public Object call(LuanState luan,Object[] args) throws LuanException { - Object key = args[1]; - if( "content".equals(key) ) { - try { - InputStream in = part.getInputStream(); - byte[] content = Utils.readAll(in); - in.close(); - return content; - } catch(IOException e) { - throw new RuntimeException(e); - } - } - return null; - } - } ); + { + InputStream inPart = part.getInputStream(); + byte[] content = Utils.readAll(inPart); + inPart.close(); + partTbl.rawPut("content",content); + } value = partTbl; } - LuanTable list = (LuanTable)parametersTbl.rawGet(name); - if( list == null ) { - list = new LuanTable(); + Object obj = parametersTbl.rawGet(name); + if( obj == null ) { + parametersTbl.rawPut(name,value); + } else if( obj instanceof LuanTable && ((LuanTable)obj).isList() ) { + LuanTable list = (LuanTable)obj; + list.rawPut(list.rawLength()+1,value); + } else { + LuanTable list = new LuanTable(); + list.rawPut(1,obj); + list.rawPut(2,value); parametersTbl.rawPut(name,list); } - list.rawPut(parametersTbl.rawLength()+1,value); } } catch(IOException e) { throw new RuntimeException(e);
--- a/src/luan/modules/http/tools/Shell_mod.luan Sun Feb 04 18:50:25 2018 -0700 +++ b/src/luan/modules/http/tools/Shell_mod.luan Sun Feb 04 19:25:12 2018 -0700 @@ -14,12 +14,12 @@ Shell_mod.env = {} function Shell_mod.respond() - if Http.request.parameter.clear ~= nil then + if Http.request.parameters.clear ~= nil then Http.clear_session() Http.response.send_redirect(Http.request.path) -- reload page return else - local cmd = Http.request.parameter.cmd + local cmd = Http.request.parameters.cmd if cmd ~= nil then Io.stdout = {} function Io.stdout.write(...)
--- a/src/luan/modules/http/tools/run.luan Sun Feb 04 18:50:25 2018 -0700 +++ b/src/luan/modules/http/tools/run.luan Sun Feb 04 19:25:12 2018 -0700 @@ -74,12 +74,12 @@ <% end return function() - local content_type = Http.request.parameter.content_type + local content_type = Http.request.parameters.content_type if content_type ~= nil then Http.response.headers["content-type"] = content_type end Io.stdout = Http.response.text_writer() - local code = Http.request.parameter.code + local code = Http.request.parameters.code if code == nil then form() return
--- a/src/luan/modules/lucene/Web_search.luan Sun Feb 04 18:50:25 2018 -0700 +++ b/src/luan/modules/lucene/Web_search.luan Sun Feb 04 19:25:12 2018 -0700 @@ -164,13 +164,13 @@ return function() Io.stdout = Http.response.text_writer() - local query = Http.request.parameter.query + local query = Http.request.parameters.query if query == nil then form() return end - local rows = string_to_number(Http.request.parameter.rows) - local sort = Http.request.parameter.sort + local rows = string_to_number(Http.request.parameters.rows) + local sort = Http.request.parameters.sort local results = index.search(query,1,rows,sort) local headers = {} local table = {}
--- a/website/src/examples/hi2.luan Sun Feb 04 18:50:25 2018 -0700 +++ b/website/src/examples/hi2.luan Sun Feb 04 19:25:12 2018 -0700 @@ -30,7 +30,7 @@ return function() Io.stdout = Http.response.text_writer() - local name = Http.request.parameter.name + local name = Http.request.parameters.name if name == nil then form() else
--- a/website/src/examples/upload-and-email.luan Sun Feb 04 18:50:25 2018 -0700 +++ b/website/src/examples/upload-and-email.luan Sun Feb 04 19:25:12 2018 -0700 @@ -38,11 +38,11 @@ return function() Io.stdout = Http.response.text_writer() - local email = Http.request.parameter.email + local email = Http.request.parameters.email if email == nil then form() else - local file = Http.request.parameter.file + local file = Http.request.parameters.file send{ from = "smtp@luanhost.com"; to = email;