view http/src/luan/modules/http/Http.luan @ 508:9218f9cf45d3

various fixes
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 21 May 2015 20:20:54 -0600
parents dbdf4b8193a8
children e3b0846dc2ef
line wrap: on
line source

java()
local Luan = require "luan:Luan"
local ipairs = Luan.ipairs
local pairs = Luan.pairs
local error = Luan.error
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] or error("invalid value "..list.." for "..key))
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(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.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