view src/luan/modules/http/impl/Http.luan @ 1162:e2d2354807f3

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 06 Feb 2018 21:25:55 -0700
parents 6baccd0c85a7
children fef8f0742da9
line wrap: on
line source

java()
local Luan = require "luan:Luan.luan"
local error = Luan.error
local ipairs = Luan.ipairs or error()
local pairs = Luan.pairs 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()
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.impl.HttpServicer"
local IoLuan = require "java:luan.modules.IoLuan"
local LuanJava = require "java:luan.Luan"
local ResponseOutputStream = require "java:luan.webserver.ResponseOutputStream"
local OutputStreamWriter = require "java:java.io.OutputStreamWriter"


local Http = {}

function Http.new_request(java)
	local this = {}
	Http.request = this
	if java == nil then
		this.method = "GET"
		this.headers = {}
		this.parameters = {}
		this.cookies = {}
	else
		this.java = java
		this.method = java.method or error()
		this.raw_path = java.rawPath or error()
		this.path = java.path or error()
		this.protocol = java.protocol or error()
		this.headers = LuanJava.toLuan(java.headers)
		this.parameters = LuanJava.toLuan(java.parameters)
		this.cookies = LuanJava.toLuan(java.cookies)
	end
	this.scheme = "http"

	function this.full_path()  -- compatible with jetty
		return this.raw_path or this.path
	end

	function this.url()
		return this.scheme.."://"..this.headers["host"]..this.raw_path
	end

	return this
end

local STATUS = {
	OK = 200
	MOVED_PERMANENTLY = 301
	FOUND = 302
	-- add more as needed
}
Http.STATUS = STATUS

function Http.new_response(java)
	java or error()
	local this = {}
	Http.response = this
	this.java = java

	this.headers = {}

	this.status = STATUS.OK

	function this.send_redirect(location)
		this.status = STATUS.FOUND
		this.headers["location"] = location
	end

	function this.send_error(status,msg)
		this.status = status
		if msg ~= nil then
			this.headers["content-type"] = "text/plain"
			local writer = this.text_writer()
			writer.write(msg)
		end
	end

	function this.set_cookie(name,value,attributes)
		HttpServicer.setCookie(this.java,name,value,attributes)
	end

	function this.set_persistent_cookie(name,value,attributes)
		attributes = attributes or {}
		attributes["Max-Age"] = "10000000"
		this.set_cookie(name,value,attributes)
	end

	function this.remove_cookie(name,attributes)
		attributes = attributes or {}
		attributes["Max-Age"] = "0"
		this.set_cookie(name,"delete",attributes)
	end

	function this.text_writer()
		this.writer = ResponseOutputStream.new(this.java)
		this.writer = OutputStreamWriter.new(this.writer)
		return IoLuan.textWriter(this.writer)
	end

	function this.binary_writer()
		this.writer = ResponseOutputStream.new(this.java)
		return IoLuan.binaryWriter(this.writer)
	end

	return this
end


function Http.uncache_site()
	for k in pairs(Table.copy(Package.loaded)) do
		if matches(k,"^site:") then
			Package.loaded[k] = nil
		end
	end
end

return Http