diff --git a/LICENSE b/LICENSE index 5040cd43f60c69c1c82455aa2f1d4e660425bf5d..edb19837f409c417dd8a94eb922516efdf771fab 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 9990aca68c573260758d8805a796d70f8a41791f..4fcb4cff574147b399599f88bd754f2a37fbed36 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 e868c33277304834804766b32b06e0d7112cadaf..b5ff7863541e417b9f6c565e54c05e6a35da3a1e 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 [<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 € +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 b4c6aa4c0b1038f1300c79363a0f39043615977e..666e49fe69090aa0cfc5b9d900d5a7587ca74758 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: + + <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, diff --git a/examples/lua/prime_numbers.lp b/examples/lua/prime_numbers.lp new file mode 100644 index 0000000000000000000000000000000000000000..b3edc129effd98b5444aa63d5d080ac27f731fe7 --- /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') +?> + +<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> ') 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> diff --git a/main.c b/main.c index 8c477831029d8be2b75e6c5e181946d157960041..d1f152a28ef41a506965d86fa6622b2ad0e7c1c3 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 5c2f5f3be1016fc2ce186cfde3a02dbe9f698a3a..c7fd9f01fb4bc5072f0b0e92d4c7c030fffb5eb1 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 5024acb79e2f5e3a1d891f4bbe343146e90e4859..b2ebee8b1685f4471eb47ab48e423d57288df764 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') + ?><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> <?