http://qs321.pair.com?node_id=219133

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

Greetings, Fellow Monks!

I have a problem with XML, HTML and browsers. I'm trying to get a script write some data into an XML-file which should then be written to the disk, and once this is done, the browser should display a message.

Now, I have managed to cobble together a script as shown below, which works fine from the command line. But as soon as I run it from any browser, I get a 500 server error, and the error_log snickeringly displays the following:

malformed header from script. Bad header=<?xml version="1.0" encoding= +": /home/httpd/cgi-bin/guestbook/test.pl

It seems to me that the server gets an XML header and thinks it's supposed to display that, even though the file should actually be written to the disk rather than be sent to the browser.

I'm stuck, and maybe it's something very obvious that I just can't see anymore after the last 9 hours of office. Can anyone help me there?

#!/usr/bin/perl -wT use strict; use CGI; use IO; use XML::Writer; my $cgi = new CGI; my $i; my @data; my $e = 0; my $outfile = new IO::File(">test.xml"); my $writer = new XML::Writer(OUTPUT => $outfile, DATA_MODE => 'true', +DATA_INDENT => 2, UNSAFE => 1); $writer->xmlDecl('ISO-8859-1'); $writer->comment('Test-Entries'); $data[$e]{date} = "11-12-2002"; $data[$e]{nom} = "Fred Bloggs "; $data[$e]{email} = 'fred@bloggs.ch'; $data[$e]{eintrag} = "This is a test!"; $writer->startTag('test'); for $i (0 .. $#data) { $writer->startTag('entry'); foreach my $data (keys %{$data[$i]}) { $writer->startTag($data); $writer->characters($data[$i]{$data}); $writer->endTag(); } $writer->endTag(); } $writer->endTag(); $writer->end(); print $cgi->header; print "Done!\n";

And yes, I realise that the structure $data[$e]{whatever} is peculiar. Has something to do with what the script is supposed to be part of later.

Anyway, thanks for trying to get your head around my problems, and have a nice localtime!

--cs

There are nights when the wolves are silent and only the moon howls. - George Carlin

Replies are listed 'Best First'.
Re: Browser choking on XML-header
by dws (Chancellor) on Dec 11, 2002 at 17:59 UTC
    But as soon as I run it from any browser, I get a 500 server error

    That's because you need to do   $cgi->header(); before you start emitting XML. (And you need to arrange to emit a correct MIME type for XML. By default, header() emits one for HTML. I'll leave that as an exercise.)

Re: Browser choking on XML-header
by dws (Chancellor) on Dec 11, 2002 at 19:38 UTC
    Add some error checking to   my $outfile = new IO::File(">test.xml"); If it fails, you'll be passing   OUTPUT => undef to the XML::Writer constructor. It looks like XML::Writer defaults to STDOUT in this case.

    This would cause your script to emit XML before it's emitted a header, which would cause the error you're seeing.

    Props to VSarkiss for prodding me to take a second look.

      Aargh!

      The moment I read your second reply, I realised that this could in fact be a problem with permissions, with me running the script from the command line as a different user than the browser would - and the browser might not have write-permission to that directory...

      Quick ssh into the server at work, and lo! it works.

      ++dws for turning my head into the right direction!

      --cs

      There are nights when the wolves are silent and only the moon howls. - George Carlin