110241c3c5c23268699b3fc5e8d6dfe5af6bfd6b
[citadel.git] / webcit-ng / server / upload.c
1 // Upload handler
2 //
3 // Copyright (c) 1996-2023 by the citadel.org team
4 //
5 // This program is open source software.  Use, duplication, or
6 // disclosure are subject to the GNU General Public License v3.
7
8 #include "webcit.h"
9
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
12
13
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)
18 {
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);
29
30         struct uploaded_file u;
31         generate_uuid(u.id);
32         safestrncpy(u.filename, filename, sizeof(u.filename));
33         safestrncpy(u.content_type, cbtype, sizeof(u.content_type));
34         u.length = length;
35
36         // Write the upload to a file that we can access later when the user saves the message.
37         u.fp = tmpfile();
38         if (!u.fp) {
39                 syslog(LOG_ERR, "upload: %m");
40                 return;
41         }
42         fwrite(content, length, 1, u.fp);                       // this file will be deleted by the OS when it is closed
43
44         // Add it to the list of uploads the server is holding.
45         pthread_mutex_lock(&upload_list_mutex);
46         if (upload_list == NULL) {
47                 upload_list = array_new(sizeof(struct uploaded_file));
48         }
49         array_append(upload_list, &u);
50         pthread_mutex_unlock(&upload_list_mutex);
51
52         for (int i=0; i<array_len(upload_list); ++i) {
53                 memcpy(&u, array_get_element_at(upload_list, i), sizeof(struct uploaded_file));
54                 syslog(LOG_DEBUG, "%d: %s %s", i, u.id, u.filename);
55         }
56
57         // Create a JSON object describing this upload
58         JsonValue *j_one_upload = NewJsonObject(HKEY(""));
59         JsonObjectAppend(j_one_upload, NewJsonPlainString(HKEY("ref"), u.id, -1));
60         JsonObjectAppend(j_one_upload, NewJsonPlainString(HKEY("uploadfilename"), u.filename, -1));
61         JsonObjectAppend(j_one_upload, NewJsonPlainString(HKEY("contenttype"), u.content_type, -1));
62         JsonObjectAppend(j_one_upload, NewJsonNumber(HKEY("contentlength"), u.length));
63
64         // ...and attach it to the array of uploads
65         JsonValue *j_uploads = (JsonValue *) userdata;
66         JsonArrayAppend(j_uploads, j_one_upload);
67 }
68
69 // upload handler
70 void upload_files(struct http_transaction *h, struct ctdlsession *c) {
71         // FIXME reject uploads if we're not logged in
72
73         // This will be a JSON Array of all files that were uploaded during this HTTP transaction.
74         // Normally the browser will upload only one file per transaction, but that behavior is not guaranteed.
75         JsonValue *j_uploads = NewJsonArray(HKEY(""));
76
77         // h->request_body will contain the upload(s) in MIME format
78         mime_parser(h->request_body, (h->request_body + h->request_body_length), *upload_handler, NULL, NULL, j_uploads, 0);
79
80         // probably do something more clever here
81         h->response_code = 200;
82         h->response_string = strdup("OK");
83
84         // send back a JSON array of all files uploaded
85         StrBuf *sj = NewStrBuf();
86         SerializeJson(sj, j_uploads, 1);        // '1' == free the source object
87         add_response_header(h, strdup("Content-type"), strdup("application/json"));
88         h->response_code = 200;
89         h->response_string = strdup("OK");
90         h->response_body_length = StrLength(sj);
91         h->response_body = SmashStrBuf(&sj);
92 }
93
94
95 // Caller has requested /ctdl/p or /ctdl/p/ but we still have to dispatch based on the method
96 void ctdl_p_base(struct http_transaction *h, struct ctdlsession *c) {
97         upload_files(h, c);             // we should only do this for POST requests
98 }
99
100
101 // delete an uploaded item
102 void delete_upload(struct uploaded_file this_one) {
103         int i;
104         struct uploaded_file *u;
105
106         pthread_mutex_lock(&upload_list_mutex);
107         for (i=0; i<array_len(upload_list); ++i) {
108                 u = (struct uploaded_file *) array_get_element_at(upload_list, i), sizeof(struct uploaded_file);
109                 if (!strcmp(u->id, this_one.id)) {
110                         fclose(u->fp);                          // this deletes the file because it has 0 links
111                         array_delete_element_at(upload_list, i);
112                         i = array_len(upload_list) + 1;         // Go out of scope; we're done here
113                 }
114         }
115         pthread_mutex_unlock(&upload_list_mutex);
116 }
117
118
119 // DAV delete an uploaded item
120 void dav_delete_upload(struct http_transaction *h, struct ctdlsession *c, struct uploaded_file this_one) {
121         delete_upload(this_one);
122         do_204(h);
123 }
124
125
126 // Handle operations on a specific upload
127 void specific_upload(struct http_transaction *h, struct ctdlsession *c, char *name) {
128         int i;
129         struct uploaded_file *u;
130         struct uploaded_file this_one;
131
132         if (upload_list == NULL) {
133                 do_404(h);
134                 return;
135         }
136
137         memset(&this_one, 0, sizeof(struct uploaded_file));
138         pthread_mutex_lock(&upload_list_mutex);
139         for (i=0; i<array_len(upload_list); ++i) {
140                 u = (struct uploaded_file *) array_get_element_at(upload_list, i), sizeof(struct uploaded_file);
141                 if (!strcmp(u->id, name)) {
142                         memcpy(&this_one, u, sizeof(struct uploaded_file));
143                         i = array_len(upload_list) + 1;         // Go out of scope; we're done here
144                 }
145         }
146         pthread_mutex_unlock(&upload_list_mutex);
147
148         // If we found a matching ID, now dispatch based on the HTTP method.
149
150         if (IsEmptyStr(this_one.id)) {                          // didn't find a match
151                 do_404(h);
152         }
153         else if (!strcasecmp(h->method, "GET")) {               // fetch the item
154                 do_405(h);
155         }
156         else if (!strcasecmp(h->method, "DELETE")) {            // delete the item
157                 dav_delete_upload(h, c, this_one);
158         }
159         else {                                                  // unsupported method
160                 do_405(h);
161         }
162 }
163
164
165 // Dispatcher for paths starting with /ctdl/p/
166 void ctdl_p(struct http_transaction *h, struct ctdlsession *c) {
167         char buf[SIZ];
168
169         if (num_tokens(h->url, '/') == 3) {     //      /ctdl/p
170                 ctdl_p_base(h, c);
171                 return;
172         }
173
174         extract_token(buf, h->url, 3, '/', sizeof buf);
175         if (num_tokens(h->url, '/') == 4) {
176                 if (IsEmptyStr(buf)) {
177                         ctdl_p_base(h, c);      //      /ctdl/p/
178                 }
179                 else {
180                         specific_upload(h, c, &h->url[8]);
181                 }
182                 return;
183         }
184
185         // If we get to this point, the client requested an action we don't know how to perform.
186         do_404(h);
187 }