Mercurial Hosting > luan
diff src/luan/modules/http/Http_test.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_test.luan@87970832a3c3 |
children | 9c13a15a4002 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/modules/http/Http_test.luan Fri Aug 26 14:36:40 2016 -0600 @@ -0,0 +1,75 @@ +local Luan = require "luan:Luan.luan" +local error = Luan.error +local set_metatable = Luan.set_metatable or error() +local try = Luan.try or error() +local Package = require "luan:Package.luan" +local Io = require "luan:Io.luan" +local String = require "luan:String.luan" +local matches = String.matches or error() +local Http = require "luan:http/Http.luan" + + +local M = {} + +M.welcome_file = "index.html" +M.cookie = {} + +function M.get_page(path) + Http.request.path = path + if M.welcome_file ~= nil and matches(path,"/$") then + path = path .. M.welcome_file + end + local old_out = Io.stdout + try { + function() + local mod = Package.load("site:"..path..".luan") + if mod ~= nil then + mod() + else + local not_found = Package.load("site:/not_found.luan") + not_found or error(path.." not found") + not_found() + end + M.text_writer.close() + end + finally = function() + Io.stdout = old_out + end + } + return M.result.read_text() +end + +function M.init() + Http.request = Http.new_request{} + Http.request.cookie = M.cookie + + Http.response = Http.new_response{ + + text_writer = function() + Http.sent_headers(Http.response.headers) + M.result = Io.uri "string:" + M.text_writer = M.result.text_writer() + return M.text_writer + end + + set_cookie = function(name,value) + M.cookie[name] = value + end + + remove_cookie = function(name) + M.cookie[name] = nil + end + + send_redirect = function(url) + Http.response.redirect = url + end + + send_error = function(code) + error("sent error "..code) + end + + } + +end + +return M