formfind 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #!/usr/bin/env perl
  2. #
  3. # formfind.pl
  4. #
  5. # This script gets a HTML page on stdin and presents form information on
  6. # stdout.
  7. #
  8. # Author: Daniel Stenberg <daniel@haxx.se>
  9. # Version: 0.2 Nov 18, 2002
  10. #
  11. # HISTORY
  12. #
  13. # 0.1 - Nov 12 1998 - Created now!
  14. # 0.2 - Nov 18 2002 - Enhanced. Removed URL support, use only stdin.
  15. #
  16. $in="";
  17. if($ARGV[0] eq "-h") {
  18. print "Usage: $0 < HTML\n";
  19. exit;
  20. }
  21. sub namevalue {
  22. my ($tag)=@_;
  23. my $name=$tag;
  24. if($name =~ /name *=/i) {
  25. if($name =~ /name *= *([^\"\']([^ \">]*))/i) {
  26. $name = $1;
  27. }
  28. elsif($name =~ /name *= *(\"|\')([^\"\']*)(\"|\')/i) {
  29. $name=$2;
  30. }
  31. else {
  32. # there is a tag but we didn't find the contents
  33. $name="[weird]";
  34. }
  35. }
  36. else {
  37. # no name given
  38. $name="";
  39. }
  40. # get value tag
  41. my $value= $tag;
  42. if($value =~ /[^\.a-zA-Z0-9]value *=/i) {
  43. if($value =~ /[^\.a-zA-Z0-9]value *= *([^\"\']([^ \">]*))/i) {
  44. $value = $1;
  45. }
  46. elsif($value =~ /[^\.a-zA-Z0-9]value *= *(\"|\')([^\"\']*)(\"|\')/i) {
  47. $value=$2;
  48. }
  49. else {
  50. # there is a tag but we didn't find the contents
  51. $value="[weird]";
  52. }
  53. }
  54. else {
  55. $value="";
  56. }
  57. return ($name, $value);
  58. }
  59. while(<STDIN>) {
  60. $line = $_;
  61. push @indoc, $line;
  62. $line=~ s/\n//g;
  63. $line=~ s/\r//g;
  64. $in=$in.$line;
  65. }
  66. while($in =~ /[^<]*(<[^>]+>)/g ) {
  67. # we have a tag in $1
  68. $tag = $1;
  69. if($tag =~ /^<!--/) {
  70. # this is a comment tag, ignore it
  71. }
  72. else {
  73. if(!$form &&
  74. ($tag =~ /^< *form/i )) {
  75. $method= $tag;
  76. if($method =~ /method *=/i) {
  77. $method=~ s/.*method *= *(\"|)([^ \">]*).*/$2/gi;
  78. }
  79. else {
  80. $method="get"; # default method
  81. }
  82. $action= $tag;
  83. $action=~ s/.*action *= *(\'|\"|)([^ \"\'>]*).*/$2/gi;
  84. $method=uc($method);
  85. $enctype=$tag;
  86. if ($enctype =~ /enctype *=/) {
  87. $enctype=~ s/.*enctype *= *(\'|\"|)([^ \"\'>]*).*/$2/gi;
  88. if($enctype eq "multipart/form-data") {
  89. $enctype="multipart form upload [use -F]"
  90. }
  91. $enctype = "\n--- type: $enctype";
  92. }
  93. else {
  94. $enctype="";
  95. }
  96. print "--- FORM report. Uses $method to URL \"$action\"$enctype\n";
  97. $form=1;
  98. }
  99. elsif($form &&
  100. ($tag =~ /< *\/form/i )) {
  101. print "--- end of FORM\n";
  102. $form=0;
  103. if( 0 ) {
  104. print "*** Fill in all or any of these: (default assigns may be shown)\n";
  105. for(@vars) {
  106. $var = $_;
  107. $def = $value{$var};
  108. print "$var=$def\n";
  109. }
  110. print "*** Pick one of these:\n";
  111. for(@alts) {
  112. print "$_\n";
  113. }
  114. }
  115. undef @vars;
  116. undef @alts;
  117. }
  118. elsif($form &&
  119. ($tag =~ /^< *(input|select)/i)) {
  120. $mtag = $1;
  121. ($name, $value)=namevalue($tag);
  122. if($mtag =~ /select/i) {
  123. print "Select: NAME=\"$name\"\n";
  124. push @vars, "$name";
  125. $select = 1;
  126. }
  127. else {
  128. $type=$tag;
  129. if($type =~ /type *=/i) {
  130. $type =~ s/.*type *= *(\'|\"|)([^ \"\'>]*).*/$2/gi;
  131. }
  132. else {
  133. $type="text"; # default type
  134. }
  135. $type=uc($type);
  136. if(lc($type) eq "reset") {
  137. # reset types are for UI only, ignore.
  138. }
  139. elsif($name eq "") {
  140. # let's read the value parameter
  141. print "Button: \"$value\" ($type)\n";
  142. push @alts, "$value";
  143. }
  144. else {
  145. print "Input: NAME=\"$name\"";
  146. if($value ne "") {
  147. print " VALUE=\"$value\"";
  148. }
  149. print " ($type)\n";
  150. push @vars, "$name";
  151. # store default value:
  152. $value{$name}=$value;
  153. }
  154. }
  155. }
  156. elsif($form &&
  157. ($tag =~ /^< *textarea/i)) {
  158. my ($name, $value)=namevalue($tag);
  159. print "Textarea: NAME=\"$name\"\n";
  160. }
  161. elsif($select) {
  162. if($tag =~ /^< *\/ *select/i) {
  163. print "[end of select]\n";
  164. $select = 0;
  165. }
  166. elsif($tag =~ /[^\/] *option/i ) {
  167. my ($name, $value)=namevalue($tag);
  168. my $s;
  169. if($tag =~ /selected/i) {
  170. $s= " (SELECTED)";
  171. }
  172. print " Option VALUE=\"$value\"$s\n";
  173. }
  174. }
  175. }
  176. }