2024-01-20 17:43:24 +08:00
|
|
|
// Copyright (c) 2020-2024 Cesanta Software Limited
|
2020-12-05 19:26:32 +08:00
|
|
|
// All rights reserved
|
2022-02-09 20:24:06 +08:00
|
|
|
//
|
2023-09-27 02:59:42 +08:00
|
|
|
// Streaming upload example. Demonstrates how to use MG_EV_READ events
|
2024-03-02 07:29:12 +08:00
|
|
|
// to save a large file without buffering it fully in memory.
|
2022-02-09 20:24:06 +08:00
|
|
|
//
|
2022-02-10 19:56:55 +08:00
|
|
|
// curl http://localhost:8000/upload?name=a.txt --data-binary @large_file.txt
|
2020-12-05 19:26:32 +08:00
|
|
|
|
|
|
|
#include "mongoose.h"
|
|
|
|
|
2024-03-02 07:29:12 +08:00
|
|
|
static void handle_uploads(struct mg_connection *c, int ev, void *ev_data) {
|
|
|
|
size_t *data = (size_t *) c->data;
|
|
|
|
|
|
|
|
// Catch /upload requests early, without buffering whole body:
|
|
|
|
if (ev == MG_EV_HTTP_HDRS) {
|
|
|
|
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
|
|
|
if (mg_match(hm->uri, mg_str("/upload"), NULL)) {
|
|
|
|
// When we receive MG_EV_HTTP_HDRS event, that means we've received all
|
|
|
|
// HTTP headers but not necessarily full HTTP body. We save HTTP body
|
|
|
|
// length in data[0]:
|
|
|
|
// data[0] contains expected number of bytes
|
|
|
|
// data[1] contains received number of bytes
|
|
|
|
data[0] = hm->body.len; // Store number of bytes we expect
|
|
|
|
mg_iobuf_del(&c->recv, 0, hm->head.len); // Delete HTTP headers
|
|
|
|
c->pfn = NULL; // Silence HTTP protocol handler, we'll use MG_EV_READ
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Catch uploaded file data for both MG_EV_READ and MG_EV_HTTP_HDRS
|
|
|
|
if (data[0] > 0 && c->recv.len > 0) {
|
|
|
|
data[1] += c->recv.len;
|
|
|
|
// MG_DEBUG(("Got chunk len %lu, %lu total", c->recv.len, data[1]));
|
|
|
|
c->recv.len = 0; // Delete received data
|
|
|
|
if (data[1] >= data[0]) {
|
|
|
|
// Uploaded everything. Send response back
|
|
|
|
MG_INFO(("Uploaded %lu bytes", data[1]));
|
|
|
|
mg_http_reply(c, 200, NULL, "%lu ok\n", data[1]);
|
|
|
|
c->is_draining = 1; // Close us when response gets sent
|
2020-12-05 19:26:32 +08:00
|
|
|
}
|
|
|
|
}
|
2024-03-02 07:29:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void fn(struct mg_connection *c, int ev, void *ev_data) {
|
|
|
|
handle_uploads(c, ev, ev_data);
|
|
|
|
|
|
|
|
// Non-upload requests, we serve normally
|
|
|
|
if (ev == MG_EV_HTTP_MSG) {
|
|
|
|
struct mg_http_serve_opts opts = {.root_dir = "web_root"};
|
|
|
|
mg_http_serve_dir(c, ev_data, &opts);
|
|
|
|
}
|
2020-12-05 19:26:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(void) {
|
|
|
|
struct mg_mgr mgr;
|
|
|
|
|
|
|
|
mg_mgr_init(&mgr);
|
2022-08-01 18:19:32 +08:00
|
|
|
mg_log_set(MG_LL_DEBUG); // Set debug log level
|
2024-03-02 07:29:12 +08:00
|
|
|
mg_http_listen(&mgr, "http://localhost:8000", fn, NULL);
|
2020-12-05 19:26:32 +08:00
|
|
|
|
|
|
|
for (;;) mg_mgr_poll(&mgr, 50);
|
|
|
|
mg_mgr_free(&mgr);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|