1-- This test checks the Lua functions:
2-- get_var, get_cookie, md5, url_encode
3
4now = os.time()
5cookie_name = "CivetWeb-test-page4"
6
7if mg.request_info.http_headers.Cookie then
8   cookie_value = tonumber(mg.get_cookie(mg.request_info.http_headers.Cookie, cookie_name))
9end
10
11mg.write("HTTP/1.0 200 OK\r\n")
12mg.write("Connection: close\r\n")
13mg.write("Content-Type: text/html; charset=utf-8\r\n")
14mg.write("Cache-Control: max-age=0, must-revalidate\r\n")
15if not cookie_value then
16    mg.write("Set-Cookie: " .. cookie_name .. "=" .. tostring(now) .. "\r\n")
17end
18mg.write("\r\n")
19
20mg.write("<html>\r\n<head><title>CivetWeb Lua script test page 4</title></head>\r\n<body>\r\n")
21mg.write("<p>Test of CivetWeb Lua Functions:</p>\r\n");
22mg.write("<pre>\r\n");
23
24-- get_var of query_string
25mg.write("get_var test (check query string):\r\n")
26if not mg.request_info.query_string then
27    mg.write("  No query string. You may try <a href='?a=a1&amp;junk&amp;b=b2&amp;cc=cNotSet&amp;d=a, b and d should be set&amp;z=z'>this example</a>.\r\n")
28else
29    for _,var in ipairs({'a','b','c','d'}) do
30       value = mg.get_var(mg.request_info.query_string, var);
31       if value then
32         mg.write("  Variable " .. var .. ": value " .. value .. "\r\n");
33       else
34         mg.write("  Variable " .. var .. " not set\r\n");
35       end
36    end
37end
38mg.write("\r\n")
39
40-- md5
41mg.write("MD5 test:\r\n")
42test_string = "abcd\0efgh"
43mg.write("  String with embedded 0, length " .. string.len(test_string))
44test_md5 = mg.md5(test_string)
45mg.write(", MD5 " .. test_md5 .. "\r\n")
46if mg.md5("") == "d41d8cd98f00b204e9800998ecf8427e" then
47    mg.write("  MD5 of empty string OK\r\n")
48else
49    mg.write("  Error: MD5 of empty string NOT OK\r\n")
50end
51if mg.md5("The quick brown fox jumps over the lazy dog.") == "e4d909c290d0fb1ca068ffaddf22cbd0" then
52    mg.write("  MD5 of test string OK\r\n")
53else
54    mg.write("  Error: MD5 of test string NOT OK\r\n")
55end
56mg.write("\r\n")
57
58-- get_cookie
59mg.write("Cookie test:\r\n")
60if not cookie_value then
61    mg.write("  Cookie not set yet. Please reload the page.\r\n")
62else
63    mg.write("  Cookie set to " .. cookie_value .. "\r\n")
64    mg.write("  You visited this page " .. os.difftime(now, cookie_value) .. " seconds before.\r\n")
65end
66mg.write("\r\n")
67
68-- test 'require' of other Lua scripts
69mg.write("require test\r\n")
70script_path = mg.script_name:match("(.*)page%d*.lua")
71if type(script_path)=='string' then
72    package.path = script_path .. "?.lua;" .. package.path
73    mg.write("  Lua search path: " .. package.path .. "\r\n")
74    require "html_esc"
75    require "require_test"
76    if htmlEscape then
77      for i=0,15 do
78        mg.write("  ")
79        for j=0,15 do
80            mg.write(tostring(htmlEscape[16*i+j]))
81        end
82        mg.write("\r\n")
83      end
84    else
85      mg.write("  'require' test failed (htmlEscape)\r\n")
86    end
87    if HugeText then
88      mg.write("\r\n")
89      local ht = HugeText(os.date("%a %b. %d"))
90      for i=1,#ht do
91        mg.write("  " .. ht[i] .. "\r\n")
92      end
93    else
94      mg.write("  'require' test failed (HugeText)\r\n")
95    end
96else
97    mg.write("  name match failed\r\n")
98end
99mg.write("\r\n")
100
101-- test get_response_code_text
102mg.write("HTTP helper methods test:\r\n")
103if (htmlEscape("<a b & c d>") == "&lt;a b &amp; c d&gt;") then
104    mg.write("  htmlEscape test OK\r\n")
105else
106    mg.write("  Error: htmlEscape test NOT OK\r\n")
107end
108if (mg.get_response_code_text(200) == "OK") then
109    mg.write("  get_response_code_text test OK\r\n")
110else
111    mg.write("  Error: get_response_code_text test NOT OK\r\n")
112end
113mg.write("\r\n")
114
115-- url_encode
116mg.write("URL encode/decode test:\r\n")
117if mg.url_encode("") == "" then
118    mg.write("  url_encode of empty string OK\r\n")
119else
120    mg.write("  Error: url_encode of empty string NOT OK\r\n")
121end
122raw_string = [[ !"#$%&'()*+,-./0123456789:;<=>?@]]
123mg.write("  original string: " .. htmlEscape(raw_string) .. "\r\n")
124mg_string = mg.url_encode(raw_string):upper()
125ref_string = "%20!%22%23%24%25%26'()*%2B%2C-.%2F0123456789%3A%3B%3C%3D%3E%3F%40" -- from http://www.w3schools.com/tags/ref_urlencode.asp
126mg.write("  mg-url:        " .. htmlEscape(mg_string) .. "\r\n")
127mg.write("  reference url: " .. htmlEscape(ref_string) .. "\r\n")
128dec_mg_string = mg.url_decode(mg_string)
129dec_ref_string = mg.url_decode(ref_string)
130mg.write("  decoded mg-url:        " .. htmlEscape(dec_mg_string) .. "\r\n")
131mg.write("  decoded reference url: " .. htmlEscape(dec_ref_string) .. "\r\n")
132dec_mg_string = mg.url_decode(mg_string, false)
133dec_ref_string = mg.url_decode(ref_string, false)
134mg.write("  decoded mg-url:        " .. htmlEscape(dec_mg_string) .. "\r\n")
135mg.write("  decoded reference url: " .. htmlEscape(dec_ref_string) .. "\r\n")
136dec_mg_string = mg.url_decode(mg_string, true)
137dec_ref_string = mg.url_decode(ref_string, true)
138mg.write("  decoded mg-url:        " .. htmlEscape(dec_mg_string) .. "\r\n")
139mg.write("  decoded reference url: " .. htmlEscape(dec_ref_string) .. "\r\n")
140mg.write("\r\n")
141
142-- base64_encode
143mg.write("BASE64 encode/decode test:\r\n")
144raw_string = [[ !"#$%&'()*+,-./0123456789:;<=>?@]]
145mg.write("  original string:  " .. htmlEscape(raw_string) .. "\r\n")
146mg_string = mg.base64_encode(raw_string)
147ref_string = "ICEiIyQlJicoKSorLC0uLzAxMjM0NTY3ODk6Ozw9Pj9A" -- from http://www.base64encode.org/
148mg.write("  mg-base64:        " .. htmlEscape(mg_string) .. "\r\n")
149mg.write("  reference base64: " .. htmlEscape(ref_string) .. "\r\n")
150dec_mg_string = mg.base64_decode(mg_string)
151dec_ref_string = mg.base64_decode(ref_string)
152mg.write("  decoded mg-base64:        " .. htmlEscape(dec_mg_string) .. "\r\n")
153mg.write("  decoded reference base64: " .. htmlEscape(dec_ref_string) .. "\r\n")
154mg.write("\r\n")
155raw_string = [[<?> -?-]]
156mg.write("  original string:  " .. htmlEscape(raw_string) .. "\r\n")
157mg_string = mg.base64_encode(raw_string)
158ref_string = "PD8+IC0/LQ==" -- from http://www.base64encode.org/
159mg.write("  mg-base64:        " .. htmlEscape(mg_string) .. "\r\n")
160mg.write("  reference base64: " .. htmlEscape(ref_string) .. "\r\n")
161dec_mg_string = mg.base64_decode(mg_string)
162dec_ref_string = mg.base64_decode(ref_string)
163mg.write("  decoded mg-base64:        " .. htmlEscape(dec_mg_string) .. "\r\n")
164mg.write("  decoded reference base64: " .. htmlEscape(dec_ref_string) .. "\r\n")
165mg.write("\r\n")
166
167-- random
168mg.write("Random numbers:\r\n")
169for i=1,10 do mg.write(string.format("%18u\r\n", mg.random())) end
170mg.write("\r\n")
171
172-- uuid
173if mg.uuid then
174mg.write("UUIDs:\r\n")
175for i=1,10 do mg.write(string.format("%40s\r\n", mg.uuid())) end
176mg.write("\r\n")
177end
178
179-- end of page
180mg.write("</pre>\r\n</body>\r\n</html>\r\n")
181