Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mongoose
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
esp
mongoose
Commits
36a83e14
Commit
36a83e14
authored
Feb 27, 2013
by
abadc0de
Browse files
Options
Browse Files
Download
Plain Diff
Merge
https://github.com/valenok/mongoose
parents
34df4ec1
69cb94f3
Changes
8
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
105 additions
and
6 deletions
+105
-6
LICENSE
LICENSE
+1
-1
Makefile
Makefile
+1
-1
README.md
README.md
+3
-3
UserManual.md
UserManual.md
+50
-0
prime_numbers.lp
examples/lua/prime_numbers.lp
+42
-0
main.c
main.c
+5
-0
mongoose.c
mongoose.c
+1
-1
page.lp
test/page.lp
+2
-0
No files found.
LICENSE
View file @
36a83e14
Copyright (c) 2004-201
0
Sergey Lyubka
Copyright (c) 2004-201
3
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
...
...
Makefile
View file @
36a83e14
...
...
@@ -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
...
...
README.md
View file @
36a83e14
...
...
@@ -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: 2
4
9/1000
€
button to donate. Donation progress: 2
5
9/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
)
![
Progress
](
http://chart.googleapis.com/chart?chxr=0,0,1000&chxt=x&chbh=30,0,0&chs=300x35&cht=bhs&chco=90c0f0&chd=t:2
4
.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:2
5
.9
)
UserManual.md
View file @
36a83e14
...
...
@@ -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,
...
...
examples/lua/prime_numbers.lp
0 → 100644
View file @
36a83e14
<?
-- 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>
main.c
View file @
36a83e14
...
...
@@ -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
);
...
...
mongoose.c
View file @
36a83e14
...
...
@@ -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
),
...
...
test/page.lp
View file @
36a83e14
...
...
@@ -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>
<?
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment