Mercurial Hosting > luan
comparison 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 |
comparison
equal
deleted
inserted
replaced
774:3e30cf310e56 | 775:1a68fc55a80c |
---|---|
1 local Luan = require "luan:Luan.luan" | |
2 local error = Luan.error | |
3 local set_metatable = Luan.set_metatable or error() | |
4 local try = Luan.try or error() | |
5 local Package = require "luan:Package.luan" | |
6 local Io = require "luan:Io.luan" | |
7 local String = require "luan:String.luan" | |
8 local matches = String.matches or error() | |
9 local Http = require "luan:http/Http.luan" | |
10 | |
11 | |
12 local M = {} | |
13 | |
14 M.welcome_file = "index.html" | |
15 M.cookie = {} | |
16 | |
17 function M.get_page(path) | |
18 Http.request.path = path | |
19 if M.welcome_file ~= nil and matches(path,"/$") then | |
20 path = path .. M.welcome_file | |
21 end | |
22 local old_out = Io.stdout | |
23 try { | |
24 function() | |
25 local mod = Package.load("site:"..path..".luan") | |
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 } | |
39 return M.result.read_text() | |
40 end | |
41 | |
42 function M.init() | |
43 Http.request = Http.new_request{} | |
44 Http.request.cookie = M.cookie | |
45 | |
46 Http.response = Http.new_response{ | |
47 | |
48 text_writer = function() | |
49 Http.sent_headers(Http.response.headers) | |
50 M.result = Io.uri "string:" | |
51 M.text_writer = M.result.text_writer() | |
52 return M.text_writer | |
53 end | |
54 | |
55 set_cookie = function(name,value) | |
56 M.cookie[name] = value | |
57 end | |
58 | |
59 remove_cookie = function(name) | |
60 M.cookie[name] = nil | |
61 end | |
62 | |
63 send_redirect = function(url) | |
64 Http.response.redirect = url | |
65 end | |
66 | |
67 send_error = function(code) | |
68 error("sent error "..code) | |
69 end | |
70 | |
71 } | |
72 | |
73 end | |
74 | |
75 return M |