Mercurial Hosting > luan
annotate 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 |
rev | line source |
---|---|
693
ca169567ce07
module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
629
diff
changeset
|
1 local Luan = require "luan:Luan.luan" |
572
f1601a4ce1aa
fix stack when calling meta-methods
Franklin Schmidt <fschmidt@gmail.com>
parents:
571
diff
changeset
|
2 local error = Luan.error |
f1601a4ce1aa
fix stack when calling meta-methods
Franklin Schmidt <fschmidt@gmail.com>
parents:
571
diff
changeset
|
3 local set_metatable = Luan.set_metatable or error() |
702 | 4 local try = Luan.try or error() |
5 local Package = require "luan:Package.luan" | |
693
ca169567ce07
module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
629
diff
changeset
|
6 local Io = require "luan:Io.luan" |
ca169567ce07
module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
629
diff
changeset
|
7 local String = require "luan:String.luan" |
572
f1601a4ce1aa
fix stack when calling meta-methods
Franklin Schmidt <fschmidt@gmail.com>
parents:
571
diff
changeset
|
8 local matches = String.matches or error() |
693
ca169567ce07
module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
629
diff
changeset
|
9 local Http = require "luan:http/Http.luan" |
503 | 10 |
572
f1601a4ce1aa
fix stack when calling meta-methods
Franklin Schmidt <fschmidt@gmail.com>
parents:
571
diff
changeset
|
11 |
503 | 12 local M = {} |
13 | |
14 M.welcome_file = "index.html" | |
508 | 15 M.cookie = {} |
503 | 16 |
17 function M.get_page(path) | |
702 | 18 Http.request.path = path |
629
35dde32c02ab
change String.matches()
Franklin Schmidt <fschmidt@gmail.com>
parents:
580
diff
changeset
|
19 if M.welcome_file ~= nil and matches(path,"/$") then |
503 | 20 path = path .. M.welcome_file |
21 end | |
22 local old_out = Io.stdout | |
702 | 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 } | |
503 | 39 return M.result.read_text() |
40 end | |
41 | |
42 function M.init() | |
43 Http.request = Http.new_request{} | |
508 | 44 Http.request.cookie = M.cookie |
503 | 45 |
46 Http.response = Http.new_response{ | |
47 | |
48 text_writer = function() | |
580 | 49 Http.sent_headers(Http.response.headers) |
503 | 50 M.result = Io.uri "string:" |
51 M.text_writer = M.result.text_writer() | |
52 return M.text_writer | |
702 | 53 end |
503 | 54 |
55 set_cookie = function(name,value) | |
508 | 56 M.cookie[name] = value |
702 | 57 end |
503 | 58 |
59 remove_cookie = function(name) | |
508 | 60 M.cookie[name] = nil |
702 | 61 end |
503 | 62 |
63 send_redirect = function(url) | |
64 Http.response.redirect = url | |
702 | 65 end |
66 | |
67 send_error = function(code) | |
68 error("sent error "..code) | |
69 end | |
503 | 70 |
71 } | |
72 | |
73 end | |
74 | |
75 return M |