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
12d98a17
Commit
12d98a17
authored
Dec 23, 2013
by
Sergey Lyubka
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Restores post.c example
parent
a6c2c2a1
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
36 additions
and
40 deletions
+36
-40
Makefile
examples/Makefile
+1
-1
post.c
examples/post.c
+35
-39
No files found.
examples/Makefile
View file @
12d98a17
...
...
@@ -19,9 +19,9 @@ endif
all
:
$(CC)
hello.c ../mongoose.c
-o
hello
$(CFLAGS)
$(CC)
qcomm.c ../mongoose.c
-o
qcomm
$(CFLAGS)
$(CC)
post.c ../mongoose.c
-o
post
$(CFLAGS)
# $(CC) upload.c ../mongoose.c -o upload $(CFLAGS)
# $(CC) post.c ../mongoose.c -o post $(CFLAGS)
# $(CC) -DUSE_WEBSOCKET websocket.c ../mongoose.c -o $@ $(CFLAGS)
# $(CC) chat.c ../mongoose.c -o chat $(CFLAGS)
# $(CC) lua_dll.c ../build/lua_5.2.1.c -o $@.so $(CFLAGS) $(DLL_FLAGS)
...
...
examples/post.c
View file @
12d98a17
...
...
@@ -10,49 +10,45 @@ static const char *html_form =
"<input type=
\"
submit
\"
/>"
"</form></body></html>"
;
static
int
event_handler
(
struct
mg_event
*
event
)
{
char
post_data
[
1024
],
input1
[
sizeof
(
post_data
)],
input2
[
sizeof
(
post_data
)];
int
post_data_len
;
if
(
event
->
type
==
MG_REQUEST_BEGIN
)
{
if
(
!
strcmp
(
event
->
request_info
->
uri
,
"/handle_post_request"
))
{
// User has submitted a form, show submitted data and a variable value
post_data_len
=
mg_read
(
event
->
conn
,
post_data
,
sizeof
(
post_data
));
// Parse form data. input1 and input2 are guaranteed to be NUL-terminated
mg_get_var
(
post_data
,
post_data_len
,
"input_1"
,
input1
,
sizeof
(
input1
));
mg_get_var
(
post_data
,
post_data_len
,
"input_2"
,
input2
,
sizeof
(
input2
));
// Send reply to the client, showing submitted form values.
mg_printf
(
event
->
conn
,
"HTTP/1.0 200 OK
\r\n
"
"Content-Type: text/plain
\r\n\r\n
"
"Submitted data: [%.*s]
\n
"
"Submitted data length: %d bytes
\n
"
"input_1: [%s]
\n
"
"input_2: [%s]
\n
"
,
post_data_len
,
post_data
,
post_data_len
,
input1
,
input2
);
}
else
{
// Show HTML form.
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
);
}
return
1
;
// Mark event as processed
static
int
handler
(
struct
mg_connection
*
conn
)
{
char
var1
[
500
],
var2
[
500
],
reply
[
2000
];
if
(
strcmp
(
conn
->
uri
,
"/handle_post_request"
)
==
0
)
{
// User has submitted a form, show submitted data and a variable value
// Parse form data. var1 and var2 are guaranteed to be NUL-terminated
mg_get_var
(
conn
,
"input_1"
,
var1
,
sizeof
(
var1
));
mg_get_var
(
conn
,
"input_2"
,
var2
,
sizeof
(
var2
));
// Send reply to the client, showing submitted form values.
// POST data is in conn->content, data length is in conn->content_len
snprintf
(
reply
,
sizeof
(
reply
),
"HTTP/1.0 200 OK
\r\n
"
"Content-Type: text/plain
\r\n\r\n
"
"Submitted data: [%.*s]
\n
"
"Submitted data length: %d bytes
\n
"
"input_1: [%s]
\n
"
"input_2: [%s]
\n
"
,
conn
->
content_len
,
conn
->
content
,
conn
->
content_len
,
var1
,
var2
);
mg_write
(
conn
,
reply
,
strlen
(
reply
));
}
else
{
// Show HTML form.
snprintf
(
reply
,
sizeof
(
reply
),
"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
);
mg_write
(
conn
,
reply
,
strlen
(
reply
));
}
// All other events are left not processed
return
0
;
return
1
;
}
int
main
(
void
)
{
struct
mg_context
*
ctx
;
const
char
*
options
[]
=
{
"listening_ports"
,
"8080"
,
NULL
};
ctx
=
mg_start
(
options
,
&
event_handler
,
NULL
);
getchar
();
// Wait until user hits "enter"
mg_stop
(
ctx
);
struct
mg_server
*
server
=
mg_create_server
(
NULL
);
mg_set_option
(
server
,
"listening_port"
,
"8080"
);
mg_add_uri_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
;
}
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