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 Array *upload_list = NULL; // all files uploaded to this webcit instance
11 pthread_mutex_t upload_list_mutex = PTHREAD_MUTEX_INITIALIZER; // Lock it before modifying
14 // This function is called by the MIME parser to handle data uploaded by the browser.
15 void upload_handler(char *name, char *filename, char *partnum, char *disp,
16 void *content, char *cbtype, char *cbcharset,
17 size_t length, char *encoding, char *cbid, void *userdata)
19 syslog(LOG_DEBUG, "upload_handler()");
20 syslog(LOG_DEBUG, " name: %s", name);
21 syslog(LOG_DEBUG, " filename: %s", filename);
22 syslog(LOG_DEBUG, " part number: %s", partnum);
23 syslog(LOG_DEBUG, " disposition: %s", disp);
24 syslog(LOG_DEBUG, "content type: %s", cbtype);
25 syslog(LOG_DEBUG, " char set: %s", cbcharset);
26 syslog(LOG_DEBUG, " length: %ld", length);
27 syslog(LOG_DEBUG, " encoding: %s", encoding);
28 syslog(LOG_DEBUG, " id: %s", cbid);
30 struct uploaded_file u;
32 // create a random ID for the attachment
33 for (int i=0; i<sizeof(u.id); ++i) {
34 u.id[i] = (rand() % 26) + 'a';
36 u.id[sizeof(u.id)-1] = 0;
38 safestrncpy(u.filename, filename, sizeof(u.filename));
39 safestrncpy(u.content_type, cbtype, sizeof(u.content_type));
42 // Write the upload to a file that we can access later when the user saves the message.
45 syslog(LOG_ERR, "upload: %m");
48 fwrite(content, length, 1, u.fp); // this file will be deleted by the OS when it is closed
50 // Add it to the list of uploads the server is holding.
51 pthread_mutex_lock(&upload_list_mutex);
52 if (upload_list == NULL) {
53 upload_list = array_new(sizeof(struct uploaded_file));
55 array_append(upload_list, &u);
56 pthread_mutex_unlock(&upload_list_mutex);
58 for (int i=0; i<array_len(upload_list); ++i) {
59 memcpy(&u, array_get_element_at(upload_list, i), sizeof(struct uploaded_file));
60 syslog(LOG_DEBUG, "%d: %s %s", i, u.id, u.filename);
63 // Create a JSON object describing this upload
64 JsonValue *j_one_upload = NewJsonObject(HKEY(""));
65 JsonObjectAppend(j_one_upload, NewJsonPlainString(HKEY("ref"), u.id, -1));
66 JsonObjectAppend(j_one_upload, NewJsonPlainString(HKEY("uploadfilename"), u.filename, -1));
67 JsonObjectAppend(j_one_upload, NewJsonPlainString(HKEY("contenttype"), u.content_type, -1));
68 JsonObjectAppend(j_one_upload, NewJsonNumber(HKEY("contentlength"), u.length));
70 // ...and attach it to the array of uploads
71 JsonValue *j_uploads = (JsonValue *) userdata;
72 JsonArrayAppend(j_uploads, j_one_upload);
76 void upload_files(struct http_transaction *h, struct ctdlsession *c) {
77 // FIXME reject uploads if we're not logged in
79 // This will be a JSON Array of all files that were uploaded during this HTTP transaction.
80 // Normally the browser will upload only one file per transaction, but that behavior is not guaranteed.
81 JsonValue *j_uploads = NewJsonArray(HKEY(""));
83 // h->request_body will contain the upload(s) in MIME format
84 mime_parser(h->request_body, (h->request_body + h->request_body_length), *upload_handler, NULL, NULL, j_uploads, 0);
86 // probably do something more clever here
87 h->response_code = 200;
88 h->response_string = strdup("OK");
90 // send back a JSON array of all files uploaded
91 StrBuf *sj = NewStrBuf();
92 SerializeJson(sj, j_uploads, 1); // '1' == free the source object
93 add_response_header(h, strdup("Content-type"), strdup("application/json"));
94 h->response_code = 200;
95 h->response_string = strdup("OK");
96 h->response_body_length = StrLength(sj);
97 h->response_body = SmashStrBuf(&sj);
101 // Caller has requested /ctdl/p or /ctdl/p/ but we still have to dispatch based on the method
102 void ctdl_p_base(struct http_transaction *h, struct ctdlsession *c) {
103 upload_files(h, c); // we should only do this for POST requests
107 // delete an uploaded item
108 void delete_upload(struct uploaded_file this_one) {
110 struct uploaded_file *u;
112 pthread_mutex_lock(&upload_list_mutex);
113 for (i=0; i<array_len(upload_list); ++i) {
114 u = (struct uploaded_file *) array_get_element_at(upload_list, i), sizeof(struct uploaded_file);
115 if (!strcmp(u->id, this_one.id)) {
116 fclose(u->fp); // this deletes the file because it has 0 links
117 array_delete_element_at(upload_list, i);
118 i = array_len(upload_list) + 1; // Go out of scope; we're done here
121 pthread_mutex_unlock(&upload_list_mutex);
125 // DAV delete an uploaded item
126 void dav_delete_upload(struct http_transaction *h, struct ctdlsession *c, struct uploaded_file this_one) {
127 delete_upload(this_one);
132 // Remove an uploaded item from the upload_list. Caller now owns the file handle and is responsible for closing it.
133 struct uploaded_file pop_upload(char *id) {
135 struct uploaded_file *u;
136 struct uploaded_file ret;
138 memset(&ret, 0, sizeof(struct uploaded_file));
140 pthread_mutex_lock(&upload_list_mutex);
141 for (i=0; i<array_len(upload_list); ++i) {
142 u = (struct uploaded_file *) array_get_element_at(upload_list, i), sizeof(struct uploaded_file);
143 if (!strcmp(u->id, id)) {
145 array_delete_element_at(upload_list, i);
146 i = array_len(upload_list) + 1; // Go out of scope; we're done here
149 pthread_mutex_unlock(&upload_list_mutex);
151 return(ret); // ret will be all-zeroes if we didn't find it
155 // Handle operations on a specific upload
156 void specific_upload(struct http_transaction *h, struct ctdlsession *c, char *name) {
158 struct uploaded_file *u;
159 struct uploaded_file this_one;
161 if (upload_list == NULL) {
166 memset(&this_one, 0, sizeof(struct uploaded_file));
167 pthread_mutex_lock(&upload_list_mutex);
168 for (i=0; i<array_len(upload_list); ++i) {
169 u = (struct uploaded_file *) array_get_element_at(upload_list, i), sizeof(struct uploaded_file);
170 if (!strcmp(u->id, name)) {
171 memcpy(&this_one, u, sizeof(struct uploaded_file));
172 i = array_len(upload_list) + 1; // Go out of scope; we're done here
175 pthread_mutex_unlock(&upload_list_mutex);
177 // If we found a matching ID, now dispatch based on the HTTP method.
179 if (IsEmptyStr(this_one.id)) { // didn't find a match
182 else if (!strcasecmp(h->method, "GET")) { // fetch the item
185 else if (!strcasecmp(h->method, "DELETE")) { // delete the item
186 dav_delete_upload(h, c, this_one);
188 else { // unsupported method
194 // Dispatcher for paths starting with /ctdl/p/
195 void ctdl_p(struct http_transaction *h, struct ctdlsession *c) {
198 if (num_tokens(h->url, '/') == 3) { // /ctdl/p
203 extract_token(buf, h->url, 3, '/', sizeof buf);
204 if (num_tokens(h->url, '/') == 4) {
205 if (IsEmptyStr(buf)) {
206 ctdl_p_base(h, c); // /ctdl/p/
209 specific_upload(h, c, &h->url[8]);
214 // If we get to this point, the client requested an action we don't know how to perform.