perlfaq9 >
5.0150039
との差分
perlfaq9 5.0150039 と 5.6.1 の差分
1 | 1 | |
2 | 2 | =encoding euc-jp |
3 | 3 | |
4 | 4 | =head1 NAME |
5 | 5 | |
6 | 6 | =begin original |
7 | 7 | |
8 | perlfaq9 - | |
8 | perlfaq9 - Networking ($Revision: 1.26 $, $Date: 1999/05/23 16:08:30 $) | |
9 | 9 | |
10 | 10 | =end original |
11 | 11 | |
12 | perlfaq9 - | |
12 | perlfaq9 - ネットワーク ($Revision: 1.26 $, $Date: 1999/05/23 16:08:30 $) | |
13 | 13 | |
14 | 14 | =head1 DESCRIPTION |
15 | 15 | |
16 | ||
16 | このセクションでは、ネットワーク、インターネット、web に関する | |
17 | 質問を扱っています。 | |
17 | 18 | |
18 | ||
19 | =head2 My CGI script runs from the command line but not the browser. (500 Server Error) | |
19 | sending and receiving email as well as general networking. | |
20 | 20 | |
21 | ||
21 | (私のCGIスクリプトはコマンドラインでは動くのだけど、ブラウザー上では動きません | |
22 | (500 Server Errorになります)) | |
22 | 23 | |
23 | このセクションでは、web サイトの実行、メールの送受信、および一般的な | |
24 | ネットワークに関する質問を扱っています。 | |
25 | ||
26 | =head2 Should I use a web framework? | |
27 | ||
28 | (web フレームワークを使うべき?) | |
29 | ||
30 | 24 | =begin original |
31 | 25 | |
32 | ||
26 | If you can demonstrate that you've read the following FAQs and that | |
33 | ||
27 | your problem isn't something simple that can be easily answered, you'll | |
34 | ||
28 | probably receive a courteous and useful reply to your question if you | |
35 | ||
29 | post it on comp.infosystems.www.authoring.cgi (if it's something to do | |
30 | with HTTP, HTML, or the CGI protocols). Questions that appear to be Perl | |
31 | questions but are really CGI ones that are posted to comp.lang.perl.misc | |
32 | may not be so well received. | |
36 | 33 | |
37 | 34 | =end original |
38 | 35 | |
39 | はい | |
36 | FAQの以下の部分を読んで、あなたの問題が簡単に答られるものではないと | |
40 | ||
37 | わかったのなら、(HTTP、HTMLやCGIプロトコルに関するものなら) | |
41 | ||
38 | comp.infosystems.www.authoring.cgiにポストすれば | |
42 | ||
39 | 有用なリプライが得られるでしょう。 | |
40 | Perlに関する質問のように見えていても、実はCGIに関するものだというものが | |
41 | comp.lang.perl.miscに投稿されることがありますが、回答はついていません。 | |
43 | 42 | |
44 | ||
43 | 便利なFAQや関連するドキュメントを以下に挙げます: | |
45 | 44 | |
46 | ||
45 | CGI FAQ | |
47 | ||
46 | http://www.webthing.com/tutorials/cgifaq.html | |
48 | or L<Plack::Middleware::TemplateToolkit> | |
49 | so maintenance of your HTML files (and other assets) is easier. | |
50 | 47 | |
51 | ||
48 | Web FAQ | |
49 | http://www.boutell.com/faq/ | |
52 | 50 | |
53 | ||
51 | WWW Security FAQ | |
54 | ||
52 | http://www.w3.org/Security/Faq/ | |
55 | L<Template Toolkit|https://metacpan.org/module/Template> や | |
56 | L<Plack::Middleware::TemplateToolkit> のようなものを使いたいかもしれません。 | |
57 | 53 | |
58 | ||
54 | HTTP Spec | |
59 | ||
55 | http://www.w3.org/pub/WWW/Protocols/HTTP/ | |
60 | 56 | |
61 | ||
57 | HTML Spec | |
58 | http://www.w3.org/TR/REC-html40/ | |
59 | http://www.w3.org/pub/WWW/MarkUp/ | |
62 | 60 | |
63 | ||
61 | CGI Spec | |
62 | http://www.w3.org/CGI/ | |
64 | 63 | |
65 | ||
64 | CGI Security FAQ | |
66 | ||
65 | http://www.go2net.com/people/paulp/cgi-security/safe-cgi.txt | |
67 | multilingual websites that are the core to international businesses. | |
68 | 66 | |
69 | =e | |
67 | =head2 How can I get better error messages from a CGI program? | |
70 | 68 | |
71 | ||
69 | (CGIプログラムから、もっとまともなエラーメッセージを得るには?) | |
72 | Perl のフレームワークには、基本的なファイルサーバとスケールの小さい | |
73 | イントラネット用から国際ビジネスのコアとなる巨大国際多言語 web サイトまで | |
74 | あらゆるものがあります。 | |
75 | 70 | |
76 | 71 | =begin original |
77 | 72 | |
78 | ||
73 | Use the CGI::Carp module. It replaces C<warn> and C<die>, plus the | |
79 | ma | |
74 | normal Carp modules C<carp>, C<croak>, and C<confess> functions with | |
80 | ||
75 | more verbose and safer versions. It still sends them to the normal | |
76 | server error log. | |
81 | 77 | |
82 | 78 | =end original |
83 | 79 | |
84 | ||
80 | CGI::Carpモジュールを使いましょう。このモジュールはC<warn>とC<die>の | |
85 | い | |
81 | 置き換えを行い、さらに通常のCarpモジュールのC<carp>、C<croak>、 | |
86 | ||
82 | C<confess>といった関数をより饒舌で安全なものに置き換えます。 | |
87 | ||
83 | その出力は、サーバーの通常のエラーログに送られます。 | |
88 | 84 | |
89 | ||
85 | use CGI::Carp; | |
86 | warn "This is a complaint"; | |
87 | die "But this one is serious"; | |
90 | 88 | |
91 | ||
89 | 以下のCGI::Carpの使用例では、エラーをあなたの選択したファイルへリダイレクトし、 | |
90 | コンパイル時の警告も同様に補足するためにBEGINブロックに | |
91 | 置いています: | |
92 | 92 | |
93 | ||
93 | BEGIN { | |
94 | use CGI::Carp qw(carpout); | |
95 | open(LOG, ">>/var/local/cgi-logs/mycgi-log") | |
96 | or die "Unable to append to mycgi-log: $!\n"; | |
97 | carpout(*LOG); | |
98 | } | |
94 | 99 | |
95 | ||
100 | 深刻なエラーをクライアントのブラウザーに戻すように変更することもできます。 | |
96 | ||
101 | これはあなたがデバッグするには良いでしょうが、エンドユーザーを | |
97 | ||
102 | 混乱させてしまうかもしれません。 | |
98 | 103 | |
99 | ||
104 | use CGI::Carp qw(fatalsToBrowser); | |
105 | die "Bad error here"; | |
100 | 106 | |
101 | 長い開発の歴史と大きなコミュニティおよびアドオン生態系を持ち、 | |
102 | 強力にオブジェクト指向で完全な機能があります。 | |
103 | サーバに対する完全な制御がある場所での大きく複雑なアプリケーションには | |
104 | 素晴らしいものです。 | |
105 | ||
106 | =item L<Dancer> | |
107 | ||
108 | 107 | =begin original |
109 | 108 | |
110 | ||
109 | Even if the error happens before you get the HTTP header out, the module | |
111 | ||
110 | will try to take care of this to avoid the dreaded server 500 errors. | |
112 | ||
111 | Normal warnings still go out to the server error log (or wherever | |
112 | you've sent them with C<carpout>) with the application name and date | |
113 | stamp prepended. | |
113 | 114 | |
114 | 115 | =end original |
115 | 116 | |
116 | ||
117 | あなたがHTTPヘッダーを受け取るよりも前にエラーが起こったとしても、 | |
117 | ||
118 | モジュールはサーバーの500エラーを避けるためにそのエラーを取り扱おうと | |
118 | ||
119 | するでしょう。 | |
120 | 通常の警告はサーバーのエラーログ(もしくはあなたがC<carpout>で指定した場所)に | |
121 | アプリケーションの名前と日付を伴って送られます。 | |
119 | 122 | |
120 | = | |
123 | =head2 How do I remove HTML from a string? | |
121 | 124 | |
125 | (ある文字列からHTMLを取り除くには?) | |
126 | ||
122 | 127 | =begin original |
123 | 128 | |
124 | ||
129 | The most correct way (albeit not the fastest) is to use HTML::Parser | |
125 | ||
130 | from CPAN. Another mostly correct | |
131 | way is to use HTML::FormatText which not only removes HTML but also | |
132 | attempts to do a little simple formatting of the resulting plain text. | |
126 | 133 | |
127 | 134 | =end original |
128 | 135 | |
129 | ||
136 | (最速ではありませんが)最も正しい方法は、CPANにあるHTML::Parserを | |
130 | ||
137 | 使うというものです。 | |
138 | もう一つのまず正しい方法は、HTML::FormatText を使って HTML を | |
139 | 取り除くだけでなく、結果のプレーンテキストを簡単に整形することです。 | |
131 | 140 | |
132 | ||
141 | 多くの人が、C<< s/<.*?>//g >>のような単純な(simple-minded)正規表現を | |
142 | 使ったアプローチを行おうとするのですが、これは多くの場合 | |
143 | 失敗していまいます。なぜなら、タグは行をまたがって継続する可能性があり、 | |
144 | クォートされたアングルブラケットを含む可能性があり、 | |
145 | HTML のコメントがあるかもしれないからです。 | |
146 | さらに、人々はC<<>のようなエンティティを変換することを忘れてしまうのです。 | |
133 | 147 | |
134 | ||
148 | 以下の例は “単純な”アプローチで、ほとんどのファイルに対しては | |
149 | うまくいきます: | |
135 | 150 | |
136 | ||
151 | #!/usr/bin/perl -p0777 | |
137 | ||
152 | s/<(?:[^>'"]*|(['"]).*?\1)*>//gs | |
138 | together existing Plack-compatible web applications with one central dispatcher. | |
139 | 153 | |
140 | ||
154 | もし、より完璧な解決策を求めているのなら、 | |
155 | http://www.perl.com/CPAN/authors/Tom_Christiansen/scripts/striphtml.gz | |
156 | にある 3-stage striphtml プログラムを参照してみてください。 | |
141 | 157 | |
142 | 現在の所実験的で、強力にオブジェクト指向で、速度重視で構築されており、 | |
143 | ごく小さい web アプリやカスタムフレームワークを構築したり、 | |
144 | 既存の Plack 互換の web アプリケーションを一つの中央ディスパッチャと | |
145 | 結びつけることを意図しています。 | |
146 | ||
147 | =back | |
148 | ||
149 | 158 | =begin original |
150 | 159 | |
151 | ||
160 | Here are some tricky cases that you should think about when picking | |
152 | ||
161 | a solution: | |
153 | L<Plack::Middleware|https://metacpan.org/search?q=plack%3A%3Amiddleware>). | |
154 | 162 | |
155 | 163 | =end original |
156 | 164 | |
157 | ||
165 | 以下に挙げたのは、あなたが自分でやろうとしたときに | |
158 | ||
166 | 考慮すべきであろうトリッキーな例です: | |
159 | L<Plack::Middleware|https://metacpan.org/search?q=plack%3A%3Amiddleware> が | |
160 | あります)。 | |
161 | 167 | |
162 | ||
168 | <IMG SRC = "foo.gif" ALT = "A > B"> | |
163 | 169 | |
164 | ||
170 | <IMG SRC = "foo.gif" | |
171 | ALT = "A > B"> | |
165 | 172 | |
166 | ||
173 | <!-- <A comment> --> | |
167 | 174 | |
168 | ||
175 | <script>if (a<b && a>c)</script> | |
169 | a standard that many Perl web frameworks use, you should not need to | |
170 | understand it to build a web site, the part you might want to use is L<Plack>. | |
171 | 176 | |
172 | ||
177 | <# Just data #> | |
173 | 178 | |
174 | ||
179 | <![INCLUDE CDATA [ >>>>>>>>>>>> ]]> | |
175 | Perl web フレームワークが使う標準で、web サイトを構築するためにこれを | |
176 | 理解する必要はないはずです; 使いたい部分は L<Plack> でしょう。 | |
177 | 180 | |
178 | 181 | =begin original |
179 | 182 | |
180 | L | |
183 | If HTML comments include other tags, those solutions would also break | |
181 | ||
184 | on text like this: | |
182 | components, a reference server and utilities for Web application frameworks. | |
183 | Plack is like Ruby's Rack or Python's Paste for WSGI. | |
184 | 185 | |
185 | 186 | =end original |
186 | 187 | |
187 | ||
188 | 以下のテキストのようにHTMLのコメントが他のタグを含んでいた場合には、 | |
188 | ||
189 | せっかくの対応策もダメにしてしまうかもしれません: | |
189 | L<ミドルウェア|https://metacpan.org/search?q=plack%3A%3Amiddleware> 要素、 | |
190 | リファレンスサーバ、ユーティリティを含みます。 | |
191 | Plack は Ruby の Rack や Python の WSGI のための Paste に似ています。 | |
192 | 190 | |
193 | ||
191 | <!-- This section commented out. | |
192 | <B>You can't see me!</B> | |
193 | --> | |
194 | 194 | |
195 | ||
195 | =head2 How do I extract URLs? | |
196 | but for anything other than a very basic web site, using a web framework | |
197 | (that uses L<Plack>) is a better option. | |
198 | 196 | |
199 | ||
197 | (URLの展開を行うには?) | |
200 | 198 | |
201 | ||
199 | 素早いけど完璧ではないやり方はこうです | |
202 | とても基本的な web サイト以外のものなら、(L<Plack> を使った) web | |
203 | フレームワークを使うのがよりよい選択肢です。 | |
204 | 200 | |
205 | ||
201 | #!/usr/bin/perl -n00 | |
202 | # qxurl - tchrist@perl.com | |
203 | print "$2\n" while m{ | |
204 | < \s* | |
205 | A \s+ HREF \s* = \s* (["']) (.*?) \1 | |
206 | \s* > | |
207 | }gsix; | |
206 | 208 | |
207 | (ある文字列から HTML を取り除くには?) | |
208 | ||
209 | 209 | =begin original |
210 | 210 | |
211 | ||
211 | This version does not adjust relative URLs, understand alternate | |
212 | b | |
212 | bases, deal with HTML comments, deal with HREF and NAME attributes | |
213 | ||
213 | in the same tag, understand extra qualifiers like TARGET, or accept | |
214 | URLs themselves as arguments. It also runs about 100x faster than a | |
215 | more "complete" solution using the LWP suite of modules, such as the | |
216 | http://www.perl.com/CPAN/authors/Tom_Christiansen/scripts/xurl.gz program. | |
214 | 217 | |
215 | 218 | =end original |
216 | 219 | |
217 | L | |
220 | これは相対URLを扱うことができず、alternate baseを理解できず、HTML のコメント、 | |
218 | ||
221 | 同じタグにあるHREFやNAMEといったアトリビュートを扱うことができませんし、 | |
222 | TARGET のような追加の修飾子を理解せず、 | |
223 | URL自身を引数として受け取ることができません。 | |
224 | これは | |
225 | http://www.perl.com/CPAN/authors/Tom_Christiansen/scripts/xurl.gz | |
226 | のような、LWPスイートにあるモジュールを使っている、より“完全な” | |
227 | 解決策よりも百倍の早さで実行されます。 | |
219 | 228 | |
220 | =head2 How do I e | |
229 | =head2 How do I download a file from the user's machine? How do I open a file on another machine? | |
221 | 230 | |
222 | ( | |
231 | (ユーザーのマシンからファイルをダウンロードするには? | |
232 | 別のマシンにあるファイルをオープンするには?) | |
223 | 233 | |
224 | ||
234 | HTMLフォームのコンテキストにあるのならば、B<multipart/form-data> | |
235 | エンコーディングとして知られているものを使うことができるでしょう。 | |
236 | CGI.pmモジュール(CPANで入手可能です)はこれを start_multipart_form() | |
237 | というstarform()メソッドとは異なるメソッドでサポートしています。 | |
225 | 238 | |
226 | ||
239 | =head2 How do I make a pop-up menu in HTML? | |
227 | images, objects, frames, and many other tags that can contain a URL. | |
228 | If you need anything more complex, you can create your own subclass of | |
229 | L<HTML::LinkExtor> or L<HTML::Parser>. You might even use | |
230 | L<HTML::SimpleLinkExtor> as an example for something specifically | |
231 | suited to your needs. | |
232 | 240 | |
233 | ||
241 | (HTMLでポップアップメニューを作るには?) | |
234 | 242 | |
235 | L<HTML::SimpleLinkExtor> は HTML から URL を抽出します; これはアンカー、 | |
236 | イメージ、オブジェクト、フレームおよび URL を含んでいるその他の多くのタグを | |
237 | 扱います。 | |
238 | より複雑なものが必要なら、独自の L<HTML::LinkExtor> や L<HTML::Parser> の | |
239 | サブクラスを作ることができます。 | |
240 | 何か特別な用途に合わせるための例として L<HTML::SimpleLinkExtor> を使うことも | |
241 | できます。 | |
242 | ||
243 | 243 | =begin original |
244 | 244 | |
245 | ||
245 | Use the B<< <SELECT> >> and B<< <OPTION> >> tags. The CGI.pm | |
246 | module (available from CPAN) supports this widget, as well as many | |
247 | others, including some that it cleverly synthesizes on its own. | |
246 | 248 | |
247 | 249 | =end original |
248 | 250 | |
249 | ||
251 | B<< <SELECT> >> タグと B<< <OPTION> >> タグを使います。 | |
252 | CGI.pmモジュール(CPANから入手可能です)はこのウィジェットを他の多くのものと同様、 | |
253 | 独自のメニューを器用に統合しているものを含めてサポートしています。 | |
250 | 254 | |
255 | ||
251 | 256 | =head2 How do I fetch an HTML file? |
252 | 257 | |
253 | (HTML | |
258 | (HTMLファイルをフェッチするには?) | |
254 | 259 | |
255 | ||
260 | 一つのやり方は、あなたがlynxというテキストベースのHTMLブラウザーを | |
261 | インストールしているとすれば、次のようなものです: | |
256 | 262 | |
257 | ||
263 | $html_code = `lynx -source $url`; | |
264 | $text_data = `lynx -dump $url`; | |
258 | 265 | |
259 | =end original | |
260 | ||
261 | (brian d foy によって寄贈されました) | |
262 | ||
263 | 266 | =begin original |
264 | 267 | |
265 | ||
268 | The libwww-perl (LWP) modules from CPAN provide a more powerful way | |
266 | ||
269 | to do this. They don't require lynx, but like lynx, can still work | |
270 | through proxies: | |
267 | 271 | |
268 | 272 | =end original |
269 | 273 | |
270 | libwww-perl | |
274 | CPANで入手できる libwww-perl (LWP)モジュールはこれを行う、 | |
271 | ||
275 | よりパワフルな方法を提供します。 | |
272 | ||
276 | これは lynx を必要とせず、プロキシ越しでも使えます: | |
273 | 277 | |
274 | | |
278 | # 最も単純なバージョン | |
279 | use LWP::Simple; | |
280 | $content = get($URL); | |
275 | 281 | |
276 | | |
282 | # もしくはURLからHTMLを出力する | |
283 | use LWP::Simple; | |
284 | getprint "http://www.linpro.no/lwp/"; | |
277 | 285 | |
278 | ||
286 | # もしくはあるURLのHTMLからASCIIを印字する | |
287 | # HTML-Treeパッケージも必要です。 | |
288 | use LWP::Simple; | |
289 | use HTML::Parser; | |
290 | use HTML::FormatText; | |
291 | my ($html, $ascii); | |
292 | $html = get("http://www.perl.com/"); | |
293 | defined $html | |
294 | or die "Can't fetch HTML from http://www.perl.com/"; | |
295 | $ascii = HTML::FormatText->new->format(parse_html($html)); | |
296 | print $ascii; | |
279 | 297 | |
280 | It can also store the resource directly in a file: | |
281 | ||
282 | =end original | |
283 | ||
284 | リソースを直接ファイルに保存することもできます: | |
285 | ||
286 | use LWP::Simple qw(getstore); | |
287 | ||
288 | getstore( "http://www.example.com/index.html", "foo.html" ); | |
289 | ||
290 | =begin original | |
291 | ||
292 | If you need to do something more complicated, you can use | |
293 | L<LWP::UserAgent> module to create your own user-agent (e.g. browser) | |
294 | to get the job done. If you want to simulate an interactive web | |
295 | browser, you can use the L<WWW::Mechanize> module. | |
296 | ||
297 | =end original | |
298 | ||
299 | もっと複雑なことをする必要がある場合は、仕事を行うための独自の | |
300 | ユーザーエージェント(例えばブラウザ)を作るために | |
301 | L<LWP::UserAgent> モジュールを使えます。 | |
302 | 対話的なウェブブラウザをシミュレートしたい場合は、 | |
303 | L<WWW::Mechanize> モジュールが使えます。 | |
304 | ||
305 | 298 | =head2 How do I automate an HTML form submission? |
306 | 299 | |
307 | (HTML | |
300 | (HTMLフォームの処理を自動化するには?) | |
308 | 301 | |
309 | ||
302 | GETメソッドを使って値を処理しているのであれば、URLを作って、 | |
303 | さらにC<query_form>メソッドを使ってフォームをエンコードします: | |
310 | 304 | |
311 | If you are doing something complex, such as moving through many pages | |
312 | and forms or a web site, you can use L<WWW::Mechanize>. See its | |
313 | documentation for all the details. | |
314 | ||
315 | =end original | |
316 | ||
317 | もし、複数のページとフォームや web サイトを移動するような、複雑なことを | |
318 | しようとしているなら、L<WWW::Mechanize> が使えます。 | |
319 | 全ての詳細についてはこれのドキュメントを参照してください。 | |
320 | ||
321 | =begin original | |
322 | ||
323 | If you're submitting values using the GET method, create a URL and encode | |
324 | the form using the C<query_form> method: | |
325 | ||
326 | =end original | |
327 | ||
328 | GET メソッドを使って値を処理しているのであれば、URL を作って、 | |
329 | さらに C<query_form> メソッドを使ってフォームをエンコードします: | |
330 | ||
331 | 305 | use LWP::Simple; |
332 | 306 | use URI::URL; |
333 | 307 | |
334 | my $url = url(' | |
308 | my $url = url('http://www.perl.com/cgi-bin/cpan_mod'); | |
335 | 309 | $url->query_form(module => 'DB_File', readme => 1); |
336 | 310 | $content = get($url); |
337 | 311 | |
338 | ||
312 | POSTメソッドを使っているのであれば、自分用のエージェントを作成して | |
339 | ||
340 | If you're using the POST method, create your own user agent and encode | |
341 | the content appropriately. | |
342 | ||
343 | =end original | |
344 | ||
345 | POST メソッドを使っているのであれば、自分用のエージェントを作成して | |
346 | 313 | コンテンツを適切にエンコードしてやります。 |
347 | 314 | |
348 | 315 | use HTTP::Request::Common qw(POST); |
349 | 316 | use LWP::UserAgent; |
350 | 317 | |
351 | | |
318 | $ua = LWP::UserAgent->new(); | |
352 | my $req = POST ' | |
319 | my $req = POST 'http://www.perl.com/cgi-bin/cpan_mod', | |
353 | 320 | [ module => 'DB_File', readme => 1 ]; |
354 | | |
321 | $content = $ua->request($req)->as_string; | |
355 | 322 | |
356 | =head2 | |
323 | =head2 how do I decode or create those %-encodings on the web? | |
357 | X<URI> X<URI::Escape> X<RFC 2396> | |
358 | 324 | |
359 | 325 | (web上で %-encodings をデコードしたり生成したりするには?) |
360 | 326 | |
361 | 327 | =begin original |
362 | 328 | |
363 | ||
329 | If you are writing a CGI script, you should be using the CGI.pm module | |
364 | ||
330 | that comes with perl, or some other equivalent module. The CGI module | |
365 | t | |
331 | automatically decodes queries for you, and provides an escape() | |
332 | function to handle encoding. | |
366 | 333 | |
367 | 334 | =end original |
368 | 335 | |
369 | ||
336 | CGI スクリプトを書いているのなら、perl に付属している CGI.pm モジュール | |
370 | ||
337 | または等価なモジュールを使うべきです。 | |
371 | ||
338 | CGI モジュールは自動的にクエリをデコードし、escape() 関数で | |
339 | エンコードもできます。 | |
372 | 340 | |
373 | 341 | =begin original |
374 | 342 | |
375 | T | |
343 | The best source of detailed information on URI encoding is RFC 2396. | |
376 | ||
344 | Basically, the following substitutions do it: | |
377 | 345 | |
378 | 346 | =end original |
379 | 347 | |
380 | ||
348 | URI エンコーディングの詳細情報に関する最良のソースは RFC 2396 です。 | |
381 | ||
349 | 基本的には、以下の置換が実行されます: | |
382 | 350 | |
383 | | |
351 | s/([^\w()'*~!.-])/sprintf '%%%02x', $1/eg; # encode | |
384 | 352 | |
385 | | |
353 | s/%([A-Fa-f\d]{2})/chr hex $1/eg; # decode | |
386 | 354 | |
387 | print "$escaped\n"; # 'Colon%20%3A%20Hash%20%23%20Percent%20%25' | |
388 | ||
389 | 355 | =begin original |
390 | 356 | |
391 | ||
357 | However, you should only apply them to individual URI components, not | |
358 | the entire URI, otherwise you'll lose information and generally mess | |
359 | things up. If that didn't explain it, don't worry. Just go read | |
360 | section 2 of the RFC, it's probably the best explanation there is. | |
392 | 361 | |
393 | 362 | =end original |
394 | 363 | |
395 | ||
364 | しかし、これは URI 全体ではなく、個々の URI コンポーネントに | |
365 | 対して適用するべきです。さもなければ、情報が失われ、 | |
366 | ぐちゃぐちゃになります。 | |
367 | これが説明になっていなくても、心配はいりません。 | |
368 | RFC の第 2 章を読んでください。 | |
369 | おそらくこの問題に関する最良の説明です。 | |
396 | 370 | |
397 | my $unescaped = uri_unescape( $escaped ); | |
398 | ||
399 | print $unescaped; # back to original | |
400 | ||
401 | 371 | =begin original |
402 | 372 | |
403 | R | |
373 | RFC 2396 also contains a lot of other useful information, including a | |
404 | ||
374 | regexp for breaking any arbitrary URI into components (Appendix B). | |
405 | 375 | |
406 | 376 | =end original |
407 | 377 | |
408 | ||
378 | RFC 2396 にはその他の有用な情報が多く含まれています。その中には | |
409 | ||
379 | 任意の URI をコンポーネントに分割するための正規表現(Appendix B)を含みます。 | |
410 | 380 | |
411 | 381 | =head2 How do I redirect to another page? |
412 | 382 | |
413 | 383 | (別のページにリダイレクトするには?) |
414 | 384 | |
415 | 385 | =begin original |
416 | 386 | |
417 | ||
387 | According to RFC 2616, "Hypertext Transfer Protocol -- HTTP/1.1", the | |
418 | ||
388 | preferred method is to send a C<Location:> header instead of a | |
389 | C<Content-Type:> header: | |
419 | 390 | |
420 | 391 | =end original |
421 | 392 | |
422 | ||
393 | RFC 2616 "Hypertext Transfer Protocol -- HTTP/1.1" によれば、 | |
423 | ||
394 | 好ましい手法は C<Content-Type:> ヘッダの代わりに C<Location:> ヘッダを | |
395 | 送ることです: | |
424 | 396 | |
425 | | |
397 | Location: http://www.domain.com/newpage | |
426 | $c->detach(); | |
427 | 398 | |
428 | 399 | =begin original |
429 | 400 | |
430 | ||
401 | Note that relative URLs in these headers can cause strange effects | |
431 | ||
402 | because of "optimizations" that servers do. | |
432 | are migrating from Apache or have URL's you want to always | |
433 | redirect. | |
434 | 403 | |
435 | 404 | =end original |
436 | 405 | |
437 | ||
406 | これらのヘッダーに相対 URL を指定すると、サーバーが行う“最適化”のために | |
438 | ||
407 | 奇妙な効果を引き起こす可能性があることに注意してください。 | |
439 | ||
408 | ||
409 | $url = "http://www.perl.com/CPAN/"; | |
410 | print "Location: $url\n\n"; | |
411 | exit; | |
440 | 412 | |
441 | =head2 How do I put a password on my web pages? | |
442 | ||
443 | (私の web ぺージでパスワードを入力するには?) | |
444 | ||
445 | 413 | =begin original |
446 | 414 | |
447 | ||
415 | To target a particular frame in a frameset, include the "Window-target:" | |
448 | ||
416 | in the header. | |
449 | 417 | |
450 | 418 | =end original |
451 | 419 | |
452 | ||
420 | フレームセットの特定のフレームを指定するには、 | |
453 | ||
421 | ヘッダーに "Window-target:" を含めます。 | |
454 | 422 | |
423 | print <<EOF; | |
424 | Location: http://www.domain.com/newpage | |
425 | Window-target: <FrameName> | |
426 | ||
427 | EOF | |
428 | ||
455 | 429 | =begin original |
456 | 430 | |
457 | ||
431 | To be correct to the spec, each of those virtual newlines should | |
458 | ||
432 | really be physical C<"\015\012"> sequences by the time your message is | |
459 | ||
433 | received by the client browser. Except for NPH scripts, though, that | |
434 | local newline should get translated by your server into standard form, | |
435 | so you shouldn't have a problem here, even if you are stuck on MacOS. | |
436 | Everybody else probably won't even notice. | |
460 | 437 | |
461 | 438 | =end original |
462 | 439 | |
463 | ||
440 | 仕様を正確に達成するには、クライアントブラウザからメッセージを受け取った時点で | |
464 | ||
441 | 全ての仮想的な改行についてC<"\015\012">に置き換えるべきです。 | |
465 | ||
442 | しかし、NPH スクリプトを除いて、このようなローカルな改行は | |
443 | サーバーによって標準的な形式に変換されることが多いので、 | |
444 | MacOS ではまっていた場合でも問題にはならないことが多いでしょう。 | |
445 | それ以外ではおそらく誰も気付きすらしないでしょう。 | |
466 | 446 | |
467 | =head2 How do I | |
447 | =head2 How do I put a password on my web pages? | |
468 | 448 | |
469 | (私の | |
449 | (私のwebぺージでパスワードを入力するには?) | |
470 | 450 | |
471 | ||
451 | 状況によります。あなたの使っているwebサーバーのドキュメントを読む | |
452 | 必要があるでしょうし、先に例示した他のFAQの幾つかをチェックする | |
453 | 必要もあるでしょう。 | |
472 | 454 | |
473 | ||
455 | =head2 How do I edit my .htpasswd and .htgroup files with Perl? | |
474 | 456 | |
475 | ||
457 | (Perlを使って.htpasswdや.htgroupといったファイルを編集するには?) | |
476 | 458 | |
477 | ||
459 | HTTPD::UserAdmin モジュールと HTTPD::GroupAdmin モジュールは、 | |
460 | ファイルがどのように格納されているかに関係なくこれらのファイルに対する | |
461 | 首尾一貫したオブジェクト指向インターフェースを提供します。 | |
462 | HTTPD::UserAdmin は`Basic' および `Digest' 認証スキームで | |
463 | 使われるファイルをサポートします。以下に例を挙げます: | |
478 | 464 | |
479 | ||
465 | use HTTPD::UserAdmin (); | |
466 | HTTPD::UserAdmin | |
467 | ->new(DB => "/foo/.htpasswd") | |
468 | ->add($username => $password); | |
480 | 469 | |
481 | ||
470 | =head2 How do I make sure users can't enter values into a form that cause my CGI script to do bad things? | |
482 | you add some client-side checks, people may disable them or bypass | |
483 | them completely. For instance, someone might use a module such as | |
484 | L<LWP> to submit to your web site. If you want to prevent data that | |
485 | try to use SQL injection or other sorts of attacks (and you should | |
486 | want to), you have to not trust any data that enter your program. | |
487 | 471 | |
488 | ||
472 | (私のCGIスクリプトに悪影響をもたらすようなものを、ユーザーが | |
473 | フォームに入力できないようにするにはどうするのでしょうか?) | |
489 | 474 | |
490 | ||
475 | http://www-genome.wi.mit.edu/WWW/faqs/www-security-faq.html, | |
491 | ||
476 | にある CGI セキュリティ FAQを読んでください。そして | |
492 | ||
477 | http://www.perl.com/CPAN/doc/FAQs/cgi/perl-cgi-faq.html. | |
493 | ||
478 | にあるPerl/CGI FAQを読んでください。 | |
494 | 投稿するかもしれません。 | |
495 | SQL インジェクションや同様の種類の攻撃を行うようなデータを防ぎたいのなら | |
496 | (そしておそらくそうしたいでしょう)、プログラムに入力される | |
497 | どんなデータも信用しないようにする必要があります。 | |
498 | 479 | |
499 | ||
480 | まとめ: スクリプトの外側(たとえばCGIパラメーター)からやってくる | |
481 | データを使ってC<eval>やC<system>を使わないようにさせる汚染検査 | |
482 | (tainting、L<perlsec>を参照してください)を使います。汚染検査に加え、 | |
483 | 一引数形式の system() や exec() を決して使わないようにします。 | |
484 | その代わりに、シェルのグロビングが行われないように、 | |
485 | リストとしてコマンドと引数とを与えるようにします。 | |
500 | 486 | |
501 | ||
487 | =head2 How do I parse an mail header? | |
502 | If you are using the L<DBI> module, use placeholder to fill in data. | |
503 | If you are running external programs with C<system> or C<exec>, use | |
504 | the list forms. There are many other precautions that you should take, | |
505 | too many to list here, and most of them fall under the category of not | |
506 | using any data that you don't intend to use. Trust no one. | |
507 | 488 | |
508 | =end original | |
509 | ||
510 | L<perlsec> 文書にはデータセキュリティに関する一般的な助言があります。 | |
511 | L<DBI> モジュールを使っているなら、データを埋めるのにプレースホルダを | |
512 | 使ってください。 | |
513 | C<system> や C<exec> で外部プログラムを実行しているなら、リスト形式を | |
514 | 使ってください。 | |
515 | その他、ここに書ききれないほどの注意するべき多くの事前注意があり、 | |
516 | そのほとんどは、使おうとしていないデータは使わないという分野に当てはまります。 | |
517 | 誰も信用しないでください。 | |
518 | ||
519 | =head2 How do I parse a mail header? | |
520 | ||
521 | 489 | (メールのヘッダーを解析するには?) |
522 | 490 | |
523 | ||
491 | 拙速な解決策なら、L<perlfunc/split> から派生した | |
492 | 以下のやり方を試してみてください。 | |
524 | 493 | |
525 | ||
494 | $/ = ''; | |
526 | ||
495 | $header = <MSG>; | |
527 | e | |
496 | $header =~ s/\n\s+/ /g; # 継続行を連結する | |
497 | %head = ( UNIX_FROM_LINE, split /^([-\w]+):\s*/m, $header ); | |
528 | 498 | |
529 | ||
499 | このやり方は、たとえば受信した行すべてを保守しようとするときには | |
500 | うまくありません。より完璧なアプローチはCPANにあるMail::Header | |
501 | モジュールを使うというものです(このモジュールはMailToolsパッケージの | |
502 | 一部です)。 | |
530 | 503 | |
531 | ||
504 | =head2 How do I decode a CGI form? | |
532 | これはよくテストされていて、現実世界で見ることになるあらゆる狂気 | |
533 | (コメント畳み込み空白、エンコーディング、コメントなど)に対応しています。 | |
534 | 505 | |
535 | ||
506 | (CGIフォームをデコードするには?) | |
536 | 507 | |
537 | my $message = Email::MIME->new($rfc2822); | |
538 | my $subject = $message->header('Subject'); | |
539 | my $from = $message->header('From'); | |
540 | ||
541 | 508 | =begin original |
542 | 509 | |
543 | ||
510 | You use a standard module, probably CGI.pm. Under no circumstances | |
544 | ||
511 | should you attempt to do so by hand! | |
545 | L<Email::MIME> object: | |
546 | 512 | |
547 | 513 | =end original |
548 | 514 | |
549 | ||
515 | 標準のモジュール、多分CGI.pmを使うとよいでしょう。 | |
550 | ||
516 | あなたが手作業で行うことは多分ありません! | |
551 | 得るために cast メソッドを使うことを考慮してください: | |
552 | 517 | |
553 | my $mail_message_object = read_message(); | |
554 | my $abstract = Email::Abstract->new($mail_message_object); | |
555 | my $email_mime_object = $abstract->cast('Email::MIME'); | |
556 | ||
557 | =head2 How do I check a valid mail address? | |
558 | ||
559 | (メールアドレスが正しいかチェックするには?) | |
560 | ||
561 | 518 | =begin original |
562 | 519 | |
563 | ||
520 | You'll see a lot of CGI programs that blindly read from STDIN the number | |
521 | of bytes equal to CONTENT_LENGTH for POSTs, or grab QUERY_STRING for | |
522 | decoding GETs. These programs are very poorly written. They only work | |
523 | sometimes. They typically forget to check the return value of the read() | |
524 | system call, which is a cardinal sin. They don't handle HEAD requests. | |
525 | They don't handle multipart forms used for file uploads. They don't deal | |
526 | with GET/POST combinations where query fields are in more than one place. | |
527 | They don't deal with keywords in the query string. | |
564 | 528 | |
565 | 529 | =end original |
566 | 530 | |
567 | ||
531 | あなたはPOSTに対するCONTENT_LENGTHと同じだけのバイトを標準入力から | |
532 | 闇雲に読み込んだり、GETをデコードするためにQUERY_STRINGを取り込んでいるような | |
533 | CGIプログラムを見ることになるでしょう。 | |
534 | これらのプログラムは非常にまずい書き方をされていて、 | |
535 | 部分的にしか動作しません。これらのプログラムで典型的なものは、 | |
536 | read()システムコールの戻り値をチェックするのを忘れていたりします。 | |
537 | それは大きな罪なのです。また、HEADリクエストを | |
538 | 取り扱っていなかったりする場合もありますし、 | |
539 | ファイルのアップロードに使われる | |
540 | マルチパート形式を扱ってないこともあります。 | |
541 | あるいは二ヶ所以上で | |
542 | 問い合わせフィールドがある場合のGET/POSTの組み合わせに対応していません。 | |
543 | さらに問い合わせ文字列中にあるキーワードの扱いもしていません。 | |
568 | 544 | |
569 | 545 | =begin original |
570 | 546 | |
571 | ||
547 | In short, they're bad hacks. Resist them at all costs. Please do not be | |
548 | tempted to reinvent the wheel. Instead, use the CGI.pm or CGI_Lite.pm | |
549 | (available from CPAN), or if you're trapped in the module-free land | |
550 | of perl1 .. perl4, you might look into cgi-lib.pl (available from | |
551 | http://www.bio.cam.ac.uk/web/form.html). | |
572 | 552 | |
573 | 553 | =end original |
574 | 554 | |
575 | ||
555 | 端的にいって、それらは良くない hack です。行わないようにしましょう。 | |
576 | こ | |
556 | どうか車輪を再発明するようなことはしないでください。 | |
557 | 代わりに、CGI.pm か CGI_Lite.pm (CPAN で入手できます)を使ってください。 | |
558 | あなたが(モジュールを使うことのできない)perl1 〜 perl4 を使っているのなら、 | |
559 | cgi-lib.pl を調べてみてください | |
560 | (http://cgi-lib.stanford.edu/cgi-lib/で入手可能です) | |
577 | 561 | |
578 | 562 | =begin original |
579 | 563 | |
580 | a | |
564 | Make sure you know whether to use a GET or a POST in your form. | |
565 | GETs should only be used for something that doesn't update the server. | |
566 | Otherwise you can get mangled databases and repeated feedback mail | |
567 | messages. The fancy word for this is ``idempotency''. This simply | |
568 | means that there should be no difference between making a GET request | |
569 | for a particular URL once or multiple times. This is because the | |
570 | HTTP protocol definition says that a GET request may be cached by the | |
571 | browser, or server, or an intervening proxy. POST requests cannot be | |
572 | cached, because each request is independent and matters. Typically, | |
573 | POST requests change or depend on state on the server (query or update | |
574 | a database, send mail, or purchase a computer). | |
581 | 575 | |
582 | 576 | =end original |
583 | 577 | |
584 | ||
578 | あなたのフォームで GET や POST をどのように使うかを良く知っておいてください。 | |
579 | GET はサーバーを更新しないようなものに対してのみ使うべきです。 | |
580 | あるいは、データベースをいじったりメールメッセージによるフィードバックを | |
581 | 繰り返し行うようなこともできます。このことを表わすのにぴったりな | |
582 | 単語は“idempotency”です。その意味は、GET リクエストを作り出したものと | |
583 | 特定の URL とに違いがないということです。 | |
584 | これはつまり、HTTP プロトコルは GET リクエストがブラウザーや | |
585 | サーバー、あるいは途中にあるプロクシーによってキャッシングしている | |
586 | 可能性があることを定義しています。POST リクエストは | |
587 | キャッシュすることはできません。なぜなら呼び出しのそれぞれは | |
588 | 独立したものであるからなのです。 | |
589 | 典型的な例では、POST リクエストはサーバーの状態を変更したり、 | |
590 | 状態に依存しています(データベースに対する問い合わせや更新、メールの送信、 | |
591 | コンピューターの購入など)。 | |
585 | 592 | |
593 | =head2 How do I check a valid mail address? | |
594 | ||
595 | (メールアドレスが正しいかチェックするには?) | |
596 | ||
586 | 597 | =begin original |
587 | 598 | |
588 | ||
599 | You can't, at least, not in real time. Bummer, eh? | |
589 | 600 | |
590 | 601 | =end original |
591 | 602 | |
592 | ||
603 | できません。少なくともリアルタイムでは。がっかりしたでしょう? | |
593 | 604 | |
594 | 605 | =begin original |
595 | 606 | |
596 | Without sending mail to the address and seeing whether t | |
607 | Without sending mail to the address and seeing whether it bounces (and | |
597 | ||
608 | even then you face the halting problem), you cannot determine whether | |
598 | ||
609 | an email address is valid. Even if you apply the email header | |
599 | ||
610 | standard, you can have problems, because there are deliverable | |
611 | addresses that aren't RFC-822 (the mail header standard) compliant, | |
612 | and addresses that aren't deliverable which are compliant. | |
600 | 613 | |
601 | 614 | =end original |
602 | 615 | |
603 | 616 | そのアドレスにメールを送ってそれが届いたかどうかを確認しなければ |
604 | ||
617 | (そしてあなたがそのhalting problemに直面したあとでさえも)、 | |
605 | ||
618 | そのアドレスが正しいものかどうかを決めることはできません。 | |
606 | ||
619 | あなたが電子メールヘッダーの標準を適用したとしても、 | |
620 | 問題が起こる可能性があります。 | |
621 | なぜなら、配達可能なアドレスには RFC-822(電子メールヘッダーの標準)に | |
622 | 適合していないものがあり、適合していても配達できないアドレスも | |
623 | あるからです。 | |
607 | 624 | |
608 | 625 | =begin original |
609 | 626 | |
610 | ||
627 | Many are tempted to try to eliminate many frequently-invalid | |
611 | ||
628 | mail addresses with a simple regex, such as | |
612 | ||
629 | C</^[\w.-]+\@(?:[\w-]+\.)+\w+$/>. It's a very bad idea. However, | |
613 | ||
630 | this also throws out many valid ones, and says nothing about | |
614 | ||
631 | potential deliverability, so it is not suggested. Instead, see | |
615 | a | |
632 | http://www.perl.com/CPAN/authors/Tom_Christiansen/scripts/ckaddr.gz, | |
633 | which actually checks against the full RFC spec (except for nested | |
634 | comments), looks for addresses you may not wish to accept mail to | |
635 | (say, Bill Clinton or your postmaster), and then makes sure that the | |
636 | hostname given can be looked up in the DNS MX records. It's not fast, | |
637 | but it works for what it tries to do. | |
616 | 638 | |
617 | 639 | =end original |
618 | 640 | |
619 | ||
641 | 多くの人が 不正メールアドレスを C</^[\w.-]+\@(?:[\w-]+\.)+\w+$/> | |
620 | ||
642 | のように単純な正規表現で削除しようとしています。しかしながら、 | |
621 | ||
643 | これは正当なアドレスも一緒に取り除いてしまい、 | |
622 | ||
644 | 潜在的な到達性についてなにも言いません。ですからこれはお薦めしません。 | |
623 | ||
645 | 代わりに、完全な RFC 仕様に突き合わせてチェックを行い | |
624 | ||
646 | (ネストしたコメントを除く)、 | |
625 | ||
647 | あなたが受け取りたくないメールアドレスを探し出し、 | |
626 | ||
648 | さらにホスト名が DNS で引けるかどうかを確かめる | |
649 | http://www.perl.com/CPAN/authors/Tom_Christiansen/scripts/ckaddr.gz | |
650 | を参照してください。これは速くはありませんが、しかしきちんと動きます。 | |
627 | 651 | |
628 | 652 | =begin original |
629 | 653 | |
630 | ||
654 | Our best advice for verifying a person's mail address is to have them | |
631 | ||
655 | enter their address twice, just as you normally do to change a password. | |
632 | ||
656 | This usually weeds out typos. If both versions match, send | |
633 | ||
657 | mail to that address with a personal message that looks somewhat like: | |
634 | valid. If they don't click on the link, either they forged the address or | |
635 | (assuming they got the message) following through wasn't important so you | |
636 | don't need to worry about it. | |
637 | 658 | |
638 | 659 | =end original |
639 | 660 | |
640 | ||
661 | 私たちができる最善のアドバイスは、個人のメールアドレスをチェックするのに | |
641 | ||
662 | パスワードを変更するときと同じように | |
642 | ||
663 | ユーザーにアドレスを二回入力させるというものです。 | |
643 | ||
664 | これによって通常は打ち間違いを防ぐことができます。 | |
644 | ||
665 | 二回の入力がマッチしたなら、以下のような内容のメッセージをメールとして | |
645 | ||
666 | そのアドレスへ送ります: | |
646 | メールがバウンスしたなら、不正であることが分かります。 | |
647 | リンクがクリックされなければ、アドレスが偽造されたか、(メッセージが | |
648 | 届いたとして) 最後まで行うことが重要ではなかったのでそれについて | |
649 | 気にする必要はないと言うことです。 | |
650 | 667 | |
651 | ||
668 | Dear someuser@host.com, | |
652 | 669 | |
653 | (MIME/BASE64 文字列のデコードを行うには?) | |
654 | ||
655 | 670 | =begin original |
656 | 671 | |
657 | ||
672 | Please confirm the mail address you gave us Wed May 6 09:38:41 | |
658 | De | |
673 | MDT 1998 by replying to this message. Include the string | |
674 | "Rumpelstiltskin" in that reply, but spelled in reverse; that is, | |
675 | start with "Nik...". Once this is done, your confirmed address will | |
676 | be entered into our records. | |
659 | 677 | |
660 | 678 | =end original |
661 | 679 | |
662 | ||
680 | [あなたがMDTの1998年5月6日(水)9時38分41秒に私たちに送ったメ | |
663 | ||
681 | イルアドレスの確認をして、このメッセージにリプライしてくださ | |
664 | ||
682 | い。リプライには"Rumpelstiltskin" という文字列を、"Nik..."の | |
683 | ように逆順にして含めてください。それが済めば、あなたのアドレ | |
684 | スの確認は終了し、私たちのレコードに入力されます。] | |
665 | 685 | |
666 | ||
686 | あなたがメッセージの返事を受け取り、そのメールの発信者があなたの | |
667 | ||
687 | 指示に従っていれば、そのアドレスが本物であると信じることができるでしょう。 | |
668 | 688 | |
669 | 689 | =begin original |
670 | 690 | |
671 | ||
691 | A related strategy that's less open to forgery is to give them a PIN | |
672 | ||
692 | (personal ID number). Record the address and PIN (best that it be a | |
693 | random one) for later processing. In the mail you send, ask them to | |
694 | include the PIN in their reply. But if it bounces, or the message is | |
695 | included via a ``vacation'' script, it'll be there anyway. So it's | |
696 | best to ask them to mail back a slight alteration of the PIN, such as | |
697 | with the characters reversed, one added or subtracted to each digit, etc. | |
673 | 698 | |
674 | 699 | =end original |
675 | 700 | |
676 | ||
701 | より偽造のやりにくい別のやり方に、チェックに対象者に対してPIN | |
677 | ||
702 | (Personal ID Number)を与えるというものがあります。 | |
703 | 後の処理のためにアドレスとPIN(ランダムであることが望ましい)を記録しておくのです。 | |
704 | あなたがメールを送るときに、宛て先人に対して彼らの出すリプライに | |
705 | PINを含めるように依頼するのです。 | |
706 | しかしそれがそのまま返ってきたり、 | |
707 | あるいは返ってきたメッセージが``vacation'' スクリプトを通じての | |
708 | ものであっても、そのまま PIN が含まれてしまいます。 | |
709 | ですから、最善なやり方はメールを送るときに返事には | |
710 | 文字を逆順にするとか、各桁に対して足し算や引き算を行うなどして | |
711 | PINを変形したものを含めて返すように依頼するという方法です。 | |
678 | 712 | |
679 | =head2 How do I | |
713 | =head2 How do I decode a MIME/BASE64 string? | |
680 | 714 | |
681 | ( | |
715 | (MIME/BASE64 文字列のデコードを行うには?) | |
682 | 716 | |
683 | 717 | =begin original |
684 | 718 | |
685 | ||
719 | The MIME-Base64 package (available from CPAN) handles this as well as | |
686 | ||
720 | the MIME/QP encoding. Decoding BASE64 becomes as simple as: | |
687 | 721 | |
688 | 722 | =end original |
689 | 723 | |
690 | ||
724 | MIME-Base64 パッケージ(CPANで入手可能です)はこの問題と、 | |
691 | ||
725 | MIME/QP エンコーディングを取り扱います。 | |
692 | ||
726 | BASE64のデコードは以下のように単純です: | |
693 | 727 | |
694 | ||
728 | use MIME::Base64; | |
729 | $decoded = decode_base64($encoded); | |
695 | 730 | |
696 | The exception is for organization-specific email (e.g. foo@yourcompany.com) | |
697 | where policy can be codified in your program. In that case, you could look at | |
698 | $ENV{USER}, $ENV{LOGNAME}, and getpwuid($<) in scalar context, like so: | |
699 | ||
700 | =end original | |
701 | ||
702 | 例外は、組織に固有のメール (例えば foo@yourcompany.com) で、ポリシーを | |
703 | プログラムにコード化できる場合です。 | |
704 | この場合、以下のように | |
705 | $ENV{USER}, $ENV{LOGNAME} およびスカラコンテキストで and getpwuid($<) を | |
706 | 見ることができます: | |
707 | ||
708 | my $user_name = getpwuid($<) | |
709 | ||
710 | 731 | =begin original |
711 | 732 | |
712 | ||
733 | The MIME-Tools package (available from CPAN) supports extraction with | |
713 | ||
734 | decoding of BASE64 encoded attachments and content directly from email | |
735 | messages. | |
714 | 736 | |
715 | 737 | =end original |
716 | 738 | |
717 | ||
739 | MIME-Tools パッケージ (CPAN にあります) は BASE64 エンコードされた | |
718 | ||
740 | 添付ファイルと本文をメールのメッセージから直接抽出できます。 | |
719 | 本当に最良なのはユーザーに聞くことです。 | |
720 | 741 | |
721 | ||
742 | もしデコードしたい文字列が短い(84 文字以下)の場合、 | |
743 | より直接的なやり方は、ちょっとした変換をした後でunpack()関数の | |
744 | “u”フォーマットを使うというものです: | |
722 | 745 | |
723 | ||
746 | tr#A-Za-z0-9+/##cd; # base64でないキャラクターを取り除く | |
747 | tr#A-Za-z0-9+/# -_#; # uuencodeフォーマットに変換 | |
748 | $len = pack("c", 32 + 0.75*length); # 長さを計算する | |
749 | print unpack("u", $len . $_); # uudecodeして出力 | |
724 | 750 | |
725 | = | |
751 | =head2 How do I return the user's email address? | |
726 | 752 | |
727 | ||
753 | (ユーザーのメールアドレスを返すには?) | |
728 | 754 | |
729 | ||
755 | getpwuidをサポートしているシステムであれば、$< という変数と | |
756 | Sys::Hostname モジュール(標準のperl配布キットの一部です)を使って | |
757 | 以下のようなことが試せるでしょう。 | |
730 | 758 | |
731 | ||
759 | use Sys::Hostname; | |
732 | ||
760 | $address = sprintf('%s@%s', getpwuid($<), hostname); | |
733 | 761 | |
734 | # first, create your message | |
735 | my $message = Email::MIME->create( | |
736 | header_str => [ | |
737 | From => 'you@example.com', | |
738 | To => 'friend@example.com', | |
739 | Subject => 'Happy birthday!', | |
740 | ], | |
741 | attributes => { | |
742 | encoding => 'quoted-printable', | |
743 | charset => 'ISO-8859-1', | |
744 | }, | |
745 | body_str => "Happy birthday to you!\n", | |
746 | ); | |
747 | ||
748 | use Email::Sender::Simple qw(sendmail); | |
749 | sendmail($message); | |
750 | ||
751 | 762 | =begin original |
752 | 763 | |
753 | ||
764 | Company policies on email address can mean that this generates addresses | |
754 | ||
765 | that the company's email system will not accept, so you should ask for | |
755 | ser | |
766 | users' email addresses when this matters. Furthermore, not all systems | |
756 | cl | |
767 | on which Perl runs are so forthcoming with this information as is Unix. | |
757 | 768 | |
758 | 769 | =end original |
759 | 770 | |
760 | ||
771 | 会社のメールアドレスに関するポリシーが、これが生成するアドレスは | |
761 | ||
772 | その会社のメールシステムが受け付けないものである可能性があります。 | |
762 | ||
773 | ですから、ユーザーに、そのユーザーのメールアドレスを尋ねるべきでしょう。 | |
763 | ||
774 | それに加え、Perlが動作する全てのシステムで | |
764 | ||
775 | この情報が(UNIXと同じように)得られるわけではありません。 | |
765 | これを書いている時点では、利用可能な転送層は以下のようなものです: | |
766 | 776 | |
767 | ||
777 | CPANにある Mail::Utilモジュール (MailToolsパッケージの一部です)は | |
778 | メールアドレスがそのユーザーのものであるかどうかを確かめようとする | |
779 | mailaddress()という関数を提供しています。 | |
780 | これは上で例示したやり方よりも賢く、 | |
781 | モジュールがインストールされたときの情報を使いますが、 | |
782 | それでも正しくない可能性があります。 | |
783 | くり返しますが、最善の方法はユーザーに尋ねること、というのがほとんどです。 | |
768 | 784 | |
769 | = | |
785 | =head2 How do I send mail? | |
770 | 786 | |
771 | ||
787 | (メールを送るには?) | |
772 | 788 | |
773 | ||
789 | C<sendmail>プログラムを直接使います: | |
774 | program to send mail from the machine where your code runs, you should | |
775 | be able to use this. | |
776 | 790 | |
777 | ||
791 | open(SENDMAIL, "|/usr/lib/sendmail -oi -t -odq") | |
792 | or die "Can't fork for sendmail: $!\n"; | |
793 | print SENDMAIL <<"EOF"; | |
794 | From: メールの発信者 <me\@host> | |
795 | To: 宛て先 <you\@otherhost> | |
796 | Subject: 適切なサブジェクト | |
778 | 797 | |
779 | ||
798 | 空行に続けてメッセージの本体を書きます。好きな行数書けます。 | |
780 | ||
799 | EOF | |
781 | ||
800 | close(SENDMAIL) or warn "sendmail didn't close nicely"; | |
782 | 801 | |
783 | =item L<Email::Sender::Transport::SMTP> | |
784 | ||
785 | 802 | =begin original |
786 | 803 | |
787 | Thi | |
804 | The B<-oi> option prevents sendmail from interpreting a line consisting | |
788 | ||
805 | of a single dot as "end of message". The B<-t> option says to use the | |
806 | headers to decide who to send the message to, and B<-odq> says to put | |
807 | the message into the queue. This last option means your message won't | |
808 | be immediately delivered, so leave it out if you want immediate | |
809 | delivery. | |
789 | 810 | |
790 | 811 | =end original |
791 | 812 | |
792 | ||
813 | B<-oi>オプションはsendmailがドットだけの行を“メッセージの終わり”と | |
793 | オプション | |
814 | みなさないようにするためのオプションです。B<-t>オプションは | |
815 | メッセージを誰に贈るかを決めるかのためにヘッダーを使うことを指示し、 | |
816 | B<-odq>オプションメッセージをキューに入れることを指示します。 | |
817 | 最後のオプションの意味は、あなたのメッセージがすぐには配送されないことを | |
818 | 意味します。ですから、すぐに配送させたいのであれば | |
819 | このオプションを取り除いてください。 | |
794 | 820 | |
795 | =item L<Email::Sender::Transport::SMTP::TLS> | |
796 | ||
797 | 821 | =begin original |
798 | 822 | |
799 | ||
823 | Alternate, less convenient approaches include calling mail (sometimes | |
800 | ||
824 | called mailx) directly or simply opening up port 25 have having an | |
801 | ||
825 | intimate conversation between just you and the remote SMTP daemon, | |
826 | probably sendmail. | |
802 | 827 | |
803 | 828 | =end original |
804 | 829 | |
805 | ||
830 | あるいは、直接 mail (mailx と呼ばれることもあります)を | |
806 | ||
831 | 呼びだしたり、単純に 25 番ポートを使ってリモートの | |
807 | ||
832 | SMTP デーモン(多分 sendmail でしょう) との間で | |
833 | 詳細な通信を行うといったあまり便利でない方法もあります。 | |
808 | 834 | |
809 | ||
835 | あるいはCPANにあるモジュールMail::Mailerが使えるかもしれません: | |
810 | 836 | |
811 | ||
837 | use Mail::Mailer; | |
812 | 838 | |
813 | ||
839 | $mailer = Mail::Mailer->new(); | |
840 | $mailer->open({ From => $from_address, | |
841 | To => $to_address, | |
842 | Subject => $subject, | |
843 | }) | |
844 | or die "Can't open: $!\n"; | |
845 | print $mailer $body; | |
846 | $mailer->close(); | |
814 | 847 | |
815 | ||
848 | Mail::Internet モジュールは Mail::Mailer より UNIX 的ではない | |
849 | Net::SMTP を使っていますが、信頼性も低いです。 | |
850 | 生の SMTP コマンドを無視します。 | |
851 | sendmail のような mail transport agent を使う理由はたくさんあります。 | |
852 | その中にはキューイングも含まれますし、MX レコードやセキュリティと | |
853 | いったものが含まれます。 | |
816 | 854 | |
817 | 使用する転送層を L<Email::Sender::Simple> に教えるのは簡単です: | |
818 | ||
819 | sendmail( | |
820 | $message, | |
821 | { | |
822 | transport => $email_sender_transport_object, | |
823 | } | |
824 | ); | |
825 | ||
826 | 855 | =head2 How do I use MIME to make an attachment to a mail message? |
827 | 856 | |
828 | 857 | (メールメッセージに添付するためにどうやって MIME を使えばいいですか?) |
829 | 858 | |
830 | 859 | =begin original |
831 | 860 | |
832 | ||
861 | This answer is extracted directly from the MIME::Lite documentation. | |
833 | ||
862 | Create a multipart message (i.e., one with attachments). | |
834 | objects. Consult the L<Email::MIME> documentation for more information, | |
835 | including all of the supported methods and examples of their use. | |
836 | 863 | |
837 | 864 | =end original |
838 | 865 | |
839 | ||
866 | この回答は MIME::Lite のドキュメントから直接持ってきたものです。 | |
840 | ||
867 | マルチパートメッセージ(つまり 添付つきのメッセージ) を作ります。 | |
841 | 添付できます。 | |
842 | 対応している全てのメソッドとその使い方の例を含む、さらなる情報については | |
843 | L<Email::MIME> 文書を参照してください。 | |
844 | 868 | |
845 | ||
869 | use MIME::Lite; | |
846 | 870 | |
847 | ||
871 | ### Create a new multipart message: | |
872 | $msg = MIME::Lite->new( | |
873 | From =>'me@myhost.com', | |
874 | To =>'you@yourhost.com', | |
875 | Cc =>'some@other.com, some@more.com', | |
876 | Subject =>'A message with 2 parts...', | |
877 | Type =>'multipart/mixed' | |
878 | ); | |
848 | 879 | |
880 | ### Add parts (each "attach" has same arguments as "new"): | |
881 | $msg->attach(Type =>'TEXT', | |
882 | Data =>"Here's the GIF file you wanted" | |
883 | ); | |
884 | $msg->attach(Type =>'image/gif', | |
885 | Path =>'aaa000123.gif', | |
886 | Filename =>'logo.gif' | |
887 | ); | |
888 | ||
889 | $text = $msg->as_string; | |
890 | ||
849 | 891 | =begin original |
850 | 892 | |
851 | ||
893 | MIME::Lite also includes a method for sending these things. | |
852 | 894 | |
853 | 895 | =end original |
854 | 896 | |
855 | ||
897 | MIME::Lite はまたこれらのものを送るためのメソッドを含みます。 | |
856 | 898 | |
857 | | |
899 | $msg->send; | |
858 | 900 | |
859 | my $folder = Email::Folder->new('/path/to/email/folder'); | |
860 | while(my $message = $folder->next_message) { | |
861 | # next_message returns Email::Simple objects, but we want | |
862 | # Email::MIME objects as they're more robust | |
863 | my $mime = Email::MIME->new($message->as_string); | |
864 | } | |
865 | ||
866 | 901 | =begin original |
867 | 902 | |
868 | Th | |
903 | This defaults to using L<sendmail(1)> but can be customized to use | |
869 | ||
904 | SMTP via L<Net::SMTP>. | |
870 | rather limited and only support B<reading> rather than writing. | |
871 | 905 | |
872 | 906 | =end original |
873 | 907 | |
874 | ||
908 | これはデフォルトでは L<sendmail(1)> を使いますが、 | |
875 | L< | |
909 | L<Net::SMTP> 経由で SMTP を使うようにカスタマイズできます。 | |
876 | これらのモジュールは一般的に B<読み込み> にのみ対応していて書き込みには | |
877 | 対応していないことに注意してください。 | |
878 | 910 | |
879 | =head2 How do I | |
911 | =head2 How do I read mail? | |
880 | X<hostname, domainname, IP address, host, domain, hostfqdn, inet_ntoa, | |
881 | gethostbyname, Socket, Net::Domain, Sys::Hostname> | |
882 | 912 | |
883 | ( | |
913 | (メールを読み出すには?) | |
884 | 914 | |
885 | ||
915 | CPAN にある Mail::Folder モジュール(MailFolder パッケージの一部です)や | |
916 | Mail::Internet モジュール(これも MailTools パッケージの一部です)が | |
917 | 使えますが、モジュールを使うのはやりすぎかもしれません。 | |
918 | 以下にメールをソートする方法を示します。 | |
886 | 919 | |
887 | ||
920 | #!/usr/bin/perl | |
921 | # bysub1 - サブジェクトによる単純なソート | |
922 | my(@msgs, @sub); | |
923 | my $msgno = -1; | |
924 | $/ = ''; # パラグラフ読み出し | |
925 | while (<>) { | |
926 | if (/^From/m) { | |
927 | /^Subject:\s*(?:Re:\s*)*(.*)/mi; | |
928 | $sub[++$msgno] = lc($1) || ''; | |
929 | } | |
930 | $msgs[$msgno] .= $_; | |
931 | } | |
932 | for my $i (sort { $sub[$a] cmp $sub[$b] || $a <=> $b } (0 .. $#msgs)) { | |
933 | print $msgs[$i]; | |
934 | } | |
888 | 935 | |
889 | ||
936 | あるいはもっと簡潔に | |
890 | 937 | |
891 | ||
938 | #!/usr/bin/perl -n00 | |
939 | # bysub2 - awki的な、サブジェクトによるソート | |
940 | BEGIN { $msgno = -1 } | |
941 | $sub[++$msgno] = (/^Subject:\s*(?:Re:\s*)*(.*)/mi)[0] if /^From/m; | |
942 | $msg[$msgno] .= $_; | |
943 | END { print @msg[ sort { $sub[$a] cmp $sub[$b] || $a <=> $b } (0 .. $#msg) ] } | |
892 | 944 | |
893 | = | |
945 | =head2 How do I find out my hostname/domainname/IP address? | |
894 | 946 | |
895 | ||
947 | (私の ホスト名/ドメイン名/IPアドレス を見つけるには?) | |
896 | in Perl 5.7.3, can get you the fully qualified domain name (FQDN), the host | |
897 | name, or the domain name. | |
898 | 948 | |
899 | ||
949 | 多くのプログラムが C<`hostname`> プログラムを呼び出しています。 | |
950 | これは便利なときもありますが、高い移植性があるというわけではありません。 | |
951 | これは、便利さと移植性とのトレードオフの一例です。 | |
900 | 952 | |
901 | ||
953 | Sys::Hostname モジュール(標準Perl配布キットの一部です)は | |
902 | ||
954 | gethostbyename()を使って(DNSが動作していることを仮定しています) | |
955 | IPアドレスを取得できたあとで、ホスト名を返します。 | |
903 | 956 | |
904 | use | |
957 | use Socket; | |
958 | use Sys::Hostname; | |
959 | my $host = hostname(); | |
960 | my $addr = inet_ntoa(scalar gethostbyname($host || 'localhost')); | |
905 | 961 | |
906 | my $host = hostfqdn(); | |
907 | ||
908 | 962 | =begin original |
909 | 963 | |
910 | ||
964 | Probably the simplest way to learn your DNS domain name is to grok | |
911 | ||
965 | it out of /etc/resolv.conf, at least under Unix. Of course, this | |
966 | assumes several things about your resolv.conf configuration, including | |
967 | that it exists. | |
912 | 968 | |
913 | 969 | =end original |
914 | 970 | |
915 | ||
971 | 少なくとも UNIX では、おそらくあなたの使っている DNS | |
916 | ||
972 | ドメイン名を知る最も単純な方法は | |
973 | /etc/resolev.conf を解析することでしょう。 | |
974 | もちろん、これはファイルが存在していることを含め、 | |
975 | resolv.confの設定に関して幾つかの仮定を行っています。 | |
917 | 976 | |
918 | use Sys::Hostname; | |
919 | ||
920 | $host = hostname(); | |
921 | ||
922 | 977 | =begin original |
923 | 978 | |
924 | ||
979 | (We still need a good DNS domain name-learning method for non-Unix | |
925 | ||
980 | systems.) | |
926 | 981 | |
927 | 982 | =end original |
928 | 983 | |
929 | ||
984 | (私たちは未だに非 UNIX システムのための良い DNS ドメイン名所得手法を | |
930 | ||
985 | 必要としています) | |
931 | 986 | |
932 | ||
987 | =head2 How do I fetch a news article or the active newsgroups? | |
933 | 988 | |
934 | ||
989 | (ニュースのアーティクルやアクティブなニュースグループを取得するには?) | |
935 | 990 | |
936 | 991 | =begin original |
937 | 992 | |
938 | ||
993 | Use the Net::NNTP or News::NNTPClient modules, both available from CPAN. | |
939 | ||
994 | This can make tasks like fetching the newsgroup list as simple as | |
940 | form (a.b.c.d) that most people expect, use the C<inet_ntoa> function | |
941 | from the L<Socket> module, which also comes with perl. | |
942 | 995 | |
943 | 996 | =end original |
944 | 997 | |
945 | ||
998 | Net::NNTP モジュールか News::NNTPClient モジュールのいずれかを使います。 | |
946 | ||
999 | これらは両方ともCPANから入手可能です。これらは以下のように | |
947 | ||
1000 | 簡単にニュースグループのリストを取得するような作業ができます。 | |
948 | 標準配布されている L<Socket> モジュールの C<inet_ntoa> 関数を使います。 | |
949 | 1001 | |
950 | | |
1002 | perl -MNews::NNTPClient | |
1003 | -e 'print News::NNTPClient->new->list("newsgroups")' | |
951 | 1004 | |
952 | | |
1005 | =head2 How do I fetch/put an FTP file? | |
953 | scalar gethostbyname( $host || 'localhost' ) | |
954 | ); | |
955 | 1006 | |
956 | ||
1007 | (FTPファイルをダウンロード/アップロードするには?) | |
957 | 1008 | |
958 | ((S)FTP ファイルをダウンロード/アップロードするには?) | |
959 | ||
960 | 1009 | =begin original |
961 | 1010 | |
962 | L | |
1011 | LWP::Simple (available from CPAN) can fetch but not put. Net::FTP (also | |
963 | ||
1012 | available from CPAN) is more complex but can put as well as fetch. | |
964 | 1013 | |
965 | 1014 | =end original |
966 | 1015 | |
967 | L | |
1016 | LWP::Simple (CPANで入手可能)はダウンロードができますがアップロードはできません。 | |
968 | ||
1017 | Net::FTP(これもCPANで入手可能)はこれよりも複雑ですが、 | |
1018 | ダウンロードとアップロードの両方ができます。 | |
969 | 1019 | |
970 | 1020 | =head2 How can I do RPC in Perl? |
971 | 1021 | |
972 | (Perl で RPC を行うには?) | |
1022 | (Perl で RPC を行うにはどうすればよいのですか?) | |
973 | 1023 | |
974 | 1024 | =begin original |
975 | 1025 | |
976 | ||
1026 | A DCE::RPC module is being developed (but is not yet available) and | |
1027 | will be released as part of the DCE-Perl package (available from | |
1028 | CPAN). The rpcgen suite, available from CPAN/authors/id/JAKE/, is | |
1029 | an RPC stub generator and includes an RPC::ONC module. | |
977 | 1030 | |
978 | 1031 | =end original |
979 | 1032 | |
980 | ||
1033 | DCE::RPCが開発中です(ただし、まだ入手可能にはなっていません)。 | |
1034 | これは DCE-Perl パッケージ(CPAN から入手可能です)の一部として | |
1035 | リリースされるでしょう。 | |
1036 | rpcgenスイートがCAPN/authors/id/JAKE/から入手可能です。 | |
1037 | これはRPCスタブのジェネレーターであり、RPC::ONCモジュールを含んでいます。 | |
981 | 1038 | |
982 | 1039 | =head1 AUTHOR AND COPYRIGHT |
983 | 1040 | |
984 | Copyright (c) 1997- | |
1041 | Copyright (c) 1997-1999 Tom Christiansen and Nathan Torkington. | |
985 | ||
1042 | All rights reserved. | |
986 | 1043 | |
987 | ||
1044 | When included as part of the Standard Version of Perl, or as part of | |
988 | ||
1045 | its complete documentation whether printed or otherwise, this work | |
1046 | may be distributed only under the terms of Perl's Artistic License. | |
1047 | Any distribution of this file or derivatives thereof I<outside> | |
1048 | of that package require that special arrangements be made with | |
1049 | copyright holder. | |
989 | 1050 | |
990 | 1051 | Irrespective of its distribution, all code examples in this file |
991 | are hereby placed into the public domain. You are permitted and | |
1052 | are hereby placed into the public domain. You are permitted and | |
992 | 1053 | encouraged to use this code in your own programs for fun |
993 | or for profit as you see fit. A simple comment in the code giving | |
1054 | or for profit as you see fit. A simple comment in the code giving | |
994 | 1055 | credit would be courteous but is not required. |
995 | ||
996 | =begin meta | |
997 | ||
998 | Translate: 吉村 寿人 <JAE00534@niftyserve.or.jp> | |
999 | Update: SHIRAKATA Kentaro <argrath@ub32.org> (5.6.1-5.14.1, 5.00150039) | |
1000 | Status: completed | |
1001 | ||
1002 | =end meta |