summary refs log tree commit diff
path: root/www
diff options
context:
space:
mode:
Diffstat (limited to 'www')
-rw-r--r--www/temp.causal.agency/up.c56
1 files changed, 38 insertions, 18 deletions
diff --git a/www/temp.causal.agency/up.c b/www/temp.causal.agency/up.c
index 9e7b4ff7..af45446e 100644
--- a/www/temp.causal.agency/up.c
+++ b/www/temp.causal.agency/up.c
@@ -45,6 +45,27 @@ static enum kcgi_err fail(struct kreq *req, enum khttp http) {
 		|| khttp_printf(req, "%s\n", khttps[http]);
 }
 
+static const char *upload(const char *ext, void *ptr, size_t len) {
+	static char name[256];
+	snprintf(
+		name, sizeof(name), "%jx%08x%s%s",
+		(intmax_t)time(NULL), arc4random(),
+		(ext && ext[0] != '.' ? "." : ""), (ext ? ext : "")
+	);
+	int fd = openat(cwd, name, O_CREAT | O_EXCL | O_WRONLY, 0644);
+	if (fd < 0) {
+		warn("%s", name);
+		return NULL;
+	}
+	ssize_t n = write(fd, ptr, len);
+	int error = close(fd);
+	if (n < 0 || error) {
+		warn("%s", name);
+		return NULL;
+	}
+	return name;
+}
+
 static enum kcgi_err handle(struct kreq *req) {
 	if (req->page) return fail(req, KHTTP_404);
 
@@ -83,31 +104,30 @@ static enum kcgi_err handle(struct kreq *req) {
 		struct kpair *field = req->fieldmap[0];
 		if (!field || !field->valsz) return fail(req, KHTTP_400);
 
-		char name[256];
 		const char *ext = strrchr(field->file, '.');
-		if (!ext) ext = "";
-		snprintf(
-			name, sizeof(name), "%jx%08x%s",
-			(intmax_t)time(NULL), arc4random(), ext
-		);
-
-		int fd = openat(cwd, name, O_CREAT | O_EXCL | O_WRONLY, 0644);
-		if (fd < 0) {
-			warn("openat");
-			return fail(req, KHTTP_507);
-		}
-		ssize_t len = write(fd, field->val, field->valsz);
-		int error = close(fd);
-		if (len < 0 || error) {
-			warn("write");
-			return fail(req, KHTTP_507);
-		}
+		const char *name = upload(ext, field->val, field->valsz);
+		if (!name) return fail(req, KHTTP_507);
 
 		return head(req, KHTTP_303, KMIME_TEXT_PLAIN)
 			|| khttp_head(req, kresps[KRESP_LOCATION], "/%s", name)
 			|| khttp_body(req)
 			|| khttp_puts(req, name);
 
+	} else if (req->method == KMETHOD_PUT) {
+		struct kpair *field = req->fields;
+		if (!field || !field->valsz) return fail(req, KHTTP_400);
+
+		const char *ext = req->suffix;
+		if (!ext[0]) ext = strrchr(field->file, '.');
+		const char *name = upload(ext, field->val, field->valsz);
+		if (!name) return fail(req, KHTTP_507);
+
+		return head(req, KHTTP_200, KMIME_TEXT_PLAIN)
+			|| khttp_body(req)
+			|| khttp_printf(
+				req, "%s://%s/%s\n", kschemes[req->scheme], req->host, name
+			);
+
 	} else {
 		return fail(req, KHTTP_405);
 	}
> 2020-03-09Import /usr/src/bin/sh from FreeBSD 12.1-RELEASEJune McEnroe 2020-03-09Remove 1sh sourcesJune McEnroe I'm going to recreate it from fresh sh sources for clean git history. 2020-03-08Add The Stone SkyJune McEnroe 2020-03-08Publish "How I Relay Chat"June McEnroe 2020-03-03Don't use $ inside $(())June McEnroe 2020-03-03Remove setoptJune McEnroe 2020-03-03Use getopts in shell scriptsJune McEnroe WTF why did no one tell me about this? 2020-02-27Style %T outside of Rs in italicJune McEnroe 2020-02-26Add Fierce Femmes and Notorious LiarsJune McEnroe 2020-02-23Add This Is How You Lose the Time WarJune McEnroe 2020-02-22Add See Ya LaterJune McEnroe 2020-02-20Remove wiki scriptJune McEnroe Wikipedia seems to have removed the one-sentence extracts from the opensearch results. Too bad. It's not a wiki script, what we need is a command that fetches single-sentence summaries of articles on Wikipedia. 2020-02-19Add The Obelisk GateJune McEnroe 2020-02-17Add Four Tet — HandsJune McEnroe One from the cafe that caught my attention. 2020-02-12Simplify macOS notify-sendJune McEnroe 2020-02-12Add imbox and notemap to pageJune McEnroe 2020-02-12Collapse simple linksJune McEnroe 2020-02-12Move catgirl up the pageJune McEnroe 2020-02-12Update catgirl pty grabJune McEnroe 2020-02-12Link to cgit /about pages where appropriateJune McEnroe 2020-02-11Separate LINKS from BINS for html to workJune McEnroe 2020-02-11Add margin to Bl-bullet itemsJune McEnroe 2020-02-10Match URLs inside parens or with paired parens insideJune McEnroe 2020-02-10Duplicate effective URL before passing it back to curlJune McEnroe Apparently sometimes it didn't like receiving its own internal storage to parse again. Understandable. 2020-02-09Add To Be Taught, If FortunateJune McEnroe 2020-02-04Add The Future of Another TimelineJune McEnroe Wow. One of the best I've read. 2020-01-31Reorganize the Makefile for the umpteenth timeJune McEnroe Broke out LDLIBS for each bin, and made everything more uniform. 2020-01-28Change scout sensitivity to 1.4June McEnroe idk it seems to work. 2020-01-28Import shows.txtJune McEnroe