Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

CGI valid header error

by csuhockey3 (Curate)
on Feb 15, 2007 at 01:53 UTC ( #600115=perlquestion: print w/replies, xml ) Need Help??

csuhockey3 has asked for the wisdom of the Perl Monks concerning the following question:

Howdy Folks,

I have been out of the perl game for a while now, but with the help of another monk, we have the script below. Essentially what I'm trying to pull off is uploading a spreadsheet and converting it to xml. After the upload is complete, I get an error stating the CGI did not produce a valid header. I have tried to play with it but can't seem to find a solution and feel like I'm grabbing at straws. Can anyone shed some light on my problem? Thanks!

UPDATE: Here is the error from the logs:
failure ( 171): for host ***.***.**.*** trying to POST /cgi-bin/upload +.cgi, cgi_scan_headers reports: HTTP4044: the CGI program /cgi-bin/up +load.cgi did not produce a valid header (program termindated without +a vaild CGI header.


UPDATE2: Bare with me here, I've been out of the game for 2 years now! Anyway, I have everything working up until the xSV_to_XML sub and I was getting an $die error on the files. I think I'm using exec correctly and not supposed to use system() to change the permissions on the files. The script just dies when it get here, no error to the browser or the log. How can I ensure that I can read/write the files?
my $IN = $upload_path . param('excel') . ".txt"; exec "chmod 775 $IN"; my $OUT = "master.xml"; exec "chmod 775 master.xml";


The Beast:
#!/usr/bin/perl use strict; use warnings; use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser); $CGI::POST_MAX = 2097152; ## (2mb upload max) $CGI::DISABLE_UPLOADS = 0; ## allow uploads my $upload_path = "/export/"; my $filename = $upload_path . param('excel'); sub print_header { my $title = shift; print header, start_html($title); } sub init { my $upload = upload('excel'); unless (param('excel')) { print_header ('Excel to XML Converter'); print_upload_form(); ## make sure there's actually something being uploaded } elsif (!$upload && cgi_error) { print_header ('Excel to XML Converter: Error!'); print h1("No file uploaded!"); ## all is well, show conversion confirmation } else { show_convert(); } } sub print_upload_form { print h1('Upload Excel File.'), start_multipart_form(), "Excel file:", filefield( -name=>'excel' ), br, submit( -name=>"Convert" ), end_form; } ## show the conversion process sub show_convert { print header, start_html ("Converting..."), "Converting...<br />", $filename, br, convert(), "<br />Done!"; } sub convert { ## convert to the xSV file... my $fh = upload('excel'); my $bytes_read = -s $fh; open OUTFILE, '>', $filename or die "Couldn't open output file: $!\n"; while ( my $bytes = read($fh, my $buffer, 2097152)) { print OUTFILE $buffer; } close OUTFILE; ## convert to xSV... txt_to_xSV(); ## convert xSV to XML... xSV_to_XML(); ## copy/back up file, not added yet "Received $bytes_read bytes<br />"; } sub txt_to_xSV { require Spreadsheet::ParseExcel; my $oExcel = new Spreadsheet::ParseExcel; my $dir = $ARGV[1]; chomp $dir; unless (-d $dir) { print "Can't find directory $dir"; exit; } my $filename = "$dir/$ARGV[0]"; chomp $filename; unless (-e $filename) { print "Can't find file $filename"; exit ; } my $fullfilename = "$filename.txt"; ## start work #1.1 Normal Excel97 open E2T, ">", $fullfilename or die $!; my $oBook = Spreadsheet::ParseExcel::Workbook->Parse($filename); my($iR, $iC, $oWkS, $oWkC); foreach my $oWkS (@{$oBook->{Worksheet}}) { print "--------- SHEET:", $oWkS->{Name}, "\n"; print E2T $oWkS->{Name}, "|"; next unless defined $oWkS->{MinRow} and defined $oWkS->{MaxRow}; for my $iR ($oWkS->{MinRow} .. $oWkS->{MaxRow}) { for my $iC ($oWkS->{MinCol} .. $oWkS->{MaxCol}) { $oWkC = $oWkS->{Cells}[$iR][$iC]; next if ! defined $oWkC; print "( $iR , $iC ) =>", $oWkC->Value, "\n" if($oWkC); print E2T $oWkC->Value, "|"; } } print E2T "\n"; } close E2T; } sub xSV_to_XML { my $IN = "master.xls.txt"; my $OUT = "master.xml"; open (OUT, ">$OUT"); open(IN) or die ("Cannot open file ($IN)"); print OUT <<TOP; <?xml version="1.0" encoding="ISO-8859-1"?> TOP my $id = 0; my @columns = ("category","code","title","summary","prereq","group","subgroup","sequ +ence","rolemandatory","rolerecommended","roleoptional","url","modalit +y","length"); foreach my $row (<IN>){ $row =~ s#\s+$##; $row =~ s#&+#&amp;#g; $row =~ s#(?<=\|)\|#0|#g; my ($category,$code,$title,$summary,$prereq,$group,$subgroup,$sequence,$r +olemandatory,$rolerecommended,$roleoptional,$url,$modality,$length) = split ('\|', $row); #will ignore lank next if $group eq ""; foreach $_ (@columns){ xmlrow($_); } print OUT "</star:course>\n"; $id++; } print OUT "</star:learning-paths>\n"; } sub xmlrow{ my $colname = shift(@_); my $ostar="<star:"; my $estar="</star:"; print OUT "<star:$colname>${$colname}</star:$colname>\n"; } init();

Replies are listed 'Best First'.
Re: CGI valid header error
by dorko (Prior) on Feb 15, 2007 at 02:21 UTC
    If I were in your place, I'd try running it from the command line and see what it does print. Or maybe putting a print header; statement before your print_header sub declaration. Just my $.02.

    Cheers,

    Brent

    -- Yeah, I'm a Delt.
Re: CGI valid header error
by f00li5h (Chaplain) on Feb 15, 2007 at 02:49 UTC

    Or perhaps have a look in your webserver's error log...

    @_=qw; ask f00li5h to appear and remain for a moment of pretend better than a lifetime;;s;;@_[map hex,split'',B204316D8C2A4516DE];;y/05/os/&print;
      The header error is out of the error log. The browser just prints the ol' generic Server Error. I'll try dorko's suggestion of moving the print outside the sub.

        I'd try commenting out chunks of code until you get the minimum code that generates the error. Very likely the bug will pop out and hit you in the eye in the process. If not, post the reduced code here. Note that you shouldn't need any external files or system calls to reproduce the problem! Show us the exact error line from your log too may help.

        Does the following produce clean results for you?

        #!/usr/bin/perl use strict; use warnings; use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser); $CGI::POST_MAX = 2097152; ## (2mb upload max) $CGI::DISABLE_UPLOADS = 0; ## allow uploads my $upload_path = "/export/"; #my $filename = $upload_path . param('excel'); sub print_header { my $title = shift; print header, start_html($title); } sub init { my $upload = 0; unless (param('excel')) { print_header ('Excel to XML Converter'); #print_upload_form(); ## make sure there's actually something being uploaded } elsif (!$upload && cgi_error) { print_header ('Excel to XML Converter: Error!'); print h1("No file uploaded!"); ## all is well, show conversion confirmation } else { show_convert(); } } init(); print end_html();

        DWIM is Perl's answer to Gödel
Re: CGI valid header error
by Mr. Muskrat (Canon) on Feb 15, 2007 at 22:17 UTC
    exec never returns to the calling process however you should be able to just use chmod without any sort of system call.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://600115]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2021-01-23 21:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?