Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

CGI http header error

by martymart (Deacon)
on Jun 03, 2003 at 12:47 UTC ( #262641=perlquestion: print w/replies, xml ) Need Help??

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

Fellow Monks, I'm using activestate perl on Windows 2000, following on from a recent post. I'm trying to run the script below, what it should be doing is putting nice output to the browser, and saving an html file on the webserver:
#!/usr/bin/perl use strict; use CGI; my $q = new CGI; open(OUT, '>out.html') || die "Couldn't open out file: $!"; print OUT $q->header, $q->start_html('hello world'), $q->h1('hello world'), $q->end_html; close OUT; END{} __END__
When I browse to this script though I keep getting this error message:
CGI Error
The specified CGI application misbehaved by not returning a complete set of HTTP headers. The headers it did return are:

As well as that, the html file produced on the server looks a bit wacky:

Content-Type: text/html; charset=ISO-8859-1 <?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""> <html xmlns="" lang="en-US"><head><title>h +ello world</title> </head><body><h1>hello world</h1></body></html>
This cleans up a bit if I comment out the $q->header, But the error message in the browser remains the same. I'd appreciate it if somebody could point out the problem with the script, or another workaround for this problem.
Many Thanks,

Replies are listed 'Best First'.
Re: CGI http header error
by Tanalis (Curate) on Jun 03, 2003 at 12:58 UTC

    The script excerpt you've provided there isn't actually outputting anything to the browser (you're printing everything to the OUT filehandle) - which is why you'll get the incomplete header message.

    You can fix this by outputting the same data to STDOUT, as well as to the file (anything written to STDOUT is directed automatically to the browser when the script is run).

    As far as the HTML file goes, that looks about right for a full set of headers and your "Hello, world". You can always add in some newlines when you're outputting to the file (eg print OUT $q->header, "\n";) to space the code out a little.

    Hope that helps.

    -- Foxcub

Re: CGI http header error
by arthas (Hermit) on Jun 03, 2003 at 13:04 UTC
    The script you posted doesn't send anything to the standard output, therefore you get an error when you browse it. Try the following variation:
    #!/usr/bin/perl use strict; use CGI; my $q = new CGI; $doc = $q->start_html('hello world'); $doc .= $q->h1('hello world'); $doc .= $q->end_html; open(OUT, '>out.html') || die "Couldn't open out file: $!"; print OUT $doc; close OUT; print $q->header(); print $doc;

    This sends the HTML code both to out.html and to the standard output (that it to say, to the web browser).

    The HTTP header (print $q->header()) is needed when outputting to the standard output via CGI, but you can (and maybe you'd better) not send it to the file you create. The example above works this way: the header is only sent to the browser, and the HTML file is sent both to the browser and to the file.


Re: CGI http header error
by Joost (Canon) on Jun 03, 2003 at 13:01 UTC
    You are writing to a local file and not sending any output to SDOUT. CGI programs expect a minimal output to STDOUT (including headers) so they can send some response to the browser.

    Most likely you need to remove all references to the OUT file(handle) so your output gets returned to the browser.

    -- Joost
Re: CGI http header error
by nite_man (Deacon) on Jun 03, 2003 at 13:30 UTC
    Under Linux and Apache your script is working correctly. I think that problem is in Windows server.
    Try to look at using NPH scripts and modify your code like this:
    #!/usr/bin/perl use strict; use CGI; my $q = new CGI; open(OUT, '>out.html') || die "Couldn't open out file: $!"; print OUT $q->header(-nph=>1), $q->start_html('hello world'), $q->h1('hello world'), $q->end_html; close OUT;
    Maybe it will help you.

    Update: If I understood clearly, you create and store an html file at Perl script and then retrive it at IIS. In this case, problem will able to resolve using my example (I can not check it because I use Linux on my computer). But if you retrive this html file at Perl script, of course, you can add on end of your script something like this:

    open (FILE, '/tmp/out.html') || die $!; while(my $row = <FILE>) { print $row } close FILE or die $!;
    And block END {} is destructor of package (BEGIN {} is package constuctor). So, you don't need use it in scripts.

    SV* sv_bless(SV* sv, HV* stash);
Re: CGI http header error
by WhiteBird (Hermit) on Jun 03, 2003 at 13:20 UTC
    What do you find "wacky" about the header? Do you mean all the extra reference to xml? I believe that is served up by IIS. If you have control over your server, you can modify that setting to output a more conventional header. If you need info on how to do that, let me know.
    As for the code, when I run it on my Win2K PC, it seems to work with no errors, but it adds two headers to the output. As you noted, getting rid of the $q->header takes care of that. I don't see the problem. Perhaps it's a browser issue that's croaking on the header. If you have an older version of IIE it might not like the XML stuff in the header. Just a guess.

    Update:Ok, I admit it was a bad guess and a worse answer. Pay no attention to me. I've been particularly dull-witted all day.

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://262641]
Approved by gjb
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2022-05-24 21:28 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (84 votes). Check out past polls.