Mercurial Hosting > luan
diff src/luan/modules/http/Http.luan @ 1152:21d157b153fe
change http parameters interface
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 04 Feb 2018 19:25:12 -0700 |
parents | 0842b9b570f8 |
children | 1f4da56abd4f |
line wrap: on
line diff
--- 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