]> git.lizzy.rs Git - plan9front.git/blob - rc/bin/hpost
crop /lib/face/48x48x8/n/nixie.1 to the right size (was 512x512)
[plan9front.git] / rc / bin / hpost
1 #!/bin/rc
2 rfork e
3 argv0=$0
4 url=()
5 at=()   # text fields
6 af=()   # file fields
7 l=()
8
9 fn usage {
10         echo usage: $argv0 '[ -l ] [ -[gpm] action ] [ -u ] url [ field:value | field@file ... ]' >[1=2]
11         exit usage
12 }
13
14 while(~ $1 -*){
15         switch($1){
16         case -l;        l=($l $1)
17         case -u;        shift; url=$1
18         case -g;        shift; action=$1; method=mget
19         case -p;        shift; action=$1; method=mpost
20         case -m;        shift; action=$1; method=multi
21         case *;         usage
22         }
23         shift
24 }
25
26 if(~ $#url 0){
27         url=$1
28         shift
29 }
30
31 if(~ $url '')
32         usage
33
34 while(! ~ $#* 0){
35         switch($1){
36         case *:*@*;     at=($1 $at)
37         case *@*;       af=($1 $af)
38         case *:*;       at=($1 $at)
39         case *;         usage
40         }
41         shift
42 }
43
44 hpost=($argv0 $l)
45
46 fn uenc {
47         f=$1
48         for(i in $at){
49                 echo -n $"f`{urlencode /env/i | sed 's!%3A!=!; s!%00$!!'}
50                 f='&'
51         }
52         for(i in $af){
53                 echo -n $"f`{urlencode /env/i | sed 's!@.*$!=!'}
54                 urlencode `{sed 's!^[^@]+@!!' /env/i}
55                 f='&'
56         }
57 }
58 fn menc {
59         f=$1
60         cr=`{echo x | tr x \015}
61         for(i in $at){
62                 k=`{sed 's!:.*$!!' /env/i}
63                 echo '--'$"f$"cr
64                 echo 'Content-Disposition: form-data; name="'$"k'"'$"cr
65                 echo $"cr
66                 i=$"i$"cr
67                 sed 's!^[^:]+:!!' /env/i
68         }
69         for(i in $af){
70                 k=`{sed 's!@.*$!!' /env/i}
71                 v=`{sed 's!^[^@]+@!!' /env/i}
72                 t=`{file -m $v}
73                 n=`{basename $v}
74                 echo '--'$"f$"cr
75                 echo 'Content-Disposition: form-data; name="'$"k'"; filename="'$"n'"'$"cr
76                 echo 'Content-Type: '$"t$"cr
77                 echo $"cr
78                 cat $v
79                 echo $"cr
80         }
81         echo '--'$"f'--'$"cr
82 }
83
84 fn mget {
85         a=`{uenc '?'}
86         action=$"action$"a
87         $hget -b $url $action
88 }
89 fn mpost {
90         uenc | $hget -b $url -P $action
91 }
92 fn multi {
93         f='HJBOUNDARY'
94         menc $"f | $hget -r 'Content-Type: multipart/form-data, boundary='$"f -b $url -P $action
95 }
96
97 if(! ~ $action ''){
98         hget=(hget $l)
99         $method
100         exit
101 }
102
103 # serialize $at and $af into a0=... a1=... for awk
104 # to preserve newlines and other special characters
105 n=()
106 for(i in $at $af){
107         a$#n=$i
108         n=(1 $n)
109 }
110 a$#n=''
111 hget $url | uhtml | sed '
112 s!^(TAG|ATT)! \1!g;             # escape our inline signaling
113 s!<[    ]*!\nTAG !g;            # find starttags, mark with TAG name ...
114 s!>[^>"'']*$!!g;                # remove garbage after the tag
115 # find attributes, mark with ATT name value
116 s!([a-zA-Z][a-zA-Z0-9:_]*)=("[^"]*"?|''[^'']*''?|[      ]*[^>   ]+)!\nATT \1 \2!g;
117 ' | awk -v 'hpost='$"hpost -v 'url='$"url '
118 BEGIN{
119         for(i=0; ENVIRON["a"i]!=""; i++){
120                 s=ENVIRON["a"i]
121                 x=index(s, ":"); y=index(s, "@")
122                 if(y > 1 && (x < 1 || x > y))
123                         x = y
124                 n=substr(s, 1, x-1)
125                 ainput[n]=substr(s, x+1, length(s))
126                 atypes[n]=substr(s, x, 1);
127         }
128 }
129 function qw(s){
130         if(s !~ /[\n\\#;\|\^$=`''{}\(\)<>       ]/)
131                 return s
132         gsub(/''/, "''''", s)
133         return "''"s"''"
134 }
135 function uq(s){
136         q=substr(s, 1, 1)
137         if(q=="\"" || q=="''"){
138                 s=substr(s, 2, length(s))
139                 x=index(s, q)
140                 if(x > 0) s=substr(s,1,x-1)
141         }
142         return s
143 }
144 function emitform(){
145         if(action!=""){
146                 printf hpost
147                 if(url!="") printf " -u %s", qw(url)
148                 if(method=="post"){
149                         if(enctype=="multipart/form-data")
150                                 printf " -m %s", qw(action)
151                         else
152                                 printf " -p %s", qw(action)
153                 } else
154                                 printf " -g %s", qw(action)
155                 for(n in ainput)
156                         printf " %s%s%s", n, atypes[n], qw(ainput[n])
157                 for(n in input)
158                         if(!(n in ainput))
159                                 printf " %s%s%s", n, types[n], qw(input[n])
160                 printf "\n"
161         }
162         delete input
163         delete types
164         action=""
165         method=""
166         enctype=""
167 }
168 function endtag(){
169         if(tag=="form"){
170                 action=attr["action"]
171                 method=tolower(attr["method"])
172                 enctype=tolower(attr["enctype"])
173         }
174         if(tag=="select")
175                 selectname=attr["name"]
176         if(tag=="option" && selectname!=""){
177                 if(attr["selected"]!=""){
178                         input[selectname]=attr["value"]
179                         selectname=""
180                 }
181         }
182         if(tag=="input" || tag=="textarea" || tag=="submit"){
183                 n=attr["name"]
184                 if(n!=""){
185                         if(tolower(attr["type"])=="file"){
186                                 input[n]="/dev/null"
187                                 types[n]="@"
188                         } else {
189                                 input[n]=attr["value"]
190                                 types[n]=":"
191                         }
192                 }
193         }
194         delete attr
195         tag=""
196 }
197 /^TAG \//{
198         etag=tolower(substr($2, 2, length(etag)-1))
199         endtag()
200         if(etag=="form")
201                 emitform()
202 }
203 /^TAG [^\/]/{
204         endtag()
205         tag=tolower($2)
206         if(tag=="form")
207                 emitform()
208 }
209 /^ATT/{
210         for(i=4; i<=NF; i++) $3=$3" "$i
211         attr[tolower(uq($2))]=uq($3)
212 }
213 END{
214         endtag()
215         emitform()
216 }
217 '