mirror of
https://github.com/cesanta/mongoose.git
synced 2026-10-17 10:49:47 +00:00
Add mg_http_stream_body() helper for generic streaming uploads
This commit is contained in:
1 parent
aa1dfe8000
commit
b93cbbeb13
6 files changed
+153
-50
No files matched your search
@@ -1,10 +1,11 @@
|
||||
// Copyright (c) 2020-2024 Cesanta Software Limited
|
||||
// All rights reserved
|
||||
//
|
||||
// Streaming upload example. Demonstrates how to use MG_EV_READ events
|
||||
// Streaming upload example. Demonstrates how to use mg_http_stream_body()
|
||||
// to save a large file without buffering it fully in memory.
|
||||
// The file is saved as /tmp/a.txt.
|
||||
//
|
||||
// curl http://localhost:8000/upload?name=a.txt --data-binary @large_file.txt
|
||||
// curl http://localhost:8000/fs/a.txt --data-binary @large_file.txt
|
||||
|
||||
#include "mongoose.h"
|
||||
|
||||
@@ -28,48 +29,24 @@ static const char *s_tls_key =
|
||||
"4hf5Gx17YJkq5/z3k6ogPDPpoAYWIw1/sw==\n"
|
||||
"-----END EC PRIVATE KEY-----\n";
|
||||
|
||||
struct upload_state {
|
||||
size_t expected; // POST data length, bytes
|
||||
size_t received; // Already received bytes
|
||||
void *fp; // Opened uploaded file
|
||||
};
|
||||
|
||||
static void handle_uploads(struct mg_connection *c, int ev, void *ev_data) {
|
||||
struct upload_state *us = (struct upload_state *) c->data;
|
||||
static void upload(struct mg_http_message *hm, struct mg_str *data, void **p) {
|
||||
struct mg_fs *fs = &mg_fs_posix;
|
||||
|
||||
// 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
|
||||
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)) {
|
||||
char path[MG_PATH_MAX];
|
||||
mg_snprintf(path, sizeof(path), "%s/%.*s", UPLOAD_DIR, hm->uri.len - 8,
|
||||
hm->uri.buf + 8);
|
||||
us->expected = 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
|
||||
if (mg_path_is_sane(mg_str(path))) {
|
||||
fs->rm(path); // Delete file if it exists
|
||||
us->fp = fs->op(path, MG_FS_WRITE); // Open file for writing
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Catch uploaded file data for both MG_EV_READ and MG_EV_HTTP_HDRS
|
||||
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
|
||||
c->recv.len = 0; // Delete received data
|
||||
if (us->received >= us->expected) {
|
||||
// Uploaded everything. Send response back
|
||||
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
|
||||
if (hm != NULL) {
|
||||
char path[MG_PATH_MAX];
|
||||
MG_INFO(("UPLOAD BEGIN, URI %.*s ", hm->uri.len, hm->uri.buf));
|
||||
mg_snprintf(path, sizeof(path), "%s/%.*s", UPLOAD_DIR, hm->uri.len - 4,
|
||||
hm->uri.buf + 4);
|
||||
*p = NULL;
|
||||
if (mg_path_is_sane(mg_str(path))) {
|
||||
fs->rm(path); // Delete file if it exists
|
||||
*p = fs->op(path, MG_FS_WRITE); // Open file for writing
|
||||
}
|
||||
} else if (data != NULL) {
|
||||
MG_INFO(("UPLOAD DATA, %zu bytes", data->len));
|
||||
if (*p) fs->wr(*p, data->buf, data->len); // Write to file
|
||||
} else {
|
||||
MG_INFO(("UPLOAD END %p", p));
|
||||
if (*p) fs->cl(*p), *p = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,16 +55,13 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
|
||||
struct mg_tls_opts opts = {.cert = mg_str(s_tls_cert),
|
||||
.key = mg_str(s_tls_key)};
|
||||
mg_tls_init(c, &opts);
|
||||
}
|
||||
|
||||
handle_uploads(c, ev, ev_data);
|
||||
|
||||
// Non-upload requests, we serve normally
|
||||
// NOTE: handle_uploads() may delete request and reset c->pfn
|
||||
if (ev == MG_EV_HTTP_MSG && c->pfn != NULL) {
|
||||
} else if (ev == MG_EV_HTTP_MSG && c->pfn != NULL) {
|
||||
struct mg_http_serve_opts opts = {.root_dir = "web_root"};
|
||||
mg_http_serve_dir(c, ev_data, &opts);
|
||||
}
|
||||
|
||||
// Handle upload requests to /fs/*
|
||||
mg_http_stream_body(c, ev, ev_data, mg_str("/fs/*"), upload);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
|
||||
@@ -50,7 +50,7 @@
|
||||
r.onload = function() {
|
||||
ev.target.value = '';
|
||||
document.getElementById('el3').innerText = 'Uploading...';
|
||||
fetch(`/upload/${encodeURIComponent(f.name)}`, {
|
||||
fetch(`/fs/${encodeURIComponent(f.name)}`, {
|
||||
method: 'POST',
|
||||
body: r.result,
|
||||
}).then(function(res) {
|
||||
|
||||
Reference in new issue
Block a user