Mercurial Hosting > luan
view http/src/luan/modules/http/Http.luan @ 504:dbdf4b8193a8
add Http.per_session()
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 21 May 2015 01:09:35 -0600 |
parents | 92c3d22745b8 |
children | 9218f9cf45d3 |
line wrap: on
line source
java() local Luan = require "luan:Luan" local ipairs = Luan.ipairs local pairs = Luan.pairs local set_metatable = Luan.set_metatable local Io = require "luan:Io" local Html = require "luan:Html" local url_encode = Html.url_encode local HttpServicer = require "java:luan.modules.http.HttpServicer" local IoLuan = require "java:luan.modules.IoLuan" local M = {} 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 new_common(this) this = this or {} this.headers = {} this.header = {__plural=this.headers} set_metatable(this.header,singular_metatable) return this end function M.new_request(this) this = new_common(this) this.method = "GET" -- default -- this.path -- this.protocol this.scheme = "http" -- default this.parameters = {} this.parameter = {__plural=this.parameters} set_metatable(this.parameter,singular_metatable) this.cookie = {} function this.query_string() local string_uri = Io.uri "string:" local out = string_uri.text_writer() local and_char = "" for name, values in pairs(this.parameters) do for _, value in ipairs(values) do out.write( and_char, url_encode(name), "=", url_encode(value) ) and_char = "&" end end out.close() local s = string_uri.read_text() return s ~= "" and s or nil end function this.url() local url = this.scheme.."://"..this.header.host..this.path if this.method ~= "POST" then local query = this.query_string() if query ~= nil then url = url.."?"..query end end return url end return this end local STATUS = { OK = 200; -- add more as needed } M.STATUS = STATUS function M.new_response(this) this = new_common(this) this.status = STATUS.OK if this.java ~= nil then this.send_redirect = this.java.sendRedirect this.send_error = this.java.sendError function this.set_cookie(name,value,is_persistent,domain) HttpServicer.setCookie(this.java,M.response.java,name,value,is_persistent,domain) end function this.remove_cookie(name,domain) HttpServicer.removeCookie(this.java,M.response.java,name,domain) end function this.text_writer() return IoLuan.textWriter(this.java.getWriter()) end function this.binary_writer() return IoLuan.binaryWriter(this.java.getOutputStream()) end end return this end -- request = new_request{} -- filled in by HttpServicer -- response = new_response{} -- filled in by HttpServicer M.per_session_pages = {} function M.per_session(page) M.per_session_pages[page] = true end return M