Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Should I just print my own HTTP headers?

by Cap'n Steve (Friar)
on Apr 05, 2007 at 06:22 UTC ( #608420=perlquestion: print w/replies, xml ) Need Help??

Cap'n Steve has asked for the wisdom of the Perl Monks concerning the following question:

I'm sure you've heard this a million times: CGI.pm is big and I don't want to use it. With this script though, I'm only ever taking one numeric parameter, so the only thing I really need to think about is the headers. Does CGI.pm add any headers transparently or is that all Apache's responsibility? Basically, if I were to just output all the headers in a simple print statement, is there anything I'm likely to screw up or miss completely?
  • Comment on Should I just print my own HTTP headers?

Replies are listed 'Best First'.
Re: Should I just print my own HTTP headers?
by ambrus (Abbot) on Apr 05, 2007 at 08:06 UTC

    You can. Just take care of that you have to output CGI headers, not http headers. This means in practice that you do not have to print a http status line (the one with a three-digit number), instead the webserver will print one as approperiate (200 normally, 302 in case you print a Location header so the server thinks it's a redirect), and that the server will correct the line endings of your header to crlfs. (Update: the server will also do the chunked encoding if you don't specify the Content-length, do the keepalive connection, and lots of other magical stuff I can't even imagine.)

    The only header you are required to print is Content-type. Just search perlmonks for "Content-type", and you'll find lots of examples. (In case you wanted to write http requests by hand, you can do that too: in that case the only required header is Host.)

    Update: it seems I didn't answer your question. Well, the CGI module does not output any headers, so it is safe to use it just the input part.

Re: Should I just print my own HTTP headers?
by zer (Deacon) on Apr 05, 2007 at 06:29 UTC

    as long as it is simple requests it shouldnt be a problem

    incoming headers tend to be more complicated from a cgi perspected if there is posted data, which cgi.pm takes care of quite nicely. But if it is basic get stuff it is pretty straight forward parsing.

    As for outgoing. There is nothing to it. Just take note of syntax esp if youre doing cookies or other meta headers

    the w3c can help you if you get jammed
    w3c on HTTP

Re: Should I just print my own HTTP headers?
by samtregar (Abbot) on Apr 05, 2007 at 17:08 UTC
    I'm sure you've heard this a million times: CGI.pm is big and I don't want to use it.

    Yup, and every time I hear it it sounds pretty foolish. CGI.pm may be big, but it uses Autoloader extensively to avoid memory bloat. And if you've got a modern Perl you don't even have to download it, it's a standard module! So just use it already! In the time it took you to ask this question you could be done already, not to mention dealing with parsing parameters.

    -sam

      True, but even ignoring the Autoloader stuff, it's still 800+ lines of code that has to be loaded to accomplish something that would probably take me around 10.
        You make it sound like you're going to load those lines of code with your bare hands in an ice storm. Trust me, this is not the bottleneck you're looking for! I've profiled a lot of slow web-apps in my day and I've never once seen a CGI.pm method in the top 10.

        -sam

        You might be better off using CGI.pm to get started, and then later on if loading the 800+ lines appears to be a problem, replace it with your 10 lines. That way you only have to write one line now: "use CGI;", and you'll be able to look at the HTML source being produced so that you know exactly what you need to replace it with, if you still want to.
Re: Should I just print my own HTTP headers?
by naikonta (Curate) on Apr 13, 2007 at 02:14 UTC
    It absolutely depends on what are you trying to accomplish. Are you only printing a single line? Don't you really need a CGI parameter processing? Oh you said only one numeric parameter. Are you sure the user will ever only type in that numeric input? Do you really trust that? Don't you ever worry that the user will indeed just fill in the parameter with whatever pleases him/her, say a 5MB beast of something?

    My simple rule of thumbs for web application:

    • If it's a simple script and I need it quickly, CGI is my not so secret weapon. I use its header(), param(), and html-related methods.
    • If it's a complex application, I use CGI::Simple. Why? Because it shares (well, it reuses) the same code for header, cookie, and param processing (to name a few) codes. I don't need any html-related methods from CGI, I use HTML template for that purpose. So CGI::Simple just fits to what I need.
      Well, it should only be run via a mod_rewrite rule, but it's easy enough to double check:
      if ($ENV{'QUERY_STRING'} !~ /^\d{,10}$/) { die 'Invalid ID'; }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2021-06-24 23:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (133 votes). Check out past polls.

    Notices?