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
6b4f7e78
Commit
6b4f7e78
authored
Jan 13, 2014
by
Sergey Lyubka
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added upload example
parent
73ed83c5
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
44 additions
and
40 deletions
+44
-40
Embed.md
docs/Embed.md
+3
-2
Makefile
examples/Makefile
+3
-2
upload.c
examples/upload.c
+38
-36
No files found.
docs/Embed.md
View file @
6b4f7e78
...
@@ -90,6 +90,7 @@ a couple of kilobytes to the executable size, and also has some runtime penalty.
...
@@ -90,6 +90,7 @@ a couple of kilobytes to the executable size, and also has some runtime penalty.
Mongoose source code contains a well-commented example code, listed below:
Mongoose source code contains a well-commented example code, listed below:
*
[
hello.c
](
https://github.com/cesanta/mongoose/blob/master/examples/hello.c
)
*
[
hello.c
](
https://github.com/cesanta/mongoose/blob/master/examples/hello.c
)
is a minimalisti
ng
hello world example
is a minimalisti
c
hello world example
*
[
post.c
](
https://github.com/cesanta/mongoose/blob/master/examples/post.c
)
*
[
post.c
](
https://github.com/cesanta/mongoose/blob/master/examples/post.c
)
shows how to handle form input
*
[
upload.c
](
https://github.com/cesanta/mongoose/blob/master/examples/post.c
)
shows how to upload files
examples/Makefile
View file @
6b4f7e78
...
@@ -23,8 +23,8 @@ all:
...
@@ -23,8 +23,8 @@ all:
$(CC)
qcomm.c ../mongoose.c
-o
qcomm
$(CFLAGS)
$(CC)
qcomm.c ../mongoose.c
-o
qcomm
$(CFLAGS)
$(CC)
post.c ../mongoose.c
-o
post
$(CFLAGS)
$(CC)
post.c ../mongoose.c
-o
post
$(CFLAGS)
$(CC)
multi_threaded.c ../mongoose.c
-o
multi_threaded
$(CFLAGS)
$(CC)
multi_threaded.c ../mongoose.c
-o
multi_threaded
$(CFLAGS)
$(CC)
upload.c ../mongoose.c
-o
upload
$(CFLAGS)
# $(CC) upload.c ../mongoose.c -o upload $(CFLAGS)
# $(CC) -DUSE_WEBSOCKET websocket.c ../mongoose.c -o $@ $(CFLAGS)
# $(CC) -DUSE_WEBSOCKET websocket.c ../mongoose.c -o $@ $(CFLAGS)
# $(CC) chat.c ../mongoose.c -o chat $(CFLAGS)
# $(CC) chat.c ../mongoose.c -o chat $(CFLAGS)
# $(CC) lua_dll.c ../build/lua_5.2.1.c -o $@.so $(CFLAGS) $(DLL_FLAGS)
# $(CC) lua_dll.c ../build/lua_5.2.1.c -o $@.so $(CFLAGS) $(DLL_FLAGS)
...
@@ -40,7 +40,8 @@ windows:
...
@@ -40,7 +40,8 @@ windows:
$(CL)
hello.c ../mongoose.c
$(CLFLAGS)
$(LFLAGS)
$(CL)
hello.c ../mongoose.c
$(CLFLAGS)
$(LFLAGS)
$(CL)
post.c ../mongoose.c
$(CLFLAGS)
$(LFLAGS)
$(CL)
post.c ../mongoose.c
$(CLFLAGS)
$(LFLAGS)
$(CL)
multi_threaded.c ../mongoose.c
$(CLFLAGS)
$(LFLAGS)
$(CL)
multi_threaded.c ../mongoose.c
$(CLFLAGS)
$(LFLAGS)
# $(CL) upload.c ../mongoose.c $(CLFLAGS) $(LFLAGS)
$(CL)
upload.c ../mongoose.c
$(CLFLAGS)
$(LFLAGS)
# $(CL) /DUSE_WEBSOCKET websocket.c ../mongoose.c $(CLFLAGS) $(LFLAGS)
# $(CL) /DUSE_WEBSOCKET websocket.c ../mongoose.c $(CLFLAGS) $(LFLAGS)
#$(CL) lua_dll.c $(CLFLAGS) $(DLL_FLAGS) /DLL $(LFLAGS) /SUBSYSTEM:WINDOWS /ENTRY:luaopen_lua_dll /EXPORT:luaopen_lua_dll /out:lua_dll.dll
#$(CL) lua_dll.c $(CLFLAGS) $(DLL_FLAGS) /DLL $(LFLAGS) /SUBSYSTEM:WINDOWS /ENTRY:luaopen_lua_dll /EXPORT:luaopen_lua_dll /out:lua_dll.dll
...
...
examples/upload.c
View file @
6b4f7e78
...
@@ -5,48 +5,50 @@
...
@@ -5,48 +5,50 @@
#include <string.h>
#include <string.h>
#include "mongoose.h"
#include "mongoose.h"
static
int
event_handler
(
struct
mg_event
*
event
)
{
static
int
index_html
(
struct
mg_connection
*
conn
)
{
const
char
*
data
;
if
(
event
->
type
==
MG_REQUEST_BEGIN
)
{
int
data_len
;
if
(
!
strcmp
(
event
->
request_info
->
uri
,
"/handle_post_request"
))
{
char
var_name
[
100
],
file_name
[
100
];
char
path
[
200
];
FILE
*
fp
=
mg_upload
(
event
->
conn
,
"/tmp"
,
path
,
sizeof
(
path
));
mg_printf_data
(
conn
,
"%s"
,
if
(
fp
!=
NULL
)
{
"<html><body>Upload example."
fclose
(
fp
);
"<form method=
\"
POST
\"
action=
\"
/handle_post_request
\"
"
mg_printf
(
event
->
conn
,
"HTTP/1.0 200 OK
\r\n\r\n
Saved: [%s]"
,
path
);
" enctype=
\"
multipart/form-data
\"
>"
}
else
{
"<input type=
\"
file
\"
name=
\"
file
\"
/> <br/>"
mg_printf
(
event
->
conn
,
"%s"
,
"HTTP/1.0 200 OK
\r\n\r\n
No files sent"
);
"<input type=
\"
submit
\"
value=
\"
Upload
\"
/>"
}
"</form>"
);
}
else
{
// Show HTML form. Make sure it has enctype="multipart/form-data" attr.
if
(
mg_parse_multipart
(
conn
->
content
,
conn
->
content_len
,
static
const
char
*
html_form
=
var_name
,
sizeof
(
var_name
),
"<html><body>Upload example."
file_name
,
sizeof
(
file_name
),
"<form method=
\"
POST
\"
action=
\"
/handle_post_request
\"
"
&
data
,
&
data_len
)
>
0
)
{
" enctype=
\"
multipart/form-data
\"
>"
"<input type=
\"
file
\"
name=
\"
file
\"
/> <br/>"
mg_printf_data
(
conn
,
"%s"
,
"Uploaded file:<pre>"
);
"<input type=
\"
submit
\"
value=
\"
Upload
\"
/>"
mg_send_data
(
conn
,
data
,
data_len
);
"</form></body></html>"
;
mg_printf_data
(
conn
,
"%s"
,
"/pre>"
);
mg_printf
(
event
->
conn
,
"HTTP/1.0 200 OK
\r\n
"
"Content-Length: %d
\r\n
"
"Content-Type: text/html
\r\n\r\n
%s"
,
(
int
)
strlen
(
html_form
),
html_form
);
}
// Mark request as processed
return
1
;
}
}
// All other events left unprocessed
mg_printf_data
(
conn
,
"%s"
,
"</body></html>"
);
return
1
;
return
1
;
}
}
int
main
(
void
)
{
int
main
(
void
)
{
struct
mg_context
*
ctx
;
struct
mg_server
*
server
;
const
char
*
options
[]
=
{
"listening_ports"
,
"8080"
,
NULL
};
ctx
=
mg_start
(
options
,
event_handler
,
NULL
);
// Create and configure the server
getchar
();
// Wait until user hits "enter"
server
=
mg_create_server
(
NULL
);
mg_stop
(
ctx
);
mg_set_option
(
server
,
"listening_port"
,
"8080"
);
mg_add_uri_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"
));
for
(;;)
{
mg_poll_server
(
server
,
1000
);
}
// Cleanup, and free server instance
mg_destroy_server
(
&
server
);
return
0
;
return
0
;
}
}
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