annotate http/src/luan/modules/http/Http.luan @ 497:55f9f74f1e55

Http.request is now pure luan
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 17 May 2015 19:25:47 -0600
parents 598123096772
children ee55be414a34
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
497
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
1 local Luan = require "luan:Luan"
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
2 local ipairs = Luan.ipairs
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
3 local pairs = Luan.pairs
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
4 local set_metatable = Luan.set_metatable
458
507c0af598ba improve Http.init_for_test
Franklin Schmidt <fschmidt@gmail.com>
parents: 377
diff changeset
5 local Io = require "luan:Io"
497
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
6 local Html = require "luan:Html"
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
7 local url_encode = Html.url_encode
320
fed1893821bf remove global namespace
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 300
diff changeset
8
fed1893821bf remove global namespace
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 300
diff changeset
9
497
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
10 local singular_metatable = {}
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
11
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
12 function singular_metatable.__index(table,key)
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
13 local list = table.__plural[key]
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
14 return list and list[1]
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
15 end
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
16
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
17 function singular_metatable.__new_index(table,key,value)
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
18 table.__plural[key] = {value}
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
19 end
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
20
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
21 function singular_metatable.__pairs(table)
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
22 local iter = pairs(table.__plural)
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
23 return function()
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
24 local key, value = iter()
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
25 return key, value and value[1]
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
26 end
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
27 end
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
28
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
29
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
30 local function new_common(this)
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
31 this = this or {}
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
32 this.headers = {}
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
33 this.header = {__plural=this.headers}
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
34 set_metatable(this.header,singular_metatable)
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
35 return this
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
36 end
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
37
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
38
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
39 function new_request(this)
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
40 this = new_common(this)
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
41 this.method = "GET" -- default
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
42 -- this.path
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
43 -- this.protocol
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
44 this.scheme = "http" -- default
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
45 this.parameters = {}
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
46 this.parameter = {__plural=this.parameters}
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
47 set_metatable(this.parameter,singular_metatable)
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
48 this.cookie = {}
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
49
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
50 function this.url()
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
51 local string_uri = Io.uri "string:"
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
52 local out = string_uri.text_writer()
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
53
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
54 out.write( this.scheme, "://", this.header.host, this.path )
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
55 if this.method ~= "POST" then
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
56 local and_char = "?"
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
57 for name, values in pairs(this.parameters) do
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
58 for _, value in ipairs(values) do
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
59 out.write( and_char, url_encode(name), "=", url_encode(value) )
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
60 and_char = "&"
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
61 end
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
62 end
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
63 end
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
64
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
65 out.close()
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
66 return string_uri.read_text()
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
67 end
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
68
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
69 return this
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
70 end
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
71
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
72 -- request = new_request{} -- filled in by HttpServicer
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
73
251
705d14f4d8ee start web testing
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
74
705d14f4d8ee start web testing
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
75
705d14f4d8ee start web testing
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
76 function init_for_test()
705d14f4d8ee start web testing
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
77
497
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
78 test = test or {}
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
79
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
80 test.welcome_file = "index.html"
463
895afcd2b281 improve Http.init_for_test
Franklin Schmidt <fschmidt@gmail.com>
parents: 458
diff changeset
81
895afcd2b281 improve Http.init_for_test
Franklin Schmidt <fschmidt@gmail.com>
parents: 458
diff changeset
82 function get_page(path)
497
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
83 if test.welcome_file ~= nil and path.matches ".*/" then
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
84 path = path .. test.welcome_file
463
895afcd2b281 improve Http.init_for_test
Franklin Schmidt <fschmidt@gmail.com>
parents: 458
diff changeset
85 end
458
507c0af598ba improve Http.init_for_test
Franklin Schmidt <fschmidt@gmail.com>
parents: 377
diff changeset
86 local old_out = Io.stdout
463
895afcd2b281 improve Http.init_for_test
Franklin Schmidt <fschmidt@gmail.com>
parents: 458
diff changeset
87 local mod = require("site:"..path)
495
598123096772 rename service() to respond()
Franklin Schmidt <fschmidt@gmail.com>
parents: 494
diff changeset
88 mod.respond()
458
507c0af598ba improve Http.init_for_test
Franklin Schmidt <fschmidt@gmail.com>
parents: 377
diff changeset
89 text_writer.close()
507c0af598ba improve Http.init_for_test
Franklin Schmidt <fschmidt@gmail.com>
parents: 377
diff changeset
90 Io.stdout = old_out
507c0af598ba improve Http.init_for_test
Franklin Schmidt <fschmidt@gmail.com>
parents: 377
diff changeset
91 return result.read_text()
251
705d14f4d8ee start web testing
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
92 end
705d14f4d8ee start web testing
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
93
497
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
94 test.cookies = test.cookies or {}
252
3896138955b1 web testing...
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 251
diff changeset
95
497
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
96 request = new_request{}
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
97 request.cookies = test.cookies
251
705d14f4d8ee start web testing
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
98
705d14f4d8ee start web testing
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
99 response = {
252
3896138955b1 web testing...
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 251
diff changeset
100
251
705d14f4d8ee start web testing
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
101 text_writer = function()
458
507c0af598ba improve Http.init_for_test
Franklin Schmidt <fschmidt@gmail.com>
parents: 377
diff changeset
102 result = Io.uri "string:"
507c0af598ba improve Http.init_for_test
Franklin Schmidt <fschmidt@gmail.com>
parents: 377
diff changeset
103 text_writer = result.text_writer()
507c0af598ba improve Http.init_for_test
Franklin Schmidt <fschmidt@gmail.com>
parents: 377
diff changeset
104 return text_writer
251
705d14f4d8ee start web testing
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
105 end;
252
3896138955b1 web testing...
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 251
diff changeset
106
3896138955b1 web testing...
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 251
diff changeset
107 set_cookie = function(name,value)
497
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
108 test.cookies[name] = value
252
3896138955b1 web testing...
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 251
diff changeset
109 end;
3896138955b1 web testing...
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 251
diff changeset
110
253
dddf4e85bfe4 finish basic web testing
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 252
diff changeset
111 remove_cookie = function(name)
497
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
112 test.cookies[name] = nil
253
dddf4e85bfe4 finish basic web testing
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 252
diff changeset
113 end;
dddf4e85bfe4 finish basic web testing
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 252
diff changeset
114
252
3896138955b1 web testing...
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 251
diff changeset
115 send_redirect = function(url)
3896138955b1 web testing...
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 251
diff changeset
116 response.redirect = url
3896138955b1 web testing...
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 251
diff changeset
117 end;
3896138955b1 web testing...
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 251
diff changeset
118
266
4dca283b9b74 add Http.response.headers for testing
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 264
diff changeset
119 headers = {};
4dca283b9b74 add Http.response.headers for testing
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 264
diff changeset
120
251
705d14f4d8ee start web testing
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
121 }
705d14f4d8ee start web testing
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
122
705d14f4d8ee start web testing
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
123 end