shithub: riscv

Download patch

ref: 7c3ea4360b64579ce63d85337fdefdbba565f312
parent: 909efb22c516c3c685159a954f164469a064e5a7
author: cinap_lenrek <[email protected]>
date: Thu Oct 18 10:38:07 EDT 2012

hpost: add -l flag to get location url after POST

--- a/rc/bin/hget
+++ b/rc/bin/hget
@@ -2,7 +2,7 @@
 rfork e
 argv0=$0
 fn usage {
-	echo 'usage: '$"argv0' [ -o file ] [ -p body | -P ] [ -r header ] [ -m method ] [ -b baseurl ] url' >[1=2]
+	echo usage: $argv0 '[ -l | -o file] [ -p body | -P ] [ -r header ] [ -m method ] [ -b baseurl ] url' >[1=2]
 	exit usage
 }
 s=0
@@ -12,6 +12,7 @@
 r=()
 m=()
 b=()
+l=()
 while(~ $1 -*){
 	switch($1){
 	case -o
@@ -25,6 +26,8 @@
 	case -r
 		r=($r $2)
 		shift
+	case -l
+		l=1
 	case -m
 		m=$2
 		shift
@@ -64,6 +67,10 @@
 		}
 	}
 	<$d/body {
+		if(~ $#l 1){
+			echo `{cat $d/parsed/url}
+			exit
+		}
 		if(~ $#o 1){
 			l=`{cat $d/contentlength >[2]/dev/null}
 			x=`{awk 'BEGIN{FS=" |-"}/^bytes ([0-9]+)\-/{print $2}' \
--- a/rc/bin/hpost
+++ b/rc/bin/hpost
@@ -4,12 +4,25 @@
 url=()
 at=()	# text fields
 af=()	# file fields
+l=()
 
 fn usage {
-	echo $argv0 '[ -u ] url [ -[gpm] action ] [ field:value | field@file ... ]' >[1=2]
+	echo usage: $argv0 '[ -l ] [ -[gpm] action ] [ -u ] url [ field:value | field@file ... ]' >[1=2]
 	exit usage
 }
 
+while(~ $1 -*){
+	switch($1){
+	case -l;	l=($l $1)
+	case -u;	shift; url=$1
+	case -g;	shift; action=$1; method=mget
+	case -p;	shift; action=$1; method=mpost
+	case -m;	shift; action=$1; method=multi
+	case *;		usage
+	}
+	shift
+}
+
 # tired of typing -u
 if(~ $1 http://* https://*){
 	url=$1
@@ -16,12 +29,11 @@
 	shift
 }
 
+if(~ $#url 0)
+	usage
+
 while(! ~ $#* 0){
 	switch($1){
-	case -u;	shift; url=$1
-	case -g;	shift; action=$1; method=mget
-	case -p;	shift; action=$1; method=mpost
-	case -m;	shift; action=$1; method=multi
 	case *:*@*;	at=($1 $at)
 	case *@*;	af=($1 $af)
 	case *:*;	at=($1 $at)
@@ -30,8 +42,7 @@
 	shift
 }
 
-if(~ $#url 0)
-	usage
+hpost=($argv0 $l)
 
 fn uenc {
 	f=$1
@@ -74,17 +85,18 @@
 fn mget {
 	a=`{uenc '?'}
 	action=$"action$"a
-	hget -b $url $action
+	$hget -b $url $action
 }
 fn mpost {
-	uenc | hget -b $url -P $action
+	uenc | $hget -b $url -P $action
 }
 fn multi {
 	f='HJBOUNDARY'
-	menc $"f | hget -r 'Content-Type: multipart/form-data, boundary='$"f -b $url -P $action
+	menc $"f | $hget -r 'Content-Type: multipart/form-data, boundary='$"f -b $url -P $action
 }
 
 if(! ~ $action ''){
+	hget=(hget $l)
 	$method
 	exit
 }
@@ -97,7 +109,6 @@
 	n=(1 $n)
 }
 a$#n=''
