shithub: front

Download patch

ref: c243d993aa70cd1582e87b1c4953817ad448cf41
parent: 804ac927aa183e34971718670499201ed6907bc8
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Mon Jun 30 06:44:35 EDT 2025

hget: add -e flag for errorbody contents

--- a/rc/bin/hget
+++ b/rc/bin/hget
@@ -2,10 +2,11 @@
 rfork e
 argv0=$0
 fn usage {
-	echo usage: $argv0 '[ -l | -o file] [ -p body | -P ] [ -r header ] [ -m method ] [ -b baseurl ] url' >[1=2]
+	echo usage: $argv0 '[ -l | -o file] [ -e file] [ -p body | -P ] [ -r header ] [ -m method ] [ -b baseurl ] url' >[1=2]
 	exit usage
 }
 s=0
+e=()
 o=()
 p=()
 P=()
@@ -15,6 +16,9 @@
 l=()
 while(~ $1 -*){
 	switch($1){
+	case -e
+		e=$2
+		shift
 	case -o
 		o=$2
 		shift
@@ -69,15 +73,23 @@
 		echo -n headers $i >[1=0]
 	if(~ $#m 1)
 		echo -n request $m >[1=0]
+	fn error {
+		err=$status
+		~ $#e 1 && cat $d/errorbody > $e
+		exit $err
+	}
 	if(! ~ $#p 0 || ! ~ $#P 0){
-		 >$d/postbody {
+		status = ''
+		@{ >$d/postbody {
 			if(! ~ $#p 0)
 				echo -n $"p
 			if(! ~ $#P 0)
 				cat <[0=3]
-		}
+			exit ''
+		}} || error
 	}
-	<$d/body {
+	status = ''
+	@{ <$d/body {
 		if(~ $#l 1){
 			echo `{cat $d/parsed/url}
 			exit
@@ -102,5 +114,5 @@
 			~ `{cat $d/contenttype >[2]/dev/null} *compress* || exec uncompress
 		}
 		exec cat
-	}
+	}} || error
 }
--- a/sys/man/1/hget
+++ b/sys/man/1/hget
@@ -9,6 +9,9 @@
 .B -o
 .I file
 ] [
+.B -e
+.I file
+] [
 .B -p
 .I body
 |
@@ -117,6 +120,14 @@
 but incomplete,
 .I hget
 will fetch the missing bytes.
+.PP
+By default,
+.I hget
+ignores the servers error response body.
+With the
+.B -e
+option, the error response can be written to
+.IR file .
 .PP
 Option
 .B -r
--