=encoding euc-jp =head1 NAME =begin original perlfaq9 - Networking ($Revision: 1.26 $, $Date: 1999/05/23 16:08:30 $) =end original perlfaq9 - ネットワーク ($Revision: 1.26 $, $Date: 1999/05/23 16:08:30 $) =head1 DESCRIPTION このセクションでは、ネットワーク、インターネット、web に関する 質問を扱っています。 =head2 My CGI script runs from the command line but not the browser. (500 Server Error) (私のCGIスクリプトはコマンドラインでは動くのだけど、ブラウザー上では動きません (500 Server Errorになります)) =begin original 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. =end original FAQの以下の部分を読んで、あなたの問題が簡単に答られるものではないと わかったのなら、(HTTP、HTMLやCGIプロトコルに関するものなら) comp.infosystems.www.authoring.cgiにポストすれば 有用なリプライが得られるでしょう。 Perlに関する質問のように見えていても、実はCGIに関するものだというものが comp.lang.perl.miscに投稿されることがありますが、回答はついていません。 便利なFAQや関連するドキュメントを以下に挙げます: 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? (CGIプログラムから、もっとまともなエラーメッセージを得るには?) =begin original Use the CGI::Carp module. It replaces C and C, plus the normal Carp modules C, C, and C functions with more verbose and safer versions. It still sends them to the normal server error log. =end original CGI::Carpモジュールを使いましょう。このモジュールはCとCの 置き換えを行い、さらに通常のCarpモジュールのC、C、 Cといった関数をより饒舌で安全なものに置き換えます。 その出力は、サーバーの通常のエラーログに送られます。 use CGI::Carp; warn "This is a complaint"; die "But this one is serious"; 以下のCGI::Carpの使用例では、エラーをあなたの選択したファイルへリダイレクトし、 コンパイル時の警告も同様に補足するためにBEGINブロックに 置いています: 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); } 深刻なエラーをクライアントのブラウザーに戻すように変更することもできます。 これはあなたがデバッグするには良いでしょうが、エンドユーザーを 混乱させてしまうかもしれません。 use CGI::Carp qw(fatalsToBrowser); die "Bad error here"; =begin original Even if the error happens before you get the HTTP header out, the module will try to take care of this to avoid the dreaded server 500 errors. Normal warnings still go out to the server error log (or wherever you've sent them with C) with the application name and date stamp prepended. =end original あなたがHTTPヘッダーを受け取るよりも前にエラーが起こったとしても、 モジュールはサーバーの500エラーを避けるためにそのエラーを取り扱おうと するでしょう。 通常の警告はサーバーのエラーログ(もしくはあなたがCで指定した場所)に アプリケーションの名前と日付を伴って送られます。 =head2 How do I remove HTML from a string? (ある文字列からHTMLを取り除くには?) =begin original 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. =end original (最速ではありませんが)最も正しい方法は、CPANにあるHTML::Parserを 使うというものです。 もう一つのまず正しい方法は、HTML::FormatText を使って HTML を 取り除くだけでなく、結果のプレーンテキストを簡単に整形することです。 多くの人が、C<< s/<.*?>//g >>のような単純な(simple-minded)正規表現を 使ったアプローチを行おうとするのですが、これは多くの場合 失敗していまいます。なぜなら、タグは行をまたがって継続する可能性があり、 クォートされたアングルブラケットを含む可能性があり、 HTML のコメントがあるかもしれないからです。 さらに、人々はC<<>のようなエンティティを変換することを忘れてしまうのです。 以下の例は “単純な”アプローチで、ほとんどのファイルに対しては うまくいきます: #!/usr/bin/perl -p0777 s/<(?:[^>'"]*|(['"]).*?\1)*>//gs もし、より完璧な解決策を求めているのなら、 http://www.perl.com/CPAN/authors/Tom_Christiansen/scripts/striphtml.gz にある 3-stage striphtml プログラムを参照してみてください。 =begin original Here are some tricky cases that you should think about when picking a solution: =end original 以下に挙げたのは、あなたが自分でやろうとしたときに 考慮すべきであろうトリッキーな例です: A > B A > B <# Just data #> >>>>>>>>>>> ]]> =begin original If HTML comments include other tags, those solutions would also break on text like this: =end original 以下のテキストのようにHTMLのコメントが他のタグを含んでいた場合には、 せっかくの対応策もダメにしてしまうかもしれません: =head2 How do I extract URLs? (URLの展開を行うには?) 素早いけど完璧ではないやり方はこうです #!/usr/bin/perl -n00 # qxurl - tchrist@perl.com print "$2\n" while m{ < \s* A \s+ HREF \s* = \s* (["']) (.*?) \1 \s* > }gsix; =begin original This version does not adjust relative URLs, understand alternate bases, deal with HTML comments, deal with HREF and NAME attributes in the same tag, understand extra qualifiers like TARGET, 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. =end original これは相対URLを扱うことができず、alternate baseを理解できず、HTML のコメント、 同じタグにあるHREFやNAMEといったアトリビュートを扱うことができませんし、 TARGET のような追加の修飾子を理解せず、 URL自身を引数として受け取ることができません。 これは http://www.perl.com/CPAN/authors/Tom_Christiansen/scripts/xurl.gz のような、LWPスイートにあるモジュールを使っている、より“完全な” 解決策よりも百倍の早さで実行されます。 =head2 How do I download a file from the user's machine? How do I open a file on another machine? (ユーザーのマシンからファイルをダウンロードするには? 別のマシンにあるファイルをオープンするには?) HTMLフォームのコンテキストにあるのならば、B エンコーディングとして知られているものを使うことができるでしょう。 CGI.pmモジュール(CPANで入手可能です)はこれを start_multipart_form() というstarform()メソッドとは異なるメソッドでサポートしています。 =head2 How do I make a pop-up menu in HTML? (HTMLでポップアップメニューを作るには?) =begin original Use the B<< >> タグと B<<