From d3b150416989aae2fa4aebf097dac96d53cd2164 Mon Sep 17 00:00:00 2001 From: lassulus Date: Fri, 28 Apr 2023 23:11:21 +0200 Subject: vendor htgen-paste --- krebs/5pkgs/simple/htgen-paste/default.nix | 27 +++++++++++ krebs/5pkgs/simple/htgen-paste/src/htgen-paste | 63 ++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 krebs/5pkgs/simple/htgen-paste/default.nix create mode 100644 krebs/5pkgs/simple/htgen-paste/src/htgen-paste (limited to 'krebs/5pkgs/simple') diff --git a/krebs/5pkgs/simple/htgen-paste/default.nix b/krebs/5pkgs/simple/htgen-paste/default.nix new file mode 100644 index 000000000..48b72833e --- /dev/null +++ b/krebs/5pkgs/simple/htgen-paste/default.nix @@ -0,0 +1,27 @@ +{ pkgs, stockholm, stdenv }: +with stockholm.lib; + +stdenv.mkDerivation rec { + pname = "htgen-paste"; + version = "1.0.0"; + + src = ./src; + + buildPhase = '' + ( + exec > htgen-paste + echo PATH=${makeBinPath [ + pkgs.nix + pkgs.file + pkgs.coreutils + pkgs.findutils + ]} + echo STATEDIR=${shell.escape "\${STATEDIR-$HOME}"} + cat $src/htgen-paste + ) + ''; + + installPhase = '' + install -D htgen-paste $out/bin/htgen-paste + ''; +} diff --git a/krebs/5pkgs/simple/htgen-paste/src/htgen-paste b/krebs/5pkgs/simple/htgen-paste/src/htgen-paste new file mode 100644 index 000000000..bec238dca --- /dev/null +++ b/krebs/5pkgs/simple/htgen-paste/src/htgen-paste @@ -0,0 +1,63 @@ +find_item() { + if test ${#1} -ge 7; then + set -- "$(find "$STATEDIR/items" -mindepth 1 -maxdepth 1 \ + -regex "$STATEDIR/items/$1[0-9A-Za-z]*$")" + if test -n "$1" && test $(echo "$1" | wc -l) = 1; then + echo "$1" + return 0 + fi + fi + return 1 +} + +abs_path=${Request_URI%%\?*} + +case "$Method $abs_path" in + "GET /"[0-9a-z]*) + if item=$(find_item ${abs_path#/}); then + printf 'HTTP/1.1 200 OK\r\n' + printf 'Content-Type: %s\r\n' "$(file -ib $item)" + printf 'Server: %s\r\n' "$Server" + printf 'Connection: close\r\n' + printf 'Content-Length: %d\r\n' $(wc -c < $item) + printf '\r\n' + cat $item + exit + fi + ;; + "POST /") + content=$(mktemp -t htgen.$$.content.XXXXXXXX) + trap "rm $content >&2" EXIT + + case ${req_expect-} in 100-continue) + printf 'HTTP/1.1 100 Continue\r\n\r\n' + esac + + head -c $req_content_length > $content + + sha256=$(sha256sum -b $content | cut -d\ -f1) + base32=$(nix-hash --to-base32 --type sha256 $sha256) + item=$STATEDIR/items/$base32 + ref=http://$req_host/$base32 + + if ! test -e $item; then + mkdir -v -p $STATEDIR/items >&2 + cp -v $content $item >&2 + fi + + base32short=$(echo $base32 | cut -b-7) + if item=$(find_item $base32short); then + ref=$(echo "$ref"; echo "http://$req_host/$base32short") + fi + + printf 'HTTP/1.1 200 OK\r\n' + printf 'Content-Type: text/plain; charset=UTF-8\r\n' + printf 'Server: %s\r\n' "$Server" + printf 'Connection: close\r\n' + printf 'Content-Length: %d\r\n' $(expr ${#ref} + 1) + printf '\r\n' + printf '%s\n' "$ref" + + exit + ;; +esac -- cgit v1.2.3 From 40345c34bf6ff873e89e12ec66927cc5ec996d93 Mon Sep 17 00:00:00 2001 From: lassulus Date: Fri, 28 Apr 2023 23:49:03 +0200 Subject: htgen-paste: allow setting of Content-Type --- krebs/5pkgs/simple/htgen-paste/src/htgen-paste | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'krebs/5pkgs/simple') diff --git a/krebs/5pkgs/simple/htgen-paste/src/htgen-paste b/krebs/5pkgs/simple/htgen-paste/src/htgen-paste index bec238dca..bc806e870 100644 --- a/krebs/5pkgs/simple/htgen-paste/src/htgen-paste +++ b/krebs/5pkgs/simple/htgen-paste/src/htgen-paste @@ -15,8 +15,9 @@ abs_path=${Request_URI%%\?*} case "$Method $abs_path" in "GET /"[0-9a-z]*) if item=$(find_item ${abs_path#/}); then + content_type=$(cat "$item".content_type 2>/dev/null || file -ib "$item") printf 'HTTP/1.1 200 OK\r\n' - printf 'Content-Type: %s\r\n' "$(file -ib $item)" + printf 'Content-Type: %s\r\n' "$content_type" printf 'Server: %s\r\n' "$Server" printf 'Connection: close\r\n' printf 'Content-Length: %d\r\n' $(wc -c < $item) @@ -45,6 +46,10 @@ case "$Method $abs_path" in cp -v $content $item >&2 fi + if test -n ${reg_content_type-}; then + echo -n "$req_content_type" > "$item".content_type + fi + base32short=$(echo $base32 | cut -b-7) if item=$(find_item $base32short); then ref=$(echo "$ref"; echo "http://$req_host/$base32short") -- cgit v1.2.3 From 194781fa5eb2fd5e9be3ac4fa57872659fec512b Mon Sep 17 00:00:00 2001 From: lassulus Date: Wed, 3 May 2023 12:28:46 +0200 Subject: kpaste: allow override of Content-Type --- krebs/5pkgs/simple/kpaste/default.nix | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'krebs/5pkgs/simple') diff --git a/krebs/5pkgs/simple/kpaste/default.nix b/krebs/5pkgs/simple/kpaste/default.nix index 9820c931d..1c0225af7 100644 --- a/krebs/5pkgs/simple/kpaste/default.nix +++ b/krebs/5pkgs/simple/kpaste/default.nix @@ -1,6 +1,7 @@ { curl, gnused, writeDashBin }: writeDashBin "kpaste" '' - ${curl}/bin/curl -sS http://p.r --data-binary @"''${1:--}" | + ${curl}/bin/curl -sS http://p.r --data-binary @"''${1:--}" \ + -H "Content-Type-Override: ''${KPASTE_CONTENT_TYPE-}" ${gnused}/bin/sed '$ {p;s|http://p.r|https://p.krebsco.de|}' '' -- cgit v1.2.3 From a2b03cb28c97be3e9ccd382f7003f128508f1018 Mon Sep 17 00:00:00 2001 From: lassulus Date: Fri, 5 May 2023 01:32:18 +0200 Subject: kpaste: fix public url --- krebs/5pkgs/simple/kpaste/default.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'krebs/5pkgs/simple') diff --git a/krebs/5pkgs/simple/kpaste/default.nix b/krebs/5pkgs/simple/kpaste/default.nix index 1c0225af7..de2de4721 100644 --- a/krebs/5pkgs/simple/kpaste/default.nix +++ b/krebs/5pkgs/simple/kpaste/default.nix @@ -2,6 +2,6 @@ writeDashBin "kpaste" '' ${curl}/bin/curl -sS http://p.r --data-binary @"''${1:--}" \ - -H "Content-Type-Override: ''${KPASTE_CONTENT_TYPE-}" + -H "Content-Type-Override: ''${KPASTE_CONTENT_TYPE-}" | ${gnused}/bin/sed '$ {p;s|http://p.r|https://p.krebsco.de|}' '' -- cgit v1.2.3