changeset 61:a93f4e3313e9

misc: add a raw option in POST data The previous paster.sh implementation was grep'ing the Location header which could fail if followlocation is enabled because multiple location can be returned by the HTTP server. And obviously while here, add the followlocation option.
author David Demelier <markand@malikania.fr>
date Thu, 26 May 2022 10:44:10 +0200
parents ecb0b90d94d8
children 9f0b3eb99640
files page-new.c paster.sh
diffstat 2 files changed, 24 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/page-new.c	Sun Jan 02 10:24:20 2022 +0100
+++ b/page-new.c	Thu May 26 10:44:10 2022 +0200
@@ -124,6 +124,7 @@
 		.visible        = true,
 		.duration       = PASTE_DURATION_DAY
 	};
+	int raw = 0;
 
 	for (size_t i = 0; i < r->fieldsz; ++i) {
 		const char *key = r->fields[i].key;
@@ -141,16 +142,29 @@
 			paste.code = estrdup(val);
 		else if (strcmp(key, "private") == 0)
 			paste.visible = strcmp(val, "on") != 0;
+		else if (strcmp(key, "raw") == 0) {
+			raw = strcmp(val, "on") == 0;
+		}
 	}
 
 	if (!database_insert(&paste))
 		page(r, NULL, KHTTP_500, "500.html");
 	else {
-		/* Redirect to paste details. */
-		khttp_head(r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_302]);
-		khttp_head(r, kresps[KRESP_LOCATION], "/paste/%s", paste.id);
-		khttp_body(r);
-		khttp_free(r);
+		if (raw) {
+			/* For CLI users (e.g. paster) just print the location. */
+			khttp_head(r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_201]);
+			khttp_body(r);
+			khttp_printf(r, "%s://%s/paste/%s\n",
+			    r->scheme == KSCHEME_HTTP ? "http" : "https",
+			    r->host, paste.id);
+			khttp_free(r);
+		} else {
+			/* Otherwise, redirect to paste details. */
+			khttp_head(r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_302]);
+			khttp_head(r, kresps[KRESP_LOCATION], "/paste/%s", paste.id);
+			khttp_body(r);
+			khttp_free(r);
+		}
 	}
 
 	paste_finish(&paste);
--- a/paster.sh	Sun Jan 02 10:24:20 2022 +0100
+++ b/paster.sh	Thu May 26 10:44:10 2022 +0200
@@ -238,13 +238,16 @@
 
 	if [ $verbose -eq 0 ]; then
 		with_verbose="-s"
+	else
+		with_verbose="-i"
 	fi
 
-	curl -i -X POST \
+	curl -L -X POST \
 		--data author="$author" \
 		--data language="$language" \
 		--data duration="$duration" \
 		--data title="$title" \
+		--data raw="on" \
 		--data-urlencode code@"$1" \
 		$with_private \
 		$with_verbose \
@@ -293,15 +296,4 @@
 	usage
 fi
 
-# If verbose, dump all headers.
-if [ $verbose -eq 1 ]; then
-	send "$1" "$2"
-else
-	url=$(send "$1" "$2" | grep -E "^Location: " | awk '{ print $2 }')
-
-	if [ -z "$url" ]; then
-		die "abort: error occured, retry with -v"
-	fi
-
-	printf "%s%s\n" "$2" "$url"
-fi
+send "$1" "$2"