mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-03 13:28:44 +00:00
Implement multipart streaming in MG (edition 2)
PUBLISHED_FROM=de89316ca3b53b8eb46ea69e88b5fbd277c80dc2
This commit is contained in:
1 parent
b4dbc825b1
commit
a6c66fb669
5 files changed
+318
-84
No files matched your search
@@ -1,2 +1,3 @@
|
||||
PROG = big_upload
|
||||
MODULE_CFLAGS = -DMG_ENABLE_HTTP_STREAMING_MULTIPART
|
||||
include ../examples.mk
|
||||
@@ -13,12 +13,11 @@ static const char *s_http_port = "8000";
|
||||
|
||||
struct file_writer_data {
|
||||
FILE *fp;
|
||||
size_t bytes_left;
|
||||
size_t bytes_written;
|
||||
};
|
||||
|
||||
static void handle_request(struct mg_connection *nc) {
|
||||
// This handler gets called only when we don't reset proto_handler in
|
||||
// handle_recv, so we just return a page unconditionally.
|
||||
// This handler gets for all endpoints but /upload
|
||||
mg_printf(nc, "%s",
|
||||
"HTTP/1.1 200 OK\r\n"
|
||||
"Content-Type: text/html\r\n"
|
||||
@@ -33,66 +32,40 @@ static void handle_request(struct mg_connection *nc) {
|
||||
nc->flags |= MG_F_SEND_AND_CLOSE;
|
||||
}
|
||||
|
||||
static void handle_recv(struct mg_connection *nc) {
|
||||
static void handle_upload(struct mg_connection *nc, int ev, void *p) {
|
||||
struct file_writer_data *data = (struct file_writer_data *) nc->user_data;
|
||||
struct mg_http_multipart_part *mp = (struct mg_http_multipart_part*)p;
|
||||
|
||||
if (data == NULL) {
|
||||
// This is a new connection, try to parse HTTP request.
|
||||
struct http_message hm;
|
||||
int req_len = mg_parse_http(nc->recv_mbuf.buf, nc->recv_mbuf.len, &hm,
|
||||
1 /* is_req */);
|
||||
switch (ev) {
|
||||
case MG_EV_HTTP_PART_BEGIN: {
|
||||
if (data == NULL) {
|
||||
data = calloc(1, sizeof(struct file_writer_data));
|
||||
data->fp = tmpfile();
|
||||
data->bytes_written = 0;
|
||||
|
||||
if (req_len < 0 ||
|
||||
(req_len == 0 && nc->recv_mbuf.len >= MG_MAX_HTTP_REQUEST_SIZE)) {
|
||||
nc->flags |= MG_F_CLOSE_IMMEDIATELY;
|
||||
} else if (req_len == 0) {
|
||||
// Request is not complete yet, do nothing.
|
||||
} else if (mg_vcasecmp(&hm.method, "POST") == 0 &&
|
||||
mg_vcmp(&hm.uri, "/upload") == 0) {
|
||||
// This is the request that we don't want to buffer in memory.
|
||||
|
||||
if (hm.body.len == (size_t) ~0 || hm.body.len == 0) {
|
||||
mg_printf(nc, "%s",
|
||||
"HTTP/1.1 411 Content-Length required\r\n"
|
||||
"Content-Length: 0\r\n\r\n");
|
||||
nc->flags |= MG_F_SEND_AND_CLOSE;
|
||||
return;
|
||||
if (data->fp == NULL) {
|
||||
mg_printf(nc, "%s",
|
||||
"HTTP/1.1 500 Failed to open a file\r\n"
|
||||
"Content-Length: 0\r\n\r\n");
|
||||
nc->flags |= MG_F_SEND_AND_CLOSE;
|
||||
return;
|
||||
}
|
||||
nc->user_data = (void *) data;
|
||||
}
|
||||
|
||||
// Reset proto_handler so Mongoose's http_handler() won't get confused
|
||||
// once we start removing data from the buffer.
|
||||
nc->proto_handler = NULL;
|
||||
|
||||
// Headers will be inaccessible later, so put everything we need into
|
||||
// user_data.
|
||||
data = calloc(1, sizeof(struct file_writer_data));
|
||||
data->bytes_left = hm.body.len;
|
||||
data->fp = tmpfile();
|
||||
if (data->fp == NULL) {
|
||||
mg_printf(nc, "%s",
|
||||
"HTTP/1.1 500 Failed to open a file\r\n"
|
||||
"Content-Length: 0\r\n\r\n");
|
||||
nc->flags |= MG_F_SEND_AND_CLOSE;
|
||||
return;
|
||||
}
|
||||
nc->user_data = (void *) data;
|
||||
|
||||
// Remove the headers from the buffer.
|
||||
mbuf_remove(&nc->recv_mbuf, hm.body.p - nc->recv_mbuf.buf);
|
||||
|
||||
// Invoke itself again to write the piece of the body that is already in
|
||||
// the buffer.
|
||||
handle_recv(nc);
|
||||
break;
|
||||
}
|
||||
} else { // data != NULL
|
||||
size_t to_write = data->bytes_left, written = 0;
|
||||
if (nc->recv_mbuf.len < to_write) to_write = nc->recv_mbuf.len;
|
||||
written = fwrite(nc->recv_mbuf.buf, 1, to_write, data->fp);
|
||||
mbuf_remove(&nc->recv_mbuf, written);
|
||||
data->bytes_left -= written;
|
||||
if (data->bytes_left <= 0) {
|
||||
// Request is complete, do something meaningful here.
|
||||
|
||||
case MG_EV_HTTP_PART_DATA: {
|
||||
if (fwrite(mp->data.p, 1, mp->data.len, data->fp) != mp->data.len) {
|
||||
mg_printf(nc, "%s",
|
||||
"HTTP/1.1 500 Failed to write to a file\r\n"
|
||||
"Content-Length: 0\r\n\r\n");
|
||||
nc->flags |= MG_F_SEND_AND_CLOSE;
|
||||
return;
|
||||
}
|
||||
data->bytes_written += mp->data.len;
|
||||
break;
|
||||
}
|
||||
case MG_EV_HTTP_PART_END: {
|
||||
mg_printf(nc,
|
||||
"HTTP/1.1 200 OK\r\n"
|
||||
"Content-Type: text/plain\r\n"
|
||||
@@ -100,22 +73,14 @@ static void handle_recv(struct mg_connection *nc) {
|
||||
"Written %ld of POST data to a temp file\n\n",
|
||||
(long) ftell(data->fp));
|
||||
nc->flags |= MG_F_SEND_AND_CLOSE;
|
||||
// handle_close will free the resources.
|
||||
fclose(data->fp);
|
||||
free(data);
|
||||
nc->user_data = NULL;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Make sure we free all allocated resources
|
||||
static void handle_close(struct mg_connection *nc) {
|
||||
struct file_writer_data *data = (struct file_writer_data *) nc->user_data;
|
||||
|
||||
if (data != NULL) {
|
||||
fclose(data->fp);
|
||||
free(data);
|
||||
nc->user_data = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
|
||||
(void) ev_data;
|
||||
switch (ev) {
|
||||
@@ -123,13 +88,6 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
|
||||
// Invoked when the full HTTP request is in the buffer (including body).
|
||||
handle_request(nc);
|
||||
break;
|
||||
case MG_EV_RECV:
|
||||
// Invoked every time new data arrives.
|
||||
handle_recv(nc);
|
||||
break;
|
||||
case MG_EV_CLOSE:
|
||||
handle_close(nc);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -140,6 +98,7 @@ int main(void) {
|
||||
mg_mgr_init(&mgr, NULL);
|
||||
nc = mg_bind(&mgr, s_http_port, ev_handler);
|
||||
|
||||
mg_register_http_endpoint(nc, "/upload", handle_upload);
|
||||
// Set up HTTP server parameters
|
||||
mg_set_protocol_http_websocket(nc);
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
SOURCES = $(PROG).c ../../mongoose.c
|
||||
CFLAGS = -W -Wall -I../.. -Wno-unused-function $(CFLAGS_EXTRA) $(MODULE_CFLAGS)
|
||||
CFLAGS = -g -W -Wall -I../.. -Wno-unused-function $(CFLAGS_EXTRA) $(MODULE_CFLAGS)
|
||||
|
||||
all: $(PROG)
|
||||
|
||||
|
||||
Reference in new issue
Block a user