shithub: riscv

ref: 68537b7e9663c45f106b45d368dc0911a1dd87f3
dir: /rc/bin/formpost/

View raw version
#!/bin/rc
rfork e
argv0=$0
fn usage {
	echo usage: $argv0 '[-d] [-M] [ -u url ] [ -g action | -p action | -m action ] [ name:value | name:@value ]' ... >[1=2]
	exit usage
}
d=()
g=()
M=()
m=()
p=()
u=()
while(~ $1 -*){
	switch($1){
	case -d
		d=1
	case -g
		g=1
		a=$2
		shift
	case -M
		M=1
	case -m
		m=1
		a=$2
		shift
	case -p
		p=1
		a=$2
		shift
	case -u
		u=$2
		shift
	case *
		usage
	}
	shift
}
if(~ $#M 0 && ~ $#u 0)
	usage
fn geth{
	hget $u |
	sed 's/ACTION\=/action\=/g;
		s/ENCTYPE\=/enctype\=/g;
		s/FORM/form/g;
		s/INPUT/input/g;
		s/METHOD\=/method\=/g;
		s/NAME\=/name\=/g;
		s/OPTION/option/g;
		s/SELECT/select/g;
		s/TEXTAREA/textarea/g;
		s/TYPE\=/type\=/g' |
	awk '/<([ ]|[	])*form/,/<\/form>/ {print}' |
	sed 's/<\/form>/&\nHJFORMEND\n/g;
		s/<([ ]|[	])*/HJBOUNDARY/g;
		s/>(\n)*([ ]|[	])*HJBOUNDARY/\n/g;
		s/([ ]|[	])*HJBOUNDARY//g;
		s/^form/HJFORMSTART\n&/g' |
	grep -e '(^HJFORM|^form|^input|^option|^select|^textarea)' >$1
}
fn getf{
	a=`{grep -e '^form' $1 | sed 's/^.*action\=//g; s/^(\''|\"|[ ]*)//g; s/(\''|\"|[ ]|>).*$//g'}
	e=`{grep -e '^form' $1 | sed 's/^.*enctype\=//g; s/^(\''|\"|[ ]*)//g; s/(\''|\"|[ ]|>).*$//g'}
	m=`{grep -e '^form' $1 | sed 's/^.*method\=//g; s/^(\''|\"|[ ]*)//g; s/(\''|\"|[ ]|>).*$//g'}
	f=$a
	if(~ $#f 0)
		f=/
	if(~ $e multipart/form-data)
		f=($f m)
	if not if(~ $m post)
		f=($f p)
	if not
		f=($f g)
	ifs='
' {
	for(j in `{grep -e '(^input|^select|^textarea)' $1}){
		if(~ $d 1)
			echo $"j >>formpost.log #debug
		if(~ $j *name'='*)
			jname=`{echo $"j |
				sed 's/^.*name\=//g;
					s/^(\''|\"|[ ]*)//g;
					s/(\''|\"|[ ]|>).*$//g;
					s/^.*\=.*$//g'}
		if(~ $j *value'='*)
			jvalue=`{echo $"j |
				sed 's/^.*value\=//g;
					s/^(\''|\"|[ ]*)//g;
					s/(\''|\"|[ ]|>).*$//g;
					s/^.*\=.*$//g;
					s/[ ]/HJSPACE/g'}
		ff=`{echo $"jname':'$"jvalue |
			sed 's/^.*\=.*:/:/g;
				s/:.*\=.*$/:/g'}
		if(! ~ $ff :*)
			f=($"f $"ff)
		jname=()
		jvalue=()
	}
}
	echo $"f
}
fn mencode{
	f=()
	cr=`{echo x | tr x \015}
	while(! ~ $#1 0){
		switch($1){
		case *:*
			f=($f $1)
		case *
			usage
		}
		shift
	}
	for(i in $f){
		echo '--HJBOUNDARY'^$"cr
		fname=`{echo $"i | awk -F ':' '{print $1}'}
		fvalue=`{echo $"i | awk -F ':' '{print $2}'}
		if(~ $fvalue @*){
			fvalue=`{echo $"fvalue | sed 's/@//g'}
			echo 'Content-Disposition: form-data; name="'$"fname'"; filename="'^`{basename $fvalue}^'"'$"cr
			echo Content-Type: `{file -m $fvalue}^$"cr
			echo $cr
			cat $fvalue
			echo $cr
		}
		if not{
			echo 'Content-Disposition: form-data; name="'$"fname'"'$"cr
			echo $"cr
			echo $"fvalue^$"cr
		}
	}
	echo '--HJBOUNDARY--'$"cr
}
fn uencode{
	f=()
	for(i){
		fname=`{echo $"i | awk -F ':' '{print $1}'}
		fvalue=`{echo $"i | awk -F ':' '{$1=""; print substr($0, 2)}' |
			sed 's/HJSPACE/ /g' |
			urlencode |
			sed 's/%0A$//g'}
		f=$"f^$"fname'='$"fvalue
		if(! ~ $"i $*($#*))
			f=$"f^'&'
	}
	echo $"f
}
fn print{
	if(~ $d 1)
		echo $"f >>formpost.log #debug
	a=`{echo $"a | sed 's/^\///g'}
	if(~ $a http*)
		u=$a
	if not
		u=$"u/$"a
	if(~ $M 1)
		mencode `{for(i in $f){echo $"i}}
	if not if(~ $m 1)
		echo formpost -M `{for(i in $f){echo ''''$"i''''}} '| hget -P -r ''Content-Type: multipart/form-data, boundary=HJBOUNDARY''' $"u
	if not if(~ $p 1)
		echo 'hget -p '''^`{uencode $f}^''' '^$"u
	if not
		echo hget ''''$"u^'?'^`{uencode $f}^''''
}
if(~ $#M 0 && ~ $#g 0 && ~ $#m 0 && ~ $#p 0){
	file=/tmp/formpost.$pid
	geth $file
	while(grep -s -e '^HJFORMSTART$' $file){
		f=`{getf $file}
		a=$f(1)
		if(~ $f(2) g)
			g=1
		if not if(~ $f(2) m)
			m=1
		if not if(~ $f(2) p)
			p=1
		f=$f(3-)
		print
		{ echo /HJFORMSTART/,/HJFORMEND/d
			echo w
			echo q
		} | sam -d $file >[2]/dev/null
	}
	rm -f $file
}
if not{
	f=$*
	print
}