Enhance upload example

This commit is contained in:
Sergey Lyubka 2024-03-02 02:36:35 +00:00
parent 00db56f7d7
commit 06a9fb3f53
2 changed files with 35 additions and 19 deletions

View File

@ -8,34 +8,49 @@
#include "mongoose.h"
static void handle_uploads(struct mg_connection *c, int ev, void *ev_data) {
size_t *data = (size_t *) c->data;
#define UPLOAD_DIR "/tmp"
// Catch /upload requests early, without buffering whole body:
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;
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)) {
// 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
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.ptr + 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(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 (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]));
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 (data[1] >= data[0]) {
if (us->received >= us->expected) {
// 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
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
}
}
}
@ -44,7 +59,8 @@ 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) {
// NOTE: handle_uploads() may delete request and reset c->pfn
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);
}

View File

@ -50,7 +50,7 @@
r.onload = function() {
ev.target.value = '';
document.getElementById('el3').innerText = 'Uploading...';
fetch('/upload?name=' + encodeURIComponent(f.name), {
fetch(`/upload/${encodeURIComponent(f.name)}`, {
method: 'POST',
body: r.result,
}).then(function(res) {