perlfaq9.pod

来自「MSYS在windows下模拟了一个类unix的终端」· POD 代码 · 共 603 行 · 第 1/2 页

POD
603
字号
=head1 NAMEperlfaq9 - Networking ($Revision: 1.26 $, $Date: 1999/05/23 16:08:30 $)=head1 DESCRIPTIONThis 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)If you can demonstrate that you've read the following FAQs and thatyour problem isn't something simple that can be easily answered, you'llprobably receive a courteous and useful reply to your question if youpost it on comp.infosystems.www.authoring.cgi (if it's something to dowith HTTP, HTML, or the CGI protocols).  Questions that appear to be Perlquestions but are really CGI ones that are posted to comp.lang.perl.miscmay not be so well received.The useful FAQs and related documents are:    CGI FAQ        http://www.webthing.com/tutorials/cgifaq.html    Web FAQ        http://www.boutell.com/faq/    WWW Security FAQ        http://www.w3.org/Security/Faq/    HTTP Spec        http://www.w3.org/pub/WWW/Protocols/HTTP/    HTML Spec        http://www.w3.org/TR/REC-html40/        http://www.w3.org/pub/WWW/MarkUp/    CGI Spec        http://www.w3.org/CGI/    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?Use the CGI::Carp module.  It replaces C<warn> and C<die>, plus thenormal Carp modules C<carp>, C<croak>, and C<confess> functions withmore verbose and safer versions.  It still sends them to the normalserver error log.    use CGI::Carp;    warn "This is a complaint";    die "But this one is serious";The following use of CGI::Carp also redirects errors to a file of your choice,placed in a BEGIN block to catch compile-time warnings as well:    BEGIN {        use CGI::Carp qw(carpout);        open(LOG, ">>/var/local/cgi-logs/mycgi-log")            or die "Unable to append to mycgi-log: $!\n";        carpout(*LOG);    }You can even arrange for fatal errors to go back to the client browser,which is nice for your own debugging, but might confuse the end user.    use CGI::Carp qw(fatalsToBrowser);    die "Bad error here";Even if the error happens before you get the HTTP header out, the modulewill try to take care of this to avoid the dreaded server 500 errors.Normal warnings still go out to the server error log (or whereveryou've sent them with C<carpout>) with the application name and datestamp prepended.=head2 How do I remove HTML from a string?The most correct way (albeit not the fastest) is to use HTML::Parserfrom CPAN.  Another mostly correctway is to use HTML::FormatText which not only removes HTML but alsoattempts to do a little simple formatting of the resulting plain text.Many folks attempt a simple-minded regular expression approach, likeC<< s/<.*?>//g >>, but that fails in many cases because the tagsmay continue over line breaks, they may contain quoted angle-brackets,or HTML comment may be present.  Plus, folks forget to convertentities--like C<&lt;> for example.Here's one "simple-minded" approach, that works for most files:    #!/usr/bin/perl -p0777    s/<(?:[^>'"]*|(['"]).*?\1)*>//gsIf you want a more complete solution, see the 3-stage striphtmlprogram inhttp://www.perl.com/CPAN/authors/Tom_Christiansen/scripts/striphtml.gz.Here are some tricky cases that you should think about when pickinga solution:    <IMG SRC = "foo.gif" ALT = "A > B">    <IMG SRC = "foo.gif"	 ALT = "A > B">    <!-- <A comment> -->    <script>if (a<b && a>c)</script>    <# Just data #>    <![INCLUDE CDATA [ >>>>>>>>>>>> ]]>If HTML comments include other tags, those solutions would also breakon text like this:    <!-- This section commented out.        <B>You can't see me!</B>    -->=head2 How do I extract URLs?A quick but imperfect approach is    #!/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 alternatebases, deal with HTML comments, deal with HREF and NAME attributesin the same tag, understand extra qualifiers like TARGET, or acceptURLs themselves as arguments.  It also runs about 100x faster than amore "complete" solution using the LWP suite of modules, such as thehttp://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?In the context of an HTML form, you can use what's known asB<multipart/form-data> encoding.  The CGI.pm module (available fromCPAN) supports this in the start_multipart_form() method, which isn'tthe same as the startform() method.=head2 How do I make a pop-up menu in HTML?Use the B<< <SELECT> >> and B<< <OPTION> >> tags.  The CGI.pmmodule (available from CPAN) supports this widget, as well as manyothers, including some that it cleverly synthesizes on its own.=head2 How do I fetch an HTML file?One approach, if you have the lynx text-based HTML browser installedon your system, is this:    $html_code = `lynx -source $url`;    $text_data = `lynx -dump $url`;The libwww-perl (LWP) modules from CPAN provide a more powerful wayto do this.  They don't require lynx, but like lynx, can still workthrough proxies:    # simplest version    use LWP::Simple;    $content = get($URL);    # or print HTML from a URL    use LWP::Simple;    getprint "http://www.linpro.no/lwp/";    # or print ASCII from HTML from a URL    # also need HTML-Tree package from CPAN    use LWP::Simple;    use HTML::Parser;    use HTML::FormatText;    my ($html, $ascii);    $html = get("http://www.perl.com/");    defined $html        or die "Can't fetch HTML from http://www.perl.com/";    $ascii = HTML::FormatText->new->format(parse_html($html));    print $ascii;=head2 How do I automate an HTML form submission?If you're submitting values using the GET method, create a URL and encodethe form using the C<query_form> method:    use LWP::Simple;    use URI::URL;    my $url = url('http://www.perl.com/cgi-bin/cpan_mod');    $url->query_form(module => 'DB_File', readme => 1);    $content = get($url);If you're using the POST method, create your own user agent and encodethe content appropriately.    use HTTP::Request::Common qw(POST);    use LWP::UserAgent;    $ua = LWP::UserAgent->new();    my $req = POST 'http://www.perl.com/cgi-bin/cpan_mod',                   [ module => 'DB_File', readme => 1 ];    $content = $ua->request($req)->as_string;=head2 How do I decode or create those %-encodings on the web?If you are writing a CGI script, you should be using the CGI.pm modulethat comes with perl, or some other equivalent module.  The CGI moduleautomatically decodes queries for you, and provides an escape()function to handle encoding.The best source of detailed information on URI encoding is RFC 2396.Basically, the following substitutions do it:    s/([^\w()'*~!.-])/sprintf '%%%02x', $1/eg;   # encode    s/%([A-Fa-f\d]{2})/chr hex $1/eg;            # decodeHowever, you should only apply them to individual URI components, notthe entire URI, otherwise you'll lose information and generally messthings up.  If that didn't explain it, don't worry.  Just go readsection 2 of the RFC, it's probably the best explanation there is.RFC 2396 also contains a lot of other useful information, including aregexp for breaking any arbitrary URI into components (Appendix B).=head2 How do I redirect to another page?According to RFC 2616, "Hypertext Transfer Protocol -- HTTP/1.1", thepreferred method is to send a C<Location:> header instead of aC<Content-Type:> header:    Location: http://www.domain.com/newpageNote that relative URLs in these headers can cause strange effectsbecause of "optimizations" that servers do.    $url = "http://www.perl.com/CPAN/";    print "Location: $url\n\n";    exit;To target a particular frame in a frameset, include the "Window-target:"in the header.    print <<EOF;    Location: http://www.domain.com/newpage    Window-target: <FrameName>    EOFTo be correct to the spec, each of those virtual newlines shouldreally be physical C<"\015\012"> sequences by the time your message isreceived by the client browser.  Except for NPH scripts, though, thatlocal newline should get translated by your server into standard form,so you shouldn't have a problem here, even if you are stuck on MacOS.Everybody else probably won't even notice.=head2 How do I put a password on my web pages?That depends.  You'll need to read the documentation for your webserver, or perhaps check some of the other FAQs referenced above.=head2 How do I edit my .htpasswd and .htgroup files with Perl?The HTTPD::UserAdmin and HTTPD::GroupAdmin modules provide aconsistent OO interface to these files, regardless of how they'restored.  Databases may be text, dbm, Berkley DB or any database with aDBI compatible driver.  HTTPD::UserAdmin supports files used by the`Basic' and `Digest' authentication schemes.  Here's an example:    use HTTPD::UserAdmin ();    HTTPD::UserAdmin	  ->new(DB => "/foo/.htpasswd")	  ->add($username => $password);=head2 How do I make sure users can't enter values into a form that cause my CGI script to do bad things?Read the CGI security FAQ, athttp://www-genome.wi.mit.edu/WWW/faqs/www-security-faq.html , and thePerl/CGI FAQ athttp://www.perl.com/CPAN/doc/FAQs/cgi/perl-cgi-faq.html .In brief: use tainting (see L<perlsec>), which makes sure that datafrom outside your script (eg, CGI parameters) are never used inC<eval> or C<system> calls.  In addition to tainting, never use thesingle-argument form of system() or exec().  Instead, supply thecommand and arguments as a list, which prevents shell globbing.=head2 How do I parse a mail header?For a quick-and-dirty solution, try this solution derivedfrom L<perlfunc/split>:    $/ = '';    $header = <MSG>;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?