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
ea9a60a5
Commit
ea9a60a5
authored
Mar 02, 2014
by
Sergey Lyubka
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Updated examples to conform to the evented API
parent
458320cf
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
114 additions
and
60 deletions
+114
-60
Makefile
examples/Makefile
+20
-11
auth.c
examples/auth.c
+16
-12
hello.c
examples/hello.c
+12
-6
multi_threaded.c
examples/multi_threaded.c
+13
-10
post.c
examples/post.c
+16
-4
server.c
examples/server.c
+1
-1
upload.c
examples/upload.c
+12
-4
websocket.c
examples/websocket.c
+24
-12
No files found.
examples/Makefile
View file @
ea9a60a5
CFLAGS
=
-W
-Wall
-I
..
-pthread
-g
-pipe
$(CFLAGS_EXTRA)
RM
=
rm
-rf
MSVC
=
../../vc6
CL
=
$(MSVC)
/bin/cl
CLFLAGS
=
/MD /TC /nologo
$(CFLAGS_EXTRA)
/W3
\
/I
$(MSVC)
/include /I.. /Dsnprintf
=
_snprintf
LFLAGS
=
/link /incremental:no /libpath:
$(MSVC)
/lib /machine:IX86
ifeq
($(OS),Windows_NT)
MSVC
=
../../vc6
RM
=
del /q /f
CC
=
$(MSVC)
/bin/cl
$(CLFLAGS)
CLFLAGS
=
/MD /TC /nologo
$(CFLAGS_EXTRA)
/W3
\
/I
$(MSVC)
/include /I.. /Dsnprintf
=
_snprintf
LFLAGS
=
/link /incremental:no /libpath:
$(MSVC)
/lib /machine:IX86
else
UNAME_S
:=
$(
shell
uname
-s
)
DLL_FLAGS
+=
-shared
ifeq
($(UNAME_S),Linux)
CFLAGS
+=
-ldl
endif
ifeq
($(UNAME_S),Darwin)
# DLL_FLAGS += -bundle -undefined dynamic_lookup -dynamiclib
DLL_FLAGS
+=
-flat_namespace
-undefined
suppress
-dynamiclib
endif
endif
all
:
hello websocket server post multi_threaded upload auth
# To enable Lua in a server, uncomment following lines
LUA
=
../lua-5.2.3/src
#CFLAGS += -I$(LUA) -L$(LUA) -llua
all
:
websocket_html
.c
hello
:
hello.c ../mongoose
.c
$(CC)
hello.c ../mongoose.c
-o
hello
$(CFLAGS)
websocket
:
websocket_html.c websocket.c ../mongoose.c
$(CC)
websocket.c websocket_html.c ../mongoose.c
-o
websocket
$(CFLAGS)
server
:
server.c ../mongoose.c
$(CC)
server.c ../mongoose.c
-o
server
$(CFLAGS)
post
:
post.c ../mongoose.c
$(CC)
post.c ../mongoose.c
-o
post
$(CFLAGS)
multi_threaded
:
multi_threaded.c ../mongoose.c
$(CC)
multi_threaded.c ../mongoose.c
-o
multi_threaded
$(CFLAGS)
upload
:
upload.c ../mongoose.c
$(CC)
upload.c ../mongoose.c
-o
upload
$(CFLAGS)
auth
:
auth.c ../mongoose.c
$(CC)
auth.c ../mongoose.c
-o
auth
$(CFLAGS)
$(CC)
server.c ../mongoose.c
-o
server
$(CFLAGS)
websocket_html.c
:
websocket.html
perl mkdata.pl
$<
>
$@
...
...
examples/auth.c
View file @
ea9a60a5
...
...
@@ -2,25 +2,29 @@
#include <string.h>
#include "mongoose.h"
static
int
auth_handler
(
struct
mg_connection
*
conn
)
{
int
result
=
MG_AUTH_FAIL
;
// Not authorized
FILE
*
fp
;
// To populate passwords file, do
// mongoose -A my_passwords.txt mydomain.com admin admin
if
((
fp
=
fopen
(
"my_passwords.txt"
,
"r"
))
!=
NULL
)
{
result
=
mg_authorize_digest
(
conn
,
fp
);
fclose
(
fp
);
static
int
ev_handler
(
struct
mg_connection
*
conn
,
enum
mg_event
ev
)
{
if
(
ev
==
MG_AUTH
)
{
int
result
=
MG_FALSE
;
// Not authorized
FILE
*
fp
;
// To populate passwords file, do
// mongoose -A my_passwords.txt mydomain.com admin admin
if
((
fp
=
fopen
(
"my_passwords.txt"
,
"r"
))
!=
NULL
)
{
result
=
mg_authorize_digest
(
conn
,
fp
);
fclose
(
fp
);
}
return
result
;
}
return
result
;
return
MG_FALSE
;
}
int
main
(
void
)
{
struct
mg_server
*
server
=
mg_create_server
(
NULL
);
struct
mg_server
*
server
=
mg_create_server
(
NULL
,
ev_handler
);
mg_set_option
(
server
,
"listening_port"
,
"8080"
);
mg_set_option
(
server
,
"document_root"
,
"."
);
mg_set_auth_handler
(
server
,
auth_handler
);
printf
(
"Starting on port %s
\n
"
,
mg_get_option
(
server
,
"listening_port"
));
for
(;;)
{
...
...
examples/hello.c
View file @
ea9a60a5
...
...
@@ -2,19 +2,25 @@
#include <string.h>
#include "mongoose.h"
// This function will be called by mongoose on every new request
static
int
index_html
(
struct
mg_connection
*
conn
)
{
mg_printf_data
(
conn
,
"Hello! Requested URI is [%s]"
,
conn
->
uri
);
return
MG_REQUEST_PROCESSED
;
static
int
ev_handler
(
struct
mg_connection
*
conn
,
enum
mg_event
ev
)
{
int
result
=
MG_FALSE
;
if
(
ev
==
MG_REQ_BEGIN
)
{
mg_printf_data
(
conn
,
"Hello! Requested URI is [%s]"
,
conn
->
uri
);
result
=
MG_TRUE
;
}
else
if
(
ev
==
MG_AUTH
)
{
result
=
MG_TRUE
;
}
return
result
;
}
int
main
(
void
)
{
struct
mg_server
*
server
;
// Create and configure the server
server
=
mg_create_server
(
NULL
);
server
=
mg_create_server
(
NULL
,
ev_handler
);
mg_set_option
(
server
,
"listening_port"
,
"8080"
);
mg_set_request_handler
(
server
,
index_html
);
// Serve request. Hit Ctrl-C to terminate the program
printf
(
"Starting on port %s
\n
"
,
mg_get_option
(
server
,
"listening_port"
));
...
...
examples/multi_threaded.c
View file @
ea9a60a5
...
...
@@ -2,11 +2,17 @@
// Start a browser and hit refresh couple of times. The replies will
// come from both server instances.
static
int
request_handler
(
struct
mg_connection
*
conn
)
{
mg_send_header
(
conn
,
"Content-Type"
,
"text/plain"
);
mg_printf_data
(
conn
,
"This is a reply from server instance # %s"
,
(
char
*
)
conn
->
server_param
);
return
MG_REQUEST_PROCESSED
;
static
int
ev_handler
(
struct
mg_connection
*
conn
,
enum
mg_event
ev
)
{
if
(
ev
==
MG_REQ_BEGIN
)
{
mg_send_header
(
conn
,
"Content-Type"
,
"text/plain"
);
mg_printf_data
(
conn
,
"This is a reply from server instance # %s"
,
(
char
*
)
conn
->
server_param
);
return
MG_TRUE
;
}
else
if
(
ev
==
MG_AUTH
)
{
return
MG_TRUE
;
}
else
{
return
MG_FALSE
;
}
}
static
void
*
serve
(
void
*
server
)
{
...
...
@@ -17,11 +23,8 @@ static void *serve(void *server) {
int
main
(
void
)
{
struct
mg_server
*
server1
,
*
server2
;
server1
=
mg_create_server
((
void
*
)
"1"
);
server2
=
mg_create_server
((
void
*
)
"2"
);
mg_set_request_handler
(
server1
,
request_handler
);
mg_set_request_handler
(
server2
,
request_handler
);
server1
=
mg_create_server
((
void
*
)
"1"
,
ev_handler
);
server2
=
mg_create_server
((
void
*
)
"2"
,
ev_handler
);
// Make both server1 and server2 listen on the same socket
mg_set_option
(
server1
,
"listening_port"
,
"8080"
);
...
...
examples/post.c
View file @
ea9a60a5
...
...
@@ -10,7 +10,7 @@ static const char *html_form =
"<input type=
\"
submit
\"
/>"
"</form></body></html>"
;
static
int
handler
(
struct
mg_connection
*
conn
)
{
static
void
send_reply
(
struct
mg_connection
*
conn
)
{
char
var1
[
500
],
var2
[
500
];
if
(
strcmp
(
conn
->
uri
,
"/handle_post_request"
)
==
0
)
{
...
...
@@ -33,18 +33,30 @@ static int handler(struct mg_connection *conn) {
// Show HTML form.
mg_send_data
(
conn
,
html_form
,
strlen
(
html_form
));
}
}
return
MG_REQUEST_PROCESSED
;
static
int
ev_handler
(
struct
mg_connection
*
conn
,
enum
mg_event
ev
)
{
if
(
ev
==
MG_REQ_BEGIN
)
{
send_reply
(
conn
);
return
MG_TRUE
;
}
else
if
(
ev
==
MG_AUTH
)
{
return
MG_TRUE
;
}
else
{
return
MG_FALSE
;
}
}
int
main
(
void
)
{
struct
mg_server
*
server
=
mg_create_server
(
NULL
);
struct
mg_server
*
server
=
mg_create_server
(
NULL
,
ev_handler
);
mg_set_option
(
server
,
"listening_port"
,
"8080"
);
mg_set_request_handler
(
server
,
handler
);
printf
(
"Starting on port %s
\n
"
,
mg_get_option
(
server
,
"listening_port"
));
for
(;;)
{
mg_poll_server
(
server
,
1000
);
}
mg_destroy_server
(
&
server
);
return
0
;
}
examples/server.c
View file @
ea9a60a5
...
...
@@ -380,7 +380,7 @@ static void start_mongoose(int argc, char *argv[]) {
char
*
options
[
MAX_OPTIONS
];
int
i
;
if
((
server
=
mg_create_server
(
NULL
))
==
NULL
)
{
if
((
server
=
mg_create_server
(
NULL
,
NULL
))
==
NULL
)
{
die
(
"%s"
,
"Failed to start Mongoose."
);
}
...
...
examples/upload.c
View file @
ea9a60a5
...
...
@@ -5,7 +5,7 @@
#include <string.h>
#include "mongoose.h"
static
int
index_html
(
struct
mg_connection
*
conn
)
{
static
void
send_index_page
(
struct
mg_connection
*
conn
)
{
const
char
*
data
;
int
data_len
;
char
var_name
[
100
],
file_name
[
100
];
...
...
@@ -29,17 +29,25 @@ static int index_html(struct mg_connection *conn) {
}
mg_printf_data
(
conn
,
"%s"
,
"</body></html>"
);
}
return
MG_REQUEST_PROCESSED
;
static
int
ev_handler
(
struct
mg_connection
*
conn
,
enum
mg_event
ev
)
{
if
(
ev
==
MG_REQ_BEGIN
)
{
send_index_page
(
conn
);
return
MG_TRUE
;
}
else
if
(
ev
==
MG_AUTH
)
{
return
MG_TRUE
;
}
else
{
return
MG_FALSE
;
}
}
int
main
(
void
)
{
struct
mg_server
*
server
;
// Create and configure the server
server
=
mg_create_server
(
NULL
);
server
=
mg_create_server
(
NULL
,
ev_handler
);
mg_set_option
(
server
,
"listening_port"
,
"8080"
);
mg_set_request_handler
(
server
,
index_html
);
// Serve request. Hit Ctrl-C to terminate the program
printf
(
"Starting on port %s
\n
"
,
mg_get_option
(
server
,
"listening_port"
));
...
...
examples/websocket.c
View file @
ea9a60a5
#include <string.h>
#include <time.h>
#include "mongoose.h"
extern
const
char
*
find_embedded_file
(
const
char
*
,
size_t
*
);
static
int
iterate_callback
(
struct
mg_connection
*
c
)
{
if
(
c
->
is_websocket
)
{
static
int
iterate_callback
(
struct
mg_connection
*
c
,
enum
mg_event
ev
)
{
static
int
counter
=
0
;
if
(
ev
==
MG_POLL
&&
c
->
is_websocket
)
{
char
buf
[
20
];
int
len
=
snprintf
(
buf
,
sizeof
(
buf
),
"%d"
,
*
(
int
*
)
c
->
callback_param
);
int
len
=
snprintf
(
buf
,
sizeof
(
buf
),
"%d"
,
counter
++
);
mg_websocket_write
(
c
,
1
,
buf
,
len
);
}
return
MG_
REQUEST_PROCESSED
;
return
MG_
TRUE
;
}
static
int
index_html
(
struct
mg_connection
*
conn
)
{
static
int
send_reply
(
struct
mg_connection
*
conn
)
{
size_t
index_size
;
const
char
*
index_html
=
find_embedded_file
(
"websocket.html"
,
&
index_size
);
...
...
@@ -22,27 +24,37 @@ static int index_html(struct mg_connection *conn) {
// Echo websocket data back to the client.
mg_websocket_write
(
conn
,
1
,
conn
->
content
,
conn
->
content_len
);
return
conn
->
content_len
==
4
&&
!
memcmp
(
conn
->
content
,
"exit"
,
4
)
?
MG_
CLIENT_CLOSE
:
MG_CLIENT_CONTIN
UE
;
MG_
FALSE
:
MG_TR
UE
;
}
else
{
mg_send_header
(
conn
,
"Content-Type"
,
"text/html"
);
mg_send_data
(
conn
,
index_html
,
index_size
);
return
MG_REQUEST_PROCESSED
;
return
MG_TRUE
;
}
}
static
int
ev_handler
(
struct
mg_connection
*
conn
,
enum
mg_event
ev
)
{
if
(
ev
==
MG_REQ_BEGIN
)
{
return
send_reply
(
conn
);
}
else
if
(
ev
==
MG_AUTH
)
{
return
MG_TRUE
;
}
else
{
return
MG_FALSE
;
}
}
int
main
(
void
)
{
struct
mg_server
*
server
=
mg_create_server
(
NULL
);
unsigned
int
current_timer
=
0
,
last_timer
=
0
;
struct
mg_server
*
server
=
mg_create_server
(
NULL
,
ev_handler
);
time_t
current_timer
=
0
,
last_timer
=
time
(
NULL
)
;
mg_set_option
(
server
,
"listening_port"
,
"8080"
);
mg_set_request_handler
(
server
,
index_html
);
printf
(
"Started on port %s
\n
"
,
mg_get_option
(
server
,
"listening_port"
));
for
(;;)
{
current_timer
=
mg_poll_server
(
server
,
100
);
mg_poll_server
(
server
,
100
);
current_timer
=
time
(
NULL
);
if
(
current_timer
-
last_timer
>
0
)
{
last_timer
=
current_timer
;
mg_iterate_over_connections
(
server
,
iterate_callback
,
&
current_timer
);
mg_iterate_over_connections
(
server
,
iterate_callback
);
}
}
...
...
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