mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-01 13:10:43 +00:00
Get rid of MG_EV_HTTP_CHUNK
This commit is contained in:
1 parent
dde699ffda
commit
7884a2f3c7
10 files changed
+224
-373
No files matched your search
@@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2020 Cesanta Software Limited
|
||||
// All rights reserved
|
||||
//
|
||||
// Streaming upload example. Demonstrates how to use MG_EV_HTTP_CHUNK events
|
||||
// to send large payload in smaller chunks. To test, use curl utility:
|
||||
// Streaming upload example. Demonstrates how to use MG_EV_READ events
|
||||
// to get large payload in smaller chunks. To test, use curl utility:
|
||||
//
|
||||
// curl http://localhost:8000/upload?name=a.txt --data-binary @large_file.txt
|
||||
|
||||
@@ -12,21 +12,33 @@
|
||||
// /upload - Saves the next file chunk
|
||||
// all other URI - serves web_root/ directory
|
||||
static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
||||
if (ev == MG_EV_HTTP_CHUNK && mg_http_match_uri(hm, "/upload")) {
|
||||
MG_INFO(("Got chunk len %lu", (unsigned long) hm->chunk.len));
|
||||
MG_INFO(("Query string: [%.*s]", (int) hm->query.len, hm->query.ptr));
|
||||
// MG_INFO(("Chunk data:\n%.*s", (int) hm->chunk.len, hm->chunk.ptr));
|
||||
mg_http_delete_chunk(c, hm);
|
||||
if (hm->chunk.len == 0) {
|
||||
MG_INFO(("Last chunk received, sending response"));
|
||||
mg_http_reply(c, 200, "", "ok (chunked)\n");
|
||||
if (ev == MG_EV_READ) {
|
||||
// Parse the incoming data ourselves. If we can parse the request,
|
||||
// store two size_t variables in the c->data: expected len and recv len.
|
||||
size_t *data = (size_t *) c->data;
|
||||
if (data[0]) { // Already parsed, simply print received data
|
||||
data[1] += c->recv.len;
|
||||
MG_INFO(("Got chunk len %lu, %lu total", c->recv.len, data[1]));
|
||||
c->recv.len = 0; // And cleanup the receive buffer. Streaming!
|
||||
if (data[1] >= data[0]) mg_http_reply(c, 200, "", "ok\n");
|
||||
} else {
|
||||
struct mg_http_message hm;
|
||||
int n = mg_http_parse((char *) c->recv.buf, c->recv.len, &hm);
|
||||
if (n < 0) mg_error(c, "Bad response");
|
||||
if (n > 0) {
|
||||
if (mg_http_match_uri(&hm, "/upload")) {
|
||||
MG_INFO(("Got chunk len %lu", c->recv.len - n));
|
||||
data[0] = hm.body.len;
|
||||
data[1] = c->recv.len - n;
|
||||
if (data[1] >= data[0]) mg_http_reply(c, 200, "", "ok\n");
|
||||
} else {
|
||||
struct mg_http_serve_opts opts = {.root_dir = "web_root"};
|
||||
mg_http_serve_dir(c, &hm, &opts);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (ev == MG_EV_HTTP_MSG) {
|
||||
struct mg_http_serve_opts opts = {.root_dir = "web_root"};
|
||||
mg_http_serve_dir(c, hm, &opts);
|
||||
}
|
||||
(void) fn_data;
|
||||
(void) fn_data, (void) ev_data;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
|
||||
Reference in new issue
Block a user