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
e4c71425
Commit
e4c71425
authored
Jun 24, 2014
by
Sergey Lyubka
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added big_upload.c
parent
6e07215c
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
88 additions
and
0 deletions
+88
-0
big_upload.c
examples/big_upload.c
+88
-0
No files found.
examples/big_upload.c
0 → 100644
View file @
e4c71425
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "mongoose.h"
struct
file_data
{
FILE
*
fp
;
const
char
*
data
;
int
data_len
;
int
written
;
};
static
int
handle_request
(
struct
mg_connection
*
conn
)
{
const
char
*
data
;
int
data_len
;
char
var_name
[
100
],
file_name
[
100
],
path
[
100
];
if
(
strcmp
(
conn
->
uri
,
"/upload"
)
==
0
)
{
if
(
mg_parse_multipart
(
conn
->
content
,
conn
->
content_len
,
var_name
,
sizeof
(
var_name
),
file_name
,
sizeof
(
file_name
),
&
data
,
&
data_len
)
>
0
)
{
struct
file_data
*
p
=
(
struct
file_data
*
)
malloc
(
sizeof
(
*
p
));
snprintf
(
path
,
sizeof
(
path
),
"UPLOAD_%s"
,
file_name
);
p
->
fp
=
fopen
(
path
,
"wb"
);
p
->
data
=
data
;
p
->
data_len
=
data_len
;
p
->
written
=
0
;
conn
->
connection_param
=
p
;
mg_send_header
(
conn
,
"Content-Type"
,
"text/html"
);
}
return
MG_MORE
;
}
else
{
mg_printf_data
(
conn
,
"%s"
,
"<html><body>Upload example."
"<form method=
\"
POST
\"
action=
\"
/upload
\"
"
" enctype=
\"
multipart/form-data
\"
>"
"<input type=
\"
file
\"
name=
\"
file
\"
/> <br/>"
"<input type=
\"
submit
\"
value=
\"
Upload
\"
/>"
"</form></body></html>"
);
return
MG_TRUE
;
}
}
static
int
handle_poll
(
struct
mg_connection
*
conn
)
{
struct
file_data
*
p
=
(
struct
file_data
*
)
conn
->
connection_param
;
if
(
p
!=
NULL
)
{
// Write no more then 100 bytes in one go
int
len
=
p
->
data_len
-
p
->
written
;
int
n
=
fwrite
(
p
->
data
+
p
->
written
,
1
,
len
>
100
?
100
:
len
,
p
->
fp
);
if
(
n
>
0
)
{
p
->
written
+=
n
;
mg_send_data
(
conn
,
" "
,
1
);
// Send something back to wake up select()
}
// If everything is written, close the connection
if
(
p
->
written
>=
p
->
data_len
)
{
mg_printf_data
(
conn
,
"Written %d bytes."
,
p
->
written
);
fclose
(
p
->
fp
);
free
(
p
);
conn
->
connection_param
=
NULL
;
return
MG_TRUE
;
}
}
return
MG_FALSE
;
}
static
int
ev_handler
(
struct
mg_connection
*
conn
,
enum
mg_event
ev
)
{
switch
(
ev
)
{
case
MG_AUTH
:
return
MG_TRUE
;
case
MG_REQUEST
:
return
handle_request
(
conn
);
case
MG_POLL
:
return
handle_poll
(
conn
);
default:
return
MG_FALSE
;
}
}
int
main
(
void
)
{
struct
mg_server
*
server
=
mg_create_server
(
NULL
,
ev_handler
);
mg_set_option
(
server
,
"listening_port"
,
"8080"
);
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