Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Unexpected "text/plain" output with cgi

by Samn (Monk)
on Nov 13, 2002 at 17:02 UTC ( #212629=perlquestion: print w/replies, xml ) Need Help??

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

I'm reworking my perl driven site and have managed to create a perplexing new error. The output of my pages comes as a "Text Dociment" instead of an HTML document.

http://www.robotskull.com/cgi-bin/index.cgi

Displays just fine in all browsers, and is (almost) W3C verified. But when accessing any of the pages on the site like

http://www.robotskull.com/cgi-bin/index.cgi?page=kittens

It is apparently transmitted as a text document. In IE this renders flawlessly, but the W3C validator on the page says it can not be parsed. As well, Mozilla based browsers just display the HTML code instead of rendering it as a webpage.

A novice, I'm baffled and don't even know where to begin.

My index.cgi looks to see if it has been passed a "page=" variable. If it has, it runs the specified subroutine. Many of the pages/subroutines are complex with database calls and what not, but the specific one above ("kittens") is only a single print statement.

If no "page=" is specified, index.cgi does nothing and leaves the main HTML cell empty.

Where should I start with this?

=== Illustration of index.cgi:
<code> #!/usr/bin/perl require *.rs; # files that contain one subroutine each print qq~ <html> <layout> ~; if ($page) { &$page; } print qq~ <layout> </html> ~;

  • Comment on Unexpected "text/plain" output with cgi

Replies are listed 'Best First'.
Re: Unexpected "text/plain" output with cgi
by janjan (Beadle) on Nov 13, 2002 at 17:15 UTC
    You can always explicitly send a header -- it doesn't look like you're doing that. CGI.pm makes it easy:
    print $q->header('text/html');
    perldoc CGI for more on this. Or, you can send your header manually:
    print "Content-type: text/html\n\n";
    Other than that, I'm not sure why the Mozilla browsers aren't rendering the code. Perhaps posting the contents of the $page variable or the *.rs files would help.
Re: Unexpected "text/plain" output with cgi
by enoch (Chaplain) on Nov 13, 2002 at 17:48 UTC
    Since no one has pointed it out, yet, you really really, really do not want to do:
    if ($page) { &$page; }
    What if someone (and, don't do this) passed in the URL http://www.robotskull.com/cgi-bin/index.cgi?page=kittens;`rm -rf /etc` (or worse). A better way would be to:
    if($page) { SWITCH: { &kitten, last SWITCH if($page eq 'kitten'); &foo, last SWITCH if($page eq 'foo'); &bar, last SWITCH if($page eq 'bar'); . . . print STDERR "invalid CGI parameter", last SWITCH; } }

    Enoch
Re: Unexpected "text/plain" output with cgi
by iburrell (Chaplain) on Nov 13, 2002 at 17:22 UTC
    You need to send back the HTTP header before sending any HTML. You may be doing this but it isn't happening for the code path that prints the secondary pages. Specifically, you need to set Content-Type header to text/html. Otherwise, Apache sets the default to text/plain which is what is showing up on your link above. The easiest way to do this is with the CGI.pm module's header method.
    use CGI; print header;

    IE is broken and recognizes the document is HTML and displays it as a web page. Mozilla is behaving properly and shows the HTML as plaintext like the server told it to.

      I guess you meant something like
      use CGI qw(:standard);
      Otherwise the header function won't be exported into your namespace.


      $|=$_="1g2i1u1l2i4e2n0k",map{print"\7",chop;select$,,$,,$,,$_/7}m{..}g

Re: Unexpected "text/plain" output with cgi
by fglock (Vicar) on Nov 13, 2002 at 17:53 UTC

    Interesting. Your HEAD is obviously wrong, maybe caused by something in your "*.rs" line.

    It shows:

    200 OK Connection: close Date: Wed, 13 Nov 2002 17:50:18 GMT Server: Apache/1.3.26 (Unix) PHP/4.2.2 mod_gzip/1.3.19.1a Content-Type: text/plain Client-Date: Wed, 13 Nov 2002 16:36:49 GMT Client-Junk: loading test.rs<br>loading admin.rs<br>loading cleanmails +.rs<br>loading testtest.rs<br>loading user.rs<br>loading robocon.rs<b +r>loading gator.rs<br>loading camn.rs<br>loading weather.rs<br>loadin +g getdef.rs<br>loading development.rs<br>loading profile.rs<br>loadin +g mail.rs<br>loading vocab.rs<br>loading rfc.rs<br>loading story.rs<b +r>loading test3.rs<br>loading admin_field_edit.rs<br>loading glossary +.rs<br>loading fack.rs<br>loading everbox.rs<br>loading emo.rs<br>loa +ding kittens.rs<br>loading icons.rs<br>loading gazelle.rs<br>loading +create.rs<br>loading comments.rs<br>loading candy.rs<br>loading babbl +e.rs<br>loading meta_change.rs<br>loading meta_display.rs<br>loading +meta_base.rs<br>loading meta_archive.rs<br>loading metapad.rs<br>load +ing login.rs<br>loading space.rs<br>loading links.rs<br>loading truth +.rs<br>loading shirtninja.rs<br>loading post.rs<br>loading modules.rs +<br>loading stayfresh.rs<br>loading titles.rs<br>loading update_syste +m_notice.rs<br>loading user_list.rs<br>loading raim.rs<br>loading ski +ns.rs<br>loading scratchpad.rs<br>loading notes.rs<br>loading news.rs +<br>loading titlechange.rs<br>loading ropasc.rs<br>loading send_note. +rs<br>loading suggest.rs<br>loading preview_module.rs<br>loading scor +e.rs<br>loading skullbot.rs<br>loading jessika_cam.rs<br>loading jess +ika.rs<br>loading printtabs.rs<br>loading chrisemails.rs<br>loading j +ournal.rs<br>loading source.rs<br>loading school.rs<br>loading thetea +m.rs<br>loading system_notice.rs<br>Content-type: text/html Client-Response-Num: 1
Re: Unexpected "text/plain" output with cgi
by Samn (Monk) on Nov 13, 2002 at 18:00 UTC
    Thanks for the input guys. The first thing that index.cgi always prints is

    print "Content-type: text/html\n\n";

    I'll let CGI.pm do it instead and see how that works out.

    And I know running &$variable is, in a word, "retarded." I'm going to switch it to checking to see if the file exists, then requiring it then running it. Gracia!

Re: Unexpected "text/plain" output with cgi
by Mr. Muskrat (Canon) on Nov 13, 2002 at 17:19 UTC

    If it is transmitted as a text document, then your cgi script is setting a plain text content-type header...

    Show us the real code please, not psueodocode.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2023-11-30 08:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?