X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=pod%2Fperlfaq9.pod;h=cdc00d6755319545fb7081bb62ca47d8107c467e;hb=8b4ac5a423952fb163d48c435ab6e00458e66f1f;hp=65360643609528cc42b50e6828a7538c44eabeb1;hpb=be94a90153eca0a67ea73e2ec074645fb9e0b70d;p=p5sagit%2Fp5-mst-13.2.git diff --git a/pod/perlfaq9.pod b/pod/perlfaq9.pod index 6536064..cdc00d6 100644 --- a/pod/perlfaq9.pod +++ b/pod/perlfaq9.pod @@ -1,45 +1,66 @@ =head1 NAME -perlfaq9 - Networking ($Revision: 1.24 $, $Date: 1999/01/08 05:39:48 $) +perlfaq9 - Networking ($Revision: 1.5 $, $Date: 2001/11/09 08:06:04 $) =head1 DESCRIPTION This section deals with questions related to networking, the internet, and a few on the web. -=head2 My CGI script runs from the command line but not the browser. (500 Server Error) +=head2 What is the correct form of response from a CGI script? -If you can demonstrate that you've read the following FAQs and that -your problem isn't something simple that can be easily answered, you'll -probably receive a courteous and useful reply to your question if you -post it on comp.infosystems.www.authoring.cgi (if it's something to do -with HTTP, HTML, or the CGI protocols). Questions that appear to be Perl -questions but are really CGI ones that are posted to comp.lang.perl.misc -may not be so well received. +(Alan Flavell answers...) + +The Common Gateway Interface (CGI) specifies a software interface between +a program ("CGI script") and a web server (HTTPD). It is not specific +to Perl, and has its own FAQs and tutorials, and usenet group, +comp.infosystems.www.authoring.cgi -The useful FAQs and related documents are: +The original CGI specification is at: http://hoohoo.ncsa.uiuc.edu/cgi/ - CGI FAQ - http://www.webthing.com/tutorials/cgifaq.html +Current best-practice RFC draft at: http://CGI-Spec.Golux.Com/ - Web FAQ - http://www.boutell.com/faq/ +Other relevant documentation listed in: http://www.perl.org/CGI_MetaFAQ.html - WWW Security FAQ - http://www.w3.org/Security/Faq/ +These Perl FAQs very selectively cover some CGI issues. However, Perl +programmers are strongly advised to use the CGI.pm module, to take care +of the details for them. - HTTP Spec - http://www.w3.org/pub/WWW/Protocols/HTTP/ +The similarity between CGI response headers (defined in the CGI +specification) and HTTP response headers (defined in the HTTP +specification, RFC2616) is intentional, but can sometimes be confusing. + +The CGI specification defines two kinds of script: the "Parsed Header" +script, and the "Non Parsed Header" (NPH) script. Check your server +documentation to see what it supports. "Parsed Header" scripts are +simpler in various respects. The CGI specification allows any of the +usual newline representations in the CGI response (it's the server's +job to create an accurate HTTP response based on it). So "\n" written in +text mode is technically correct, and recommended. NPH scripts are more +tricky: they must put out a complete and accurate set of HTTP +transaction response headers; the HTTP specification calls for records +to be terminated with carriage-return and line-feed, i.e ASCII \015\012 +written in binary mode. + +Using CGI.pm gives excellent platform independence, including EBCDIC +systems. CGI.pm selects an appropriate newline representation +($CGI::CRLF) and sets binmode as appropriate. + +=head2 My CGI script runs from the command line but not the browser. (500 Server Error) + +If you can demonstrate that you've read the FAQs and that +your problem isn't something simple that can be easily answered, you'll +probably receive a courteous and useful reply to your question if you +post it on comp.infosystems.www.authoring.cgi (if it's something to do +with HTTP or the CGI protocols). Questions that appear to be Perl +questions but are really CGI ones that are posted to comp.lang.perl.misc +are not so well received. - HTML Spec - http://www.w3.org/TR/REC-html40/ - http://www.w3.org/pub/WWW/MarkUp/ +The useful FAQs, related documents, and troubleshooting guides are +listed in the CGI Meta FAQ: - CGI Spec - http://www.w3.org/CGI/ + http://www.perl.org/CGI_MetaFAQ.html - CGI Security FAQ - http://www.go2net.com/people/paulp/cgi-security/safe-cgi.txt =head2 How can I get better error messages from a CGI program? @@ -76,14 +97,16 @@ stamp prepended. =head2 How do I remove HTML from a string? -The most correct way (albeit not the fastest) is to use HTML::Parse -from CPAN (part of the HTML-Tree package on CPAN). +The most correct way (albeit not the fastest) is to use HTML::Parser +from CPAN. Another mostly correct +way is to use HTML::FormatText which not only removes HTML but also +attempts to do a little simple formatting of the resulting plain text. Many folks attempt a simple-minded regular expression approach, like -C.*?E//g>, but that fails in many cases because the tags +C<< s/<.*?>//g >>, but that fails in many cases because the tags may continue over line breaks, they may contain quoted angle-brackets, -or HTML comment may be present. Plus folks forget to convert -entities, like C<<> for example. +or HTML comment may be present. Plus, folks forget to convert +entities--like C<<> for example. Here's one "simple-minded" approach, that works for most files: @@ -92,7 +115,7 @@ Here's one "simple-minded" approach, that works for most files: If you want a more complete solution, see the 3-stage striphtml program in -http://www.perl.com/CPAN/authors/Tom_Christiansen/scripts/striphtml.gz +http://www.cpan.org/authors/Tom_Christiansen/scripts/striphtml.gz . Here are some tricky cases that you should think about when picking @@ -100,7 +123,7 @@ a solution: A > B - A > B @@ -120,23 +143,28 @@ on text like this: =head2 How do I extract URLs? -A quick but imperfect approach is +You can easily extract all sorts of URLs from HTML with +C which handles anchors, images, objects, +frames, and many other tags that can contain a URL. If you need +anything more complex, you can create your own subclass of +C or C. You might even use +C as an example for something specifically +suited to your needs. + +Less complete solutions involving regular expressions can save +you a lot of processing time if you know that the input is simple. One +solution from Tom Christiansen runs 100 times faster than most +module based approaches but only extracts URLs from anchors where the first +attribute is HREF and there are no other attributes. + + #!/usr/bin/perl -n00 + # qxurl - tchrist@perl.com + print "$2\n" while m{ + < \s* + A \s+ HREF \s* = \s* (["']) (.*?) \1 + \s* > + }gsix; - #!/usr/bin/perl -n00 - # qxurl - tchrist@perl.com - print "$2\n" while m{ - < \s* - A \s+ HREF \s* = \s* (["']) (.*?) \1 - \s* > - }gsix; - -This version does not adjust relative URLs, understand alternate -bases, deal with HTML comments, deal with HREF and NAME attributes in -the same tag, or accept URLs themselves as arguments. It also runs -about 100x faster than a more "complete" solution using the LWP suite -of modules, such as the -http://www.perl.com/CPAN/authors/Tom_Christiansen/scripts/xurl.gz -program. =head2 How do I download a file from the user's machine? How do I open a file on another machine? @@ -147,7 +175,7 @@ the same as the startform() method. =head2 How do I make a pop-up menu in HTML? -Use the BSELECTE> and BOPTIONE> tags. The CGI.pm +Use the B<<