Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
mongoose
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Admin message
Gitlab has been updated. More info
here
and
there
.
Show more breadcrumbs
Ganil-acq
GANILinux
linux-service
library
mongoose
Commits
12d98a17
Commit
12d98a17
authored
11 years ago
by
Sergey Lyubka
Browse files
Options
Downloads
Patches
Plain Diff
Restores post.c example
parent
a6c2c2a1
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
examples/Makefile
+1
-1
1 addition, 1 deletion
examples/Makefile
examples/post.c
+35
-39
35 additions, 39 deletions
examples/post.c
with
36 additions
and
40 deletions
examples/Makefile
+
1
−
1
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)
...
...
This diff is collapsed.
Click to expand it.
examples/post.c
+
35
−
39
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
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment