Mercurial Hosting > luan
comparison http/src/luan/modules/http/Http_test.luan @ 702:87970832a3c3
improve Http_test
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 04 May 2016 20:45:40 -0600 |
parents | ca169567ce07 |
children |
comparison
equal
deleted
inserted
replaced
701:d0280c7fdc3a | 702:87970832a3c3 |
---|---|
1 local Luan = require "luan:Luan.luan" | 1 local Luan = require "luan:Luan.luan" |
2 local error = Luan.error | 2 local error = Luan.error |
3 local set_metatable = Luan.set_metatable or error() | 3 local set_metatable = Luan.set_metatable or error() |
4 local try = Luan.try or error() | |
5 local Package = require "luan:Package.luan" | |
4 local Io = require "luan:Io.luan" | 6 local Io = require "luan:Io.luan" |
5 local String = require "luan:String.luan" | 7 local String = require "luan:String.luan" |
6 local matches = String.matches or error() | 8 local matches = String.matches or error() |
7 local Http = require "luan:http/Http.luan" | 9 local Http = require "luan:http/Http.luan" |
8 | 10 |
11 | 13 |
12 M.welcome_file = "index.html" | 14 M.welcome_file = "index.html" |
13 M.cookie = {} | 15 M.cookie = {} |
14 | 16 |
15 function M.get_page(path) | 17 function M.get_page(path) |
18 Http.request.path = path | |
16 if M.welcome_file ~= nil and matches(path,"/$") then | 19 if M.welcome_file ~= nil and matches(path,"/$") then |
17 path = path .. M.welcome_file | 20 path = path .. M.welcome_file |
18 end | 21 end |
19 local old_out = Io.stdout | 22 local old_out = Io.stdout |
20 local mod = require("site:"..path..".luan") | 23 try { |
21 mod() | 24 function() |
22 M.text_writer.close() | 25 local mod = Package.load("site:"..path..".luan") |
23 Io.stdout = old_out | 26 if mod ~= nil then |
27 mod() | |
28 else | |
29 local not_found = Package.load("site:/not_found.luan") | |
30 not_found or error(path.." not found") | |
31 not_found() | |
32 end | |
33 M.text_writer.close() | |
34 end | |
35 finally = function() | |
36 Io.stdout = old_out | |
37 end | |
38 } | |
24 return M.result.read_text() | 39 return M.result.read_text() |
25 end | 40 end |
26 | 41 |
27 function M.init() | 42 function M.init() |
28 Http.request = Http.new_request{} | 43 Http.request = Http.new_request{} |
33 text_writer = function() | 48 text_writer = function() |
34 Http.sent_headers(Http.response.headers) | 49 Http.sent_headers(Http.response.headers) |
35 M.result = Io.uri "string:" | 50 M.result = Io.uri "string:" |
36 M.text_writer = M.result.text_writer() | 51 M.text_writer = M.result.text_writer() |
37 return M.text_writer | 52 return M.text_writer |
38 end; | 53 end |
39 | 54 |
40 set_cookie = function(name,value) | 55 set_cookie = function(name,value) |
41 M.cookie[name] = value | 56 M.cookie[name] = value |
42 end; | 57 end |
43 | 58 |
44 remove_cookie = function(name) | 59 remove_cookie = function(name) |
45 M.cookie[name] = nil | 60 M.cookie[name] = nil |
46 end; | 61 end |
47 | 62 |
48 send_redirect = function(url) | 63 send_redirect = function(url) |
49 Http.response.redirect = url | 64 Http.response.redirect = url |
50 end; | 65 end |
66 | |
67 send_error = function(code) | |
68 error("sent error "..code) | |
69 end | |
51 | 70 |
52 } | 71 } |
53 | 72 |
54 end | 73 end |
55 | 74 |