Share
Embed
モダンmod_perl入門 #yapcasia
2012/09/29 に YAPC::Asia Tokyo 2012 で発表したスライドです。
OGATA Tetsuji
September 29, 2012
Transcript
97 slides
-
Ϟμϯmod_perlೖ (Modern mod_perl Guide) ඌܗ మ࣍ (OGATA Tetsuji) Twitter: @xtetsuji
2012/09/29 YAPC::Asia Tokyo 2012 -
Attention for audience • This slide is mainly written by
Japanese, and few English. In the future, I will write and share this slide of English version, perhaps. • I speach by Japanese language. • If you do not known Japanese language, please fun and feel from some Perl code and few English description on this slide. -
ࣗݾհ • ඌܗ మ࣍ (OGATA Tetsuji) • Twitter: @xtetsuji •
Blog: http://post.tetsuji.jp/ • SlideShare: http://www.slideshare.net/xtetsuji • Α͘ߦ͘: Hokkaido.pm, Hachioji.pm • ಘҙٕ: mod_perl (ଞΑ͘Βͳ͍) -
Agenda • About mod_perl basics • mod_perl handler basics •
Refactoring mod_rewrite • mod_perl meets PHP • Apache worker MPM and Perl ithreads • compare mod_{lang} families • In future my activities -
If time is left • Compare with others • Connection
cycle: Let’s make SMTP Server by mod_perl • Other topics -
ࢀߟจݙ • ༸ॻʹͳΓ·͕͢ɺmod_perlΛษڧ͢Δ্Ͱ ཱͭ3 • Practical mod_perl (Orelly 2003; http://modperlbook.org/)
• The mod_perl Developer’s Cookbook (Sams Publishing 2002; http://www.modperlcookbook.org/) • mod_perl2 User’s Guide (Onyx Neon 2007; http://modperl2book.org/) -
History of mod_perl • mod_perl1 fi rst public release: 1996/3/25
(see: http://perl.apache.org/dist/mod_perl-1.0-current/Changes) • mod_perl2 fi rst public release: 2002/4/6 (see: http://perl.apache.org/dist/mod_perl-2.0-current/Changes) -
• mod_perl1 • mod_perl2 • ͜͏ॻ͘͜ͱͰɺ֦ுࢠ”.pl”ͷPerl CGI ͕ߴԽ͢Δ (Registry→PerlRunͰՄ) Perl
CGI ͷߴԽ AddHandler perl-script .pl PerlHandler Apache::Registry AddHandler perl-script .pl PerlResponseHandler \ ModPerl::RegistryPrefork # ↓Apache Con fi gurations -
About mod_perl basics • Perl CGIͷߴԽʹ͍͍ͭͯͩͿׂѪ ͍͖ͤͯͨ͞·͕͢ɺੈؒʹग़ճͬͯ ͍Δؒҧͬͨࣝͷੋਖ਼͚ͩͲ͔͜ Ͱ͍ͨ͠ •
ޙApacheͷmod_perl֦ுϋϯυϥΛ ಡΈॻָ͖ͯ͠͠Έ·͠ΐ͏ -
mod_perl essence is extension of Apache • mod_perl ͷ Perl
CGIߴԽڥͰɺ HTTPϦΫΤετΛड͚ͯϨεϙϯεΛ ฦ͢͜ͱ͕ग़དྷΔͷपͷ௨Γ • mod_perlHTTPϨεϙϯεΛฦ͚ͩ͢ Ͱͳ͘ɺͦͷલॲཧޙॲཧͰ͖Δ -
Apache internal • ϦΫΤετΛड͚Δ • ϔομΛղੳ͢Δ • ඞཁʹԠͯ͡URLΛมͨ͠ΓɺDocumentRootΛख͕͔Γʹ࣮ύε ΛׂΓग़͢ •
ΞΫηε੍ޚɺೝূɺঝೝ(BASICೝূͳͲ) • MIMEλΠϓΛߟ͑Δ • ϨεϙϯεΛग़͢(੩తϑΝΠϧɺϓϩάϥϜग़ྗ) • ϩά(ΞΫηεϩάɺΤϥʔϩά)Λग़ྗ͢Δ • ࣍ͷϦΫΤετΛͭ -
Apache internal • ֤ॲཧΛߦ͍ͬͯΔ෦ʹϑοΫ͕ ͋ͬͯɺ͜͜ʹ֤छॲཧΛڬΉ͜ͱ͕ Մೳ → Apache Moduleʢmod_xxxxxx) •Apache
Module ͰͰ͖Δ͜ͱΛ Perl ͰͰ͖ΔΑ͏ʹͨ͠ͷ → mod_perl -
Phase of mod_perl1 PerlChildInitHandler PerlPostReadRequestHandler PerlInitHandler PerlTransHandler PerlHeaderParserHandler PerlAccessHandler PerlAuthenHandler
PerlAuthzHandler PerlTypeHandler PerlFixupHandler PerlFixupHandler PerlHandler PerlLogHandler PerlCleanupHandler PerlChildExitHandler ※Ұ෦লུ͕͋Γ·͢ɻৄ͘͠ҎԼΛࢀর http://perl.apache.org/docs/1.0/guide/con fi g.html#toc_Perl_Handlers -
Phase of mod_perl2 PerlChildInitHandler PerlPostReadRequestHandler PerlInitHandler PerlTransHandler PerlMapToStorageHandler PerlHeaderParserHandler PerlAccessHandler
PerlAuthenHandler PerlAuthzHandler PerlTypeHandler PerlFixupHandler PerlFixupHandler PerlResponseHandler PerlLogHandler PerlCleanupHandler PerlChildExitHandler ※͔ͳΓলུ͕͋Γ·͢ɻৄ͘͠ҎԼΛࢀর http://perl.apache.org/docs/2.0/user/con fi g/con fi g.html -
fi rst mod_perl1 handler package MyApache::Hello; use strict; use warnings;
use Apache::Constants qw(OK); sub handler { my $r = shift; # "Apache" object $r->send_http_header('text/plain'); $r->print("Hello! mod_perl1.\n"); return OK; } 1; -
fi rst mod_perl2 handler package MyApache2::Hello; use strict; use warnings;
use Apache2::RequestRec (); # for $r->content_type() use Apache2::RequestIO (); # for $r->print() use Apache2::Const -compile => qw(OK); sub handler { my $r = shift; # "Apache2::RequestRec" object $r->content_type('text/plain'); $r->print("Hello! mod_perl2.\n"); return Apache2::Const::OK; } 1; -
Apache{1,2} con fi gs # Apache1 <Location /> SetHandler perl-script
PerlHandler MyApache1::Hello </Location> # Apache2 <Location /> SetHandler modperl PerlResponseHandler MyApache2::Hello </Location> -
fi rst mod_perl handler • શͯ sub handler ͱ $r
ͷड͚औΓ͔Β • ͜ΕͲͷॲཧϑΣʔζͰಉ༷ • mod_perl2 Ͱ $r ͷϝιου܈͕֤छ ผύοέʔδͰཧ͞Ε͍ͯΔͨΊɺ ϝιουʹΑͬͯࣄલʹదͳ Apache2::* Λ use ͓ͯ͘͠ඞཁ͕͋Δ -
fi rst mod_perl handler • PerlHandler / PerlResponseHandler ʹϋϯ υϥΛηοτ͓͚ͯ͠ɺશͯͷϑΝ
ΠϧͷॲཧΛϑοΫͰ͖ΔͷͰɺ*.html ϑΝΠϧΛ͔͋ͨಁաతʹॲཧ͢ Δ͜ͱ͕Ͱ͖Δ → ֆจࣈॲཧͳͲ -
Apache1 URI Trans package MyApache1::Trans; use strict; use warnings; use
Apache::Constants qw(DECLINED); sub handler { my $r = shift; my $uri = $r->uri(); # e.g. "/path/to/foo.html" ### ... modify $uri ... $r->uri($uri); return DECLINED; # I tell a lie that I do nothing. } 1; -
Apache2 URI Trans package MyApache2::Trans; use strict; use warnings; use
Apache2::RequestRec (); # for $r->uri() use Apache2::Const -compile => qw(DECLINED); sub handler { my $r = shift; my $uri = $r->uri(); # e.g. "/path/to/foo.html" ### ... modify $uri ... $r->uri($uri); # set $uri return Apache2::Const::DECLINED; # I tell a lie that I do nothing. } 1; -
Apache{1,2} con fi gs # Apache1 # e.g. In <VirtualHost>
Directive PerlTransHandler MyApache1::Trans # Apache2 # e.g. In <VirtualHost> Directive PerlTransHandler MyApache2::Trans -
mod_perl URI Trans cons than mod_rewrite • ෳͷϑΝΠϧཧ͕ඞཁͱͳΔ(*.pm) • Perl/mod_perl͕͔Βͳ͍୲ऀͷ
Ҿ͖ܧ͕͗͘͠ͳΔ • Directory Context(<Location>, .htaccess) ʹॻ͔Εͨmod_rewriteઃఆͷ߹ɺ ผ్έΞ͕ඞཁͳέʔε͋Δ -
PHP pathetic story of Perl Monger • ୭͔͕ൃͨ͠ΞϓϦ͕PHPͰɺطʹ উखʹೲͪ͠Ό͍ͬͯΔ •
֖Λ։͚Δͱ࣮ͱ͔͕ςΩτա͗ • ܖॾʑͷཧ༝ͰखΛೖΕΒΕͳ͍ • ӡ༻୲ͰPerl͔͠Βͳ͍Զ͕PHPͷ σόοάͱ͔… -
package MyApache2::Auth; use strict; use warnings; # $r->headers_in() and $r->headers_out()
returns APR::Table use APR::Table (); use Apache2::RequestRec (); use Apache2::Const -compile => qw(OK REDIRECT); sub handler { my $r = shift; my $cookie = $r->headers_in->get('Cookie'); # raw cookie my $x_up_subno = $r->headers_in->get('X-UP-Subno'); my ($is_success, $location, $set_cookie); ### ### ... modify and analyze this session information ... ### if ( $is_success ) { $r->headers_out->set('Set-Cookie' => $set_cookie); return Apache2::Const::OK; } else { $r->headers_out->set(Location => $location); $r->err_headers_out->set(Location => $location); return Apache2::Const::REDIRECT; } } 1; # e.g. In <VirtualHost> Directive PerlAccessHandler MyApache2::Auth -
Request output fi lter and PHP • PHPͷग़ྗΛϑΟϧλ͢Δ • Apache2ͷωΠςΟϒϑΟϧλͳͷͰɺ
PHPͷ ob_*() (output buffering) ͷઃఆ ʹҰӨڹ͞Εͳ͍ • Ԡ༻ྫ: ֆจࣈมɺPHPͰॻ͖͖Εͳ ͍෦ΛޙͰஔ͢Δɺʑ -
Requets output fi lter practice • Filter ͷ߹ sub handler
{ ... } $r (Request Object)Ͱͳ͘ɺ $f (Filter Object) ΛୈҰҾʹड͚औΔ • PerlOutputFilterHandler σΟϨΫςΟϒ • ࠓճվߦΛআڈ͢Δ؆୯ͳαϯϓϧ Λհ -
package MyApache2::FilterObfuscate; use APR::Table (); use Apache2::Filter (); use Apache2::RequestRec
(); use Apache2::Const -compile => qw(OK); my $READ_CHUNK_LENGTH = 2048; sub handler { my $f = shift; # $f is "Apache2::Filter" object ### If filter is chained, this filter is first? unless ($f->ctx) { $f->r->headers_out->unset('Content-Length'); $f->ctx(1); } while ($f->read(my $buffer, $READ_CHUNK_LENGTH)) { $buffer =~ s/[\r\n]//g; $f->print($buffer); } return Apache2::Const::OK; } 1; # e.g. In <VirtualHost> Directive <FilesMatch “.*(html?|php)$”> PerlOutputFilterHandler MyApache2::FilterObfuscate </FilesMatch> -
Apache worker MPM and Perl ithreads • Apache worker MPM:
εϨου(pthread) ͱϓϩηεͷϋΠϒϦοτಈ࡞ • Perl ithreds: ݏΘΕऀ • ಉ࣌ฒߦॲཧ͢ΔͳΒParallel::Prefork AnyEventΛ͏ͷ͕ࠓͷPerlྲّྀ -
Instruction worker MPM & mod_perl • worker MPM্Ͱͷmod_perlͷϊϋ ಛʹগͳ͍ͷͰϋϚΔͱةݥ •
PerlͷεϨουෆ҆ఆͰ͋ΔࣄΛ৺ಘ ্ͨͰɺෳࡶͳࣄ͍͚ͤͯ͞ͳ͍ -
Queue server by mod_perl2 thread • ࠓճεϨουͷมڞ༗Λར༻ͨ͠ mod_perl2ͷจࣈྻQueueαʔόΛ࣮ԋ • “POST
/” ͰRequest BodyΛQueueingɺ • “GET /” ͰQueueΛPickup -
Queue server by mod_perl2 thread (1) package My::Queue; use strict;
use warnings; use threads; use threads::shared; my @queue :shared; sub push_queue { my $value = shift; return 0 if !defined $value; push @queue, $value; return 1; } sub shift_queue { return shift @queue } 1; -
Queue server by mod_perl2 thread (2) package MyApache2::ThreadQueue; use strict;
use warnings; use threads; use Apache2::RequestRec (); use Apache2::RequestIO (); use Apache2::Const -compile => qw(OK HTTP_METHOD_NOT_ALLOWED); use My::Queue; my $READ_CHUNK_LENGH = 2048; ### ### ... following sub handler { ... } ... ### -
Queue server by mod_perl2 thread (3) ### continued sub handler
{ my $r = shift; if ( $r->method eq 'POST' ) { my $value; while ( $r->read(my $buf, $READ_CHUNK_LENGH) ) { $value .= $buf; } My::Queue::push_queue($value); $r->content_type('text/plain'); $r->print('Queued'); return Apache2::Const::OK; } elsif ( $r->method eq 'GET' ) { my $retval = My::Queue::shift_queue(); $r->content_type('text/plain'); $r->print($retval); return Apache2::Const::OK; } else { return Apache2::Const::HTTP_METHOD_NOT_ALLOWED; } } 1; -
worker MPM and perl ithreads practice • ෳࡶͳࣄ͍͚ͤͯ͞ͳ͍ • ෳࡶͳࣄΛͤ͞Δ߹ʹेࢼݧΛ
• େࣄͳ͜ͱͳͷͰ(ry • લग़ͷQueue server100KBఔͷจࣈ ྻͷೖग़ྗΛ10K/minͤͯ͞ແ͠ -
mod_{lang} (1) • mod_perl: ࠷ྺ࢙͕ݹ͍ͷͷҰͭ • mod_ruby: ։ൃఀঢ়ଶˠmod_mruby • mod_python:
։ൃఀঢ়ଶ • mod_lua: Apache2.4͔ΒίΞϞδϡʔϧ • mod_mruby: 20124݄ʹొ͠׆ൃ -
mod_{lang} (2) • mod_wsgi: Python WSGI ࣮ • mod_php: ͍ΘΏΔPHP
• ͪ͜Βࠓ·ͰͷҙຯͰApacheΛ ʮ֦ுʯ͢ΔͷͰͳ͍ • ଞʹͨ͘͞Μmod_{lang}͋ΔΒ͍͠ -
Benchmark • ࣮ࡍɺmod_perlωΠςΟϒϋϯυϥͰ ಉͷϕϯνϚʔΫΛߦ͏ͱ About 7000 Response/secͱͷ͜ͱ (Thanks @matsumotory) •
Yet another perl5 implement Ͱ mod_*perl Λ࡞Δͱྑ͍উෛ͕Ͱ͖Δ͔ͳ -
In future my activities • ࠓޙmod_perlք۾ΛΓ্͍͛ͨ • TwitterΞΧϯτ࡞ͬͨ @mod_perl_info •
ΣϒαΠτ࡞த(ະ) http://modperl.info/ • ৄࡉɺׂѪͨ͠෦ͷଓ͖Ͳ͔͜Ͱ -
Should you choice mod_perl? • Apacheͱ͍͏ͷதͰ͋Δͷͷɺ mod_perlͷՄೳੑͱ͍ͯ • ύϑΥʔϚϯεɺҠ২ੑɺ༷ʑͳཁૉ Λݕ౼ͯ͠ɺmod_perlΛ͏͔ߟ͑Δ
• طʹ͋ΔApache্ͷϨΨγʔίʔυΛ ϦϑΝΫλϦϯά͢ΔͳΒྑ͍બ -
Why I want to write SMTP server by perl •
DBҾ͖͍ͨ • ֆจࣈॲཧ͍ͨ͠ • ௨ৗͷMTA(Sendmail/qmail/Post fi x)ͷ pipeͰεέʔϧͰ͖ͳ͍ -
Qpsmtpd has some Engine=Transport • pipe (CGI like) • fork-server
• prefork-server •Apache (Apache::Qpsmtpd) • async (Danga::Socket base) -
Qpsmtpd’s Apache::Qpsmtpd • Apache2 / mod_perl2 ͷ Connection cycle ͷྑ͍Ԡ༻ྫ
• ͜ΕΛݟΔͱApache2ΛEngineͱͯ͠ MTAΛ࡞Δํ๏͕ྑ͔͘Δ -
Test of mod_perl handler • ʮϞμϯPerlೖʯʹৄ͘͠ॻ͍ͯ͋Δ • Apache2::FakeRequestΛِͬͯͷ$r Λ࡞ͬͯϋϯυϥΛςετͰ͖Δ •
ॲཧ୯ҐͷςετTest::MoreͰ • ৄׂ͘͠Ѫɺ্͘͠ड़ͷॻ੶Λ -
nginx: HttpPerlModule • ߏจߟ͑ํ͕mod_perl1ͱࠅࣅ ˠmod_perlͷࣝΛྲྀ༻Մೳ • ͨͩ͠ΠϕϯτۦಈαʔόͳΒͰͷ ֤छ੍ݶ͋ΔͷͰҙ • ੈؒͰͷԠ༻ྫগ͠ग़͖͍ͯͯΔ
• ҎલEmbeddedPerlModuleͱ͍͏ ໊લͩͬͨ(1.0→1.2Ͱ໊শมߋʁ) -
nginx: HttpPerlModule package hello; use nginx; sub handler { my
$r = shift; $r->send_http_header("text/html"); return OK if $r->header_only; $r->print("hello!\n<br/>"); $r->rflush; if (-f $r->filename or -d _) { $r->print($r->uri, " exists!\n"); } return OK; } 1; __END__ http://wiki.nginx.org/HttpPerlModule ΑΓ