HTTP header issues with Perl CGI module

software development

#1

HTTP header issues with Perl CGI module

I am troubleshooting a Perl script which uses the CGI module. My difficulty is that I need to parse the parameters that the browser passes to the CGI script, and generate one of several possible HTTP header types based on the parameters provided. (Specifically, the request is generated when the user activates a scroll button on the web page. If the user scrolls past the end of the array on the server-side database, I want the CGI script to respond by generating an HTTP redirect header.)

My problem: the CGI module appears to require that I explicitly generate a header immediately after the “use CGI” statement, and before I call any other CGI module functions. If I do not do this, then the Apache server generates a default HTTP header that will precede the header that I ultimately generate from my program. And the default header will have a type of “text/plain.” The result is that my HTML page is displayed as text by the browser, including the HTTP header that I intended to be the HTTP header.

I tried to get around this by writing Perl code to parse the CGI script parameters. Unfortunately, the server apparently does not pass the CGI parameters to the Perl script through the command-line; the parameters are accessible only through functions in the CGI module. And this takes me right back to my original problem: the CGI module requires that I generate a header before using these functions.

Any suggestions?

Thanks
-Mark


#2

Post some code that demonstrates the problem. It’s probably more complicated than you think.

Try this

[code]#!/usr/local/bin/perl

use CGI qw(:standard);

$name = param(‘name’) || ‘(none provided)’;

print
header(),
start_html(‘Test’),
p('Name: ’ . $name),
end_html();

exit;[/code]Save it as test.pl, and call it like http://example.com/test.pl?name=JohnDoe

Works for me. Perhaps you also need to use CGI::Carp qw(fatalsToBrowser), you’re probably generating an error somewhere.

:cool: Perl / MySQL / HTML+CSS


#3

I am not seeing any errors returned by the script.

Here are the HTTP headers that are returned. You can see the header with the problematic text/plain MIME type, followed by the header that I generated. Any idea what the “5e1” is about? If this is some spurious junk that one of my scripts is managing to produce, would the Apache server automatically stick an HTTP header in front of it before returning it to the browser? If so, then that is probably the source of my problem.

Thanks
-Mark

HTTP/1.1 200 OK
Date: Tue, 08 Feb 2005 07:49:43 GMT
Server: Apache/1.3.31 (Unix) DAV/1.0.3 mod_gzip/1.3.26.1a PHP/4.3.10 mod_ssl/2.8.19 OpenSSL/0.9.6c
Transfer-Encoding: chunked
Content-Type: text/plain

5e1
Expires: Tue, 08 Feb 2005 07:49:50 GMT
Date: Tue, 08 Feb 2005 07:49:50 GMT
Newstyle-urls: 0
Content-Type: text/html; charset=ISO-8859-1


#4

I finally found the cause of my difficulties. My script called an external subroutine which returns the dimensions of a JPG file. Without delving into the guts of the JPG module, it appears that something happens there which causes the web server to return an HTTP header with a “text/plain” MIME type. So I need to generate my “text/HTML” HTTP header before using this subroutine.

Problem solved.

Thanks for your help!
-Mark