From af313ccdaa0665c19df6b69b96916e7c176605ea Mon Sep 17 00:00:00 2001 From: Art Cancro Date: Tue, 26 Sep 2023 23:44:05 -0400 Subject: [PATCH] upload.c: write uploaded data to a temp file --- webcit-ng/server/upload.c | 51 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 webcit-ng/server/upload.c diff --git a/webcit-ng/server/upload.c b/webcit-ng/server/upload.c new file mode 100644 index 000000000..e2d7d1c30 --- /dev/null +++ b/webcit-ng/server/upload.c @@ -0,0 +1,51 @@ +// Upload handler +// +// Copyright (c) 1996-2022 by the citadel.org team +// +// This program is open source software. Use, duplication, or +// disclosure are subject to the GNU General Public License v3. + +#include "webcit.h" + +// This function is called by the MIME parser to handle data uploaded by the browser. +void upload_handler(char *name, char *filename, char *partnum, char *disp, + void *content, char *cbtype, char *cbcharset, + size_t length, char *encoding, char *cbid, void *userdata) +{ + syslog(LOG_DEBUG, "upload_handler()"); + syslog(LOG_DEBUG, " name: %s", name); + syslog(LOG_DEBUG, " filename: %s", filename); + syslog(LOG_DEBUG, " part number: %s", partnum); + syslog(LOG_DEBUG, " disposition: %s", disp); + syslog(LOG_DEBUG, "content type: %s", cbtype); + syslog(LOG_DEBUG, " char set: %s", cbcharset); + syslog(LOG_DEBUG, " length: %ld", length); + syslog(LOG_DEBUG, " encoding: %s", encoding); + syslog(LOG_DEBUG, " id: %s", cbid); + + // Write the upload to a file that we can pull later when the user saves the message. + char tempfile[PATH_MAX]; + snprintf(tempfile, sizeof tempfile, "/tmp/ctdl_upload_XXXXXX"); + int fd = mkstemp(tempfile); + if (fd < 0) { + syslog(LOG_ERR, "upload: %s: %m", tempfile); + return; + } + write(fd, content, length); + close(fd); +} + +// upload handler +void upload_files(struct http_transaction *h, struct ctdlsession *c) { + // FIXME reject uploads if we're not logged in + + // h->request_body will contain the upload(s) in MIME format + mime_parser(h->request_body, (h->request_body + h->request_body_length), *upload_handler, NULL, NULL, NULL, 0); + + // probably do something more clever here + h->response_code = 200; + h->response_string = strdup("OK"); + add_response_header(h, strdup("Content-type"), strdup("application/json")); + h->response_body = "{}"; + h->response_body_length = strlen(h->response_body); +} \ No newline at end of file -- 2.39.2