Lua functionality change: print renamed to mg.write, read renamed to mg.read. Expanded user manual, fixed example pages.

This commit is contained in:
Sergey Lyubka committed 2013-05-15 17:42:34 +01:00
1 parent d72645bf91
commit 2e232b3304
6 files changed
+311 -334

No files matched your search

+15 -12
View File
@@ -1,24 +1,25 @@
<?
-- Lua server pages have full control over the output, including HTTP
-- headers they send to the client. Send HTTP headers:
print('HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n')
HTTP/1.0 200 OK
Content-Type: text/html
<html><body>
?><html><body>
<p>This is an example Lua server page served by
<a href="http://code.google.com/p/mongoose">Mongoose web server</a>.
Mongoose has Lua, Sqlite, and other functionality built in the binary.
This example page stores the request in the Sqlite database, and shows
all requests done previously.</p>
<p> Today is <? print(os.date("%A")) ?>
<p> Today is <? mg.write(os.date("%A")) ?>
<pre>
<?
-- for k,v in pairs(_G) do mg.write(k, '\n') end
-- Open database
local db = sqlite3.open('requests.db')
-- Setup a trace callback, to show SQL statements we'll be executing.
-- db:trace(function(data, sql) print('Executing: ' .. sql .. '\n') end, nil)
-- db:trace(function(data, sql) mg.write('Executing: ', sql: '\n') end, nil)
-- Create a table if it is not created already
db:exec([[
@@ -27,24 +28,26 @@ all requests done previously.</p>
timestamp NOT NULL,
method NOT NULL,
uri NOT NULL,
user_agent
addr
);
]])
-- Add entry about this request
local stmt = db:prepare(
'INSERT INTO requests VALUES(NULL, datetime("now"), ?, ?, ?);');
stmt:bind_values(request_info.request_method, request_info.uri,
request_info.http_headers['User-Agent'])
stmt:bind_values(mg.request_info.request_method,
mg.request_info.uri,
mg.request_info.remote_port)
stmt:step()
stmt:finalize()
-- Show all previous records
print('Previous requests:\n')
mg.write('Previous requests:\n')
stmt = db:prepare('SELECT * FROM requests ORDER BY id DESC;')
while stmt:step() == sqlite3.ROW do
local v = stmt:get_values()
print(v[1] .. ' ' .. v[2] .. ' ' .. v[3] .. ' '
mg.write(v[1] .. ' ' .. v[2] .. ' ' .. v[3] .. ' '
.. v[4] .. ' ' .. v[5] .. '\n')
end
+7 -7
View File
@@ -467,13 +467,13 @@ static void test_lua(void) {
ASSERT(lua_gettop(L) == 0);
check_lua_expr(L, "'hi'", "hi");
check_lua_expr(L, "request_info.request_method", "POST");
check_lua_expr(L, "request_info.uri", "/foo/bar");
check_lua_expr(L, "request_info.num_headers", "2");
check_lua_expr(L, "request_info.remote_ip", "0");
check_lua_expr(L, "request_info.http_headers['Content-Length']", "12");
check_lua_expr(L, "request_info.http_headers['Connection']", "close");
(void) luaL_dostring(L, "post = read()");
check_lua_expr(L, "mg.request_info.request_method", "POST");
check_lua_expr(L, "mg.request_info.uri", "/foo/bar");
check_lua_expr(L, "mg.request_info.num_headers", "2");
check_lua_expr(L, "mg.request_info.remote_ip", "0");
check_lua_expr(L, "mg.request_info.http_headers['Content-Length']", "12");
check_lua_expr(L, "mg.request_info.http_headers['Connection']", "close");
(void) luaL_dostring(L, "post = mg.read()");
check_lua_expr(L, "# post", "12");
check_lua_expr(L, "post", "hello world!");
lua_close(L);