From: Art Cancro Date: Fri, 6 Oct 2023 03:10:24 +0000 (-0400) Subject: Various operations in the /ctdl/p/ hierarchy to handle uploading of attachments X-Git-Tag: v997~126 X-Git-Url: https://code.citadel.org/?p=citadel.git;a=commitdiff_plain;h=f3e77c1d487e8977bc41282553e31cc8145993f8 Various operations in the /ctdl/p/ hierarchy to handle uploading of attachments --- diff --git a/webcit-ng/api.txt b/webcit-ng/api.txt index 992a30f3e..189818ef6 100644 --- a/webcit-ng/api.txt +++ b/webcit-ng/api.txt @@ -34,4 +34,6 @@ GET /ctdl/a/biff Check for new mail GET /ctdl/u//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/ Fetch a specific item that was uploaded +DELETE /ctdl/p/ Delete a specific item that was uploaded diff --git a/webcit-ng/server/request.c b/webcit-ng/server/request.c index 4b8b3b1d1..e598cf1f6 100644 --- a/webcit-ng/server/request.c +++ b/webcit-ng/server/request.c @@ -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; diff --git a/webcit-ng/server/upload.c b/webcit-ng/server/upload.c index 015fea14a..711bbf352 100644 --- a/webcit-ng/server/upload.c +++ b/webcit-ng/server/upload.c @@ -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; iid, 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 diff --git a/webcit-ng/server/webcit.h b/webcit-ng/server/webcit.h index 7cc853a4d..a52ac5993 100644 --- a/webcit-ng/server/webcit.h +++ b/webcit-ng/server/webcit.h @@ -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 *);