Mercurial Hosting > luan
annotate http/src/luan/modules/http/Http_test.luan @ 572:f1601a4ce1aa
fix stack when calling meta-methods
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 12 Jul 2015 21:34:23 -0600 |
parents | cd944b010f25 |
children | 1e69d9c21461 |
rev | line source |
---|---|
572
f1601a4ce1aa
fix stack when calling meta-methods
Franklin Schmidt <fschmidt@gmail.com>
parents:
571
diff
changeset
|
1 local Luan = require "luan:Luan" |
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() |
503 | 4 local Io = require "luan:Io" |
539
473e456444ff
Remove object-oriented primitive methods for string and binary
Franklin Schmidt <fschmidt@gmail.com>
parents:
508
diff
changeset
|
5 local String = require "luan:String" |
572
f1601a4ce1aa
fix stack when calling meta-methods
Franklin Schmidt <fschmidt@gmail.com>
parents:
571
diff
changeset
|
6 local matches = String.matches or error() |
503 | 7 local Http = require "luan:http/Http" |
8 | |
572
f1601a4ce1aa
fix stack when calling meta-methods
Franklin Schmidt <fschmidt@gmail.com>
parents:
571
diff
changeset
|
9 |
503 | 10 local M = {} |
11 | |
12 M.welcome_file = "index.html" | |
508 | 13 M.cookie = {} |
503 | 14 |
15 function M.get_page(path) | |
539
473e456444ff
Remove object-oriented primitive methods for string and binary
Franklin Schmidt <fschmidt@gmail.com>
parents:
508
diff
changeset
|
16 if M.welcome_file ~= nil and matches(path,".*/") then |
503 | 17 path = path .. M.welcome_file |
18 end | |
19 local old_out = Io.stdout | |
20 local mod = require("site:"..path) | |
505
7bc63886d4f2
web page modules now return a function
Franklin Schmidt <fschmidt@gmail.com>
parents:
503
diff
changeset
|
21 mod() |
503 | 22 M.text_writer.close() |
23 Io.stdout = old_out | |
24 return M.result.read_text() | |
25 end | |
26 | |
27 function M.init() | |
28 Http.request = Http.new_request{} | |
508 | 29 Http.request.cookie = M.cookie |
503 | 30 |
31 Http.response = Http.new_response{ | |
32 | |
33 text_writer = function() | |
572
f1601a4ce1aa
fix stack when calling meta-methods
Franklin Schmidt <fschmidt@gmail.com>
parents:
571
diff
changeset
|
34 set_metatable(Http.response.headers,M.sent_error_metatable) |
503 | 35 M.result = Io.uri "string:" |
36 M.text_writer = M.result.text_writer() | |
37 return M.text_writer | |
38 end; | |
39 | |
40 set_cookie = function(name,value) | |
508 | 41 M.cookie[name] = value |
503 | 42 end; |
43 | |
44 remove_cookie = function(name) | |
508 | 45 M.cookie[name] = nil |
503 | 46 end; |
47 | |
48 send_redirect = function(url) | |
49 Http.response.redirect = url | |
50 end; | |
51 | |
52 } | |
53 | |
54 end | |
55 | |
56 return M |