Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

Parsing HTML input with perl as cgi, only partial result

Because Winrunner's (mercury interactive's gui-testing software) editor isn't too good, and doesn't even show opening and closing brackets, and while working in 5th if-loop, it's way too easy to mess up closing brackets, I tried to make cgi-script to grep out lines containing brackets and counting how many are open.

Even my script was succesful when entering input from commandline, results were not too good when ran from cgi-bin; it doesn't give any output, if amount of inputdata is minimal, and when processing 300 line input, it'll process only ~200 lines of it (I tried commenting unlink function, and $tmpfile contants whole 300 line input).

#!/usr/local/bin/perl

print "Content-type: text/html";
print "\n\n";

# I don't use CGI module to enable running script from commandline also;
# tried also with module with no result

read(STDIN,$temp,$ENV{'CONTENT_LENGTH'});
@pairs=split(/&/,$temp);
foreach $item(@pairs)
{
    ($key,$content)=split(/=/,$item,2);
    $content=~tr/+/ /;
    $content=~s/%(..)/pack("c",hex($1))/ge;
    $field{$key}=$content;
}
# If input comes from webpage, output as html
$runtype = $field{runtype};

# create temporary file using epoch time to make name unique
$systime = time;
$tmpfile = "/tmp/sulkeis_$systime";

if ($runtype eq "fromweb")
{
    open LOG, "> $tmpfile";
    select LOG;
    print $field{inputdata};
    select STDOUT;

    print("<HTML><BODY>\n");

    if ($field{format} == "1")
    {
        print("<table border=\"1\">");
        print("<tr>");
        print("<td>line number<td>");
        print("<td>line content</td>");
        print("<td>brackets open</td>");
        print("</tr><br><br>");
    } else {
        print("<PRE>\n");
    }
    if ($field{startrow})
    {
        $linenum = $field{startrow};
        if ($linenum > 0) { $linenum--; }
    }

} else {

    system("cp $ARGV[0] $tmpfile");
    if ($ARGV[1])
    {
        $linenum = $ARGV[1];
    } else {
        $linenum = 0;
    }
}


open (INFILE, $tmpfile);
$open = 0;

while (<INFILE>)
{
    chomp($_);
    $linenum++;

    if ($_ =~ /.*\}.*\{.*/)
    {
        # most propably row containing '} else {', so no modification to open brackets value
        if ($field{format} == 1)
        {
            print("<tr>");
            print("<td>$linenum</td>");
            print("<td><pre> $_ </pre></td>");
            print("<td>$open</td>");
            print("</tr>\n");
        } else {
            print("$linenum: $_ :$open\n");
        }

    } else {

        if ($_ =~ /.*\}.*/)
        {
            $open--;
            if ($field{format} == 1)
            {
                print ("<tr>");
                print("<td>$linenum</td>");
                print("<td><pre> $_ </pre></td>");
                print("<td>$open</td>");
                print("</tr>\n");
            } else {
                print("$linenum: $_ :$open\n");
            }
        }

        if ($_ =~ /.*\{.*/)
        {
            $open++;
            if ($field{format} == 1)
            {
                print ("<tr>");
                print("<td>$linenum</td>");
                print("<td><pre> $_ </pre></td>");
                print("<td>$open</td>");
                print("</tr>\n");
            } else {
                print("$linenum: $_ :$open\n");
            }
        }
    }
}
close (INFILE);

# delete temporarly file
unlink($tmpfile);

if ($runtype eq "fromweb")
{
    if ($field{format} == 0)
    {
        print("<PRE>\n");
    } else {
        print("</TABLE>");
    }

    print "</BODY></HTML>";
}


-- html input form --

<html>
<body>
<form action="/cgi-bin/sulkeiset.pl" method="post">
<table width="80%"><tr>
<td>Begin line<input name="startrow"></td>
<td>Show using format
<select name="format">
<option value="1" selected> HTML </option>
<option value="0"> ASCII </option>

</select></tr></table>
<textarea name="inputdata" rows="20" cols="90">
</textarea><br>
<input name="runtype" type="hidden" value="fromweb">
<input type="submit" value="show">
</form>

</body>
</html>
0
setae
Asked:
setae
1 Solution
 
jmcgOwnerCommented:
It's a buffering problem. Try adding the line

close LOG;

right after the line

print $field{inputdata};

(which could have been written

print LOG $field{inputdata};

without all the selects.
0
 
setaeAuthor Commented:
blind me..

thanks ;)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now