CGI.PM not setting HTTP header

M

Mark

My Perl CGI script is producing valid HTML (as far as I can tell,)
but the server is returning a MIME type of 'text/plain'. I have tried
using the CGI.PM module to set the MIME type in the header,
but this merely adds a _second_ header to the output stream,
and this second header ends up displaying as text in the browser
window! The actual HTTp header is unaffected, still returning the
'text/plain' MIME type.

Any suggestions?

Thanks
-Mark
 
T

Tore Aursand

My Perl CGI script is producing valid HTML (as far as I can tell,)
but the server is returning a MIME type of 'text/plain'.
^^^^^^^^^^

Is the server written in Perl?
 
J

John Bokma

Mark said:
My Perl CGI script is producing valid HTML (as far as I can tell,)

Don't tell, check. Run your script from the CLI, or connect to the
webserver using telnet port 80. Nowadays you often have to include a host
header when you use telnet (since many virtual hosts share one ip)
but the server is returning a MIME type of 'text/plain'.

Uhmmm... maybe the server is confused about the default "XHTML" header?
I have tried
using the CGI.PM module to set the MIME type in the header,
but this merely adds a _second_ header to the output stream,
and this second header ends up displaying as text in the browser
window! The actual HTTp header is unaffected, still returning the
'text/plain' MIME type.

Any suggestions?

Reduce your script to the bare minimum that shows this result, and post it
(in this thread, don't start a new one)
 
A

Alan Mead

My Perl CGI script is producing valid HTML (as far as I can tell,)
but the server is returning a MIME type of 'text/plain'. I have tried
using the CGI.PM module to set the MIME type in the header,
but this merely adds a _second_ header to the output stream,
and this second header ends up displaying as text in the browser
window! The actual HTTp header is unaffected, still returning the
'text/plain' MIME type.

Well, here's a stupid question: Is the very first thing you print the
CGI.pm function "header"? If you have a complicated script, you can test
this by printing header near the top.

I've had weird results using IE (and less often Netscape) to browse the
results of web applications. The browser would think it's an XML
document and then complain that it's broken. I don't recall what went
wrong, but it was always a bug in my script that I was able to find.

Or if you're saying that even the most basic CGI script always gets
returned as plain text then I guess you're having an apache/iis/whatever
issue, not a Perl issue.

-Alan
 
M

Mark

Thanks to everyone who replied. It appears that Alan was on the
right track. My CGI script is implemented in two separate modules,
and each module creates a new CGI object. I was setting the header
from the second instance, which was streamed after whatever
default header was being produced by the _first_ instance.

I added my header code to the first instance, and now everything
works fine.

So. . .it appears that the web server will output an HTTP header
for each instance of a CGI object that I create? Is there any way
to create a CGI object _without_ producing an HTTP header?
I am using CGI in my first module simply to parse the URL.
I would prefer not to produce any output from the first module,
although I suppose I can live with it.

Thanks
-Mark
 
A

Alan J. Flavell

So. . .it appears that the web server will output an HTTP header
for each instance of a CGI object that I create? Is there any way
to create a CGI object _without_ producing an HTTP header?

I feel sure you're looking at this from the wrong end. The idea of
the CGI.pm object is that it handles the CGI request/response
transaction. Each invocation of a CGI script will handle one
request/response transaction (that's the way that CGI scripts work in
general, it's not in any way special to CGI.pm), and, as such, needs
one CGI object which will carry the transaction through from
invocation to completion.

It's only in specialised uses of CGI.pm that one would ever use more
than one CGI object, and I see no reason to think that you're dealing
with such a specialised requirement from your description.
I am using CGI in my first module simply to parse the URL.
I would prefer not to produce any output from the first module,
although I suppose I can live with it.

We haven't see any code yet; but my hunch is that what you really need
your script to do is to hand the same CGI object around to the various
parts which use it.

hope this helps
 
M

Mark

Alan J. Flavell said:
On Thu, 25 Nov 2004, Mark wrote:

We haven't see any code yet; but my hunch is that what you really need
your script to do is to hand the same CGI object around to the various
parts which use it.

Yep. I solved the problem by using only one CGI object.
No doubt I can refine it a bit more, but it works for now.
Thanks again for your help.

-Mark
 
A

Alan J. Flavell


Watch out. The hon Usenauts are trying to advise you, in their
somewhat grumpy and obtuse fashion, that you started off by saying

|| but the server is returning a MIME type of 'text/plain'.

If you truly had a web server problem, then your problem would be
wildly off-topic here. I think they want you to acknowledge that.

As it turned out, you've solved your problem by using CGI.pm better.
It's still not (quite) a Perl language problem, and might have been
more at home on a usenet group that's primarily about the CGI, but
you've rubbed-up several of the respected contributors here in the
wrong way, and I wouldn't really recommend that: they may prove
infinitely helpful to you in future, if you give them the idea that
you're keen to learn.

all the best
 
T

Tore Aursand

It's an Apache server.

....and Apache, last time I checked, is not written in Perl. Your problem,
as you describe it, is that the _web server_ (Apache) is not sending the
correct header(s) to the client.

Thus, this isn't a Perl problem at all. At least you haven't showed us
the _Perl relevant_ information to make it a Perl problem.

I'm not emphasizing this just to be rude or something, but sometimes (ie.
always) it's smarter to post a question in the most relevant newsgroups.
 
M

Mark

Alan J. Flavell said:
As it turned out, you've solved your problem by using CGI.pm better.
It's still not (quite) a Perl language problem, and might have been
more at home on a usenet group that's primarily about the CGI, but
you've rubbed-up several of the respected contributors here in the
wrong way, and I wouldn't really recommend that: they may prove
infinitely helpful to you in future, if you give them the idea that
you're keen to learn.

I certainly do not believe that I said or did anything even remotely
offensive.
 
A

Alan Mead

So. . .it appears that the web server will output an HTTP header
for each instance of a CGI object that I create? Is there any way

No, I think the server adds something but it doesn't print the
headers. It's up to you to print an appropriate header (usually
"print header" after importing the CGI shortcuts) once for each
HTML page that you create.

You might wonder why? Well, headers are also the way you transmit cookies
and do things like redirect. So you want to control them in your program
rather than have the server do it... or automatically by CGI.pm.

-Alan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top