Initial revision
[libcurl.git] / perl / formfind.pl
blobcd199764298bcd4a8c84208d915f5d32c7993497
1 #!/usr/local/bin/perl
3 # formfind.pl
5 # This script gets a HTML page from the specified URL and presents form
6 # information you may need in order to machine-make a respond to the form.
8 # Written to use 'curl' for URL fetching.
10 # Author: Daniel Stenberg <Daniel.Stenberg@sth.frontec.se>
11 # Version: 0.1 Nov 12, 1998
13 # HISTORY
15 # 0.1 - Created now!
17 # TODO
18 # respect file:// URLs for local file fetches!
20 $in="";
22 $usestdin = 0;
23 if($ARGV[0] eq "" ) {
24 $usestdin = 1;
26 else {
27 $geturl = $ARGV[0];
30 if(($geturl eq "") && !$usestdin) {
31 print "Usage: $0 <full source URL>\n",
32 " Use a traling slash for directory URLs!\n";
33 exit;
35 # If you need a proxy for web access, edit your .curlrc file to feature
36 # -x <proxy:port>
38 # linkchecker, URL will be appended to the right of this command line
39 # this is the one using HEAD:
40 $linkcheck = "curl -s -m 20 -I";
42 # as a second attempt, this will be used. This is not using HEAD but will
43 # get the whole frigging document!
44 $linkcheckfull = "curl -s -m 20 -i";
46 # htmlget, URL will be appended to the right of this command line
47 $htmlget = "curl -s";
49 # urlget, URL will be appended to the right of this command line
50 # this stores the file with the remote file name in the current dir
51 $urlget = "curl -O -s";
53 # Parse the input URL and split it into the relevant parts:
55 sub SplitURL {
56 my $inurl = $_[0];
58 if($inurl=~ /^([^:]+):\/\/([^\/]*)\/(.*)\/(.*)/ ) {
59 $getprotocol = $1;
60 $getserver = $2;
61 $getpath = $3;
62 $getdocument = $4;
64 elsif ($inurl=~ /^([^:]+):\/\/([^\/]*)\/(.*)/ ) {
65 $getprotocol = $1;
66 $getserver = $2;
67 $getpath = $3;
68 $getdocument = "";
70 if($getpath !~ /\//) {
71 $getpath ="";
72 $getdocument = $3;
76 elsif ($inurl=~ /^([^:]+):\/\/(.*)/ ) {
77 $getprotocol = $1;
78 $getserver = $2;
79 $getpath = "";
80 $getdocument = "";
82 else {
83 print "Couldn't parse the specified URL, retry please!\n";
84 exit;
89 if(!$usestdin) {
91 &SplitURL($geturl);
92 #print "protocol = $getprotocol\n";
93 #print "server = $getserver\n";
94 #print "path = $getpath\n";
95 #print "document = $getdocument\n";
96 #exit;
98 open(HEADGET, "$linkcheck $geturl|") ||
99 die "Couldn't get web page for some reason";
100 headget:
101 while(<HEADGET>) {
102 # print $_;
103 if($_ =~ /HTTP\/.*3\d\d /) {
104 $pagemoved=1;
106 elsif($pagemoved &&
107 ($_ =~ /^Location: (.*)/)) {
108 $geturl = $1;
110 &SplitURL($geturl);
112 $pagemoved++;
113 last headget;
116 close(HEADGET);
118 if($pagemoved == 1) {
119 print "Page is moved but we don't know where. Did you forget the ",
120 "traling slash?\n";
121 exit;
124 open(WEBGET, "$htmlget $geturl|") ||
125 die "Couldn't get web page for some reason";
127 while(<WEBGET>) {
128 $line = $_;
129 push @indoc, $line;
130 $line=~ s/\n//g;
131 $line=~ s/\r//g;
132 # print $line."\n";
133 $in=$in.$line;
136 close(WEBGET);
138 else {
139 while(<STDIN>) {
140 $line = $_;
141 push @indoc, $line;
142 $line=~ s/\n//g;
143 $line=~ s/\r//g;
144 $in=$in.$line;
148 getlinkloop:
149 while($in =~ /[^<]*(<[^>]+>)/g ) {
150 # we have a tag in $1
151 $tag = $1;
153 if($tag =~ /^<!--/) {
154 # this is a comment tag, ignore it
156 else {
157 if(!$form &&
158 ($tag =~ /^< *form/i )) {
159 $method= $tag;
160 if($method =~ /method *=/i) {
161 $method=~ s/.*method *= *(\"|)([^ \">]*).*/$2/gi;
163 else {
164 $method="get"; # default method
166 $action= $tag;
167 $action=~ s/.*action *= *(\"|)([^ \">]*).*/$2/gi;
169 $method=uc($method);
171 $enctype=$tag;
172 if ($enctype =~ /enctype *=/) {
173 $enctype=~ s/.*enctype *= *(\'|\"|)([^ \"\'>]*).*/$2/gi;
175 if($enctype eq "multipart/form-data") {
176 $enctype="multipart form upload [use -F]"
178 $enctype = "\n--- type: $enctype";
180 else {
181 $enctype="";
184 print "--- FORM report. Uses $method to URL \"$action\"$enctype\n";
185 # print "TAG: $tag\n";
186 # print "METHOD: $method\n";
187 # print "ACTION: $action\n";
188 $form=1;
190 elsif($form &&
191 ($tag =~ /< *\/form/i )) {
192 # print "TAG: $tag\n";
193 print "--- end of FORM\n";
194 $form=0;
195 if( 0 ) {
196 print "*** Fill in all or any of these: (default assigns may be shown)\n";
197 for(@vars) {
198 $var = $_;
199 $def = $value{$var};
200 print "$var=$def\n";
202 print "*** Pick one of these:\n";
203 for(@alts) {
204 print "$_\n";
207 undef @vars;
208 undef @alts;
210 elsif($form &&
211 ($tag =~ /^< *(input|select)/i)) {
212 $mtag = $1;
213 # print "TAG: $tag\n";
215 $name=$tag;
216 if($name =~ /name *=/i) {
217 $name=~ s/.*name *= *(\"|)([^ \">]*).*/$2/gi;
219 else {
220 # no name given
221 $name="";
223 # get value tag
224 $value= $tag;
225 if($value =~ /value *=/i) {
226 $value=~ s/.*value *= *(\"|)([^ \">]*).*/$2/gi;
228 else {
229 $value="";
232 if($mtag =~ /select/i) {
233 print "Select: $name\n";
234 push @vars, "$name";
235 $select = 1;
237 else {
238 $type=$tag;
239 if($type =~ /type *=/i) {
240 $type =~ s/.*type *= *(\"|)([^ \">]*).*/$2/gi;
242 else {
243 $type="text"; # default type
245 $type=uc($type);
246 if(lc($type) eq "reset") {
247 # reset types are for UI only, ignore.
249 elsif($name eq "") {
250 # let's read the value parameter
252 print "Button: \"$value\" ($type)\n";
253 push @alts, "$value";
255 else {
256 $info="";
257 if($value ne "") {
258 $info="=$value";
260 print "Input: $name$info ($type)\n";
261 push @vars, "$name";
262 # store default value:
263 $value{$name}=$value;
267 elsif($select &&
268 ($tag =~ /^< *\/ *select/i)) {
269 $select = 0;