-
 hget $url | uhtml | sed '
 s!^(TAG|ATT)! \1!g;		# escape our inline signaling
 s!<[ 	]*!\nTAG !g;		# find starttags, mark with TAG name ...
@@ -104,7 +115,7 @@
 s!>[^>"'']*$!!g;		# remove garbage after the tag
 # find attributes, mark with ATT name value
 s!([a-zA-Z][a-zA-Z0-9:_]*)=("[^"]*"?|''[^'']*''?|[ 	]*[^> 	]+)!\nATT \1 \2!g;
-' | awk -v 'argv0='$"argv0 -v 'url='$"url '
+' | awk -v 'hpost='$"hpost -v 'url='$"url '
 BEGIN{
 	for(i=0; ENVIRON["a"i]!=""; i++){
 		s=ENVIRON["a"i]
@@ -133,7 +144,7 @@
 }
 function emitform(){
 	if(action!=""){
-		printf argv0
+		printf hpost
 		if(url!="") printf " -u %s", qw(url)
 		if(method=="post"){
 			if(enctype=="multipart/form-data")
--- a/sys/man/1/hget
+++ b/sys/man/1/hget
@@ -4,6 +4,8 @@
 .SH SYNOPSIS
 .B hget
 [
+.B -l
+|
 .B -o
 .I file
 ] [
@@ -25,10 +27,8 @@
 .PP
 .B hpost
 [
-.B -u
-]
-.I url
-[
+.B -l
+] [
 .B -g
 .I action
 ] [
@@ -38,6 +38,10 @@
 .B -m
 .I action
 ] [
+.B -u
+]
+.I url
+[
 .I name:value
 |
 .I name@file
@@ -48,8 +52,10 @@
 retrieves the web page specified by the URL
 .I url
 and writes it, absent the
+.B -l
+and
 .B -o
-option, to standard output.
+options, to standard output.
 .PP
 The
 .I url
@@ -77,6 +83,18 @@
 read from standard input.
 .PP
 The
+.B -l
+option causes
+.I hget
+and
+.I hpost
+to print the location URL from the transactoin response
+instead of retriving the the body data. This is usefull
+for HTTP POST transactions that redirect to a URL containing
+the prosted data so we wont refetch the data we just uploaded
+to the site.
+.PP
+The
 .B -o
 option is used to keep a local file in sync with a
 web page.  If the web page has been modified later than the
@@ -112,17 +130,6 @@
 is invoked to execute the transaction submitting the form data.
 .PP
 The
-.B -u
-flag sets the target URL to
-.I url.
-As the
-.I url
-parameter is always required, it can be optionally specified
-in the first argument without the
-.B -u
-flag.
-.PP
-The
 .B -g
 and
 .B -p
@@ -133,6 +140,18 @@
 In all cases, the form action URL is set to
 .I action.
 .PP
+The
+.B -u
+flag sets the target URL to
+.I url.
+As the
+.I url
+parameter is always required, the
+.B -u
+flag can be omited when
+.I url
+follows directly after the last option if any.
+.PP
 The remaining arguments of the form
 .B name:value
 are interpreted as text form field names and values to be submitted. An
@@ -152,8 +171,8 @@
 edited and sent.
 .IP
 .EX
-% hpost http://p.intma.in
-/bin/hpost -u http://p.intma.in -p paste.cgi text:
+% hpost -l http://p.intma.in
+/bin/hpost -l -u http://p.intma.in -p paste.cgi text:
 .EE
 .PP
 Manually specify fields to be sent to a given
@@ -160,13 +179,13 @@
 .I url.
 .IP
 .EX
-% hpost -u http://p.intma.in -p paste.cgi text:'test post'
+% hpost -l -u http://p.intma.in -p paste.cgi text:'test post'
 .EE
 .PP
-Upload a file.
+Upload a file, print the resulting URL
 .IP
 .EX
-% hpost http://i.intma.in file@/tmp/screen.png | rc >/dev/null
+% hpost -l http://i.intma.in file@/tmp/screen.png | rc
 .EE
 .SH SOURCE
 .B /rc/bin/hget