diff --git a/LICENSE b/LICENSE
index 5040cd43..edb19837 100644
--- a/LICENSE
+++ b/LICENSE
@@ -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
diff --git a/Makefile b/Makefile
index 9990aca6..4fcb4cff 100644
--- a/Makefile
+++ b/Makefile
@@ -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
diff --git a/README.md b/README.md
index e868c332..b5ff7863 100644
--- a/README.md
+++ b/README.md
@@ -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 [
](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=DGZ2FMP95TAL6)
-button to donate. Donation progress: 249/1000 €
+button to donate. Donation progress: 259/1000 €
(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)
-
+
diff --git a/UserManual.md b/UserManual.md
index b4c6aa4c..666e49fe 100644
--- a/UserManual.md
+++ b/UserManual.md
@@ -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:
+
+
+ Today is: + print(os.date("%A")) ?> +
+ +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') ?> + + ... 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, diff --git a/examples/lua/prime_numbers.lp b/examples/lua/prime_numbers.lp new file mode 100644 index 00000000..b3edc129 --- /dev/null +++ b/examples/lua/prime_numbers.lp @@ -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') +?> + + +Prime numbers from 0 to 100, calculated by Lua:
+ + 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('' .. i .. ' ') end + end + + ?> + +Reading POST data from Lua (click submit):
+ + ++ 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 + ?> ++ diff --git a/main.c b/main.c index 8c477831..d1f152a2 100644 --- a/main.c +++ b/main.c @@ -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); diff --git a/mongoose.c b/mongoose.c index 5c2f5f3b..c7fd9f01 100644 --- a/mongoose.c +++ b/mongoose.c @@ -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), diff --git a/test/page.lp b/test/page.lp index 5024acb7..b2ebee8b 100644 --- a/test/page.lp +++ b/test/page.lp @@ -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') + ?>
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.
+Today is print(os.date("%A")) ?>