Forum Moderators: coopster & phranque

Message Too Old, No Replies

New to Perl: Premature end of script headers

Error message persists despite many attempted solutions.

         

danpritchard

9:42 am on Feb 23, 2003 (gmt 0)

10+ Year Member



Hi! This username is new (I used to be dan2k on these forums, but I forgot the password and don't have access to my old e-mail account) but I'm not quite a newbie here.

New to Perl, I read some Perl tutorials and managed to understand it enough to write this simple script. Purpose follows after script.
__ Begin PERL Script __


#!/usr/bin/perl

&ReadParse;

# prog body

$u = $in{'user'};

if ($in{'action'} eq 'friends') {
$pref = 'http://www...com/users/';
$post = '/friends';
}
elsif ($in{'action'} eq 'lj') {
$pref = 'http://www...com/users/';
$post = ''
}
print "Location: $pref$u$post\n";

# end prog body

sub ReadParse {
local (*in) = @_ if @_;
local ($i, $key, $val);

if ( $ENV{'REQUEST_METHOD'} eq "GET" ) {
$in = $ENV{'QUERY_STRING'};
} elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
read(STDIN,$in,$ENV{'CONTENT_LENGTH'});
} else {
# Added for command line debugging
# Supply name/value form data as a command line argument
# Format: name1=value1\&name2=value2\&...
# (need to escape & for shell)
# Find the first argument that's not a switch (-)
$in = ( grep(!/^-/, @ARGV )) [0];
$in =~ s/\\&/&/g;
}

@in = split(/&/,$in);

foreach $i (0 .. $#in) {
# Convert plus's to spaces
$in[$i] =~ s/\+/ /g;

# Split into key and value.
($key, $val) = split(/=/,$in[$i],2); # splits on the first =.

# Convert %XX from hex numbers to alphanumeric
$key =~ s/%(..)/pack("c",hex($1))/ge;
$val =~ s/%(..)/pack("c",hex($1))/ge;

# Associate key and value. \0 is the multiple separator
$in{$key} .= "\0" if (defined($in{$key}));
$in{$key} .= $val;
}
return length($in);
}


__ End PERL Script __
Purpose: The script reads information from a form (located at /lj/toolbar.html on the site in my profile). The fields are "user" which contains a username, and "action" which contains an action to perform. It's for use with LiveJournal. Some of the function URLs look like "http://...USER" and others like "http://...USER...otherstuff" . So I am basically checking "action" with if statements and assigning values to "pref" (prefix) and "post" (post-fix? What comes after the username in the URL is what I mean here) depending on the value of "action". Then I'm trying to redirect the browser with a Location: header, pasting together prefix+username+post as the URL. (There will be more "action"s later than these two, I just picked one that has a "post" and one that doesn't to test with for now.)

First try:
Error message as follows:
failed to open log file
fopen: Permission denied
[Sun Feb 23 02:20:29 2003] [error] [client 130.212.157.141] Premature end of script headers: /usr/local/psa/home/vhosts/domain.net/cgi-bin/ljtoolbar.pl

- Don't know what that log file crap was, but it went away after this try.
- I researched what "Premature..." meant and realized my scripts were probably being saved with Windows CR/LF line endings. I changed an option to "UNIX" and tried again.

Second try:
Error message as follows:
syntax error at ljtoolbar.pl line 10, near ") {"
syntax error at ljtoolbar.pl line 13, near "}"
Execution of ljtoolbar.pl aborted due to compilation errors.
[Sun Feb 23 02:21:37 2003] [error] [client 130.212.157.141] Premature end of script headers: /usr/local/psa/home/vhosts/domain.net/cgi-bin/ljtoolbar.pl

K. Forgot a semicolon. Fixed that. But still the "Premature..." error message persists. This time, I switched to "Crimson Editor" instead of "Win32Pad" and to an FTP program which could be placed in ASCII mode instead of using Windows Explorer/MyNetPlaces to FTP the file. Still exactly the same problem.
Basically, if I add in a silly syntax error (such as pounding on the keyboard after the #!/usr/bin/perl line) it will catch that and tell me what's wrong...and then also let me know "Premature..." again. Without a silly error, I just get "Premature..." by itself, as follows, the current state of the script:
[Sun Feb 23 03:58:08 2003] [error] [client 130.212.157.141] Premature end of script headers: /usr/local/psa/home/vhosts/domain.net/cgi-bin/ljtoolbar.pl

I'm totally at a loss for how to fix this. Here's what I've tried so far:

- As said above, tried saving with different editors (all with the UNIX/LF option on); Uploaded in ASCII mode.
- My permissions on ljtoolbar.pl are 755.
- Tried adding "exit(0);". I'm not sure where to put this, but results were equally failure with it at the point in the script marked "#end prog body" and at the very end after the sub. Where should this go, and is it necessary?
- Tried stripping the script down to the following:

#!/usr/bin/perl
print 'Location: [(wellknownwebsite)\n';...]
exit(0);

...pretty much a "hello world!" trying to get some sign of life, but no good. Exact same error message.

I don't have shell access, but I of course have access to the error logs I've been quoting this whole time.

Please let me know any ideas you might have.
Thanks for the help.

[edited by: jatar_k at 7:17 pm (utc) on Feb. 23, 2003]
[edit reason] fixed sidescroll [/edit]

danpritchard

10:05 am on Feb 23, 2003 (gmt 0)

10+ Year Member



Wow. I think i've got it!

Adding
print "Content-type: text/plain\n\n";
to the end fixed everything... nevermind. I'd delete the thread but I don't know how. Moderator(s), please feel free to do so.

Thanks
Dan

Robber

11:31 am on Feb 23, 2003 (gmt 0)

10+ Year Member



I think if you change:

print "Location: $pref$u$post\n";

to:

print "Location: $pref$u$post\n\n";

That ought to sort it too.

andreasfriedrich

11:57 am on Feb 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As Robber already pointed out, the problem was not the missing Content-Type header field - in fact such a field is not required for a redirect message - but the missing header terminator. BTW what status code are you sending?

This thread explains about the correct format of a http header:

Redirection syntax in Perl - It works but I don't know why. [webmasterworld.com]

It might prove helpful in the future.

Andreas