Various operations in the /ctdl/p/ hierarchy to handle uploading of attachments
authorArt Cancro <ajc@citadel.org>
Fri, 6 Oct 2023 03:10:24 +0000 (23:10 -0400)
committerArt Cancro <ajc@citadel.org>
Fri, 6 Oct 2023 03:10:24 +0000 (23:10 -0400)
webcit-ng/api.txt
webcit-ng/server/request.c
webcit-ng/server/upload.c
webcit-ng/server/webcit.h

index 992a30f3e2c7fd9c4f17d33a13515616a55d16dc..189818ef6cd105b9d7fe6305ed02662658e813c1 100644 (file)
@@ -34,4 +34,6 @@ GET             /ctdl/a/biff                       Check for new mail
 GET             /ctdl/u/<username>/userpic         Returns an image containing the photo/avatar of the specified user
 GET             /ctdl/s/                           Static content (html, css, js, images...)
 GET             /.well-known/                      Static content (RFC5785 compliant paths)
-POST            /ctdl/p/                           Handler for uploading attachments and other file items
+POST            /ctdl/p                            Handler for uploading attachments and other file items
+GET             /ctdl/p/<id>                       Fetch a specific item that was uploaded
+DELETE          /ctdl/p/<id>                       Delete a specific item that was uploaded
index 4b8b3b1d1363a80d2e995e51bbbd0ebf961ddd08..e598cf1f68dd7c8c41499a66070449fbbd47e68f 100644 (file)
@@ -22,6 +22,13 @@ void do_404(struct http_transaction *h) {
 }
 
 
+// Method not allowed
+void do_405(struct http_transaction *h) {
+       h->response_code = 412;
+       h->response_string = strdup("METHOD NOT ALLOWED");
+}
+
+
 // Precondition failed (such as if-match)
 void do_412(struct http_transaction *h) {
        h->response_code = 412;
index 015fea14a38da310902d6a87153a2a256526e34e..711bbf3523af9f7c6d7d41aa7c5f70740b014d4a 100644 (file)
@@ -49,6 +49,19 @@ void upload_handler(char *name, char *filename, char *partnum, char *disp,
        }
        fwrite(content, length, 1, u.fp);                       // this file will be deleted by the OS when it is closed
 
+       // Add it to the list of uploads the server is holding.
+       pthread_mutex_lock(&upload_list_mutex);
+       if (upload_list == NULL) {
+               upload_list = array_new(sizeof(struct uploaded_file));
+       }
+       array_append(upload_list, &u);
+       pthread_mutex_unlock(&upload_list_mutex);
+
+       for (int i=0; i<array_len(upload_list); ++i) {
+               memcpy(&u, array_get_element_at(upload_list, i), sizeof(struct uploaded_file));
+               syslog(LOG_DEBUG, "%d: %s %s", i, u.id, u.filename);
+       }
+
        // Create a JSON object describing this upload
        JsonValue *j_one_upload = NewJsonObject(HKEY(""));
        JsonObjectAppend(j_one_upload, NewJsonPlainString(HKEY("ref"), u.id, -1));
@@ -87,12 +100,71 @@ void upload_files(struct http_transaction *h, struct ctdlsession *c) {
 }
 
 
+// Caller has requested /ctdl/p or /ctdl/p/ but we still have to dispatch based on the method
+void ctdl_p_base(struct http_transaction *h, struct ctdlsession *c) {
+       upload_files(h, c);             // we should only do this for POST requests
+}
+
+
+// Handle operations on a specific upload
+void specific_upload(struct http_transaction *h, struct ctdlsession *c, char *name) {
+       int i;
+       struct uploaded_file *u;
+       struct uploaded_file this_one;
+
+       if (upload_list == NULL) {
+               do_404(h);
+               return;
+       }
+
+       memset(&this_one, 0, sizeof(struct uploaded_file));
+       pthread_mutex_lock(&upload_list_mutex);
+       for (i=0; i<array_len(upload_list); ++i) {
+               u = (struct uploaded_file *) array_get_element_at(upload_list, i), sizeof(struct uploaded_file);
+               if (!strcmp(u->id, name)) {
+                       memcpy(&this_one, u, sizeof(struct uploaded_file));
+                       i = array_len(upload_list) + 1;         // Go out of scope; we're done here
+               }
+       }
+       pthread_mutex_unlock(&upload_list_mutex);
+
+       // If we found a matching ID, now dispatch based on the HTTP method.
+
+       if (IsEmptyStr(this_one.id)) {                          // didn't find a match
+               do_404(h);
+       }
+       else if (!strcasecmp(h->method, "GET")) {               // fetch the item
+               do_405(h);
+       }
+       else if (!strcasecmp(h->method, "DELETE")) {            // delete the item
+               do_405(h);
+       }
+       else {                                                  // unsupported method
+               do_405(h);
+       }
+}
+
+
 // Dispatcher for paths starting with /ctdl/p/
 void ctdl_p(struct http_transaction *h, struct ctdlsession *c) {
-       if (!strcasecmp(h->url, "/ctdl/p/")) {          // upload files
-               upload_files(h, c);
+       char buf[SIZ];
+
+       if (num_tokens(h->url, '/') == 3) {     //      /ctdl/p
+               ctdl_p_base(h, c);
                return;
        }
 
-       do_404(h);                                      // unknown
-}
+       extract_token(buf, h->url, 3, '/', sizeof buf);
+       if (num_tokens(h->url, '/') == 4) {
+               if (IsEmptyStr(buf)) {
+                       ctdl_p_base(h, c);      //      /ctdl/p/
+               }
+               else {
+                       specific_upload(h, c, &h->url[8]);
+               }
+               return;
+       }
+
+       // If we get to this point, the client requested an action we don't know how to perform.
+       do_404(h);
+}
\ No newline at end of file
index 7cc853a4de93a013f8a8aba908c59ac2cad5dcc2..a52ac59930bf530a3e5beef8bc0397c40c9007db 100644 (file)
@@ -130,6 +130,7 @@ void add_response_header(struct http_transaction *h, char *key, char *val);
 void perform_request(struct http_transaction *);
 void do_204(struct http_transaction *);
 void do_404(struct http_transaction *);
+void do_405(struct http_transaction *);
 void do_412(struct http_transaction *);
 void do_502(struct http_transaction *);
 void output_static(struct http_transaction *);