This commit is contained in:
abadc0de 2013-02-27 15:32:43 -05:00
commit 36a83e1452
8 changed files with 105 additions and 6 deletions

View File

@ -1,4 +1,4 @@
Copyright (c) 2004-2010 Sergey Lyubka
Copyright (c) 2004-2013 Sergey Lyubka
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@ -76,7 +76,7 @@ all:
# To build with lua, make sure you have Lua unpacked into lua-5.2.1 directory
linux_lua:
$(CC) mongoose.c main.c $(LUA_SOURCES) -DUSE_LUA -I$(LUA) -o $(PROG) -ldl $(CFLAGS)
$(CC) mongoose.c main.c build/lsqlite3.c build/sqlite3.c $(LUA_SOURCES) -DUSE_LUA -DUSE_LUA_SQLITE3 -DLUA_COMPAT_ALL -I$(LUA) -o $(PROG) -ldl $(CFLAGS)
# Make sure that the compiler flags come last in the compilation string.
# If not so, this can break some on some Linux distros which use

View File

@ -54,9 +54,9 @@ to all who already did so: T.Barmann, D.Hughes, J.C.Sloan, R.Romeo,
L.E.Spencer, S.Kotay, R.M.Shorter, W.Mar, J.Wilander and 7 others.
Appreciated guys, you keep my brains going! Cash is also welcome indeed.
Press [<img src="http://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif">](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=DGZ2FMP95TAL6)
button to donate. Donation progress: 249/1000 &euro;
button to donate. Donation progress: 259/1000 &euro;
(thanks to O.M.Vilhunen, C.Radik, G.Woodcock, M.Szczepkowski,
Eternal Lands Development Team, T.Tollet, C.Tangerino, G.Karsai, A.Bourgett,
C.Blakemore)
C.Blakemore, D.Fonaryov)
![Progress](http://chart.googleapis.com/chart?chxr=0,0,1000&chxt=x&chbh=30,0,0&chs=300x35&cht=bhs&chco=90c0f0&chd=t:24.9)
![Progress](http://chart.googleapis.com/chart?chxr=0,0,1000&chxt=x&chbh=30,0,0&chs=300x35&cht=bhs&chco=90c0f0&chd=t:25.9)

View File

@ -279,6 +279,56 @@ must be for a file name only, not including directory name. Example:
mongoose -hide_files_patterns secret.txt|even_more_secret.txt
# Lua Server Pages
Pre-built Windows and Mac mongoose binaries have built-in Lua Server Pages
support. That means it is possible to write PHP-like scripts with mongoose,
using Lua programming language instead of PHP. Lua is known
for it's speed and small size. Mongoose uses Lua version 5.2.1, the
documentation for it can be found at
[Lua 5.2 reference manual](http://www.lua.org/manual/5.2/).
To create a Lua Page, make sure a file has `.lp` extension. For example,
let's say it is going to be `my_page.lp`. The contents of the file, just like
with PHP, is HTML with embedded Lua code. Lua code must be enclosed in
`<? ?>` blocks, and can appear anywhere on the page. For example, to
print current weekday name, one can write:
<p>
<span>Today is:</span>
<? print(os.date("%A")) ?>
</p>
Note that this example uses function `print()`, which prints data to the
web page. Using function `print()` is the way to generate web content from
inside Lua code. In addition to `print()`, all standard library functions
are accessible from the Lua code (please check reference manual for details),
and also information about the request is available in `request_info` object,
like request method, all headers, etcetera. Please refer to
`struct mg_request_info` definition in
[mongoose.h](https://github.com/valenok/mongoose/blob/master/mongoose.h)
to see what kind of information is present in `request_info` object. Also,
[page.lp](https://github.com/valenok/mongoose/blob/master/test/page.lp) and
[prime_numbers.lp](https://github.com/valenok/mongoose/blob/master/examples/lua/prime_numbers.lp)
contains some example code that uses `request_info` and other functions(form submitting for example).
One substantial difference of mongoose's Lua Pages and PHP is that Mongoose
expects Lua page to output HTTP headers. Therefore, **at the very beginning of
every Lua Page must be a Lua block that outputs HTTP headers**, like this:
<? print('HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n') ?>
<html><body>
... the rest of the web page ...
It is easy to do things like redirects, for example:
<? print('HTTP/1.0 302 Found\r\nLocation: http://google.com\r\n\r\n') ?>
To serve Lua Page, mongoose creates Lua context. That context is used for
all Lua blocks within the page. That means, all Lua blocks on the same page
share the same context. If one block defines a variable, for example, that
variable is visible in the block that follows.
# Common Problems
- PHP doesn't work - getting empty page, or 'File not found' error. The
reason for that is wrong paths to the interpreter. Remember that with PHP,

View File

@ -0,0 +1,42 @@
<?
-- 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')
?>
<html>
<p>Prime numbers from 0 to 100, calculated by Lua:</p>
<?
function is_prime(n)
if n <= 0 then return false end
if n <= 2 then return true end
if (n % 2 == 0) then return false end
for i = 3, n / 2, 2 do
if (n % i == 0) then return false end
end
return true
end
for i = 1, 100 do
if is_prime(i) then print('<span>' .. i .. '</span>&nbsp;') end
end
?>
<p>Reading POST data from Lua (click submit):</p>
<form method="POST" ><input type="text" name="t1"/><input type="submit"></form>
<pre>
POST data: [<? print(read())?>]
request method: [<? print(request_info.request_method) ?>]
IP/port: [<? print(request_info.remote_ip, ':', request_info.remote_port) ?>]
URI: [<? print(request_info.uri) ?>]
HTTP version [<? print(request_info.http_version) ?>]
HEADERS:
<?
for name, value in pairs(request_info.http_headers) do
print(name, ':', value, '\n')
end
?>
</pre>
</html>

5
main.c
View File

@ -697,6 +697,7 @@ static LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam,
char buf[200], *service_argv[] = {__argv[0], NULL};
POINT pt;
HMENU hMenu;
static UINT s_uTaskbarRestart; // for taskbar creation
switch (msg) {
case WM_CREATE:
@ -707,6 +708,7 @@ static LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam,
exit(EXIT_SUCCESS);
} else {
start_mongoose(__argc, __argv);
s_uTaskbarRestart = RegisterWindowMessage(TEXT("TaskbarCreated"));
}
break;
case WM_COMMAND:
@ -764,6 +766,9 @@ static LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam,
Shell_NotifyIcon(NIM_DELETE, &TrayIcon);
PostQuitMessage(0);
return 0; // We've just sent our own quit message, with proper hwnd.
default:
if (msg==s_uTaskbarRestart)
Shell_NotifyIcon(NIM_ADD, &TrayIcon);
}
return DefWindowProc(hWnd, msg, wParam, lParam);

View File

@ -2786,7 +2786,7 @@ static void handle_file_request(struct mg_connection *conn, const char *path,
r1 = r2 = 0;
hdr = mg_get_header(conn, "Range");
if (hdr != NULL && (n = parse_range_header(hdr, &r1, &r2)) > 0 &&
r1 >= 0 && r2 > 0) {
r1 >= 0 && r2 >= 0) {
conn->status_code = 206;
cl = n == 2 ? (r2 > cl ? cl : r2) - r1 + 1: cl - r1;
mg_snprintf(conn, range, sizeof(range),

View File

@ -2,6 +2,7 @@
-- 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')
?><html><body>
<p>This is an example Lua server page served by
@ -9,6 +10,7 @@
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")) ?>
<pre>
<?