3 // Copyright (c) 1996-2023 by the citadel.org team
5 // This program is open source software. Use, duplication, or
6 // disclosure are subject to the GNU General Public License v3.
10 struct uploaded_file {
13 char content_type[256];
18 Array *upload_list = NULL; // all files uploaded to this webcit instance
19 pthread_mutex_t upload_list_mutex = PTHREAD_MUTEX_INITIALIZER; // Lock it before modifying
22 // This function is called by the MIME parser to handle data uploaded by the browser.
23 void upload_handler(char *name, char *filename, char *partnum, char *disp,
24 void *content, char *cbtype, char *cbcharset,
25 size_t length, char *encoding, char *cbid, void *userdata)
27 syslog(LOG_DEBUG, "upload_handler()");
28 syslog(LOG_DEBUG, " name: %s", name);
29 syslog(LOG_DEBUG, " filename: %s", filename);
30 syslog(LOG_DEBUG, " part number: %s", partnum);
31 syslog(LOG_DEBUG, " disposition: %s", disp);
32 syslog(LOG_DEBUG, "content type: %s", cbtype);
33 syslog(LOG_DEBUG, " char set: %s", cbcharset);
34 syslog(LOG_DEBUG, " length: %ld", length);
35 syslog(LOG_DEBUG, " encoding: %s", encoding);
36 syslog(LOG_DEBUG, " id: %s", cbid);
38 struct uploaded_file u;
40 safestrncpy(u.filename, filename, sizeof(u.filename));
41 safestrncpy(u.content_type, cbtype, sizeof(u.content_type));
44 // Write the upload to a file that we can access later when the user saves the message.
47 syslog(LOG_ERR, "upload: %m");
50 fwrite(content, length, 1, u.fp); // this file will be deleted by the OS when it is closed
52 // Create a JSON object describing this upload
53 JsonValue *j_one_upload = NewJsonObject(HKEY(""));
54 JsonObjectAppend(j_one_upload, NewJsonPlainString(HKEY("ref"), u.id, -1));
55 JsonObjectAppend(j_one_upload, NewJsonPlainString(HKEY("uploadfilename"), u.filename, -1));
56 JsonObjectAppend(j_one_upload, NewJsonPlainString(HKEY("contenttype"), u.content_type, -1));
57 JsonObjectAppend(j_one_upload, NewJsonNumber(HKEY("contentlength"), u.length));
59 // ...and attach it to the array of uploads
60 JsonValue *j_uploads = (JsonValue *) userdata;
61 JsonArrayAppend(j_uploads, j_one_upload);
65 void upload_files(struct http_transaction *h, struct ctdlsession *c) {
66 // FIXME reject uploads if we're not logged in
68 // This will be a JSON Array of all files that were uploaded during this HTTP transaction.
69 // Normally the browser will upload only one file per transaction, but that behavior is not guaranteed.
70 JsonValue *j_uploads = NewJsonArray(HKEY(""));
72 // h->request_body will contain the upload(s) in MIME format
73 mime_parser(h->request_body, (h->request_body + h->request_body_length), *upload_handler, NULL, NULL, j_uploads, 0);
75 // probably do something more clever here
76 h->response_code = 200;
77 h->response_string = strdup("OK");
79 // send back a JSON array of all files uploaded
80 StrBuf *sj = NewStrBuf();
81 SerializeJson(sj, j_uploads, 1); // '1' == free the source object
82 add_response_header(h, strdup("Content-type"), strdup("application/json"));
83 h->response_code = 200;
84 h->response_string = strdup("OK");
85 h->response_body_length = StrLength(sj);
86 h->response_body = SmashStrBuf(&sj);
90 // Dispatcher for paths starting with /ctdl/p/
91 void ctdl_p(struct http_transaction *h, struct ctdlsession *c) {
92 if (!strcasecmp(h->url, "/ctdl/p/")) { // upload files