mongoose/tutorials/http/file-upload-single-post/main.c

81 lines
2.8 KiB
C
Raw Normal View History

// Copyright (c) 2020-2024 Cesanta Software Limited
2020-12-05 19:26:32 +08:00
// All rights reserved
//
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-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 10:36:35 +08:00
#define UPLOAD_DIR "/tmp"
struct upload_state {
size_t expected; // POST data length, bytes
size_t received; // Already received bytes
void *fp; // Opened uploaded file
};
2024-03-02 07:29:12 +08:00
static void handle_uploads(struct mg_connection *c, int ev, void *ev_data) {
2024-03-02 10:36:35 +08:00
struct upload_state *us = (struct upload_state *) c->data;
struct mg_fs *fs = &mg_fs_posix;
2024-03-02 07:29:12 +08:00
2024-03-02 10:36:35 +08:00
// Catch /upload requests early, without buffering whole body
// When we receive MG_EV_HTTP_HDRS event, that means we've received all
// HTTP headers but not necessarily full HTTP body
2024-03-02 07:29:12 +08:00
if (ev == MG_EV_HTTP_HDRS) {
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
2024-03-02 10:36:35 +08:00
if (mg_match(hm->uri, mg_str("/upload/*"), NULL)) {
char path[MG_PATH_MAX];
mg_snprintf(path, sizeof(path), "%s/%.*s", UPLOAD_DIR, hm->uri.len - 8,
2024-03-15 15:42:24 +08:00
hm->uri.buf + 8);
2024-03-02 10:36:35 +08:00
us->expected = hm->body.len; // Store number of bytes we expect
2024-03-02 07:29:12 +08:00
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
2024-04-22 22:27:22 +08:00
if (mg_path_is_sane(mg_str(path))) {
2024-03-02 10:36:35 +08:00
fs->rm(path); // Delete file if it exists
us->fp = fs->op(path, MG_FS_WRITE); // Open file for writing
}
2024-03-02 07:29:12 +08:00
}
}
// Catch uploaded file data for both MG_EV_READ and MG_EV_HTTP_HDRS
2024-03-02 10:36:35 +08:00
if (us->expected > 0 && c->recv.len > 0) {
us->received += c->recv.len;
if (us->fp) fs->wr(us->fp, c->recv.buf, c->recv.len); // Write to file
2024-03-02 07:29:12 +08:00
c->recv.len = 0; // Delete received data
2024-03-02 10:36:35 +08:00
if (us->received >= us->expected) {
2024-03-02 07:29:12 +08:00
// Uploaded everything. Send response back
2024-03-02 10:36:35 +08:00
MG_INFO(("Uploaded %lu bytes", us->received));
mg_http_reply(c, 200, NULL, "%lu ok\n", us->received);
if (us->fp) fs->cl(us->fp); // Close file
memset(us, 0, sizeof(*us)); // Cleanup upload state
c->is_draining = 1; // Close connection 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
2024-03-02 10:36:35 +08:00
// NOTE: handle_uploads() may delete request and reset c->pfn
if (ev == MG_EV_HTTP_MSG && c->pfn != NULL) {
2024-03-02 07:29:12 +08:00
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;
}