about summary refs log tree commit diff stats
path: root/adapter
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-01-03 14:40:21 +0100
committerbptato <nincsnevem662@gmail.com>2024-01-03 14:43:47 +0100
commitb512b00d161317422127d633b1ab8ad4412ebfc9 (patch)
tree3a20b28fd08a8d76a37dd9d6426e2cdbfaf850ee /adapter
parentae3c255a6a03224464cdc5f24eff8428147a7e7d (diff)
downloadchawan-b512b00d161317422127d633b1ab8ad4412ebfc9.tar.gz
Add spartan support
why not
Diffstat (limited to 'adapter')
-rwxr-xr-xadapter/protocol/spartan47
1 files changed, 47 insertions, 0 deletions
diff --git a/adapter/protocol/spartan b/adapter/protocol/spartan
new file mode 100755
index 00000000..ac0f590e
--- /dev/null
+++ b/adapter/protocol/spartan
@@ -0,0 +1,47 @@
+#!/bin/sh
+# Adapter for the spartan protocol. See: spartan://mozz.us
+# Requires netcat (nc).
+#
+# Usage: add the following line to your urimethodmap:
+#
+# spartan: /cgi-bin/spartan.cgi
+
+if ! type nc >/dev/null 2>/dev/null
+then	printf 'Cha-Control: ConnectionError 1 nc not found\n'
+	exit 1
+fi
+MAPPED_URI_PORT=${MAPPED_URI_PORT:=300} # default port is 300
+MAPPED_URI_PATH=${MAPPED_URI_PATH:=/} # needs / for root
+if test "$REQUEST_METHOD" != POST && test "$MAPPED_URI_QUERY" = "input"
+then	printf "Content-Type: text/html
+
+<!DOCTYPE html>
+<form method=POST action=\"$MAPPED_URI_PATH\">
+Input text to upload:
+<p>
+<textarea name=q></textarea>
+<p>
+<input type=submit>
+</form>"
+	exit 0
+fi
+CONTENT_LENGTH=${CONTENT_LENGTH:+$(($CONTENT_LENGTH - 2))} # skip q=
+CONTENT_LENGTH=${CONTENT_LENGTH:=0} # if missing, set to 0
+{
+	printf '%s %d\n' "$MAPPED_URI_HOST $MAPPED_URI_PATH" "$CONTENT_LENGTH"
+	tail -c+3 # skip q=
+} | nc "$MAPPED_URI_HOST" "$MAPPED_URI_PORT" 2>/dev/null | {
+	IFS= read -r line
+	case $line in
+	'2 '*)	ctype=${line#2 }
+		printf "Content-Type: %s\n\n" "$ctype"
+		# Spartan does something very rude by extending text/gemini
+		# for the spartan protocol only. Horrible hack to make it work:
+		test "${ctype%
}" = "text/gemini" &&
+			sed 's/^=: \([^ ]*\)/=> \1?input/' || cat ;;
+	'3 '*)	printf "Status: 301\nLocation: %s\n" "${line#3 }" ;;
+	'4 '*)	printf "Status: 403\n\n%s" "${line#4 }" ;;
+	'5 '*)	printf "Status: 500\n\n%s" "${line#5 }" ;;
+	*)	printf "Cha-Control: ConnectionError 1 malformed response\n"
+	esac
+}