diff src/luan/modules/http/Http.luan @ 775:1a68fc55a80c

simplify dir structure
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 26 Aug 2016 14:36:40 -0600
parents http/src/luan/modules/http/Http.luan@d7371dc8c2e7
children 022ff91bde60
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/luan/modules/http/Http.luan	Fri Aug 26 14:36:40 2016 -0600
@@ -0,0 +1,170 @@
+java()
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local ipairs = Luan.ipairs or error()
+local pairs = Luan.pairs or error()
+local set_metatable = Luan.set_metatable or error()
+local Io = require "luan:Io.luan"
+local Html = require "luan:Html.luan"
+local url_encode = Html.url_encode or error()
+local Table = require "luan:Table.luan"
+local clear = Table.clear or error()
+local Package = require "luan:Package.luan"
+local String = require "luan:String.luan"
+local matches = String.matches or error()
+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 sent_error()
+	error "headers are not accessible after you start writing content"
+end
+
+local sent_error_metatable = { __index=sent_error, __new_index=sent_error }
+
+function M.sent_headers(headers)
+	clear(headers)
+	set_metatable(headers,sent_error_metatable)
+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.port = 80  -- 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(M.request.java,this.java,name,value,is_persistent,domain)
+		end
+
+		function this.remove_cookie(name,domain)
+			HttpServicer.removeCookie(M.request.java,this.java,name,domain)
+		end
+
+		function this.set()
+			HttpServicer.setResponse(this,this.java)
+			M.sent_headers(this.headers)
+		end
+
+		function this.text_writer()
+			this.set()
+			return IoLuan.textWriter(this.java.getWriter())
+		end
+
+		function this.binary_writer()
+			this.set()
+			return IoLuan.binaryWriter(this.java.getOutputStream())
+		end
+
+		function this.reset()
+			this.java.reset()
+			set_metatable(this.headers,nil)
+		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
+
+function M.clear_session()
+	M.request.java.getSession().removeAttribute("luan")
+end
+
+
+function M.uncache_site()
+	for k in pairs(Table.copy(Package.loaded)) do
+		if matches(k,"^site:") then
+			Package.loaded[k] = nil
+		end
+	end
+end
+
+M.run_later = HttpServicer.run_later
+
+return M