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